add DynamicClassException to DynamicClassService.compileAndSave
parent
09c43efc96
commit
9a4eab44be
|
@ -0,0 +1,15 @@
|
|||
package us.codecraft.webmagic.exception;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
*/
|
||||
public class DynamicClassCompileException extends Exception{
|
||||
|
||||
public DynamicClassCompileException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DynamicClassCompileException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
package us.codecraft.webmagic.service;
|
||||
|
||||
import us.codecraft.webmagic.exception.DynamicClassCompileException;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
*/
|
||||
public interface DynamicClassService {
|
||||
|
||||
public String compileAndSave(String sourceCode);
|
||||
public Class compileAndSave(String sourceCode) throws DynamicClassCompileException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package us.codecraft.webmagic.service.impl;
|
||||
|
||||
import org.codehaus.groovy.control.CompilationFailedException;
|
||||
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.exception.DynamicClassCompileException;
|
||||
import us.codecraft.webmagic.model.DynamicClass;
|
||||
import us.codecraft.webmagic.service.DynamicClassService;
|
||||
|
||||
|
@ -21,14 +23,19 @@ public class DynamicClassServiceImpl implements DynamicClassService {
|
|||
private ForgerFactory forgerFactory;
|
||||
|
||||
@Override
|
||||
public String compileAndSave(String sourceCode) {
|
||||
Forger<Object> forger = forgerFactory.compile(sourceCode);
|
||||
public Class compileAndSave(String sourceCode) throws DynamicClassCompileException {
|
||||
Forger<Object> forger;
|
||||
try {
|
||||
forger = forgerFactory.compile(sourceCode);
|
||||
} catch (CompilationFailedException e) {
|
||||
throw new DynamicClassCompileException(e.getMessage(),e);
|
||||
}
|
||||
String className = forger.getClazz().getCanonicalName();
|
||||
DynamicClass dynamicClass = new DynamicClass();
|
||||
dynamicClass.setClassName(className);
|
||||
dynamicClass.setSourceCode(sourceCode);
|
||||
dynamicClassDao.add(dynamicClass);
|
||||
return className;
|
||||
return forger.getClazz();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,9 +13,11 @@ 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.exception.DynamicClassCompileException;
|
||||
import us.codecraft.webmagic.service.impl.DynamicClassServiceImpl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
|
@ -25,7 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
public class DynamicClassServiceImplTest {
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
|
@ -41,7 +43,16 @@ public class DynamicClassServiceImplTest {
|
|||
|
||||
@Test
|
||||
public void testCompileAndSave() throws Exception {
|
||||
String className = dynamicClassService.compileAndSave(Foo.SOURCE_CODE);
|
||||
assertThat(className).isEqualTo("us.codecraft.webmagic.Foo");
|
||||
Class aClass = dynamicClassService.compileAndSave(Foo.SOURCE_CODE);
|
||||
assertThat(aClass.getCanonicalName()).isEqualTo("us.codecraft.webmagic.Foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompileFail() {
|
||||
try {
|
||||
dynamicClassService.compileAndSave("class s((");
|
||||
failBecauseExceptionWasNotThrown(DynamicClassCompileException.class);
|
||||
} catch (DynamicClassCompileException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue