Refactored router hierarchy by adding an AbstractMessageRouter base class and removing the ChannelResolver delegation. The routers that return channel names now accept a ChannelMapping strategy. The BeanNameChannelMapping is typically used as a default. The mapping routers also support "prefix" and "suffix" properties.

This commit is contained in:
Mark Fisher
2008-10-10 23:47:22 +00:00
parent ac85cb1ecf
commit cb90d39657
33 changed files with 910 additions and 822 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -19,9 +19,7 @@ package org.springframework.integration.xml.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
*
* @author Jonas Partner
*
*/
public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
@@ -32,7 +30,6 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());
}
}

View File

@@ -23,8 +23,8 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.xml.router.XPathMultiChannelNameResolver;
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
import org.springframework.integration.xml.router.XPathMultiChannelRouter;
import org.springframework.integration.xml.router.XPathSingleChannelRouter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -47,32 +47,30 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified");
boolean xPathExpressionChildPresent = xPathExpressionNodes.getLength() == 1;
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(
element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2,
"Only one xpath-expression child can be specified.");
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.");
if (multiChannel) {
builder.getBeanDefinition().setBeanClass(XPathMultiChannelNameResolver.class);
builder.getBeanDefinition().setBeanClass(XPathMultiChannelRouter.class);
}
else {
builder.getBeanDefinition().setBeanClass(XPathSingleChannelNameResolver.class);
builder.getBeanDefinition().setBeanClass(XPathSingleChannelRouter.class);
}
if (xPathExpressionChildPresent) {
BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
BeanDefinition beanDefinition = this.xpathParser.parse(
(Element) xPathExpressionNodes.item(0), parserContext);
builder.addConstructorArgValue(beanDefinition);
} else {
}
else {
builder.addConstructorArgReference(xPathExpressionRef);
}
}
}

View File

