service and mock
parent
7036950f28
commit
09c43efc96
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue