INT-1727 added namespace support for script-variable-source

This commit is contained in:
Oleg Zhurakousky
2011-01-27 16:46:59 -05:00
parent c3db043334
commit e9b85bca18
6 changed files with 129 additions and 29 deletions

View File

@@ -17,6 +17,15 @@
<groovy:variable name="date" ref="date"/>
</groovy:script>
</service-activator>
<service-activator input-channel="withScriptVariableSource">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"
script-variable-source="scriptVarSource"/>
</service-activator>
<beans:bean id="scriptVarSource"
class="org.springframework.integration.groovy.config.GroovyServiceActivatorTests.SampleScriptVariSource"/>
<service-activator input-channel="inlineScriptInput">
<groovy:script>

View File

@@ -0,0 +1,21 @@
<?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-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<service-activator input-channel="withScriptVariableSource">
<groovy:script location="org/springframework/integration/groovy/config/GroovyServiceActivatorTests.groovy"
script-variable-source="scriptVarSource">
<groovy:variable name="foo" value="foo"/>
<groovy:variable name="bar" value="bar"/>
</groovy:script>
</service-activator>
</beans:beans>

View File

@@ -21,6 +21,9 @@ 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.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,6 +33,7 @@ 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.groovy.DefaultScriptVariableSource;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -47,6 +51,9 @@ public class GroovyServiceActivatorTests {
@Autowired
private MessageChannel inlineScriptInput;
@Autowired
private MessageChannel withScriptVariableSource;
@Test
@@ -71,6 +78,29 @@ public class GroovyServiceActivatorTests {
assertNull(replyChannel.receive(0));
}
@Test
public void withScriptVariableSource() 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.withScriptVariableSource.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("groovy-test-1-foo - bar"));
assertTrue(value2.startsWith("groovy-test-2-foo - bar"));
assertTrue(value3.startsWith("groovy-test-3-foo - bar"));
// becouse 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{
@@ -90,5 +120,17 @@ public class GroovyServiceActivatorTests {
public void inlineScriptAndVariables() throws Exception{
new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-context.xml", this.getClass());
}
@Test(expected=BeanDefinitionParsingException.class)
public void variablesAndScriptVariableSource() throws Exception{
new ClassPathXmlApplicationContext("GroovyServiceActivatorTests-fail-withsource-context.xml", this.getClass());
}
public static class SampleScriptVariSource extends DefaultScriptVariableSource{
protected void doResolveScriptVariables(Map<String, Object> variables){
variables.put("foo", "foo");
variables.put("bar", "bar");
variables.put("date", new Date());
}
}
}