@@ -4,8 +4,10 @@
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration/xml"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:annotation>
<xsd:documentation>
Defines the configuration elements for Spring Integration's XML support.
@@ -20,8 +22,7 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="marshaller" type="xsd:string"
use="required" />
<xsd:attribute name="marshaller" type="xsd:string" use="required" />
<xsd:attribute name="result-type" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
@@ -31,8 +32,7 @@
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-factory" use="optional" />
<xsd:attribute name="result-transformer" type="xsd:string"
use="optional" />
<xsd:attribute name="result-transformer" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -46,8 +46,7 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="unmarshaller" type="xsd:string"
use="required" />
<xsd:attribute name="unmarshaller" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -61,14 +60,10 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="transformerType">
<xsd:attribute name="xsl-resource" type="xsd:string"
use="optional" />
<xsd:attribute name="xsl-templates" type="xsd:string"
use="optional" />
<xsd:attribute name="source-factory" type="xsd:string"
use="optional" />
<xsd:attribute name="result-factory" type="xsd:string"
use="optional" />
<xsd:attribute name="xsl-resource" type="xsd:string" use="optional" />
<xsd:attribute name="xsl-templates" type="xsd:string" use="optional" />
<xsd:attribute name="source-factory" type="xsd:string" use="optional" />
<xsd:attribute name="result-factory" type="xsd:string" use="optional" />
<xsd:attribute name="result-type" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
@@ -77,13 +72,12 @@
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-transformer" type="xsd:string"
use="optional" />
<xsd:attribute name="result-transformer" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-router">
<xsd:complexType>
<xsd:annotation>
@@ -95,26 +89,23 @@
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="multi-channel" type="xsd:boolean"
default="false" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string" use="optional" />
<xsd:attribute name="multi-channel" type="xsd:boolean" default="false" />
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-selector">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an XPath selector.
Defines an XPath selector.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string" use="optional" />
<xsd:attribute name="evaluation-result-type" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
@@ -123,12 +114,10 @@
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="string-test-value" type="xsd:string"
use="optional" />
<xsd:attribute name="string-test-value" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-expression">
<xsd:complexType>
<xsd:annotation>
@@ -140,23 +129,17 @@
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="optional" />
<xsd:attribute name="expression" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-prefix" type="xsd:string"
use="optional" />
<xsd:attribute name="ns-uri" type="xsd:string"
use="optional" />
<xsd:attribute name="namespace-map" type="xsd:string"
use="optional" />
<xsd:attribute name="expression" type="xsd:string" use="optional" />
<xsd:attribute name="ns-prefix" type="xsd:string" use="optional" />
<xsd:attribute name="ns-uri" type="xsd:string" use="optional" />
<xsd:attribute name="namespace-map" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:complexType name="transformerType">
<xsd:attribute name="id" type="xsd:string" use="optional" />
<xsd:attribute name="input-channel" type="xsd:string"
use="required" />
<xsd:attribute name="output-channel" type="xsd:string"
use="required" />
<xsd:attribute name="input-channel" type="xsd:string" use="required" />
<xsd:attribute name="output-channel" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:schema>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -13,85 +13,91 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.router;
import java.util.HashMap;
import java.util.Map;
import org.springframework.integration.router.AbstractChannelNameResolver;
import org.springframework.integration.router.ChannelResolver;
import org.springframework.integration.router.AbstractChannelMappingMessageRouter;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* Abstract base class for {@link ChannelResolver} classes that use
* {@link XPathExpression} evaluation to determine channel names
* @author Jonas Partner
* Abstract base class for Message Routers that use
* {@link XPathExpression} evaluation to determine channel names.
*
* @author Jonas Partner
*/
public abstract class AbstractXPathChannelNameResolver extends AbstractChannelNameResolver {
public abstract class AbstractXPathRouter extends AbstractChannelMappingMessageRouter {
private final XPathExpression xPathExpression;
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
/**
* Creates an channel name resolver using an XPath expression which may
* contain zero or more namespace prefixes
* @param pathExpression
* Create a router that uses an XPath expression. The expression may
* contain zero or more namespace prefixes.
*
* @param expression
* @param namespaces
*/
public AbstractXPathChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression, namespaces);
public AbstractXPathRouter(String expression, Map<String, String> namespaces) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
/**
* Create a channel name resolver using an XPath expression with one
* namespace. For example expression '/ns1:one/@type' prefix 'ns1' namespace
* 'www.example.org'
* @param pathExpression
* Create a router uses an XPath expression with one namespace. For example,
* expression='/ns1:one/@type' prefix='ns1' namespace='www.example.org'
*
* @param expression
* @param prefix
* @param namespace
*/
public AbstractXPathChannelNameResolver(String pathExpression, String prefix, String namespace) {
public AbstractXPathRouter(String expression, String prefix, String namespace) {
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put(prefix, namespace);
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression, namespaces);
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
/**
* Creates a channel name resolver using an XPath expression with no
* namespaces For example '/one/@type'
* @param pathExpression
* Create a router that uses an XPath expression with no namespaces.
* For example '/one/@type'
*
* @param expression
*/
public AbstractXPathChannelNameResolver(String pathExpression) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression);
public AbstractXPathRouter(String expression) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression);
}
/**
* Creates a channel name resolver using the provided XPath expression
* @param pathExpression
* Create a router that uses the provided XPath expression.
*
* @param expression
*/
public AbstractXPathChannelNameResolver(XPathExpression pathExpression) {
this.xPathExpression = pathExpression;
public AbstractXPathRouter(XPathExpression expression) {
this.xPathExpression = expression;
}
protected XmlPayloadConverter getConverter() {
return converter;
return this.converter;
}
/**
* Converter used to convert payloads prior to XPAth testing
* Converter used to convert payloads prior to XPath testing.
*
* @param converter
*/
public void setConverter(XmlPayloadConverter converter) {
this.converter = converter;
}
protected XPathExpression getXPathExpresion() {
return xPathExpression;
protected XPathExpression getXPathExpression() {
return this.xPathExpression;
}
}

View File

@@ -19,56 +19,58 @@ package org.springframework.integration.xml.router;
import java.util.List;
import java.util.Map;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.springframework.integration.message.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;
/**
* Evaluates the XPath expression using
* 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 XPathMultiChannelNameResolver extends AbstractXPathChannelNameResolver {
public class XPathMultiChannelRouter extends AbstractXPathRouter {
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
* Map)
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, Map)
*/
public XPathMultiChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
super(pathExpression, namespaces);
public XPathMultiChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
* String, String)
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, String, String)
*/
public XPathMultiChannelNameResolver(String pathExpression, String prefix, String namespace) {
super(pathExpression, prefix, namespace);
public XPathMultiChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String)
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String)
*/
public XPathMultiChannelNameResolver(String pathExpression) {
super(pathExpression);
public XPathMultiChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(XPathExpression)
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(XPathExpression)
*/
public XPathMultiChannelNameResolver(XPathExpression pathExpression) {
super(pathExpression);
public XPathMultiChannelRouter(XPathExpression expression) {
super(expression);
}
public void setNodeMapper(NodeMapper nodeMapper) {
Assert.notNull(nodeMapper, "NodeMapper must not be null");
this.nodeMapper = nodeMapper;
@@ -77,10 +79,11 @@ public class XPathMultiChannelNameResolver extends AbstractXPathChannelNameResol
@SuppressWarnings("unchecked")
public String[] resolveChannelNames(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
List channelNamesList = getXPathExpresion().evaluate(node, this.nodeMapper);
List channelNamesList = getXPathExpression().evaluate(node, this.nodeMapper);
return (String[]) channelNamesList.toArray(new String[channelNamesList.size()]);
}
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {

View File

@@ -1,81 +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.Map;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Node;
/**
* 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} should evaluate to a non empty string
* @author Jonas Partner
*/
public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver {
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
* Map)
*/
public XPathSingleChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
super(pathExpression, namespaces);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
* String, String)
*/
public XPathSingleChannelNameResolver(String pathExpression, String prefix, String namespace) {
super(pathExpression, prefix, namespace);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String)
*/
public XPathSingleChannelNameResolver(String pathExpression) {
super(pathExpression);
}
/**
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(XPathExpression)
*/
public XPathSingleChannelNameResolver(XPathExpression pathExpression) {
super(pathExpression);
}
/**
* Evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
* @throws MessagingException if the {@link XPathExpression} evaluates to empty string
*/
public String[] resolveChannelNames(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
String result = getXPathExpresion().evaluateAsString(node);
if(result.equals("")){
throw new MessagingException(message,"XPath expression evaluated to empty string");
}
return new String[] { result };
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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.Map;
import org.w3c.dom.Node;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
/**
* 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.
*
* <p>The provided {@link XPathExpression} must evaluate to a non-empty String.
*
* @author Jonas Partner
*/
public class XPathSingleChannelRouter extends AbstractXPathRouter {
/**
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, Map)
*/
public XPathSingleChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, String, String)
*/
public XPathSingleChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String)
*/
public XPathSingleChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(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
*/
public String[] resolveChannelNames(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
String result = getXPathExpression().evaluateAsString(node);
if ("".equals(result)) {
throw new MessagingException(message,"XPath expression must not be empty");
}
if (result == null) {
return null;
}
return new String[] { result };
}
}

View File

@@ -19,11 +19,12 @@ package org.springframework.integration.xml.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.w3c.dom.Document;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
import org.springframework.integration.xml.router.XPathSingleChannelRouter;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
@@ -35,25 +36,21 @@ public class XPathRouterParserTests {
public void testSimpleStringExpression() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>");
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(
"<si-xml:xpath-router id='router'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>");
XPathSingleChannelRouter router = (XPathSingleChannelRouter) 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 testNamespacedStringExpression() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
TestXmlApplicationContext ctx =
TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router'><si-xml:xpath-expression expression='/ns2:name' ns-prefix='ns2' ns-uri='www.example.org' /></si-xml:xpath-router>");
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(
"<si-xml:xpath-router id='router'><si-xml:xpath-expression expression='/ns2:name' ns-prefix='ns2' ns-uri='www.example.org' /></si-xml:xpath-router>");
XPathSingleChannelRouter router = (XPathSingleChannelRouter) 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]);
@@ -61,19 +58,15 @@ public class XPathRouterParserTests {
@Test
public void testStringExpressionWithNestedNamespaceMap() throws Exception {
Document doc = XmlTestUtil
.getDocumentForString("<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
Document doc = XmlTestUtil.getDocumentForString(
"<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer buffer = new StringBuffer(
"<si-xml:xpath-router id='router'><si-xml:xpath-expression expression='/ns1:name/ns2:type'> ");
buffer
.append("<map><entry key='ns1' value='www.example.org' /> <entry key='ns2' value='www.example.org2'/></map>");
buffer.append("<map><entry key='ns1' value='www.example.org' /> <entry key='ns2' value='www.example.org2'/></map>");
buffer.append("</si-xml:xpath-expression></si-xml:xpath-router>");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
XPathSingleChannelRouter router = (XPathSingleChannelRouter) 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]);
@@ -81,24 +74,18 @@ public class XPathRouterParserTests {
@Test
public void testStringExpressionWithReferenceToNamespaceMap() throws Exception {
Document doc = XmlTestUtil
.getDocumentForString("<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
Document doc = XmlTestUtil.getDocumentForString(
"<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer buffer = new StringBuffer(
"<si-xml:xpath-router id='router' ><si-xml:xpath-expression expression='/ns1:name/ns2:type' namespace-map='nsMap'/>");
buffer
.append("</si-xml:xpath-router>")
.append("<util:map id='nsMap'><entry key='ns1' value='www.example.org' /><entry key='ns2' value='www.example.org2' /></util:map>");
buffer.append("</si-xml:xpath-router>");
buffer.append("<util:map id='nsMap'><entry key='ns1' value='www.example.org' /><entry key='ns2' value='www.example.org2' /></util:map>");
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
XPathSingleChannelRouter router = (XPathSingleChannelRouter) 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]);
}
}

View File

@@ -19,28 +19,29 @@ 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.message.GenericMessage;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* @author Jonas Partner
*/
public class XPathMultiChannelNameResolverTests {
public class XPathMultiChannelRouterTests {
@Test
@SuppressWarnings("unchecked")
public void testSimpleSingleeAttribute() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
String[] channelNames = resolver.resolveChannelNames(new GenericMessage(doc));
assertEquals("Wrong number of channels returend", 1, channelNames.length);
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
String[] channelNames = router.resolveChannelNames(new GenericMessage(doc));
assertEquals("Wrong number of channels returned", 1, channelNames.length);
assertEquals("Wrong channel name", "one", channelNames[0]);
}
@@ -49,20 +50,20 @@ public class XPathMultiChannelNameResolverTests {
public void testMultipleNodeValues() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
String[] channelNames = resolver.resolveChannelNames(new GenericMessage(doc));
assertEquals("Wrong number of channels returend", 2, channelNames.length);
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
String[] channelNames = router.resolveChannelNames(new GenericMessage(doc));
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
assertEquals("Wrong channel name", "bTwo", channelNames[1]);
}
@Test
@SuppressWarnings("unchecked")
public void testMultipleNodeValuesAsString() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
String[] channelNames = resolver.resolveChannelNames(new GenericMessage("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>"));
assertEquals("Wrong number of channels returend", 2, channelNames.length);
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
String[] channelNames = router.resolveChannelNames(new GenericMessage("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>"));
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
assertEquals("Wrong channel name", "bTwo", channelNames[1]);
}
@@ -70,17 +71,17 @@ public class XPathMultiChannelNameResolverTests {
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
resolver.resolveChannelNames(new StringMessage("test"));
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
router.resolveChannelNames(new StringMessage("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver("./three/text()");
XPathMultiChannelRouter router = new XPathMultiChannelRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three><three>dave</three></two></one>");
String[] channelNames = resolver.resolveChannelNames(new GenericMessage<Node>(testDocument.getElementsByTagName("two").item(0)));
String[] channelNames = router.resolveChannelNames(new GenericMessage<Node>(testDocument.getElementsByTagName("two").item(0)));
assertEquals("bob",channelNames[0]);
assertEquals("dave",channelNames[1]);
}
}

View File

@@ -19,58 +19,59 @@ 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.message.GenericMessage;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* @author Jonas Partner
*/
public class XPathSingleChannelNameResolverTests {
public class XPathSingleChannelRouterTests {
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
String channelName = resolver.resolveChannelNames(new GenericMessage<Document>(doc))[0];
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
String channelName = router.resolveChannelNames(new GenericMessage<Document>(doc))[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test
public void testSimpleStringDoc() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
String channelName = resolver.resolveChannelNames(new GenericMessage<String>("<doc type='one' />"))[0];
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
String channelName = router.resolveChannelNames(new GenericMessage<String>("<doc type='one' />"))[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
resolver.resolveChannelNames(new StringMessage("test"));
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
router.resolveChannelNames(new StringMessage("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver("./three/text()");
XPathSingleChannelRouter router = new XPathSingleChannelRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
String[] channelNames = resolver.resolveChannelNames(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0)));
String[] channelNames = router.resolveChannelNames(new GenericMessage<Node>(
testDocument.getElementsByTagName("two").item(0)));
assertEquals("bob", channelNames[0]);
}
@Test(expected=MessagingException.class)
public void testEvaluationReturnsEmptyString() throws Exception {
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver("/yellow");
XPathSingleChannelRouter router = new XPathSingleChannelRouter("/yellow");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
resolver.resolveChannelNames(new GenericMessage<Node>(testDocument));
router.resolveChannelNames(new GenericMessage<Node>(testDocument));
}
}