INT-1377, added support for dynamics in XML XPath router, added mapping support for XPath router configuration

This commit is contained in:
Oleg Zhurakousky
2010-10-13 16:16:48 -04:00
parent 5b6a96c34f
commit 6c053a9e11
5 changed files with 136 additions and 14 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.xml.config;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import org.junit.After;
@@ -25,11 +27,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.w3c.dom.Document;
@@ -195,5 +201,68 @@ public class XPathRouterParserTests {
inputChannel.send(MessageBuilder.withPayload("<unrelated/>").build());
assertEquals("Wrong count of messages on default output channel",1, defaultOutput.getQueueSize());
}
@Test
public void testWithDynamicChanges() throws Exception {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
MessageChannel inputChannel = ac.getBean("xpathRouterEmptyChannel", MessageChannel.class);
PollableChannel channelA = ac.getBean("channelA", PollableChannel.class);
PollableChannel channelB = ac.getBean("channelB", PollableChannel.class);
Document doc = XmlTestUtil.getDocumentForString("<name>channelA</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputChannel.send(docMessage);
assertNotNull(channelA.receive(10));
assertNull(channelB.receive(10));
EventDrivenConsumer routerEndpoint = ac.getBean("xpathRouterEmpty", EventDrivenConsumer.class);
AbstractMessageRouter xpathRouter = (AbstractMessageRouter) TestUtils.getPropertyValue(routerEndpoint, "handler");
xpathRouter.setChannelMapping("channelA", "channelB");
inputChannel.send(docMessage);
assertNotNull(channelB.receive(10));
assertNull(channelA.receive(10));
}
@Test
public void testWithDynamicChangesWithExistingMappings() throws Exception {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
MessageChannel inputChannel = ac.getBean("xpathRouterWithMappingChannel", MessageChannel.class);
PollableChannel channelA = ac.getBean("channelA", PollableChannel.class);
PollableChannel channelB = ac.getBean("channelB", PollableChannel.class);
Document doc = XmlTestUtil.getDocumentForString("<name>channelA</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputChannel.send(docMessage);
assertNull(channelA.receive(10));
assertNotNull(channelB.receive(10));
EventDrivenConsumer routerEndpoint = ac.getBean("xpathRouterWithMapping", EventDrivenConsumer.class);
AbstractMessageRouter xpathRouter = (AbstractMessageRouter) TestUtils.getPropertyValue(routerEndpoint, "handler");
xpathRouter.removeChannelMapping("channelA");
inputChannel.send(docMessage);
assertNotNull(channelA.receive(10));
assertNull(channelB.receive(10));
}
@Test
public void testWithDynamicChangesWithExistingMappingsAndMultiChannel() throws Exception {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
MessageChannel inputChannel = ac.getBean("multiChannelRouterChannel", MessageChannel.class);
PollableChannel channelA = ac.getBean("channelA", PollableChannel.class);
PollableChannel channelB = ac.getBean("channelB", PollableChannel.class);
Document doc = XmlTestUtil.getDocumentForString("<root><name>channelA</name><name>channelB</name></root>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputChannel.send(docMessage);
assertNotNull(channelA.receive(10));
assertNotNull(channelA.receive(10));
assertNull(channelB.receive(10));
EventDrivenConsumer routerEndpoint = ac.getBean("xpathRouterWithMappingMultiChannel", EventDrivenConsumer.class);
AbstractMessageRouter xpathRouter = (AbstractMessageRouter) TestUtils.getPropertyValue(routerEndpoint, "handler");
xpathRouter.removeChannelMapping("channelA");
xpathRouter.removeChannelMapping("channelB");
inputChannel.send(docMessage);
assertNotNull(channelA.receive(10));
assertNotNull(channelB.receive(10));
}
}

View File

@@ -0,0 +1,34 @@
<?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:int-xml="http://www.springframework.org/schema/integration/xml"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml-2.0.xsd">
<int-xml:xpath-router id="xpathRouterEmpty" input-channel="xpathRouterEmptyChannel">
<int-xml:xpath-expression expression="/name"/>
</int-xml:xpath-router>
<int-xml:xpath-router id="xpathRouterWithMapping" input-channel="xpathRouterWithMappingChannel">
<int-xml:xpath-expression expression="/name"/>
<int-xml:mapping value="channelA" channel="channelB"/>
</int-xml:xpath-router>
<int-xml:xpath-router id="xpathRouterWithMappingMultiChannel" input-channel="multiChannelRouterChannel" multi-channel="true">
<int-xml:xpath-expression expression="/root/name"/>
<int-xml:mapping value="channelA" channel="channelA"/>
<int-xml:mapping value="channelB" channel="channelA"/>
</int-xml:xpath-router>
<int:channel id="channelA">
<int:queue/>
</int:channel>
<int:channel id="channelB">
<int:queue/>
</int:channel>
</beans>