JSR-223 based StandardScriptFactory (including <lang:std> support)

This commit also completes 4.2 schema variants in spring-context.

Issue: SPR-5215
This commit is contained in:
Juergen Hoeller
2015-05-08 23:56:08 +02:00
parent 4bf32578b5
commit 1722fa6678
24 changed files with 2439 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -90,6 +90,35 @@ public class GroovyScriptFactoryTests {
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testStaticScriptUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
Calculator calc = (Calculator) ctx.getBean("calculator");
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(calc));
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
assertEquals(calc, calc);
assertEquals(messenger, messenger);
assertTrue(!messenger.equals(calc));
assertTrue(messenger.hashCode() != calc.hashCode());
assertTrue(!messenger.toString().equals(calc.toString()));
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
assertTrue(ctx.getBeansOfType(Calculator.class).values().contains(calc));
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testStaticPrototypeScript() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
@@ -109,6 +138,25 @@ public class GroovyScriptFactoryTests {
assertEquals("Byebye World!", messenger2.getMessage());
}
@Test
public void testStaticPrototypeScriptUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
assertNotSame(messenger, messenger2);
assertSame(messenger.getClass(), messenger2.getClass());
assertEquals("Hello World!", messenger.getMessage());
assertEquals("Hello World!", messenger2.getMessage());
messenger.setMessage("Bye World!");
messenger2.setMessage("Byebye World!");
assertEquals("Bye World!", messenger.getMessage());
assertEquals("Byebye World!", messenger2.getMessage());
}
@Test
public void testStaticScriptWithInstance() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
@@ -123,6 +171,20 @@ public class GroovyScriptFactoryTests {
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testStaticScriptWithInstanceUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testStaticScriptWithInlineDefinedInstance() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
@@ -137,6 +199,20 @@ public class GroovyScriptFactoryTests {
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testStaticScriptWithInlineDefinedInstanceUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstanceInline"));
Messenger messenger = (Messenger) ctx.getBean("messengerInstanceInline");
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
@Test
public void testNonStaticScript() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyRefreshableContext.xml", getClass());
@@ -311,8 +387,6 @@ public class GroovyScriptFactoryTests {
}
}
@Ignore
// see http://build.springframework.org/browse/SPR-TRUNKQUICK-908
@Test
public void testResourceScriptFromTag() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
@@ -409,6 +483,49 @@ public class GroovyScriptFactoryTests {
assertEquals(4, beans.size());
}
@Test
public void testJsr223FromTag() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertFalse(AopUtils.isAopProxy(messenger));
assertEquals("Hello World!", messenger.getMessage());
}
@Test
public void testJsr223FromTagWithInterface() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithInterface"));
Messenger messenger = (Messenger) ctx.getBean("messengerWithInterface");
assertFalse(AopUtils.isAopProxy(messenger));
}
@Test
public void testRefreshableJsr223FromTag() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
assertTrue(AopUtils.isAopProxy(messenger));
assertTrue(messenger instanceof Refreshable);
assertEquals("Hello World!", messenger.getMessage());
}
@Test
public void testInlineJsr223FromTag() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessenger"));
Messenger messenger = (Messenger) ctx.getBean("inlineMessenger");
assertFalse(AopUtils.isAopProxy(messenger));
}
@Test
public void testInlineJsr223FromTagWithInterface() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessengerWithInterface"));
Messenger messenger = (Messenger) ctx.getBean("inlineMessengerWithInterface");
assertFalse(AopUtils.isAopProxy(messenger));
}
/**
* Tests the SPR-2098 bug whereby no more than 1 property element could be
* passed to a scripted bean :(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -64,6 +64,27 @@ public class JRubyScriptFactoryTests {
assertNotSame(messenger.hashCode(), calc.hashCode());
assertTrue(!messenger.toString().equals(calc.toString()));
assertEquals(3, calc.add(1, 2));
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
@Test
public void testStaticScriptUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jrubyContextWithJsr223.xml", getClass());
Calculator calc = (Calculator) ctx.getBean("calculator");
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
assertEquals(calc, calc);
assertEquals(messenger, messenger);
assertTrue(!messenger.equals(calc));
assertNotSame(messenger.hashCode(), calc.hashCode());
assertTrue(!messenger.toString().equals(calc.toString()));
assertEquals(3, calc.add(1, 2));
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
@@ -163,6 +184,15 @@ public class JRubyScriptFactoryTests {
assertEquals(testBean, messengerByName.getTestBean());
}
@Test
public void testResourceScriptFromTagUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd-jsr223.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertEquals("Hello World!", messenger.getMessage());
assertFalse(messenger instanceof Refreshable);
}
@Test
public void testPrototypeScriptFromTag() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
@@ -185,6 +215,16 @@ public class JRubyScriptFactoryTests {
Calculator calculator = (Calculator) ctx.getBean("calculator");
assertNotNull(calculator);
assertFalse(calculator instanceof Refreshable);
assertEquals(3, calculator.add(1, 2));
}
@Test
public void testInlineScriptFromTagUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd-jsr223.xml", getClass());
Calculator calculator = (Calculator) ctx.getBean("calculator");
assertNotNull(calculator);
assertFalse(calculator instanceof Refreshable);
assertEquals(3, calculator.add(1, 2));
}
@Test
@@ -195,6 +235,15 @@ public class JRubyScriptFactoryTests {
assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}
@Test
public void testRefreshableFromTagUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd-jsr223.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
assertEquals("Hello World!", messenger.getMessage());
assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}
@Test
public void testThatMultipleScriptInterfacesAreSupported() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("calculatingMessenger");
@@ -214,6 +263,15 @@ public class JRubyScriptFactoryTests {
assertEquals(1, printable.count);
}
@Test
public void testWithComplexArgUsingJsr223() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jrubyContextWithJsr223.xml", getClass());
Printer printer = (Printer) ctx.getBean("printer");
CountingPrintable printable = new CountingPrintable();
printer.print(printable);
assertEquals(1, printable.count);
}
@Test
public void testWithPrimitiveArgsInReturnTypeAndParameters() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jrubyContextForPrimitives.xml", getClass());

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2015 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.support;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;
import static org.junit.Assert.*;
/**
* {@link StandardScriptFactory} (lang:std) tests for JavaScript.
*
* @author Juergen Hoeller
* @since 4.2
*/
public class StandardScriptFactoryTests {
@Test
public void testJsr223FromTagWithInterface() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithInterface"));
Messenger messenger = (Messenger) ctx.getBean("messengerWithInterface");
assertFalse(AopUtils.isAopProxy(messenger));
assertEquals("Hello World!", messenger.getMessage());
}
@Test
public void testRefreshableJsr223FromTagWithInterface() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessengerWithInterface"));
Messenger messenger = (Messenger) ctx.getBean("refreshableMessengerWithInterface");
assertTrue(AopUtils.isAopProxy(messenger));
assertTrue(messenger instanceof Refreshable);
assertEquals("Hello World!", messenger.getMessage());
}
@Test
public void testInlineJsr223FromTagWithInterface() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessengerWithInterface"));
Messenger messenger = (Messenger) ctx.getBean("inlineMessengerWithInterface");
assertFalse(AopUtils.isAopProxy(messenger));
assertEquals("Hello World!", messenger.getMessage());
}
}