From 1d0b773f2b29f6d681a725a0fa52ffa2dfcfcc0c Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 14 Oct 2010 06:45:22 -0400 Subject: [PATCH] INT-1517, combined Single and Multi channel XPath routers, modified XPathRouterParser to inherit form the base parser for all routers, combine single/multiple xpath router tests into one --- ...tractChannelNameResolvingRouterParser.java | 11 ++- .../xml/config/XPathRouterParser.java | 62 +++--------- .../xml/router/XPathMultiChannelRouter.java | 95 ------------------- ...tractXPathRouter.java => XPathRouter.java} | 34 ++++++- .../xml/router/XPathSingleChannelRouter.java | 93 ------------------ .../xml/config/spring-integration-xml-2.0.xsd | 1 - .../xml/config/XPathRouterParserTests.java | 1 + .../xml/config/XPathRouterTests-context.xml | 2 +- ...RouterTests.java => XPathRouterTests.java} | 56 +++++++++-- .../router/XPathSingleChannelRouterTests.java | 78 --------------- 10 files changed, 101 insertions(+), 332 deletions(-) delete mode 100644 spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelRouter.java rename spring-integration-xml/src/main/java/org/springframework/integration/xml/router/{AbstractXPathRouter.java => XPathRouter.java} (73%) delete mode 100644 spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelRouter.java rename spring-integration-xml/src/test/java/org/springframework/integration/xml/router/{XPathMultiChannelRouterTests.java => XPathRouterTests.java} (59%) delete mode 100644 spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathSingleChannelRouterTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractChannelNameResolvingRouterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractChannelNameResolvingRouterParser.java index 3c4c3f7c33..0d274480b6 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractChannelNameResolvingRouterParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractChannelNameResolvingRouterParser.java @@ -20,9 +20,11 @@ import java.util.List; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -38,6 +40,10 @@ public abstract class AbstractChannelNameResolvingRouterParser extends AbstractR protected final BeanDefinition parseRouter(Element element, ParserContext parserContext) { BeanDefinition beanDefinition = this.doParseRouter(element, parserContext); if (beanDefinition != null) { + String channelResolver = element.getAttribute("channel-resolver"); + if (StringUtils.hasText(channelResolver)){ + beanDefinition.getPropertyValues().add("channelResolver", new RuntimeBeanReference(channelResolver)); + } // check if mapping is provided otherwise returned values will be treated as channel names List childElements = DomUtils.getChildElementsByTagName(element, "mapping"); if (childElements != null && childElements.size() > 0) { @@ -48,12 +54,9 @@ public abstract class AbstractChannelNameResolvingRouterParser extends AbstractR if (beanClassName.endsWith("PayloadTypeRouter")){ key = childElement.getAttribute("type"); } - else if (beanClassName.endsWith("HeaderValueRouter")){ + else { key = childElement.getAttribute("value"); } - else { - throw new BeanCreationException("Building '" + beanClassName + "' is not supported by this parser"); - } channelMap.put(key, childElement.getAttribute("channel")); } beanDefinition.getPropertyValues().add("channelIdentifierMap", channelMap); diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java index 95f5a7de6a..372f934501 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java @@ -16,84 +16,48 @@ package org.springframework.integration.xml.config; -import java.util.List; - -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.integration.config.xml.AbstractConsumerEndpointParser; -import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.integration.config.xml.AbstractChannelNameResolvingRouterParser; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; /** * Parser for the <xpath-router/> element. * * @author Jonas Partner * @author Mark Fisher + * @author Oleg Zhurakousky */ -public class XPathRouterParser extends AbstractConsumerEndpointParser { +public class XPathRouterParser extends AbstractChannelNameResolvingRouterParser { private XPathExpressionParser xpathParser = new XPathExpressionParser(); - @Override - protected boolean shouldGenerateId() { - return false; - } - - @Override - protected boolean shouldGenerateIdAsFallback() { - return true; - } - - @Override - protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) { + protected BeanDefinition doParseRouter(Element element, + ParserContext parserContext) { + BeanDefinitionBuilder xpathRouterBuilder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.xml.router.XPathRouter"); NodeList xPathExpressionNodes = element.getElementsByTagNameNS( element.getNamespaceURI(), "xpath-expression"); - Assert.isTrue(xPathExpressionNodes.getLength() < 2, - "Only one xpath-expression child can be specified."); + Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified."); String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); boolean xPathExpressionChildPresent = (xPathExpressionNodes.getLength() == 1); boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef); Assert.isTrue(xPathExpressionChildPresent ^ xPathReferencePresent, "Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); - boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); - String classname = "org.springframework.integration.xml.router." + - ((multiChannel) ? "XPathMultiChannelRouter" : "XPathSingleChannelRouter"); - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(classname); - - - List childElements = DomUtils.getChildElementsByTagName(element, "mapping"); - if (childElements != null && childElements.size() > 0) { - ManagedMap channelMap = new ManagedMap(); - for (Element childElement : childElements) { - String key = childElement.getAttribute("value"); - channelMap.put(key, childElement.getAttribute("channel")); - } - builder.addPropertyValue("channelIdentifierMap", channelMap); - } - - if (xPathExpressionChildPresent) { BeanDefinition beanDefinition = this.xpathParser.parse( (Element) xPathExpressionNodes.item(0), parserContext); - builder.addConstructorArgValue(beanDefinition); + xpathRouterBuilder.addConstructorArgValue(beanDefinition); } else { - builder.addConstructorArgReference(xPathExpressionRef); + xpathRouterBuilder.addConstructorArgReference(xPathExpressionRef); } - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required"); - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures"); - IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel-resolver"); - IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel"); - return builder; + return xpathRouterBuilder.getBeanDefinition(); } } diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelRouter.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelRouter.java deleted file mode 100644 index 3da132a52a..0000000000 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathMultiChannelRouter.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.xml.router; - -import java.util.List; -import java.util.Map; - -import org.springframework.integration.Message; -import org.springframework.integration.xml.XmlPayloadConverter; -import org.springframework.util.Assert; -import org.springframework.xml.xpath.NodeMapper; -import org.springframework.xml.xpath.XPathExpression; -import org.w3c.dom.DOMException; -import org.w3c.dom.Node; - -/** - * A router that evaluates the XPath expression using - * {@link XPathExpression#evaluateAsNodeList(Node)} which returns zero or more - * nodes in conjunction with an instance of {@link NodeMapper} to produce zero - * or more channel names. An instance of {@link XmlPayloadConverter} is used to - * extract the payload as a {@link Node}. - * - * @author Jonas Partner - */ -public class XPathMultiChannelRouter extends AbstractXPathRouter { - - private volatile NodeMapper nodeMapper = new TextContentNodeMapper(); - - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String, Map) - */ - public XPathMultiChannelRouter(String expression, Map namespaces) { - super(expression, namespaces); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String, String, String) - */ - public XPathMultiChannelRouter(String expression, String prefix, String namespace) { - super(expression, prefix, namespace); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String) - */ - public XPathMultiChannelRouter(String expression) { - super(expression); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression) - */ - public XPathMultiChannelRouter(XPathExpression expression) { - super(expression); - } - - - public void setNodeMapper(NodeMapper nodeMapper) { - Assert.notNull(nodeMapper, "NodeMapper must not be null"); - this.nodeMapper = nodeMapper; - } - - @SuppressWarnings("unchecked") - public List getChannelIndicatorList(Message message) { - Node node = getConverter().convertToNode(message.getPayload()); - return getXPathExpression().evaluate(node, this.nodeMapper); - } - - - private static class TextContentNodeMapper implements NodeMapper { - - public Object mapNode(Node node, int nodeNum) throws DOMException { - return node.getTextContent(); - } - - } - - - -} diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathRouter.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathRouter.java similarity index 73% rename from spring-integration-xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathRouter.java rename to spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathRouter.java index 4bc897c5ac..23bc2720bc 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/AbstractXPathRouter.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathRouter.java @@ -17,21 +17,29 @@ package org.springframework.integration.xml.router; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.springframework.integration.Message; import org.springframework.integration.router.AbstractMessageRouter; import org.springframework.integration.xml.DefaultXmlPayloadConverter; import org.springframework.integration.xml.XmlPayloadConverter; +import org.springframework.xml.xpath.NodeMapper; import org.springframework.xml.xpath.XPathExpression; import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; /** * Abstract base class for Message Routers that use * {@link XPathExpression} evaluation to determine channel names. * * @author Jonas Partner + * @author Oleg Zhurakousky */ -public abstract class AbstractXPathRouter extends AbstractMessageRouter { +public class XPathRouter extends AbstractMessageRouter { + + private volatile NodeMapper nodeMapper = new TextContentNodeMapper(); private final XPathExpression xPathExpression; @@ -45,7 +53,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter { * @param expression * @param namespaces */ - public AbstractXPathRouter(String expression, Map namespaces) { + public XPathRouter(String expression, Map namespaces) { this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces); } @@ -57,7 +65,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter { * @param prefix * @param namespace */ - public AbstractXPathRouter(String expression, String prefix, String namespace) { + public XPathRouter(String expression, String prefix, String namespace) { Map namespaces = new HashMap(); namespaces.put(prefix, namespace); this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces); @@ -69,7 +77,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter { * * @param expression */ - public AbstractXPathRouter(String expression) { + public XPathRouter(String expression) { this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression); } @@ -78,7 +86,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter { * * @param expression */ - public AbstractXPathRouter(XPathExpression expression) { + public XPathRouter(XPathExpression expression) { this.xPathExpression = expression; } @@ -103,4 +111,20 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter { public String getComponentType(){ return "xml:xpath-router"; } + + @Override + @SuppressWarnings("unchecked") + public List getChannelIndicatorList(Message message) { + Node node = getConverter().convertToNode(message.getPayload()); + return getXPathExpression().evaluate(node, this.nodeMapper); + } + + + private static class TextContentNodeMapper implements NodeMapper { + + public Object mapNode(Node node, int nodeNum) throws DOMException { + return node.getTextContent(); + } + + } } diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelRouter.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelRouter.java deleted file mode 100644 index e5298a314a..0000000000 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelRouter.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2002-2008 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.xml.router; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.springframework.integration.Message; -import org.springframework.integration.MessagingException; -import org.springframework.integration.xml.DefaultXmlPayloadConverter; -import org.springframework.integration.xml.XmlPayloadConverter; -import org.springframework.xml.xpath.XPathExpression; -import org.w3c.dom.Node; - -/** - * Router that evaluates the payload using {@link XPathExpression#evaluateAsString(Node)} - * to extract a channel name. The payload is extracted as a node using the - * provided {@link XmlPayloadConverter} with {@link DefaultXmlPayloadConverter} - * being the default. - * - *

The provided {@link XPathExpression} must evaluate to a non-empty String. - * - * @author Jonas Partner - */ -public class XPathSingleChannelRouter extends AbstractXPathRouter { - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String, Map) - */ - public XPathSingleChannelRouter(String expression, Map namespaces) { - super(expression, namespaces); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String, String, String) - */ - public XPathSingleChannelRouter(String expression, String prefix, String namespace) { - super(expression, prefix, namespace); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(String) - */ - public XPathSingleChannelRouter(String expression) { - super(expression); - } - - /** - * @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression) - */ - public XPathSingleChannelRouter(XPathExpression expression) { - super(expression); - } - - - /** - * Evaluates the payload using {@link XPathExpression#evaluateAsString(Node)} - * - * @throws MessagingException if the {@link XPathExpression} evaluates to - * an empty string - */ - - @Override - protected List getChannelIndicatorList(Message message) { - List channels = new ArrayList(); - Node node = getConverter().convertToNode(message.getPayload()); - String result = getXPathExpression().evaluateAsString(node); - if (result == null || "".equals(result)) { - return null; - } else { - channels.add(result); - } - - return channels; - - } - -} diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd index 438f986123..dde312d875 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd @@ -387,7 +387,6 @@ - diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java index 18abbb4381..7f972da868 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java @@ -43,6 +43,7 @@ import org.w3c.dom.Document; /** * @author Jonas Partner * @author Mark Fisher + * @author Oleg Zhurakousky */ @ContextConfiguration public class XPathRouterParserTests { diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterTests-context.xml b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterTests-context.xml index f9ecfa7233..a6635ee746 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterTests-context.xml +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathRouterTests-context.xml @@ -17,7 +17,7 @@ - + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathMultiChannelRouterTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathRouterTests.java similarity index 59% rename from spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathMultiChannelRouterTests.java rename to spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathRouterTests.java index a01c8ed642..ee71c63ad5 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathMultiChannelRouterTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathRouterTests.java @@ -18,6 +18,8 @@ package org.springframework.integration.xml.router; import static org.junit.Assert.assertEquals; +import java.util.List; + import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -31,14 +33,14 @@ import org.springframework.xml.xpath.XPathExpressionFactory; /** * @author Jonas Partner */ -public class XPathMultiChannelRouterTests { +public class XPathRouterTests { @Test @SuppressWarnings("unchecked") public void simpleSingleAttribute() throws Exception { Document doc = XmlTestUtil.getDocumentForString(""); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); - XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression); + XPathRouter router = new XPathRouter(expression); Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 1, channelNames.length); assertEquals("Wrong channel name", "one", channelNames[0]); @@ -49,7 +51,7 @@ public class XPathMultiChannelRouterTests { public void multipleNodeValues() throws Exception { Document doc = XmlTestUtil.getDocumentForString("bOnebTwo"); XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book"); - XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression); + XPathRouter router = new XPathRouter(expression); Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray(); assertEquals("Wrong number of channels returned", 2, channelNames.length); assertEquals("Wrong channel name", "bOne", channelNames[0]); @@ -60,7 +62,7 @@ public class XPathMultiChannelRouterTests { @SuppressWarnings("unchecked") public void multipleNodeValuesAsString() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book"); - XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression); + XPathRouter router = new XPathRouter(expression); Object[] channelNames = router.getChannelIndicatorList(new GenericMessage("bOnebTwo")).toArray(); assertEquals("Wrong number of channels returned", 2, channelNames.length); assertEquals("Wrong channel name", "bOne", channelNames[0]); @@ -70,17 +72,59 @@ public class XPathMultiChannelRouterTests { @Test(expected = MessagingException.class) public void nonNodePayload() throws Exception { XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); - XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression); + XPathRouter router = new XPathRouter(expression); router.getChannelIndicatorList(new GenericMessage("test")); } @Test public void nodePayload() throws Exception { - XPathMultiChannelRouter router = new XPathMultiChannelRouter("./three/text()"); + XPathRouter router = new XPathRouter("./three/text()"); Document testDocument = XmlTestUtil.getDocumentForString("bobdave"); Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(testDocument.getElementsByTagName("two").item(0))).toArray(); assertEquals("bob",channelNames[0]); assertEquals("dave",channelNames[1]); } + + @Test + public void testSimpleDocType() throws Exception { + Document doc = XmlTestUtil.getDocumentForString(""); + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathRouter router = new XPathRouter(expression); + Object channelName = router.getChannelIndicatorList(new GenericMessage(doc)).toArray()[0]; + assertEquals("Wrong channel name", "one", channelName); + } + + @Test + public void testSimpleStringDoc() throws Exception { + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathRouter router = new XPathRouter(expression); + Object channelName = router.getChannelIndicatorList(new GenericMessage("")).toArray()[0]; + assertEquals("Wrong channel name", "one", channelName); + } + + @Test(expected = MessagingException.class) + public void testNonNodePayload() throws Exception { + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); + XPathRouter router = new XPathRouter(expression); + router.getChannelIndicatorList(new GenericMessage("test")); + } + + @Test + public void testNodePayload() throws Exception { + XPathRouter router = new XPathRouter("./three/text()"); + Document testDocument = XmlTestUtil.getDocumentForString("bob"); + Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(testDocument + .getElementsByTagName("two").item(0))).toArray(); + assertEquals("bob", channelNames[0]); + } + + @Test + public void testEvaluationReturnsEmptyString() throws Exception { + Document doc = XmlTestUtil.getDocumentForString(""); + XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type"); + XPathRouter router = new XPathRouter(expression); + List channelNames = router.getChannelIndicatorList(new GenericMessage(doc)); + assertEquals(0, channelNames.size()); + } } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathSingleChannelRouterTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathSingleChannelRouterTests.java deleted file mode 100644 index a53b7fb5fc..0000000000 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/router/XPathSingleChannelRouterTests.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.xml.router; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.Node; - -import org.springframework.integration.MessagingException; -import org.springframework.integration.message.GenericMessage; -import org.springframework.integration.xml.util.XmlTestUtil; -import org.springframework.xml.xpath.XPathExpression; -import org.springframework.xml.xpath.XPathExpressionFactory; - -/** - * @author Jonas Partner - */ -public class XPathSingleChannelRouterTests { - - @Test - public void testSimpleDocType() throws Exception { - Document doc = XmlTestUtil.getDocumentForString(""); - XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); - XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression); - Object channelName = router.getChannelIndicatorList(new GenericMessage(doc)).toArray()[0]; - assertEquals("Wrong channel name", "one", channelName); - } - - @Test - public void testSimpleStringDoc() throws Exception { - XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); - XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression); - Object channelName = router.getChannelIndicatorList(new GenericMessage("")).toArray()[0]; - assertEquals("Wrong channel name", "one", channelName); - } - - @Test(expected = MessagingException.class) - public void testNonNodePayload() throws Exception { - XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type"); - XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression); - router.getChannelIndicatorList(new GenericMessage("test")); - } - - @Test - public void testNodePayload() throws Exception { - XPathSingleChannelRouter router = new XPathSingleChannelRouter("./three/text()"); - Document testDocument = XmlTestUtil.getDocumentForString("bob"); - Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(testDocument - .getElementsByTagName("two").item(0))).toArray(); - assertEquals("bob", channelNames[0]); - } - - @Test - public void testEvaluationReturnsEmptyString() throws Exception { - Document doc = XmlTestUtil.getDocumentForString(""); - XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type"); - XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression); - Object channelNames = router.getChannelIndicatorList(new GenericMessage(doc)); - assertEquals("Wrong channel name", null, channelNames); - } - -}