INT-1576,INT-1579,INT-2025: added spring-integration-scripting and fixed HeaderEnricher issue

This commit is contained in:
David Turanski
2011-08-03 15:15:00 +05:30
parent f94411e11d
commit 207f043587
64 changed files with 1735 additions and 239 deletions

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<filter input-channel="referencedScriptInput">
<script:script lang="groovy" location="org/springframework/integration/scripting/config/jsr223/Jsr223FilterTests.groovy"/>
</filter>
<filter input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return payload == 'good'
]]></script:script>
</filter>
</beans:beans>

View File

@@ -0,0 +1,83 @@
/*
* 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.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223FilterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message1 = MessageBuilder.withPayload("test-1")
.setReplyChannel(replyChannel)
.setHeader("type", "bad")
.build();
Message<?> message2 = MessageBuilder.withPayload("test-2")
.setReplyChannel(replyChannel)
.setHeader("type", "good")
.build();
this.referencedScriptInput.send(message1);
this.referencedScriptInput.send(message2);
assertEquals("test-2", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message1 = MessageBuilder.withPayload("bad").setReplyChannel(replyChannel).build();
Message<?> message2 = MessageBuilder.withPayload("good").setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message1);
this.inlineScriptInput.send(message2);
Message<?> received = replyChannel.receive(0);
assertNotNull(received);
assertEquals("good", received.getPayload());
assertEquals(message2, received);
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<int:channel id="inputA"/>
<int:header-enricher input-channel="inputA" output-channel="outputA">
<int:header name="TEST_HEADER">
<script:script lang="jruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223HeaderEnricherTests.rb"/>
</int:header>
</int:header-enricher>
<int:channel id="outputA">
<int:queue/>
</int:channel>
<int:channel id="inputB"/>
<int:header-enricher input-channel="inputB" output-channel="outputB">
<int:header name="TEST_HEADER">
<script:script lang="javascript">
<![CDATA[
function js(){ return 'js';} js();
]]>
</script:script>
</int:header>
</int:header-enricher>
<int:channel id="outputB">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,63 @@
/*
* 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.config.jsr223;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223HeaderEnricherTests {
@Autowired
private MessageChannel inputA;
@Autowired
private QueueChannel outputA;
@Autowired
private MessageChannel inputB;
@Autowired
private QueueChannel outputB;
@Test
public void referencedScript() throws Exception{
inputA.send(new GenericMessage<String>("Hello"));
assertEquals("jruby", outputA.receive(1000).getHeaders().get("TEST_HEADER"));
}
@Test
public void inlineScript() throws Exception{
inputB.send(new GenericMessage<String>("Hello"));
assertEquals("js", outputB.receive(1000).getHeaders().get("TEST_HEADER"));
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" xmlns="http://www.springframework.org/schema/beans">
<property name="customEditors">
<map>
<entry key="org.springframework.core.io.Resource" value="org.springframework.integration.scripting.config.jsr223.Jsr223RefreshTests$ResourceEditor"/>
</map>
</property>
</bean>
<transformer input-channel="referencedScriptInput">
<script:script lang="ruby" location="Jsr223RefreshTests.rb" refresh-check-delay="0"/>
</transformer>
</beans:beans>

View File

@@ -0,0 +1,94 @@
/*
* 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.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.beans.PropertyEditorSupport;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223RefreshTests {
@Autowired
private MessageChannel referencedScriptInput;
@Test
public void referencedScript() throws Exception {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
this.referencedScriptInput.send(MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build());
assertEquals("ruby-test-1", replyChannel.receive(0).getPayload());
this.referencedScriptInput.send(MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build());
assertEquals("ruby-test-0", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
public static class ResourceEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setValue(new CycleResource());
}
}
private static class CycleResource extends AbstractResource {
private int count = -1;
private String[] scripts = {"\"ruby-#{$payload}-0\"", "\"ruby-#{$payload}-1\""};
public String getDescription() {
return "CycleResource";
}
@Override
public String getFilename() throws IllegalStateException {
return "CycleResource";
}
@Override
public long lastModified() throws IOException {
return -1;
}
public InputStream getInputStream() throws IOException {
if (++count>scripts.length-1) {
count = 0;
}
return new ByteArrayInputStream(scripts[count].getBytes());
}
}
}

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:int-script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<channel id="shortStrings">
<queue/>
</channel>
<channel id="longStrings">
<queue/>
</channel>
<router input-channel="referencedScriptInput">
<int-script:script lang="javascript" location="org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js">
<variable name="maxLen" value="3"/>
</int-script:script>
</router>
<router input-channel="inlineScriptInput">
<int-script:script lang="javascript"><![CDATA[
(function(){
return payload.length > 5 ? "longStrings" : "shortStrings";
})();
]]></int-script:script>
</router>
</beans:beans>

View File

@@ -0,0 +1,97 @@
/*
* 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.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author David Turanski
* @since 2.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223RouterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Autowired
private PollableChannel longStrings;
@Autowired
private PollableChannel shortStrings;
@Test
public void referencedScript() { // long is > 3
Message<?> message1 = new GenericMessage<String>("aardvark");
Message<?> message2 = new GenericMessage<String>("bear");
Message<?> message3 = new GenericMessage<String>("cat");
Message<?> message4 = new GenericMessage<String>("dog");
Message<?> message5 = new GenericMessage<String>("elephant");
this.referencedScriptInput.send(message1);
this.referencedScriptInput.send(message2);
this.referencedScriptInput.send(message3);
this.referencedScriptInput.send(message4);
this.referencedScriptInput.send(message5);
assertEquals("cat", shortStrings.receive(0).getPayload());
assertEquals("dog", shortStrings.receive(0).getPayload());
assertEquals("aardvark", longStrings.receive(0).getPayload());
assertEquals("bear", longStrings.receive(0).getPayload());
assertEquals("elephant", longStrings.receive(0).getPayload());
assertNull(shortStrings.receive(0));
assertNull(longStrings.receive(0));
}
@Test
public void inlineScript() { // long is > 5
Message<?> message1 = new GenericMessage<String>("aardvark");
Message<?> message2 = new GenericMessage<String>("bear");
Message<?> message3 = new GenericMessage<String>("cat");
Message<?> message4 = new GenericMessage<String>("dog");
Message<?> message5 = new GenericMessage<String>("elephant");
this.inlineScriptInput.send(message1);
this.inlineScriptInput.send(message2);
this.inlineScriptInput.send(message3);
this.inlineScriptInput.send(message4);
this.inlineScriptInput.send(message5);
assertEquals("bear", shortStrings.receive(0).getPayload());
assertEquals("cat", shortStrings.receive(0).getPayload());
assertEquals("dog", shortStrings.receive(0).getPayload());
assertEquals("aardvark", longStrings.receive(0).getPayload());
assertEquals("elephant", longStrings.receive(0).getPayload());
assertNull(shortStrings.receive(0));
assertNull(longStrings.receive(0));
}
}

View File

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

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="referencedScriptInput">
<script:script
lang="ruby"
location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb">
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
<script:variable name="date" ref="date"/>
</script:script>
</service-activator>
<service-activator input-channel="withScriptVariableGenerator">
<script:script lang="ruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb"
script-variable-generator="scriptVarSource"/>
</service-activator>
<beans:bean id="scriptVarSource"
class="org.springframework.integration.scripting.config.jsr223.Jsr223ServiceActivatorTests.SampleScriptVariSource"/>
<service-activator input-channel="inlineScriptInput">
<script:script lang="ruby">
<![CDATA[
"inline-#{$payload}"
]]>
</script:script>
</service-activator>
<beans:bean id="date" class="java.util.Date" scope="prototype">
<aop:scoped-proxy/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="inlineScriptInput">
<script:script lang="ruby">
<![CDATA[
return "inline-#{$payload}-" + "#{$foo}" + " - " + $bar + " - " + $date
]]>
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
</script:script>
</service-activator>
</beans:beans>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<service-activator input-channel="withScriptVariableGenerator">
<script:script lang="ruby" location="org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.rb"
script-variable-generator="scriptVarSource">
<script:variable name="foo" value="foo"/>
<script:variable name="bar" value="bar"/>
</script:script>
</service-activator>
</beans:beans>

View File

@@ -0,0 +1,152 @@
/*
* 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.config.jsr223;
import static junit.framework.Assert.assertFalse;
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;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.scripting.ScriptVariableGenerator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @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{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
Thread.sleep(1000);
}
String value1 = (String) replyChannel.receive(0).getPayload();
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
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{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.withScriptVariableGenerator.send(message);
Thread.sleep(1000);
}
String value1 = (String) replyChannel.receive(0).getPayload();
String value2 = (String) replyChannel.receive(0).getPayload();
String value3 = (String) replyChannel.receive(0).getPayload();
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
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{
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
}
assertEquals("inline-test-1", replyChannel.receive(0).getPayload());
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{
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());
}
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");
variables.put("bar", "bar");
variables.put("date", new Date());
variables.put("payload", message.getPayload());
variables.put("headers", message.getHeaders());
return variables;
}
}
}

View File

@@ -0,0 +1 @@
"ruby-#{$payload}-#{$foo} - #{$bar} - #{$date}"

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:jsr223="http://www.springframework.org/schema/integration/jsr223"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd
http://www.springframework.org/schema/integration/jsr223 http://www.springframework.org/schema/integration/jsr223/spring-integration-jsr223.xsd">
<splitter input-channel="referencedScriptInput">
<script:script lang="groovy"
location="org/springframework/integration/scripting/config/jsr223/Jsr223SplitterTests.groovy"/>
</splitter>
<splitter input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return payload.split('\\s+')
]]></script:script>
</splitter>
</beans:beans>

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2010 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.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223SplitterTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message = MessageBuilder.withPayload("x,y,z").setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
assertEquals("x", replyChannel.receive(0).getPayload());
assertEquals("y", replyChannel.receive(0).getPayload());
assertEquals("z", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message = MessageBuilder.withPayload("a b c").setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
assertEquals("a", replyChannel.receive(0).getPayload());
assertEquals("b", replyChannel.receive(0).getPayload());
assertEquals("c", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:script="http://www.springframework.org/schema/integration/script"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/script http://www.springframework.org/schema/integration/script.xsd">
<transformer input-channel="referencedScriptInput">
<script:script
lang="ruby"
location="org/springframework/integration/scripting/config/jsr223/Jsr223TransformerTests.rb"/>
</transformer>
<transformer input-channel="inlineScriptInput">
<script:script lang="groovy"><![CDATA[
return "inline-$payload"
]]></script:script>
</transformer>
</beans:beans>

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2010 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.config.jsr223;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Jsr223TransformerTests {
@Autowired
private MessageChannel referencedScriptInput;
@Autowired
private MessageChannel inlineScriptInput;
@Test
public void referencedScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.referencedScriptInput.send(message);
}
assertEquals("ruby-test-1", replyChannel.receive(0).getPayload());
assertEquals("ruby-test-2", replyChannel.receive(0).getPayload());
assertEquals("ruby-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
@Test
public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
for (int i = 1; i <= 3; i++) {
Message<?> message = MessageBuilder.withPayload("test-" + i).setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message);
}
assertEquals("inline-test-1", replyChannel.receive(0).getPayload().toString());
assertEquals("inline-test-2", replyChannel.receive(0).getPayload().toString());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload().toString());
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,8 @@
class Transformer
def transform(payload)
"ruby-#{payload}"
end
end
Transformer.new.transform($payload)

View File

@@ -0,0 +1,73 @@
/*
* 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.springframework.core.io.ClassPathResource;
import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutingMessageProcessor;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author David Turanski
*
*/
public class Jsr223ScriptExecutingMessageProcessorTests {
Jsr223ScriptExecutor executor;
@Before
public void setUp() {
executor = new Jsr223ScriptExecutor("jruby");
}
@Test
public void testExecuteWithVariables(){
Map<String,Object> vars = new HashMap<String,Object>();
vars.put("one",1);
vars.put("two","two");
vars.put("three", new Integer(3));
ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb"));
Jsr223ScriptExecutingMessageProcessor messageProcessor = new Jsr223ScriptExecutingMessageProcessor(scriptSource,executor,vars);
Message<?> message = new GenericMessage<String>("hello");
Object obj = messageProcessor.processMessage(message);
assertEquals("hello modified",obj.toString().substring(0,"hello modified".length()));
}
@Test
public void testWithNoVars(){
ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb"));
Jsr223ScriptExecutingMessageProcessor messageProcessor = new Jsr223ScriptExecutingMessageProcessor(scriptSource,executor);
Message<?> message = new GenericMessage<String>("hello");
Object obj = messageProcessor.processMessage(message);
assertEquals("hello modified",obj.toString().substring(0,"hello modified".length()));
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.scripting.ScriptExecutor;
import org.springframework.integration.scripting.jsr223.Jsr223ScriptExecutor;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.scripting.support.StaticScriptSource;
/**
* @author David Turanski
*
*/
public class Jsr223ScriptExecutorTests {
@Test
public void test(){
ScriptExecutor executor = new Jsr223ScriptExecutor("jruby");
executor.executeScript(new StaticScriptSource("puts 'hello, world'"));
executor.executeScript(new StaticScriptSource("puts 'hello, again'"));
Map<String,Object> variables = new HashMap<String,Object>();
Map<String,Object> headers = new HashMap<String,Object>();
headers.put("one",1);
headers.put("two","two");
headers.put("three", new Integer(3));
variables.put("payload", "payload");
variables.put("headers", headers);
String result = (String)executor.executeScript(
new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")),
variables
);
assertEquals("payload modified",result.substring(0,"payload modified".length()));
}
@Test
public void testJs(){
ScriptExecutor executor = new Jsr223ScriptExecutor("js");
Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();"));
assertEquals("js",obj.toString());
}
}

View File

@@ -0,0 +1,9 @@
require 'java'
class RubyHello
include_class 'com.dturanski.test.jruby.Hello'
def say
"hello,world"
end
end
RubyHello.new

View File

@@ -0,0 +1,12 @@
include Java
include_class 'java.util.Date'
#payload and headers a global variable
if $payload
$payload = $payload+" modified #{Date.new}"
end
puts $payload
if $headers
$headers.each {|key, value| puts "#{key} is #{value}" }
end
puts "#{$one} #{$two} #{$three}"
$payload

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %-5p: %c - %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework">
<level value="info" />
</logger>
<logger name="org.springframework.integration.jsr223">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
</root>
</log4j:configuration>