service and mock

pull/79/head
yihua.huang 11 years ago
parent 7036950f28
commit 09c43efc96

@ -125,6 +125,12 @@
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

@ -1,6 +1,6 @@
CREATE TABLE `DynamicClass` (
`Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ClassName` varchar(20) NOT NULL,
`ClassName` varchar(200) NOT NULL,
`SourceCode` text NOT NULL,
`AddTime` datetime NOT NULL,
`UpdateTime` datetime NOT NULL,

@ -22,6 +22,12 @@
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>forger</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
@ -82,6 +88,11 @@
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>

@ -0,0 +1,10 @@
package us.codecraft.webmagic.service;
/**
* @author code4crafter@gmail.com
*/
public interface DynamicClassService {
public String compileAndSave(String sourceCode);
}

@ -0,0 +1,34 @@
package us.codecraft.webmagic.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.forger.Forger;
import us.codecraft.forger.ForgerFactory;
import us.codecraft.webmagic.dao.DynamicClassDao;
import us.codecraft.webmagic.model.DynamicClass;
import us.codecraft.webmagic.service.DynamicClassService;
/**
* @author code4crafter@gmail.com
*/
@Service
public class DynamicClassServiceImpl implements DynamicClassService {
@Autowired
private DynamicClassDao dynamicClassDao;
@Autowired
private ForgerFactory forgerFactory;
@Override
public String compileAndSave(String sourceCode) {
Forger<Object> forger = forgerFactory.compile(sourceCode);
String className = forger.getClazz().getCanonicalName();
DynamicClass dynamicClass = new DynamicClass();
dynamicClass.setClassName(className);
dynamicClass.setSourceCode(sourceCode);
dynamicClassDao.add(dynamicClass);
return className;
}
}

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="forgerFactory" class="us.codecraft.forger.ForgerFactory">
<constructor-arg>
<bean class="us.codecraft.forger.property.AnnotationPropertyLoader"></bean>
</constructor-arg>
<constructor-arg>
<bean class="us.codecraft.forger.compiler.GroovyForgerCompiler"></bean>
</constructor-arg>
</bean>
</beans>

@ -0,0 +1,45 @@
package us.codecraft.webmagic;
import us.codecraft.forger.property.Inject;
import us.codecraft.forger.property.format.Formatter;
/**
* @author code4crafter@gmail.com
*/
public class Foo {
@Formatter("")
@Inject("fooa")
private String foo;
public static final String SOURCE_CODE="package us.codecraft.webmagic;\n" +
"\n" +
"import us.codecraft.forger.property.Inject;\n" +
"import us.codecraft.forger.property.format.Formatter;\n" +
"\n" +
"/**\n" +
" * @author code4crafter@gmail.com\n" +
" */\n" +
"public class Foo {\n" +
"\n" +
" @Formatter(\"\")\n" +
" @Inject(\"fooa\")\n" +
" private String foo;\n" +
"\n" +
" public String getFoo() {\n" +
" return foo;\n" +
" }\n" +
"\n" +
" public String foo() {\n" +
" return foo;\n" +
" }\n" +
"}";
public String getFoo() {
return foo;
}
public String foo() {
return foo;
}
}

@ -0,0 +1,47 @@
package us.codecraft.webmagic.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import us.codecraft.forger.ForgerFactory;
import us.codecraft.webmagic.Foo;
import us.codecraft.webmagic.dao.DynamicClassDao;
import us.codecraft.webmagic.service.impl.DynamicClassServiceImpl;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author code4crafter@gmail.com
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring/applicationContext*.xml"})
public class DynamicClassServiceImplTest {
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Spy
@Autowired
private ForgerFactory forgerFactory;
@InjectMocks
private DynamicClassService dynamicClassService = new DynamicClassServiceImpl();
@Mock
private DynamicClassDao dynamicClassDao;
@Test
public void testCompileAndSave() throws Exception {
String className = dynamicClassService.compileAndSave(Foo.SOURCE_CODE);
assertThat(className).isEqualTo("us.codecraft.webmagic.Foo");
}
}

@ -9,6 +9,7 @@
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

Loading…
Cancel
Save