Introduced ScriptEvaluator strategy interface

Including ScriptEvaluator implementations for JSR-223, Groovy and BeanShell. BeanShell consistently receives the bean ClassLoader now. Also revised ScriptFactory and its implementations for varargs.

Issue: SPR-11007
This commit is contained in:
Juergen Hoeller
2013-10-26 01:17:32 +02:00
committed by unknown
parent 12e896ed8b
commit dfb29f4296
21 changed files with 629 additions and 110 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scripting.bsh;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scripting.ScriptEvaluator;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.scripting.support.StaticScriptSource;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
*/
public class BshScriptEvaluatorTests {
@Test
public void testBshScriptFromString() {
ScriptEvaluator evaluator = new BshScriptEvaluator();
Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2;"));
assertEquals(6, result);
}
@Test
public void testBshScriptFromFile() {
ScriptEvaluator evaluator = new BshScriptEvaluator();
Object result = evaluator.evaluate(new ResourceScriptSource(new ClassPathResource("simple.bsh", getClass())));
assertEquals(6, result);
}
@Test
public void testGroovyScriptWithArguments() {
ScriptEvaluator evaluator = new BshScriptEvaluator();
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("a", 3);
arguments.put("b", 2);
Object result = evaluator.evaluate(new StaticScriptSource("return a * b;"), arguments);
assertEquals(6, result);
}
}

View File

@@ -195,10 +195,9 @@ public class BshScriptFactoryTests extends TestCase {
given(script.getScriptAsString()).willReturn(badScript);
given(script.isModified()).willReturn(true);
BshScriptFactory factory = new BshScriptFactory(
ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript,
new Class<?>[] {Messenger.class});
ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class);
try {
Messenger messenger = (Messenger) factory.getScriptedObject(script, new Class<?>[]{Messenger.class});
Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class);
messenger.getMessage();
fail("Must have thrown a BshScriptUtils.BshExecutionException.");
}
@@ -208,7 +207,7 @@ public class BshScriptFactoryTests extends TestCase {
public void testCtorWithNullScriptSourceLocator() throws Exception {
try {
new BshScriptFactory(null, new Class<?>[] {Messenger.class});
new BshScriptFactory(null, Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {

View File

@@ -0,0 +1 @@
return 3 * 2;

View File

@@ -25,7 +25,7 @@ public class GroovyAspectTests {
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
new ClassPathResource("GroovyServiceImpl.grv", getClass())));
testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "GroovyServiceImpl");
@@ -37,7 +37,7 @@ public class GroovyAspectTests {
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
new ClassPathResource("GroovyServiceImpl.grv", getClass())));
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(String.format("execution(* %s.TestService+.*(..))", ClassUtils.getPackageName(getClass())));
@@ -51,7 +51,7 @@ public class GroovyAspectTests {
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
new ClassPathResource("GroovyServiceImpl.grv", getClass())));
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));
@@ -66,7 +66,7 @@ public class GroovyAspectTests {
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv");
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource(
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null);
new ClassPathResource("GroovyServiceImpl.grv", getClass())));
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass())));

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scripting.groovy;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scripting.ScriptEvaluator;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.scripting.support.StandardScriptEvaluator;
import org.springframework.scripting.support.StaticScriptSource;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
*/
public class GroovyScriptEvaluatorTests {
@Test
public void testGroovyScriptFromString() {
ScriptEvaluator evaluator = new GroovyScriptEvaluator();
Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2"));
assertEquals(6, result);
}
@Test
public void testGroovyScriptFromFile() {
ScriptEvaluator evaluator = new GroovyScriptEvaluator();
Object result = evaluator.evaluate(new ResourceScriptSource(new ClassPathResource("simple.groovy", getClass())));
assertEquals(6, result);
}
@Test
public void testGroovyScriptWithArguments() {
ScriptEvaluator evaluator = new GroovyScriptEvaluator();
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("a", 3);
arguments.put("b", 2);
Object result = evaluator.evaluate(new StaticScriptSource("return a * b"), arguments);
assertEquals(6, result);
}
@Test
public void testGroovyScriptFromStringUsingJsr223() {
StandardScriptEvaluator evaluator = new StandardScriptEvaluator();
evaluator.setLanguage("Groovy");
Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2"));
assertEquals(6, result);
}
@Test
public void testGroovyScriptFromFileUsingJsr223() {
ScriptEvaluator evaluator = new StandardScriptEvaluator();
Object result = evaluator.evaluate(new ResourceScriptSource(new ClassPathResource("simple.groovy", getClass())));
assertEquals(6, result);
}
@Test
public void testGroovyScriptWithArgumentsUsingJsr223() {
StandardScriptEvaluator evaluator = new StandardScriptEvaluator();
evaluator.setLanguage("Groovy");
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("a", 3);
arguments.put("b", 2);
Object result = evaluator.evaluate(new StaticScriptSource("return a * b"), arguments);
assertEquals(6, result);
}
}

