@Override public void setBeanClassLoader(ClassLoader classLoader) { this.groovyClassLoader = buildGroovyClassLoader(classLoader); }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
@Override public String sayHello() { throw new TestException("TestServiceImpl"); } }
@Test public void testGetScriptedObjectDoesChokeOnNullScriptSourceBeingPassedIn() throws Exception { GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); try { factory.getScriptedObject(null); fail("Must have thrown a NullPointerException as per contract ('null' ScriptSource supplied"); } catch (NullPointerException expected) { } }
@Test public void testGroovyScriptWithCompilerConfiguration() { GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator(); MyBytecodeProcessor processor = new MyBytecodeProcessor(); evaluator.getCompilerConfiguration().setBytecodePostprocessor(processor); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); assertTrue(processor.processed.contains("Script1")); }
@Test public void testCtorWithEmptyScriptSourceLocator() throws Exception { try { new GroovyScriptFactory(""); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
@Test public void manualGroovyBeanWithUnconditionalPointcut() throws Exception { TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( new ClassPathResource("GroovyServiceImpl.grv", getClass()))); testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "GroovyServiceImpl"); }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, boolean proxyTargetClass) throws Exception { logAdvice.reset(); ProxyFactory factory = new ProxyFactory(target); factory.setProxyTargetClass(proxyTargetClass); factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals(message, ex.getMessage()); } assertEquals(1, logAdvice.getCountThrows()); }
@Test public void testGroovyBeanProxyTargetClass() { context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml"); TestService bean = context.getBean("groovyBean", TestService.class); LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals("GroovyServiceImpl", ex.getMessage()); } assertEquals(1, logAdvice.getCountBefore()); assertEquals(1, logAdvice.getCountThrows()); }
@Test public void testMetaClassWithXsd() { testMetaClass("org/springframework/scripting/groovy/calculators-with-xsd.xml"); }
@Log public class TestServiceImpl implements TestService{ @Override public String sayHello() { throw new TestException("TestServiceImpl"); } }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message) throws Exception { testAdvice(advisor, logAdvice, target, message, false); }
public MyImportCustomizer() { addStarImports("org.springframework.scripting.groovy"); }
@Test public void testCtorWithWhitespacedScriptSourceLocator() throws Exception { try { new GroovyScriptFactory("\n "); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
@Override public void setBeanClassLoader(ClassLoader classLoader) { this.groovyClassLoader = buildGroovyClassLoader(classLoader); }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
@Test public void testMetaClassWithBeans() { testMetaClass("org/springframework/scripting/groovy/calculators.xml"); }
@Test public void testCtorWithNullScriptSourceLocator() throws Exception { try { new GroovyScriptFactory(null); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { } }
/** * Return the GroovyClassLoader used by this script factory. */ public GroovyClassLoader getGroovyClassLoader() { synchronized (this.scriptClassMonitor) { if (this.groovyClassLoader == null) { this.groovyClassLoader = buildGroovyClassLoader(ClassUtils.getDefaultClassLoader()); } return this.groovyClassLoader; } }
/** * Return the GroovyClassLoader used by this script factory. */ public GroovyClassLoader getGroovyClassLoader() { synchronized (this.scriptClassMonitor) { if (this.groovyClassLoader == null) { this.groovyClassLoader = buildGroovyClassLoader(ClassUtils.getDefaultClassLoader()); } return this.groovyClassLoader; } }