moving spring-integration-groovy from sandbox to trunk

This commit is contained in:
Mark Fisher
2010-07-21 16:47:12 +00:00
parent af6bd18145
commit a39b3bd1fd
30 changed files with 1160 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-integration-groovy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,6 @@
#Wed Jul 14 06:20:12 BST 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5

View File

@@ -0,0 +1,9 @@
#Wed Jul 14 06:19:35 BST 2010
activeProfiles=
eclipse.preferences.version=1
fullBuildGoals=process-test-resources
includeModules=false
resolveWorkspaceProjects=true
resourceFilterGoals=process-resources resources\:testResources
skipCompilerPlugin=true
version=1

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.3.3.201005160334-M1]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-groovy</artifactId>
<packaging>jar</packaging>
<name>Spring Integration Groovy Support</name>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,77 @@
/*
* 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.groovy;
import groovy.lang.Binding;
import groovy.lang.GString;
import groovy.lang.GroovyObject;
import groovy.lang.Script;
import org.springframework.integration.core.Message;
import org.springframework.integration.handler.AbstractScriptExecutingMessageProcessor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
import org.springframework.scripting.groovy.GroovyScriptFactory;
import org.springframework.util.Assert;
/**
* @author Dave Syer
* @author Mark Fisher
* @since 2.0
*/
public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor {
private final GroovyScriptFactory scriptFactory;
private final MessageContextBindingCustomizer customizer = new MessageContextBindingCustomizer();
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource) {
super(scriptSource);
this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizer);
}
@Override
protected Object executeScript(ScriptSource scriptSource, Message<?> message) throws Exception {
synchronized (this) {
this.customizer.setMessage(message);
Object result = this.scriptFactory.getScriptedObject(scriptSource, null);
return (result instanceof GString) ? result.toString() : result;
}
}
private static class MessageContextBindingCustomizer implements GroovyObjectCustomizer {
private volatile Message<?> message;
public void setMessage(Message<?> message) {
this.message = message;
}
public void customize(GroovyObject goo) {
Assert.state(goo instanceof Script, "Expected a Script");
if (this.message != null) {
Binding binding = ((Script) goo).getBinding();
binding.setVariable("payload", this.message.getPayload());
binding.setVariable("headers", this.message.getHeaders());
}
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.groovy.config;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
/**
* @author Mark Fisher
* @since 2.0
*/
public class GroovyNamespaceHandler extends AbstractIntegrationNamespaceHandler {
public void init() {
this.registerBeanDefinitionParser("script", new GroovyScriptParser());
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.groovy.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
/**
* @author Mark Fisher
* @since 2.0
*/
public class GroovyScriptParser extends AbstractSingleBeanDefinitionParser {
private static final String LOCATION_ATTRIBUTE = "location";
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor";
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
builder.addConstructorArgValue(this.resolveScriptSource(element, parserContext.getReaderContext()));
}
private Object resolveScriptSource(Element element, XmlReaderContext readerContext) {
boolean hasScriptLocation = element.hasAttribute(LOCATION_ATTRIBUTE);
String scriptText = DomUtils.getTextValue(element);
if (!(hasScriptLocation ^ StringUtils.hasText(scriptText))) {
readerContext.error("Either the 'location' attribute or inline script text must be provided, but not both.", element);
return null;
}
else if (hasScriptLocation) {
BeanDefinitionBuilder resourceScriptSourceBuilder =
BeanDefinitionBuilder.genericBeanDefinition("org.springframework.scripting.support.ResourceScriptSource");
resourceScriptSourceBuilder.addConstructorArgValue(element.getAttribute(LOCATION_ATTRIBUTE));
return resourceScriptSourceBuilder.getBeanDefinition();
}
return new StaticScriptSource(scriptText, "groovy.lang.Script");
}
}

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/groovy=org.springframework.integration.groovy.config.GroovyNamespaceHandler

View File

@@ -0,0 +1,2 @@
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy-2.0.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.0.xsd
http\://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd=org/springframework/integration/groovy/config/spring-integration-groovy-2.0.xsd

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/groovy"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:integration="http://www.springframework.org/schema/integration"
targetNamespace="http://www.springframework.org/schema/integration/groovy"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:import namespace="http://www.springframework.org/schema/integration"
schemaLocation="http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" />
<xsd:element name="script">
<xsd:annotation>
<xsd:documentation>
Configures an inner bean that will generate a Groovy Script.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="location">
<xsd:annotation>
<xsd:documentation>
Resource location path for the Script. Either this or an inline script
as body text should be provided, but not both.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,213 @@
/*
* 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.groovy;
import static org.junit.Assert.assertEquals;
import groovy.lang.GroovyObject;
import groovy.lang.Script;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
import org.springframework.scripting.groovy.GroovyScriptFactory;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.Assert;
/**
* @author Dave Syer
*/
public class GroovyExpressionTests {
private static Log logger = LogFactory.getLog(GroovyExpressionTests.class);
@Before
public void setLogLevel() {
LogManager.getLogger(getClass()).setLevel(Level.DEBUG);
}
@After
public void resetLogLevel() {
LogManager.getLogger(getClass()).setLevel(Level.INFO);
}
@Test
public void testScriptFactoryCustomizer() throws Exception {
Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
ResourceScriptSource scriptSource = new ResourceScriptSource(new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript"));
Object scriptedObject = factory.getScriptedObject(scriptSource, null);
assertEquals("name=foo", scriptedObject.toString());
customizer.setMap(Collections.singletonMap("name", (Object) "bar"));
scriptedObject = factory.getScriptedObject(scriptSource, null);
assertEquals("name=bar", scriptedObject.toString());
}
@Test
public void testScriptFactoryCustomizerThreadSafety() throws Exception {
final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
final ResourceScriptSource scriptSource = new ResourceScriptSource(new NamedByteArrayResource(
"\"name=${name}\"".getBytes(), "InlineScript"));
Object scriptedObject = factory.getScriptedObject(scriptSource, null);
assertEquals("name=foo", scriptedObject.toString());
CompletionService<String> completionService = new ExecutorCompletionService<String>(Executors.newFixedThreadPool(10));
for (int i = 0; i < 100; i++) {
final String name = "bar" + i;
completionService.submit(new Callable<String>() {
public String call() throws Exception {
Object scriptedObject;
synchronized (customizer) {
customizer.setMap(Collections.singletonMap("name", (Object) name));
scriptedObject = factory.getScriptedObject(scriptSource, null);
}
String result = scriptedObject.toString();
logger.debug("Result=" + result + " with name=" + name);
if (!("name=" + name).equals(result)) {
throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
}
return name;
}
});
}
Set<String> set = new HashSet<String>();
for (int i = 0; i < 100; i++) {
set.add(completionService.take().get());
}
assertEquals(100, set.size());
}
@Test
public void testScriptFactoryCustomizerStatic() throws Exception {
final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
final ResourceScriptSource scriptSource = new ResourceScriptSource(new NamedByteArrayResource(
"\"name=${name}\"".getBytes(), "InlineScript"));
Object scriptedObject = factory.getScriptedObject(scriptSource, null);
assertEquals("name=foo", scriptedObject.toString());
CompletionService<String> completionService = new ExecutorCompletionService<String>(Executors.newFixedThreadPool(10));
for (int i = 0; i < 100; i++) {
final String name = "bar" + i;
completionService.submit(new Callable<String>() {
public String call() throws Exception {
Object scriptedObject = factory.getScriptedObject(scriptSource, null);
String result = scriptedObject.toString();
logger.debug("Result=" + result + " with name=" + name);
if (!("name=foo").equals(result)) {
throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
}
return name;
}
});
}
Set<String> set = new HashSet<String>();
for (int i = 0; i < 100; i++) {
set.add(completionService.take().get());
}
assertEquals(100, set.size());
}
@Test
public void testScriptFactoryCustomizerThreadSafetyWithNewScript() throws Exception {
final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
CompletionService<String> completionService = new ExecutorCompletionService<String>(Executors.newFixedThreadPool(5));
for (int i = 0; i < 100; i++) {
final String name = "Bar" + i;
completionService.submit(new Callable<String>() {
public String call() throws Exception {
Object scriptedObject;
synchronized (customizer) {
customizer.setMap(Collections.singletonMap("name", (Object) name));
ResourceScriptSource scriptSource = new ResourceScriptSource(new NamedByteArrayResource(
"\"name=${name}\"".getBytes(), "InlineScript" + name));
scriptedObject = factory.getScriptedObject(scriptSource, null);
}
String result = scriptedObject.toString();
logger.debug("Result=" + result + " with name=" + name);
if (!("name=" + name).equals(result)) {
throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
}
return name;
}
});
}
Set<String> set = new HashSet<String>();
for (int i = 0; i < 100; i++) {
set.add(completionService.take().get());
}
assertEquals(100, set.size());
}
private static class Customizer implements GroovyObjectCustomizer {
private Map<String, Object> map = new HashMap<String, Object>();
public Customizer(Map<String, Object> map) {
super();
this.map.putAll(map);
}
public void customize(GroovyObject goo) {
Assert.state(goo instanceof Script, "Expected a Script");
for (Map.Entry<String, Object> entry : map.entrySet()) {
((Script) goo).getBinding().setVariable(entry.getKey(), entry.getValue());
}
}
public void setMap(Map<String, Object> map) {
this.map.clear();
this.map.putAll(map);
}
}
private static class NamedByteArrayResource extends ByteArrayResource {
private final String fileName;
public NamedByteArrayResource(byte[] bytes, String fileName) {
super(bytes);
this.fileName = fileName;
}
@Override
public String getFilename() throws IllegalStateException {
return fileName;
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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.groovy;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.springframework.core.io.AbstractResource;
import org.springframework.integration.core.Message;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author Mark Fisher
* @since 2.0
*/
public class GroovyScriptExecutingMessageProcessorTests {
@Test
public void simpleTest() throws Exception {
String script = "return \"payload is $payload, header is $headers.testHeader\"";
Message<?> message = MessageBuilder.withPayload("foo").setHeader("testHeader", "bar").build();
TestResource resource = new TestResource(script, "simpleTest");
ScriptSource scriptSource = new ResourceScriptSource(resource);
MessageProcessor processor = new GroovyScriptExecutingMessageProcessor(scriptSource);
Object result = processor.processMessage(message);
assertEquals("payload is foo, header is bar", result.toString());
}
private static class TestResource extends AbstractResource {
private final String scriptString;
private final String filename;
private TestResource(String scriptString, String filename) {
this.scriptString = scriptString;
this.filename = filename;
}
public String getDescription() {
return "test";
}
@Override
public String getFilename() {
return this.filename;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(scriptString.getBytes("UTF-8"));
}
}
}

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:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<filter input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyFilterTests.groovy"/>
</filter>
<filter input-channel="inlineScriptInput">
<groovy:script><![CDATA[
return payload == 'good'
]]></groovy:script>
</filter>
</beans:beans>

View File

@@ -0,0 +1,82 @@
/*
* 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.groovy.config;
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.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.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 GroovyFilterTests {
@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,31 @@
<?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:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<channel id="shortStrings">
<queue/>
</channel>
<channel id="longStrings">
<queue/>
</channel>
<router input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyRouterTests.groovy"/>
</router>
<router input-channel="inlineScriptInput">
<groovy:script><![CDATA[
return payload.length() > 5 ? 'longStrings' : 'shortStrings'
]]></groovy:script>
</router>
</beans:beans>

View File

@@ -0,0 +1 @@
return payload.length() > 3 ? 'longStrings' : 'shortStrings'

View File

@@ -0,0 +1,96 @@
/*
* 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.groovy.config;
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.channel.PollableChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.StringMessage;
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 GroovyRouterTests {
@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 StringMessage("aardvark");
Message<?> message2 = new StringMessage("bear");
Message<?> message3 = new StringMessage("cat");
Message<?> message4 = new StringMessage("dog");
Message<?> message5 = new StringMessage("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 StringMessage("aardvark");
Message<?> message2 = new StringMessage("bear");
Message<?> message3 = new StringMessage("cat");
Message<?> message4 = new StringMessage("dog");
Message<?> message5 = new StringMessage("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,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:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<service-activator input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"/>
</service-activator>
<service-activator input-channel="inlineScriptInput">
<groovy:script><![CDATA[
return "inline-$payload"
]]></groovy:script>
</service-activator>
</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.groovy.config;
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.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.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 GroovyServiceActivatorTests {
@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("groovy-test-1", replyChannel.receive(0).getPayload());
assertEquals("groovy-test-2", replyChannel.receive(0).getPayload());
assertEquals("groovy-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());
assertEquals("inline-test-2", replyChannel.receive(0).getPayload());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
}

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:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<splitter input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovySplitterTests.groovy"/>
</splitter>
<splitter input-channel="inlineScriptInput">
<groovy:script><![CDATA[
return payload.split('\\s+')
]]></groovy: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.groovy.config;
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.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.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 GroovySplitterTests {
@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,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:groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy
http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<transformer input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyTransformerTests.groovy"/>
</transformer>
<transformer input-channel="inlineScriptInput">
<groovy:script><![CDATA[
return "inline-$payload"
]]></groovy: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.groovy.config;
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.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.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 GroovyTransformerTests {
@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("groovy-test-1", replyChannel.receive(0).getPayload());
assertEquals("groovy-test-2", replyChannel.receive(0).getPayload());
assertEquals("groovy-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());
assertEquals("inline-test-2", replyChannel.receive(0).getPayload());
assertEquals("inline-test-3", replyChannel.receive(0).getPayload());
assertNull(replyChannel.receive(0));
}
}

View File

@@ -0,0 +1,9 @@
Bundle-SymbolicName: org.springframework.integration.groovy
Bundle-Name: Spring Integration Groovy Support
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template:
org.apache.commons.logging;version="[1.1.1, 2.0.0)",
org.springframework.integration.*;version="[2.0.0, 2.0.1)",
org.springframework.*;version="[3.0.0, 4.0.0)",
groovy.*;version="[1.7.3, 2.0.0)"