View File

@@ -16,16 +16,16 @@
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;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.factory.BeanCreationException;
@@ -191,7 +191,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 +206,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 +223,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 +260,8 @@ public class GroovyScriptFactoryTests {
try {
new GroovyScriptFactory(null);
fail("Must have thrown exception by this point.");
} catch (IllegalArgumentException expected) {
}
catch (IllegalArgumentException expected) {
}
}
@@ -266,7 +270,8 @@ public class GroovyScriptFactoryTests {
try {
new GroovyScriptFactory("");
fail("Must have thrown exception by this point.");
} catch (IllegalArgumentException expected) {
}
catch (IllegalArgumentException expected) {
}
}
@@ -275,7 +280,8 @@ public class GroovyScriptFactoryTests {
try {
new GroovyScriptFactory("\n ");
fail("Must have thrown exception by this point.");
} catch (IllegalArgumentException expected) {
}
catch (IllegalArgumentException expected) {
}
}
@@ -284,7 +290,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 +303,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 +311,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,8 +403,9 @@ 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"));
}
}
@@ -431,7 +440,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 +464,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());
}
}
@@ -486,9 +497,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);
}
}

View File

@@ -0,0 +1 @@
return 3 * 2

View File

@@ -106,7 +106,7 @@ public class JRubyScriptFactoryTests {
@Test
public void testCtorWithNullScriptSourceLocator() throws Exception {
try {
new JRubyScriptFactory(null, new Class<?>[]{Messenger.class});
new JRubyScriptFactory(null, Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
@@ -116,7 +116,7 @@ public class JRubyScriptFactoryTests {
@Test
public void testCtorWithEmptyScriptSourceLocator() throws Exception {
try {
new JRubyScriptFactory("", new Class<?>[]{Messenger.class});
new JRubyScriptFactory("", Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
@@ -126,7 +126,7 @@ public class JRubyScriptFactoryTests {
@Test
public void testCtorWithWhitespacedScriptSourceLocator() throws Exception {
try {
new JRubyScriptFactory("\n ", new Class<?>[]{Messenger.class});
new JRubyScriptFactory("\n ", Messenger.class);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
@@ -136,7 +136,7 @@ public class JRubyScriptFactoryTests {
@Test
public void testCtorWithNullScriptInterfacesArray() throws Exception {
try {
new JRubyScriptFactory(RUBY_SCRIPT_SOURCE_LOCATOR, null);
new JRubyScriptFactory(RUBY_SCRIPT_SOURCE_LOCATOR);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {

View File

@@ -32,15 +32,6 @@ import static org.mockito.BDDMockito.*;
*/
public class ResourceScriptSourceTests extends TestCase {
public void testCtorWithNullResource() throws Exception {
try {
new ResourceScriptSource(null);
fail("Must have thrown exception by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testDoesNotPropagateFatalExceptionOnResourceThatCannotBeResolvedToAFile() throws Exception {
Resource resource = mock(Resource.class);
given(resource.lastModified()).willThrow(new IOException());