From 8cc19d88b195c7fa078ac4584d286f67c814904b Mon Sep 17 00:00:00 2001 From: Jonas Partner Date: Wed, 1 Oct 2008 12:28:27 +0000 Subject: [PATCH] OPEN - issue INT-309: XPath Message Selector http://jira.springframework.org/browse/INT-309 namespace support --- .../.settings/org.eclipse.jdt.core.prefs | 12 ++ .../IntegrationXmlNamespaceHandler.java | 1 + .../XPathExpressionBeanDefintionBuilder.java | 93 ++++++++++++ .../xml/config/XPathRouterParser.java | 28 +++- .../xml/config/XPathSelectorParser.java | 93 ++++++++++++ .../xml/config/spring-integration-xml-1.0.xsd | 118 ++++++++++----- .../XPathSingleChannelNameResolver.java | 1 + .../xml/config/TestXmlApplicationContext.java | 62 ++++++++ .../TestXmlApplicationContextHelper.java | 42 ++++++ .../config/XPathRouterParserTests-context.xml | 54 ------- .../xml/config/XPathRouterParserTests.java | 140 +++++++++--------- .../xml/config/XPathSelectorParserTests.java | 67 +++++++++ 12 files changed, 543 insertions(+), 168 deletions(-) create mode 100644 org.springframework.integration.xml/.settings/org.eclipse.jdt.core.prefs create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionBeanDefintionBuilder.java create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathSelectorParser.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContext.java create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java delete mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests-context.xml create mode 100644 org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathSelectorParserTests.java diff --git a/org.springframework.integration.xml/.settings/org.eclipse.jdt.core.prefs b/org.springframework.integration.xml/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..ebd13c3bdf --- /dev/null +++ b/org.springframework.integration.xml/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Wed Oct 01 13:21:01 BST 2008 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java index 377d1b87d1..0f4e19dc58 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -30,6 +30,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport { registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser()); registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser()); registerBeanDefinitionParser("xpath-router", new XPathRouterParser()); + registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser()); } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionBeanDefintionBuilder.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionBeanDefintionBuilder.java new file mode 100644 index 0000000000..3b4d16bb05 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionBeanDefintionBuilder.java @@ -0,0 +1,93 @@ +/* + * Copyright 2002-2007 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.config; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class XPathExpressionBeanDefintionBuilder { + + public AbstractBeanDefinition handleXpathExpression(Element element, ParserContext parserContext) { + String strXpathExpression = element.getAttribute("xpath-expression"); + String strXpathExpressionPrefix = element.getAttribute("ns-prefix"); + String strXpathExpressionNamespace = element.getAttribute("ns-uri"); + String nameSpaceMapRef = element.getAttribute("namespace-map"); + + Assert.hasText(strXpathExpression, "xpath-expression attribute is required"); + + + + boolean prefixProvided = StringUtils.hasText(strXpathExpressionPrefix); + boolean namespaceProvided = StringUtils.hasText(strXpathExpressionNamespace); + boolean namespaceMapProvided = StringUtils.hasText(nameSpaceMapRef); + + if (prefixProvided || namespaceProvided) { + Assert.isTrue(prefixProvided && namespaceProvided, + "Both xpath-prefix and xpath-namespace must be specified if one is specified"); + Assert.isTrue(!namespaceMapProvided, "It is not valid to sepcify both xpath-namespace and namespace-map"); + } + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XPathExpressionFactory.class); + builder.setFactoryMethod("createXPathExpression"); + builder.addConstructorArgValue(strXpathExpression); + + if (prefixProvided) { + Map namespaceMap = new HashMap(); + namespaceMap.put(strXpathExpressionPrefix, strXpathExpressionNamespace); + builder.addConstructorArgValue(namespaceMap); + } + else if (StringUtils.hasText(nameSpaceMapRef)) { + builder.addConstructorArgReference(nameSpaceMapRef); + } + else if (element.getChildNodes().getLength() > 0) { + NodeList nodeList = element.getChildNodes(); + Element mapElement = null; + int elementCount = 0; + for (int i = 0; i < nodeList.getLength(); i++) { + Node currentNode = nodeList.item(i); + if (currentNode.getNodeType() == Node.ELEMENT_NODE) { + mapElement = (Element) currentNode; + elementCount++; + } + } + Assert.isTrue(elementCount == 1, "Only one namespace map child allowed"); + if (mapElement != null) { + Map namespaceMap = parseNamespaceMapElement(mapElement, parserContext, builder.getBeanDefinition()); + builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(namespaceMap); + } + } + return builder.getBeanDefinition(); + } + + protected Map parseNamespaceMapElement(Element element, ParserContext parserContext, BeanDefinition parentDefinition) { + BeanDefinitionParserDelegate beanParser = new BeanDefinitionParserDelegate(parserContext.getReaderContext()); + beanParser.initDefaults(element.getOwnerDocument().getDocumentElement()); + return beanParser.parseMapElement(element, parentDefinition); + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java index 51c5f98e40..ece7d10575 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java @@ -16,23 +16,24 @@ package org.springframework.integration.xml.config; -import org.w3c.dom.Element; - +import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; import org.springframework.integration.xml.router.XPathMultiChannelNameResolver; import org.springframework.integration.xml.router.XPathSingleChannelNameResolver; import org.springframework.util.StringUtils; -import org.springframework.xml.xpath.XPathExpression; -import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Element; /** * @author Jonas Partner */ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser { + private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder(); + @Override protected boolean shouldGenerateId() { return false; @@ -45,22 +46,35 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser { @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); String xPathExpression = element.getAttribute("xpath-expression"); + String strXpathExpressionPrefix = element.getAttribute("xpath-prefix"); + String strXpathExpressionNamespace = element.getAttribute("xpath-namespace"); + String nameSpaceMapRef = element.getAttribute("namespace-map"); + String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); - if ((StringUtils.hasText(xPathExpression) && StringUtils.hasText(xPathExpressionRef)) + + boolean strXpathAttSpecified = StringUtils.hasText(xPathExpression) + || StringUtils.hasText(strXpathExpressionPrefix) || StringUtils.hasText(nameSpaceMapRef) + || StringUtils.hasText(strXpathExpressionNamespace); + if ((strXpathAttSpecified && StringUtils.hasText(xPathExpressionRef)) || (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) { throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); } + if (multiChannel) { builder.getBeanDefinition().setBeanClass(XPathMultiChannelNameResolver.class); } else { builder.getBeanDefinition().setBeanClass(XPathSingleChannelNameResolver.class); } + if (StringUtils.hasText(xPathExpression)) { - XPathExpression expression = XPathExpressionFactory.createXPathExpression(xPathExpression); - builder.addConstructorArgValue(expression); + AbstractBeanDefinition xPathExpressionBeanDefinition = xpathBuilder.handleXpathExpression(element, parserContext); + String xpathExpressionBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName( + xPathExpressionBeanDefinition, parserContext.getRegistry()); + builder.addConstructorArgReference(xpathExpressionBeanName); } else { builder.addConstructorArgReference(xPathExpressionRef); diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathSelectorParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathSelectorParser.java new file mode 100644 index 0000000000..597c7a40fa --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathSelectorParser.java @@ -0,0 +1,93 @@ +/* + * Copyright 2002-2007 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.config; + +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.ConfigurationException; +import org.springframework.integration.xml.selector.BooleanTestXPathMessageSelector; +import org.springframework.integration.xml.selector.StringValueTestXPathMessageSelector; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * + * @author Jonas Partner + * + */ +public class XPathSelectorParser extends AbstractSingleBeanDefinitionParser { + + private XPathExpressionBeanDefintionBuilder xpathBuilder = new XPathExpressionBeanDefintionBuilder(); + + @Override + protected boolean shouldGenerateId() { + return false; + } + + @Override + protected boolean shouldGenerateIdAsFallback() { + return true; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + + String evaluationType = element.getAttribute("evaluation-result-type"); + String xPathExpression = element.getAttribute("xpath-expression"); + String strXpathExpressionPrefix = element.getAttribute("xpath-prefix"); + String strXpathExpressionNamespace = element.getAttribute("xpath-namespace"); + String nameSpaceMapRef = element.getAttribute("namespace-map"); + String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); + String stringTestValue = element.getAttribute("string-test-value"); + + boolean strXpathAttSpecified = StringUtils.hasText(xPathExpression) + || StringUtils.hasText(strXpathExpressionPrefix) || StringUtils.hasText(nameSpaceMapRef) + || StringUtils.hasText(strXpathExpressionNamespace); + + if ((strXpathAttSpecified && StringUtils.hasText(xPathExpressionRef)) + || (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) { + throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); + } + + if (evaluationType.equals("boolean")) { + builder.getBeanDefinition().setBeanClass(BooleanTestXPathMessageSelector.class); + Assert.state(!StringUtils.hasText(stringTestValue), "string-test-value should not be specified when evaluation-result-type is boolean"); + } + else if (evaluationType.equals("string")){ + Assert.hasText(stringTestValue, "string-test-value must be specified when evaluation-result-type is string"); + builder.addConstructorArgValue(stringTestValue); + builder.getBeanDefinition().setBeanClass(StringValueTestXPathMessageSelector.class); + + } else { + throw new ConfigurationException("Unrecognised value: " + evaluationType + " for evaluation-result-type only boolean or string supported"); + } + + if (StringUtils.hasText(xPathExpression)) { + AbstractBeanDefinition xPathExpressionBeanDefinition = xpathBuilder.handleXpathExpression(element, null); + String xpathExpressionBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName( + xPathExpressionBeanDefinition, parserContext.getRegistry()); + builder.addConstructorArgReference(xpathExpressionBeanName); + } + else { + builder.addConstructorArgReference(xPathExpressionRef); + } + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd index 1342c3b4c0..d3f4832996 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd @@ -1,20 +1,16 @@ - - - - + xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" + xmlns:tool="http://www.springframework.org/schema/tool" + targetNamespace="http://www.springframework.org/schema/integration/xml" + elementFormDefault="qualified" attributeFormDefault="unqualified"> + + Defines the configuration elements for Spring Integration's XML support. - @@ -24,22 +20,23 @@ - + - - + + - - + + - @@ -49,12 +46,12 @@ - + - @@ -64,24 +61,28 @@ - - - - + + + + - - + + - + - @@ -89,17 +90,64 @@ Defines an XPath router. - - - - + + + + + + + + + + - + + + + + Defines an XPath selector. + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - \ No newline at end of file diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java index c7ec5fbe2d..1e7c3bf83b 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/router/XPathSingleChannelNameResolver.java @@ -30,6 +30,7 @@ import org.w3c.dom.Node; * 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} should evaluate to a non empty string * @author Jonas Partner */ public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver { diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContext.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContext.java new file mode 100644 index 0000000000..187bbc625f --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContext.java @@ -0,0 +1,62 @@ +/* + * Copyright 2002-2007 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.config; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.core.io.AbstractResource; +import org.springframework.core.io.Resource; + +public class TestXmlApplicationContext extends AbstractXmlApplicationContext { + + private final Resource[] resources; + + public TestXmlApplicationContext(String ... xmlStrings){ + resources = new Resource[xmlStrings.length]; + for (int i = 0 ; i < xmlStrings.length; i++) { + resources[i] = new TestResource(xmlStrings[i]); + } + refresh(); + } + + @Override + protected Resource[] getConfigResources() { + return resources; + } + + private static class TestResource extends AbstractResource{ + + String xmlString; + + TestResource(String xmlString){ + this.xmlString = xmlString; + } + + + public String getDescription() { + return "test"; + } + + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(xmlString.getBytes("UTF-8")); + } + + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java new file mode 100644 index 0000000000..3d3154bb50 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2007 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.config; + +public class TestXmlApplicationContextHelper { + + public static TestXmlApplicationContext getTestAppContext(String xmlFragment) { + String xml = header + xmlFragment + footer; + TestXmlApplicationContext ctx = new TestXmlApplicationContext(xml); + return ctx; + } + + private final static String header = "" + + ""; + + private final static String footer = ""; + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests-context.xml b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests-context.xml deleted file mode 100644 index 02e46d21e2..0000000000 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests-context.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java index 01f94bdbdf..4c53cc0ad3 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java @@ -16,100 +16,96 @@ package org.springframework.integration.xml.config; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.w3c.dom.Document; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.xml.router.XPathSingleChannelNameResolver; import org.springframework.integration.xml.util.XmlTestUtil; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import org.w3c.dom.Document; /** * @author Jonas Partner */ @ContextConfiguration -public class XPathRouterParserTests extends AbstractJUnit4SpringContextTests{ +public class XPathRouterParserTests { - @Autowired @Qualifier("inputOne") - MessageChannel inputOne; - - @Autowired @Qualifier("inputTwo") - MessageChannel inputTwo; - - @Autowired @Qualifier("outputOne") - PollableChannel outputOne; - - @Autowired @Qualifier("outputTwo") - PollableChannel outputTwo; - - @Autowired @Qualifier("errorChannel") - PollableChannel errorChannel; - - - @SuppressWarnings("unchecked") @Test - public void testOutputOne() throws Exception{ + public void testSimpleStringExpression() throws Exception { Document doc = XmlTestUtil.getDocumentForString("outputOne"); GenericMessage docMessage = new GenericMessage(doc); - inputOne.send(docMessage); - GenericMessage received = (GenericMessage) outputOne.receive(1000); - assertNotNull("Did not receive message from outputOne", received); - } - - @SuppressWarnings("unchecked") - @Test - public void testOutputTwo() throws Exception{ - Document doc = XmlTestUtil.getDocumentForString("outputTwo"); - GenericMessage docMessage = new GenericMessage(doc); - inputOne.send(docMessage); - GenericMessage received = (GenericMessage) outputTwo.receive(1000); - assertNotNull("Did not receive message from two", received); + + TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(""); + XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router"); + + String[] channelNames = router.resolveChannelNames(docMessage); + assertEquals("Wrong number of channel names returned", 1, channelNames.length); + assertEquals("Wrong channel name", "outputOne", channelNames[0]); + } - @SuppressWarnings("unchecked") @Test - public void testOutputThree() throws Exception{ - Document doc = XmlTestUtil.getDocumentForString("outputThree"); + public void testNamespacedStringExpression() throws Exception { + Document doc = XmlTestUtil.getDocumentForString("outputOne"); GenericMessage docMessage = new GenericMessage(doc); - inputOne.send(docMessage); - GenericMessage received = (GenericMessage) errorChannel.receive(1000); - assertNotNull("Did not receive message on errors", received); - } - - @SuppressWarnings("unchecked") - @Test - public void testOutputOneMulti() throws Exception{ - Document doc = XmlTestUtil.getDocumentForString("outputOne"); - GenericMessage docMessage = new GenericMessage(doc); - inputTwo.send(docMessage); - GenericMessage received = (GenericMessage) outputOne.receive(1000); - assertNotNull("Did not receive message from outputOne", received); - } - - @SuppressWarnings("unchecked") - @Test - public void testOutputOneAndTwoMulti() throws Exception{ - Document doc = XmlTestUtil.getDocumentForString("outputOneoutputTwo"); - GenericMessage docMessage = new GenericMessage(doc); - inputTwo.send(docMessage); - GenericMessage received = (GenericMessage) outputTwo.receive(1000); - assertNotNull("Did not receive message from two", received); + + TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(""); + XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router"); + + String[] channelNames = router.resolveChannelNames(docMessage); + assertEquals("Wrong number of channel names returned", 1, channelNames.length); + assertEquals("Wrong channel name", "outputOne", channelNames[0]); } - @SuppressWarnings("unchecked") @Test - public void testOutputThreeMulti() throws Exception{ - Document doc = XmlTestUtil.getDocumentForString("outputThree"); + public void testStringExpressionWithNestedNamespaceMap() throws Exception { + Document doc = XmlTestUtil + .getDocumentForString("outputOne"); GenericMessage docMessage = new GenericMessage(doc); - inputTwo.send(docMessage); - GenericMessage received = (GenericMessage) errorChannel.receive(1000); - assertNotNull("Did not receive message on errors", received); + + StringBuffer buffer = new StringBuffer( + ""); + buffer + .append(" "); + buffer.append(""); + + TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString()); + XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router"); + + String[] channelNames = router.resolveChannelNames(docMessage); + assertEquals("Wrong number of channel names returned", 1, channelNames.length); + assertEquals("Wrong channel name", "outputOne", channelNames[0]); + } + + @Test + public void testStringExpressionWithReferenceToNamespaceMap() throws Exception { + Document doc = XmlTestUtil + .getDocumentForString("outputOne"); + GenericMessage docMessage = new GenericMessage(doc); + StringBuffer buffer = new StringBuffer( + ""); + buffer + .append("") + .append(""); + + TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString()); + XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router"); + + String[] channelNames = router.resolveChannelNames(docMessage); + assertEquals("Wrong number of channel names returned", 1, channelNames.length); + assertEquals("Wrong channel name", "outputOne", channelNames[0]); + } + + + @Test(expected = Exception.class) + public void testNamespacedWithNoPrefixStringExpression() throws Exception { + TestXmlApplicationContextHelper.getTestAppContext(""); + } + + @Test(expected = Exception.class) + public void testPrefixWithNoNamespaceStringExpression() throws Exception { + TestXmlApplicationContextHelper.getTestAppContext(""); } } diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathSelectorParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathSelectorParserTests.java new file mode 100644 index 0000000000..ca5a448aed --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathSelectorParserTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2002-2007 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.config; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.selector.MessageSelector; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.w3c.dom.Document; + +public class XPathSelectorParserTests { + + @Test + public void testSimpleStringExpressionBoolean() throws Exception { + String contextXml = ""; + MessageSelector selector =getSelector( contextXml); + + assertTrue(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + assertFalse(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + } + + @Test + public void testStringExpressionWithNamespaceBoolean() throws Exception { + String contextXml = ""; + MessageSelector selector = getSelector(contextXml); + + assertTrue(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + assertFalse(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + } + + + @Test + public void testStringExpressionWithNestedMap() throws Exception { + String contextXml = ""; + MessageSelector selector = getSelector(contextXml); + + assertTrue(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + assertFalse(selector.accept(new GenericMessage(XmlTestUtil.getDocumentForString("outputOne")))); + + } + + public MessageSelector getSelector( String testcontextXml) throws Exception{ + TestXmlApplicationContext ctx = + TestXmlApplicationContextHelper.getTestAppContext(testcontextXml); + return (MessageSelector) ctx.getBean("selector"); + + } + + + +}