|
|
|
|
@ -16,13 +16,13 @@
@@ -16,13 +16,13 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.scripting.groovy; |
|
|
|
|
|
|
|
|
|
import groovy.lang.DelegatingMetaClass; |
|
|
|
|
import groovy.lang.GroovyObject; |
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import groovy.lang.DelegatingMetaClass; |
|
|
|
|
import groovy.lang.GroovyObject; |
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
|
import org.junit.Ignore; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
@ -61,13 +61,9 @@ import static org.mockito.BDDMockito.*;
@@ -61,13 +61,9 @@ import static org.mockito.BDDMockito.*;
|
|
|
|
|
* @author Mark Fisher |
|
|
|
|
* @author Chris Beams |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("resource") |
|
|
|
|
public class GroovyScriptFactoryTests { |
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
public void setUp() { |
|
|
|
|
Assume.group(TestGroup.LONG_RUNNING); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testStaticScript() throws Exception { |
|
|
|
|
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass()); |
|
|
|
|
@ -191,7 +187,8 @@ public class GroovyScriptFactoryTests {
@@ -191,7 +187,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
new ClassPathXmlApplicationContext("org/springframework/scripting/groovy/groovyBrokenContext.xml"); |
|
|
|
|
fail("Should throw exception for broken script file"); |
|
|
|
|
} catch (NestedRuntimeException ex) { |
|
|
|
|
} |
|
|
|
|
catch (NestedRuntimeException ex) { |
|
|
|
|
assertTrue("Wrong root cause: " + ex, ex.contains(ScriptCompilationException.class)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -205,9 +202,10 @@ public class GroovyScriptFactoryTests {
@@ -205,9 +202,10 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX |
|
|
|
|
+ badScript); |
|
|
|
|
try { |
|
|
|
|
factory.getScriptedObject(script, new Class<?>[] {}); |
|
|
|
|
factory.getScriptedObject(script); |
|
|
|
|
fail("Must have thrown a ScriptCompilationException (no public no-arg ctor in scripted class)."); |
|
|
|
|
} catch (ScriptCompilationException expected) { |
|
|
|
|
} |
|
|
|
|
catch (ScriptCompilationException expected) { |
|
|
|
|
assertTrue(expected.contains(InstantiationException.class)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -221,9 +219,10 @@ public class GroovyScriptFactoryTests {
@@ -221,9 +219,10 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX |
|
|
|
|
+ badScript); |
|
|
|
|
try { |
|
|
|
|
factory.getScriptedObject(script, new Class<?>[] {}); |
|
|
|
|
factory.getScriptedObject(script); |
|
|
|
|
fail("Must have thrown a ScriptCompilationException (no oublic no-arg ctor in scripted class)."); |
|
|
|
|
} catch (ScriptCompilationException expected) { |
|
|
|
|
} |
|
|
|
|
catch (ScriptCompilationException expected) { |
|
|
|
|
assertTrue(expected.contains(IllegalAccessException.class)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -257,7 +256,8 @@ public class GroovyScriptFactoryTests {
@@ -257,7 +256,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
new GroovyScriptFactory(null); |
|
|
|
|
fail("Must have thrown exception by this point."); |
|
|
|
|
} catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -266,7 +266,8 @@ public class GroovyScriptFactoryTests {
@@ -266,7 +266,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
new GroovyScriptFactory(""); |
|
|
|
|
fail("Must have thrown exception by this point."); |
|
|
|
|
} catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -275,7 +276,8 @@ public class GroovyScriptFactoryTests {
@@ -275,7 +276,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
new GroovyScriptFactory("\n "); |
|
|
|
|
fail("Must have thrown exception by this point."); |
|
|
|
|
} catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
catch (IllegalArgumentException expected) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -284,7 +286,8 @@ public class GroovyScriptFactoryTests {
@@ -284,7 +286,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
new ClassPathXmlApplicationContext("lwspBadGroovyContext.xml", getClass()); |
|
|
|
|
fail("Must have thrown a BeanCreationException ('inline:' prefix was preceded by whitespace"); |
|
|
|
|
} catch (BeanCreationException expected) { |
|
|
|
|
} |
|
|
|
|
catch (BeanCreationException expected) { |
|
|
|
|
assertTrue(expected.contains(FileNotFoundException.class)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -296,7 +299,7 @@ public class GroovyScriptFactoryTests {
@@ -296,7 +299,7 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
given(script.suggestedClassName()).willReturn("someName"); |
|
|
|
|
|
|
|
|
|
GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); |
|
|
|
|
Object scriptedObject = factory.getScriptedObject(script, null); |
|
|
|
|
Object scriptedObject = factory.getScriptedObject(script); |
|
|
|
|
assertNotNull(scriptedObject); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -304,9 +307,10 @@ public class GroovyScriptFactoryTests {
@@ -304,9 +307,10 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
public void testGetScriptedObjectDoesChokeOnNullScriptSourceBeingPassedIn() throws Exception { |
|
|
|
|
GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); |
|
|
|
|
try { |
|
|
|
|
factory.getScriptedObject(null, null); |
|
|
|
|
factory.getScriptedObject(null); |
|
|
|
|
fail("Must have thrown a NullPointerException as per contract ('null' ScriptSource supplied"); |
|
|
|
|
} catch (NullPointerException expected) { |
|
|
|
|
} |
|
|
|
|
catch (NullPointerException expected) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -395,15 +399,14 @@ public class GroovyScriptFactoryTests {
@@ -395,15 +399,14 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
public void testProxyTargetClassNotAllowedIfNotGroovy() throws Exception { |
|
|
|
|
try { |
|
|
|
|
new ClassPathXmlApplicationContext("jruby-with-xsd-proxy-target-class.xml", getClass()); |
|
|
|
|
} catch (BeanCreationException e) { |
|
|
|
|
assertTrue(e.getMessage().contains("Cannot use proxyTargetClass=true")); |
|
|
|
|
} |
|
|
|
|
catch (BeanCreationException ex) { |
|
|
|
|
assertTrue(ex.getMessage().contains("Cannot use proxyTargetClass=true")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testAnonymousScriptDetected() throws Exception { |
|
|
|
|
Assume.group(TestGroup.LONG_RUNNING); |
|
|
|
|
|
|
|
|
|
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass()); |
|
|
|
|
Map<?, Messenger> beans = ctx.getBeansOfType(Messenger.class); |
|
|
|
|
assertEquals(4, beans.size()); |
|
|
|
|
@ -431,7 +434,8 @@ public class GroovyScriptFactoryTests {
@@ -431,7 +434,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
try { |
|
|
|
|
ctx.getBean("bean3"); |
|
|
|
|
fail("Should have thrown BeanCreationException"); |
|
|
|
|
} catch (BeanCreationException ex) { |
|
|
|
|
} |
|
|
|
|
catch (BeanCreationException ex) { |
|
|
|
|
// expected
|
|
|
|
|
assertTrue(ex.contains(UnsatisfiedDependencyException.class)); |
|
|
|
|
} |
|
|
|
|
@ -454,7 +458,8 @@ public class GroovyScriptFactoryTests {
@@ -454,7 +458,8 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
Calculator calc = (Calculator) ctx.getBean("delegatingCalculator"); |
|
|
|
|
calc.add(1, 2); |
|
|
|
|
fail("expected IllegalStateException"); |
|
|
|
|
} catch (IllegalStateException ex) { |
|
|
|
|
} |
|
|
|
|
catch (IllegalStateException ex) { |
|
|
|
|
assertEquals("Gotcha", ex.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -479,6 +484,7 @@ public class GroovyScriptFactoryTests {
@@ -479,6 +484,7 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
assertEquals("test", result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class TestCustomizer implements GroovyObjectCustomizer { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@ -486,9 +492,10 @@ public class GroovyScriptFactoryTests {
@@ -486,9 +492,10 @@ public class GroovyScriptFactoryTests {
|
|
|
|
|
DelegatingMetaClass dmc = new DelegatingMetaClass(goo.getMetaClass()) { |
|
|
|
|
@Override |
|
|
|
|
public Object invokeMethod(Object arg0, String mName, Object[] arg2) { |
|
|
|
|
if (mName.indexOf("Missing") != -1) { |
|
|
|
|
if (mName.contains("Missing")) { |
|
|
|
|
throw new IllegalStateException("Gotcha"); |
|
|
|
|
} else { |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return super.invokeMethod(arg0, mName, arg2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|