INT-2177 - Added python support and related refactoring

This commit is contained in:
David Turanski
2011-10-14 09:57:21 -04:00
committed by Mark Fisher
parent 1d80d34225
commit a0e7b3cd78
21 changed files with 449 additions and 117 deletions

View File

@@ -1,5 +1,4 @@
function route(max){
return payload.length > max ? "longStrings" : "shortStrings";
function route(max) {
return payload.length > max ? "longStrings" : "shortStrings";
}
route(maxLen);

View File

@@ -11,8 +11,8 @@
<service-activator input-channel="referencedScriptInput">
<script:script
lang="ruby"
location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb">
lang="python"
location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.py">
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
<script:variable name="date" ref="date"/>

View File

@@ -21,7 +21,6 @@ import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -43,27 +42,25 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author David Turanski
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223ServiceActivatorTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Autowired
private MessageChannel withScriptVariableGenerator;
@Test
public void referencedScriptAndCustomiser() throws Exception{
public void referencedScript() throws Exception {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
@@ -75,20 +72,21 @@ public class Jsr223ServiceActivatorTests {
String value2 = (String) replyChannel.receive(0).getPayload();
String value3 = (String) replyChannel.receive(0).getPayload();
System.out.println(value1 + "\n" + value2 + "\n" + value3);
assertTrue(value1.startsWith("ruby-test-1-foo - bar"));
assertTrue(value2.startsWith("ruby-test-2-foo - bar"));
assertTrue(value3.startsWith("ruby-test-3-foo - bar"));
// because we are using 'prototype bean the suffix date will be different
assertTrue(value1.startsWith("python-test-1-foo - bar"));
assertTrue(value2.startsWith("python-test-2-foo - bar"));
assertTrue(value3.startsWith("python-test-3-foo - bar"));
// because we are using 'prototype bean the suffix date will be
// different
assertFalse(value1.substring(26).equals(value2.substring(26)));
assertFalse(value2.substring(26).equals(value3.substring(26)));
assertNull(replyChannel.receive(0));
}
@Test
public void withScriptVariableGenerator() throws Exception{
public void withScriptVariableGenerator() throws Exception {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
@@ -102,17 +100,18 @@ public class Jsr223ServiceActivatorTests {
assertTrue(value1.startsWith("ruby-test-1-foo - bar"));
assertTrue(value2.startsWith("ruby-test-2-foo - bar"));
assertTrue(value3.startsWith("ruby-test-3-foo - bar"));
// because we are using 'prototype bean the suffix date will be different
// because we are using 'prototype bean the suffix date will be
// different
assertFalse(value1.substring(26).equals(value2.substring(26)));
assertFalse(value2.substring(26).equals(value3.substring(26)));
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() throws Exception{
public void inlineScript() throws Exception {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
@@ -123,21 +122,21 @@ public class Jsr223ServiceActivatorTests {
assertEquals("inline-test-2", replyChannel.receive(0).getPayload());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test(expected=BeanDefinitionParsingException.class)
public void inlineScriptAndVariables() throws Exception{
@Test(expected = BeanDefinitionParsingException.class)
public void inlineScriptAndVariables() throws Exception {
new ClassPathXmlApplicationContext("Jsr223ServiceActivatorTests-fail-context.xml", this.getClass());
}
@Test(expected=BeanDefinitionParsingException.class)
public void variablesAndScriptVariableGenerator() throws Exception{
new ClassPathXmlApplicationContext("Jsr223ServiceActivatorTests-fail-withgenerator-context.xml", this.getClass());
@Test(expected = BeanDefinitionParsingException.class)
public void variablesAndScriptVariableGenerator() throws Exception {
new ClassPathXmlApplicationContext("Jsr223ServiceActivatorTests-fail-withgenerator-context.xml",
this.getClass());
}
public static class SampleScriptVariSource implements ScriptVariableGenerator{
public static class SampleScriptVariSource implements ScriptVariableGenerator {
public Map<String, Object> generateScriptVariables(Message<?> message) {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("foo", "foo");

View File

@@ -0,0 +1 @@
"python-%s-%s - %s - %s" %(payload,foo,bar,date)

View File

@@ -22,8 +22,7 @@ import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.jsr223.DefaultScriptExecutor;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
@@ -32,10 +31,10 @@ import org.springframework.scripting.support.ResourceScriptSource;
*
*/
public class Jsr223ScriptExecutingMessageProcessorTests {
DefaultScriptExecutor executor;
ScriptExecutor executor;
@Before
public void setUp() {
executor = new DefaultScriptExecutor("jruby");
executor = ScriptExecutorFactory.getScriptExecutor("jruby");
}
@Test
public void testExecuteWithVariables(){

View File

@@ -31,7 +31,7 @@ import org.springframework.scripting.support.StaticScriptSource;
public class Jsr223ScriptExecutorTests {
@Test
public void test(){
ScriptExecutor executor = new DefaultScriptExecutor("jruby");
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("jruby");
executor.executeScript(new StaticScriptSource("puts 'hello, world'"));
executor.executeScript(new StaticScriptSource("puts 'hello, again'"));
@@ -54,8 +54,17 @@ public class Jsr223ScriptExecutorTests {
}
@Test
public void testJs(){
ScriptExecutor executor = new DefaultScriptExecutor("js");
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("js");
Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();"));
assertEquals("js",obj.toString());
}
@Test public void testPython() {
ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python");
Object obj = executor.executeScript(new StaticScriptSource("x=2") );
assertEquals(2,obj);
obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)") );
assertEquals(2,obj);
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2011 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.integration.scripting.jsr223;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.python.core.PyTuple;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.scripting.support.StaticScriptSource;
/**
* @author David Turanski
*
*/
public class PythonScriptExecutorTests {
ScriptExecutor executor;
@Before
public void init() {
executor = new PythonScriptExecutor();
}
@Test
public void testLiteral() {
Object obj = executor.executeScript(new StaticScriptSource("3+4") );
assertEquals(7,obj);
obj = executor.executeScript(new StaticScriptSource("'hello,world'") );
assertEquals("hello,world",obj);
}
@Test
public void test1() {
Object obj = executor.executeScript(new StaticScriptSource("x=2") );
assertEquals(2,obj);
}
@Test
public void test2() {
Object obj = executor.executeScript(new StaticScriptSource("def foo(y):\n\tx=y\n\treturn y\nz=foo(2)") );
assertEquals(2,obj);
}
@Test
public void test3() {
ScriptSource source =
new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/test3.py"));
Object obj = executor.executeScript(source);
System.out.println(obj);
PyTuple tuple = (PyTuple) obj;
assertEquals(1, tuple.get(0));
}
@Test
public void testEmbeddedVariable() {
Map<String,Object> variables = new HashMap<String,Object>();
variables.put("scope", "world");
Object obj = executor.executeScript(new StaticScriptSource("\"hello, %s\"% scope"),variables);
assertEquals("hello, world",obj);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-2011 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.integration.scripting.jsr223;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author David Turanski
*
*/
public class PythonVariableParserTests {
@Test
public void testBasic() throws IOException {
String var = PythonScriptExecutor.PythonVariableParser.parseReturnVariable("x=2");
assertEquals("x",var);
var = PythonScriptExecutor.PythonVariableParser.parseReturnVariable("\n\n\nx = 2\n\n\n");
assertEquals("x",var);
var = PythonScriptExecutor.PythonVariableParser.parseReturnVariable("\n\n\nx\n\n\n");
}
@Test
public void test2() throws IOException {
ScriptSource source =
new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/test2.py"));
String var = PythonScriptExecutor.PythonVariableParser.parseReturnVariable(source.getScriptAsString());
assertEquals("bar",var);
}
}

View File

@@ -0,0 +1,5 @@
def foo(y):
x=y
return x
bar = foo(7)

View File

@@ -0,0 +1,6 @@
def foo():
return(1,2,3)
(x,y,z) = foo()
result = (x,y,z)

View File

@@ -12,10 +12,10 @@
<!-- Loggers -->
<logger name="org.springframework">
<level value="info" />
<level value="warn" />
</logger>
<logger name="org.springframework.integration.jsr223">
<logger name="org.springframework.integration.scripting.jsr223">
<level value="info" />
</logger>