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));
}
}

View File

@@ -22,8 +22,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.MessageConsumer;
import org.springframework.integration.router.MethodInvokingChannelResolver;
import org.springframework.integration.router.RouterEndpoint;
import org.springframework.integration.router.MethodInvokingRouter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -41,8 +40,7 @@ public class RouterAnnotationPostProcessor extends AbstractMethodAnnotationPostP
@Override
protected MessageConsumer createConsumer(Object bean, Method method, Router annotation) {
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(bean, method);
RouterEndpoint router = new RouterEndpoint(resolver);
MethodInvokingRouter router = new MethodInvokingRouter(bean, method);
String defaultOutputChannelName = annotation.defaultOutputChannel();
if (StringUtils.hasText(defaultOutputChannelName)) {
MessageChannel defaultOutputChannel = this.channelRegistry.lookupChannel(defaultOutputChannelName);

View File

@@ -19,9 +19,12 @@ package org.springframework.integration.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.router.MethodInvokingChannelResolver;
import org.springframework.integration.router.RouterEndpoint;
import org.springframework.integration.router.BeanNameChannelMapping;
import org.springframework.integration.router.MethodInvokingRouter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;router/&gt; element.
@@ -32,10 +35,19 @@ public class RouterParser extends AbstractConsumerEndpointParser {
@Override
protected BeanDefinitionBuilder parseConsumer(Element element, ParserContext parserContext) {
String adapterBeanName = this.parseAdapter(element, parserContext, MethodInvokingChannelResolver.class);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RouterEndpoint.class);
builder.addConstructorArgReference(adapterBeanName);
builder.addPropertyReference("channelRegistry", MessageBusParser.MESSAGE_BUS_BEAN_NAME);
String ref = element.getAttribute(REF_ATTRIBUTE);
Assert.hasText(ref, "The '" + REF_ATTRIBUTE + "' attribute is required.");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingRouter.class);
builder.addConstructorArgReference(ref);
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
String method = element.getAttribute(METHOD_ATTRIBUTE);
builder.addConstructorArgValue(method);
}
BeanDefinitionBuilder channelMappingBuilder =
BeanDefinitionBuilder.genericBeanDefinition(BeanNameChannelMapping.class);
String channelMappingBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
channelMappingBuilder.getBeanDefinition(), parserContext.getRegistry());
builder.addPropertyReference("channelMapping", channelMappingBeanName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel");
return builder;
}

View File

@@ -0,0 +1,104 @@
/*
* 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.router;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* A base class for router implementations that return only
* the channel name(s) rather than {@link MessageChannel} instances.
*
* @author Mark Fisher
*/
public abstract class AbstractChannelMappingMessageRouter extends AbstractMessageRouter implements BeanFactoryAware, InitializingBean {
private volatile ChannelMapping channelMapping;
private volatile String prefix;
private volatile String suffix;
private volatile BeanFactory beanFactory;
public void setChannelMapping(ChannelMapping channelMapping) {
this.channelMapping = channelMapping;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void afterPropertiesSet() {
if (this.channelMapping == null) {
Assert.notNull(beanFactory, "either a ChannelMapping or BeanFactory is required");
this.channelMapping = new BeanNameChannelMapping(this.beanFactory);
}
}
@Override
protected final Collection<MessageChannel> resolveChannels(Message<?> message) {
this.afterPropertiesSet();
Collection<MessageChannel> channels = new ArrayList<MessageChannel>();
String[] channelNames = this.resolveChannelNames(message);
if (channelNames == null) {
return null;
}
for (String channelName : channelNames) {
if (channelName != null) {
Assert.state(this.channelMapping != null,
"unable to resolve channels, no ChannelMapping available");
if (this.prefix != null) {
channelName = this.prefix + channelName;
}
if (this.suffix != null) {
channelName = channelName + suffix;
}
MessageChannel channel = this.channelMapping.getChannel(channelName);
if (channel == null) {
throw new MessagingException(message,
"unable to resolve channel '" + channelName + "'");
}
channels.add(channel);
}
}
return channels;
}
/**
* Subclasses must implement this method to return the channel name(s).
*/
protected abstract String[] resolveChannelNames(Message<?> message);
}

View File

@@ -1,70 +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.router;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* A base class for {@link ChannelResolver} implementations that return only
* the channel name(s) rather than {@link MessageChannel} instances.
*
* @author Mark Fisher
*/
public abstract class AbstractChannelNameResolver implements ChannelResolver, ChannelRegistryAware {
private ChannelRegistry channelRegistry;
public void setChannelRegistry(ChannelRegistry channelRegistry) {
this.channelRegistry = channelRegistry;
}
public final Collection<MessageChannel> resolveChannels(Message<?> message) {
Collection<MessageChannel> channels = new ArrayList<MessageChannel>();
String[] channelNames = this.resolveChannelNames(message);
if (channelNames == null) {
return null;
}
for (String channelName : channelNames) {
if (channelName != null) {
Assert.state(this.channelRegistry != null,
"unable to resolve channels, no ChannelRegistry available");
MessageChannel channel = this.channelRegistry.lookupChannel(channelName);
if (channel == null) {
throw new MessagingException(message,
"unable to resolve chnanel '" + channelName + "'");
}
channels.add(channel);
}
}
return channels;
}
/**
* Subclasses must implement this method to return the channel name(s).
*/
protected abstract String[] resolveChannelNames(Message<?> message);
}

View File

@@ -18,21 +18,18 @@ package org.springframework.integration.router;
import java.util.Collection;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.MessageChannelTemplate;
import org.springframework.integration.endpoint.AbstractMessageConsumer;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.util.Assert;
/**
* Base class for Message Routers.
*
* @author Mark Fisher
*/
public class RouterEndpoint extends AbstractMessageConsumer implements ChannelRegistryAware {
private final ChannelResolver channelResolver;
public abstract class AbstractMessageRouter extends AbstractMessageConsumer {
private volatile MessageChannel defaultOutputChannel;
@@ -41,18 +38,12 @@ public class RouterEndpoint extends AbstractMessageConsumer implements ChannelRe
private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate();
public RouterEndpoint(ChannelResolver channelResolver) {
Assert.notNull(channelResolver, "ChannelResolver must not be null");
this.channelResolver = channelResolver;
}
public void setChannelRegistry(ChannelRegistry channelRegistry) {
if (this.channelResolver instanceof ChannelRegistryAware) {
((ChannelRegistryAware) this.channelResolver).setChannelRegistry(channelRegistry);
}
}
/**
* Set the default channel where Messages should be sent if channel
* resolution fails to return any channels. If no default channel is
* provided, the router will either drop the Message or throw an Exception
* depending on the value of {@link #resolutionRequired}.
*/
public void setDefaultOutputChannel(MessageChannel defaultOutputChannel) {
this.defaultOutputChannel = defaultOutputChannel;
}
@@ -78,7 +69,7 @@ public class RouterEndpoint extends AbstractMessageConsumer implements ChannelRe
@Override
protected void onMessageInternal(Message<?> message) {
boolean sent = false;
Collection<MessageChannel> results = this.channelResolver.resolveChannels(message);
Collection<MessageChannel> results = this.resolveChannels(message);
if (results != null) {
for (MessageChannel channel : results) {
if (channel != null) {
@@ -99,4 +90,10 @@ public class RouterEndpoint extends AbstractMessageConsumer implements ChannelRe
}
}
/**
* Subclasses must implement this method to return the target channels for
* a given Message.
*/
protected abstract Collection<MessageChannel> resolveChannels(Message<?> message);
}

View File

@@ -21,9 +21,10 @@ import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public abstract class AbstractSingleChannelNameResolver extends AbstractChannelNameResolver {
public abstract class AbstractSingleChannelNameRouter extends AbstractChannelMappingMessageRouter {
public final String[] resolveChannelNames(Message<?> message) {
@Override
protected final String[] resolveChannelNames(Message<?> message) {
String channelName = this.resolveChannelName(message);
return (channelName != null) ? new String[] { channelName } : null;
}

View File

@@ -25,12 +25,12 @@ import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public abstract class AbstractSingleChannelResolver implements ChannelResolver {
public abstract class AbstractSingleChannelRouter extends AbstractMessageRouter {
public Collection<MessageChannel> resolveChannels(Message<?> message) {
@Override
protected final Collection<MessageChannel> resolveChannels(Message<?> message) {
MessageChannel channel = this.resolveChannel(message);
return (channel != null) ?
Collections.singletonList(channel) : null;
return (channel != null) ? Collections.singletonList(channel) : null;
}
protected abstract MessageChannel resolveChannel(Message<?> message);

View File

@@ -0,0 +1,61 @@
/*
* 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.router;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.util.Assert;
/**
* An implementation of the ChannelMapping strategy that retrieves a
* MessageChannel instance from the {@link BeanFactory} using the provided
* name.
*
* @author Mark Fisher
*/
public class BeanNameChannelMapping implements ChannelMapping, BeanFactoryAware {
private volatile BeanFactory beanFactory;
/**
* Constructor for use within a context where the BeanFactory will be
* injected via the {@link #setBeanFactory(BeanFactory)} callback method.
*/
public BeanNameChannelMapping() {
}
/**
* Constructor for programmatic creation from within other components that
* already have access to the {@link BeanFactory}.
*/
public BeanNameChannelMapping(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public MessageChannel getChannel(String name) {
Assert.state(this.beanFactory != null, "beanFactory must not be null");
return (MessageChannel) this.beanFactory.getBean(name, MessageChannel.class);
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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.router;
import org.springframework.integration.channel.MessageChannel;
/**
* Strategy for mapping from a name to a {@link MessageChannel}.
*
* @author Mark Fisher
*/
public interface ChannelMapping {
MessageChannel getChannel(String name);
}

View File

@@ -23,8 +23,6 @@ import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMappingMethodInvoker;
@@ -32,38 +30,44 @@ import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* A {@link ChannelResolver} implementation that invokes the specified method
* on the given object. The method's return value may be a single MessageChannel
* instance, a single String to be interpreted as a channel name, or a Collection
* (or Array) of either type.
* A Message Router that invokes the specified method on the given object. The
* method's return value may be a single MessageChannel instance, a single
* String to be interpreted as a channel name, or a Collection (or Array) of
* either type. If the method returns channel names, then a
* {@link ChannelMapping} is required.
*
* @author Mark Fisher
*/
public class MethodInvokingChannelResolver implements ChannelResolver, ChannelRegistryAware, InitializingBean {
public class MethodInvokingRouter extends AbstractMessageRouter implements InitializingBean {
private final MessageMappingMethodInvoker invoker;
private volatile ChannelRegistry channelRegistry;
private volatile ChannelMapping channelMapping;
public MethodInvokingChannelResolver(Object object, Method method) {
public MethodInvokingRouter(Object object, Method method) {
this.invoker = new MessageMappingMethodInvoker(object, method);
}
public MethodInvokingChannelResolver(Object object, String methodName) {
public MethodInvokingRouter(Object object, String methodName) {
this.invoker = new MessageMappingMethodInvoker(object, methodName);
}
public void setChannelRegistry(ChannelRegistry channelRegistry) {
this.channelRegistry = channelRegistry;
/**
* Provide the ChannelMapping strategy to use for methods that return a
* channel name rather than a {@link MessageChannel} instance.
*/
public void setChannelMapping(ChannelMapping channelMapping) {
this.channelMapping = channelMapping;
}
public void afterPropertiesSet() throws Exception {
this.invoker.afterPropertiesSet();
}
public final Collection<MessageChannel> resolveChannels(Message<?> message) {
@Override
protected final Collection<MessageChannel> resolveChannels(Message<?> message) {
Object result = this.invoker.invokeMethod(message);
if (result == null) {
return null;
@@ -104,9 +108,9 @@ public class MethodInvokingChannelResolver implements ChannelResolver, ChannelRe
}
else if (channelOrName instanceof String) {
String channelName = (String) channelOrName;
Assert.state(this.channelRegistry != null,
"ChannelRegistry is required for resolving channel names");
MessageChannel channel = this.channelRegistry.lookupChannel(channelName);
Assert.state(this.channelMapping != null,
"ChannelMapping is required for resolving channel names");
MessageChannel channel = this.channelMapping.getChannel(channelName);
if (channel == null) {
throw new MessagingException("unable to resolve channel '" + channelName + "'");
}

View File

@@ -24,24 +24,25 @@ import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* A ChannelResolver implementation that resolves the {@link MessageChannel} based
* on the {@link Message Message's} payload type.
* A Message Router that resolves the {@link MessageChannel} based on the
* {@link Message Message's} payload type.
*
* @author Mark Fisher
*/
public class PayloadTypeChannelResolver extends AbstractSingleChannelResolver {
public class PayloadTypeRouter extends AbstractSingleChannelRouter {
private Map<Class<?>, MessageChannel> channelMappings = new ConcurrentHashMap<Class<?>, MessageChannel>();
private volatile Map<Class<?>, MessageChannel> payloadTypeChannelMap =
new ConcurrentHashMap<Class<?>, MessageChannel>();
public void setChannelMappings(Map<Class<?>, MessageChannel> channelMappings) {
Assert.notNull(channelMappings, "'channelMappings' must not be null");
this.channelMappings = channelMappings;
public void setPayloadTypeChannelMap(Map<Class<?>, MessageChannel> payloadTypeChannelMap) {
Assert.notNull(payloadTypeChannelMap, "payloadTypeChannelMap must not be null");
this.payloadTypeChannelMap = payloadTypeChannelMap;
}
@Override
protected MessageChannel resolveChannel(Message<?> message) {
return this.channelMappings.get(message.getPayload().getClass());
return this.payloadTypeChannelMap.get(message.getPayload().getClass());
}
}

View File

@@ -25,21 +25,19 @@ import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* A {@link ChannelResolver} implementation that routes to a statically
* configured list of recipients. The recipients are provided as a list of
* {@link MessageChannel} instances. For dynamic recipient lists, consider
* either implementing the {@link ChannelResolver} interface directly or
* extending the {@link AbstractChannelNameResolver} base class.
* A Message Router that sends Messages to a statically configured list of
* recipients. The recipients are provided as a list of {@link MessageChannel}
* instances. For dynamic recipient lists, consider instead using the @Router
* annotation or extending {@link AbstractChannelMappingMessageRouter}.
*
* @author Mark Fisher
*/
public class RecipientListChannelResolver implements ChannelResolver, InitializingBean {
public class RecipientListRouter extends AbstractMessageRouter implements InitializingBean {
private volatile List<MessageChannel> channels;
public void setChannels(List<MessageChannel> channels) {
Assert.notEmpty(channels, "a non-empty channel list is required");
this.channels = channels;
}
@@ -47,7 +45,8 @@ public class RecipientListChannelResolver implements ChannelResolver, Initializi
Assert.notEmpty(this.channels, "a non-empty channel list is required");
}
public Collection<MessageChannel> resolveChannels(Message<?> message) {
@Override
protected Collection<MessageChannel> resolveChannels(Message<?> message) {
return this.channels;
}

View File

@@ -24,21 +24,21 @@ import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* A ChannelResolver implementation that resolves the {@link MessageChannel} for
* messages whose payload is an Exception. The channel resolution is based upon the
* most specific cause of the error for which a channel-mapping exists.
* A Message Router that resolves the target {@link MessageChannel} for
* messages whose payload is an Exception. The channel resolution is based upon
* the most specific cause of the error for which a channel-mapping exists.
*
* @author Mark Fisher
*/
public class RootCauseErrorMessageChannelResolver extends AbstractSingleChannelResolver {
public class RootCauseErrorMessageRouter extends AbstractSingleChannelRouter {
private Map<Class<? extends Throwable>, MessageChannel> channelMappings =
private volatile Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new ConcurrentHashMap<Class<? extends Throwable>, MessageChannel>();
public void setChannelMappings(Map<Class<? extends Throwable>, MessageChannel> channelMappings) {
Assert.notNull(channelMappings, "'channelMappings' must not be null");
this.channelMappings = channelMappings;
public void setExceptionTypeChannelMap(Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap) {
Assert.notNull(exceptionTypeChannelMap, "exceptionTypeChannelMap must not be null");
this.exceptionTypeChannelMap = exceptionTypeChannelMap;
}
@@ -49,7 +49,7 @@ public class RootCauseErrorMessageChannelResolver extends AbstractSingleChannelR
if (payload != null && (payload instanceof Throwable)) {
Throwable mostSpecificCause = (Throwable) payload;
while (mostSpecificCause != null) {
MessageChannel mappedChannel = this.channelMappings.get(mostSpecificCause.getClass());
MessageChannel mappedChannel = this.exceptionTypeChannelMap.get(mostSpecificCause.getClass());
if (mappedChannel != null) {
channel = mappedChannel;
}

View File

@@ -21,12 +21,15 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.message.Message;
/**
@@ -169,7 +172,20 @@ public class PublisherAnnotationAdvisorTests {
public Integer publishReturnValue() {
return 123;
}
}
private static class TestChannelRegistry implements ChannelRegistry {
private final Map<String, MessageChannel> channels = new HashMap<String, MessageChannel>();
public MessageChannel lookupChannel(String channelName) {
return this.channels.get(channelName);
}
public void registerChannel(MessageChannel channel) {
this.channels.put(channel.getName(), channel);
}
}
}

View File

@@ -19,21 +19,22 @@ package org.springframework.integration.channel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.integration.router.ChannelMapping;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
*/
public class TestChannelRegistry implements ChannelRegistry {
public class TestChannelMapping implements ChannelMapping {
private final Map<String, MessageChannel> channels = new ConcurrentHashMap<String, MessageChannel>();
public MessageChannel lookupChannel(String channelName) {
public MessageChannel getChannel(String channelName) {
return this.channels.get(channelName);
}
public void registerChannel(MessageChannel channel) {
public void addChannel(MessageChannel channel) {
Assert.notNull(channel, "'channel' must not be null");
Assert.notNull(channel.getName(), "channel name must not be null");
this.channels.put(channel.getName(), channel);

View File

@@ -30,8 +30,8 @@ import org.junit.Test;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
@@ -106,8 +106,6 @@ public class ServiceActivatorEndpointTests {
final QueueChannel replyChannel1 = new QueueChannel();
final QueueChannel replyChannel2 = new QueueChannel();
replyChannel2.setBeanName("replyChannel2");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(replyChannel2);
Object handler = new Object() {
@SuppressWarnings("unused")
public Message<?> handle(Message<?> message) {
@@ -115,7 +113,16 @@ public class ServiceActivatorEndpointTests {
}
};
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler, "handle");
endpoint.setChannelRegistry(channelRegistry);
endpoint.setChannelRegistry(new ChannelRegistry() {
public MessageChannel lookupChannel(String channelName) {
if (channelName.equals("replyChannel2")) {
return replyChannel2;
}
return null;
}
public void registerChannel(MessageChannel channel) {
}
});
Message<String> testMessage1 = MessageBuilder.withPayload("bar")
.setReturnAddress(replyChannel1).build();
endpoint.onMessage(testMessage1);

View File

@@ -29,7 +29,7 @@ import org.junit.Test;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.channel.TestChannelMapping;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
@@ -47,15 +47,14 @@ public class MethodInvokingRouterTests {
public void channelNameResolutionByPayloadConfiguredByMethodReference() throws Exception {
QueueChannel barChannel = new QueueChannel();
barChannel.setBeanName("bar-channel");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(barChannel);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(barChannel);
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routePayload", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setChannelRegistry(channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
router.setChannelMapping(channelMapping);
Message<String> message = new GenericMessage<String>("bar");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> replyMessage = barChannel.receive();
assertNotNull(replyMessage);
assertEquals(message, replyMessage);
@@ -65,14 +64,13 @@ public class MethodInvokingRouterTests {
public void channelNameResolutionByPayloadConfiguredByMethodName() {
QueueChannel barChannel = new QueueChannel();
barChannel.setBeanName("bar-channel");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(barChannel);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(barChannel);
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routePayload");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setChannelRegistry(channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routePayload");
router.setChannelMapping(channelMapping);
Message<String> message = new GenericMessage<String>("bar");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> replyMessage = barChannel.receive();
assertNotNull(replyMessage);
assertEquals(message, replyMessage);
@@ -84,17 +82,16 @@ public class MethodInvokingRouterTests {
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routeByHeader", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setChannelRegistry(channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
router.setChannelMapping(channelMapping);
Message<String> message = MessageBuilder.withPayload("bar")
.setHeader("targetChannel", "foo").build();
endpoint.onMessage(message);
router.onMessage(message);
Message<?> fooReply = fooChannel.receive(0);
Message<?> barReply = barChannel.receive(0);
assertNotNull(fooReply);
@@ -106,71 +103,66 @@ public class MethodInvokingRouterTests {
public void failsWhenRequiredHeaderIsNotProvided() throws Exception {
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routeByHeader", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.onMessage(new GenericMessage<String>("testing"));
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
router.onMessage(new GenericMessage<String>("testing"));
}
@Test
public void channelNameResolutionByMessageConfiguredByMethodReference() throws Exception {
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routeMessage", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelNameResolutionByMessage(endpoint);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestChannelNameResolutionByMessage(router);
}
@Test
public void channelNameResolutionByMessageConfiguredByMethodName() {
SingleChannelNameRoutingTestBean testBean = new SingleChannelNameRoutingTestBean();
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessage");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelNameResolutionByMessage(endpoint);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessage");
this.doTestChannelNameResolutionByMessage(router);
}
private void doTestChannelNameResolutionByMessage(RouterEndpoint endpoint) {
private void doTestChannelNameResolutionByMessage(MethodInvokingRouter router) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1 = fooChannel.receive(0);
assertNotNull(result1);
assertEquals("foo", result1.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void channelInstanceResolutionByPayloadConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelMapping);
Method routingMethod = testBean.getClass().getMethod("routePayload", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelInstanceResolutionByPayload(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestChannelInstanceResolutionByPayload(router, channelMapping);
}
@Test
public void channelInstanceResolutionByPayloadConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelRegistry);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routePayload");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelInstanceResolutionByPayload(endpoint, channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelMapping);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routePayload");
this.doTestChannelInstanceResolutionByPayload(router, channelMapping);
}
private void doTestChannelInstanceResolutionByPayload(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestChannelInstanceResolutionByPayload(MethodInvokingRouter router, TestChannelMapping channelMapping) {
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
@@ -178,341 +170,327 @@ public class MethodInvokingRouterTests {
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
endpoint.onMessage(fooMessage);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
router.onMessage(fooMessage);
Message<?> result1 = fooChannel.receive(0);
assertNotNull(result1);
assertEquals("foo", result1.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void channelInstanceResolutionByMessageConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelMapping);
Method routingMethod = testBean.getClass().getMethod("routeMessage", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelInstanceResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestChannelInstanceResolutionByMessage(router, channelMapping);
}
@Test
public void channelInstanceResolutionByMessageConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelRegistry);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessage");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestChannelInstanceResolutionByMessage(endpoint, channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
SingleChannelInstanceRoutingTestBean testBean = new SingleChannelInstanceRoutingTestBean(channelMapping);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessage");
this.doTestChannelInstanceResolutionByMessage(router, channelMapping);
}
private void doTestChannelInstanceResolutionByMessage(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestChannelInstanceResolutionByMessage(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1 = fooChannel.receive(0);
assertNotNull(result1);
assertEquals("foo", result1.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelNameResolutionByPayloadConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routePayload", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameResolutionByPayload(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelNameResolutionByPayload(router, channelMapping);
}
@Test
public void multiChannelNameResolutionByPayloadConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routePayload");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameResolutionByPayload(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routePayload");
this.doTestMultiChannelNameResolutionByPayload(router, channelMapping);
}
private void doTestMultiChannelNameResolutionByPayload(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelNameResolutionByPayload(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelNameResolutionByMessageConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routeMessage", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelNameResolutionByMessage(router, channelMapping);
}
@Test
public void multiChannelNameResolutionByMessageConfiguredByMethodName() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessage");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessage");
this.doTestMultiChannelNameResolutionByMessage(router, channelMapping);
}
private void doTestMultiChannelNameResolutionByMessage(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelNameResolutionByMessage(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelNameArrayResolutionByMessageConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
Method routingMethod = testBean.getClass().getMethod("routeMessageToArray", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameArrayResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelNameArrayResolutionByMessage(router, channelMapping);
}
@Test
public void multiChannelNameArrayResolutionByMessageConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelNameRoutingTestBean testBean = new MultiChannelNameRoutingTestBean();
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessageToArray");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelNameArrayResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessageToArray");
this.doTestMultiChannelNameArrayResolutionByMessage(router, channelMapping);
}
private void doTestMultiChannelNameArrayResolutionByMessage(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelNameArrayResolutionByMessage(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelListResolutionByPayloadConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
Method routingMethod = testBean.getClass().getMethod("routePayload", String.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelListResolutionByPayload(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelListResolutionByPayload(router, channelMapping);
}
@Test
public void multiChannelListResolutionByPayloadConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routePayload");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelListResolutionByPayload(endpoint, channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routePayload");
this.doTestMultiChannelListResolutionByPayload(router, channelMapping);
}
private void doTestMultiChannelListResolutionByPayload(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelListResolutionByPayload(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelListResolutionByMessageConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
Method routingMethod = testBean.getClass().getMethod("routeMessage", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelListResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelListResolutionByMessage(router, channelMapping);
}
@Test
public void multiChannelListResolutionByMessageConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessage");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelListResolutionByMessage(endpoint, channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessage");
this.doTestMultiChannelListResolutionByMessage(router, channelMapping);
}
private void doTestMultiChannelListResolutionByMessage(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelListResolutionByMessage(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@Test
public void multiChannelArrayResolutionByMessageConfiguredByMethodReference() throws Exception {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
Method routingMethod = testBean.getClass().getMethod("routeMessageToArray", Message.class);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, routingMethod);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelArrayResolutionByMessage(endpoint, channelRegistry);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, routingMethod);
this.doTestMultiChannelArrayResolutionByMessage(router, channelMapping);
}
@Test
public void multiChannelArrayResolutionByMessageConfiguredByMethodName() {
ChannelRegistry channelRegistry = new TestChannelRegistry();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelRegistry);
MethodInvokingChannelResolver resolver = new MethodInvokingChannelResolver(testBean, "routeMessageToArray");
RouterEndpoint endpoint = new RouterEndpoint(resolver);
this.doTestMultiChannelArrayResolutionByMessage(endpoint, channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
MultiChannelInstanceRoutingTestBean testBean = new MultiChannelInstanceRoutingTestBean(channelMapping);
MethodInvokingRouter router = new MethodInvokingRouter(testBean, "routeMessageToArray");
this.doTestMultiChannelArrayResolutionByMessage(router, channelMapping);
}
private void doTestMultiChannelArrayResolutionByMessage(RouterEndpoint endpoint, ChannelRegistry channelRegistry) {
private void doTestMultiChannelArrayResolutionByMessage(MethodInvokingRouter router, TestChannelMapping channelMapping) {
QueueChannel fooChannel = new QueueChannel();
QueueChannel barChannel = new QueueChannel();
fooChannel.setBeanName("foo-channel");
barChannel.setBeanName("bar-channel");
channelRegistry.registerChannel(fooChannel);
channelRegistry.registerChannel(barChannel);
endpoint.setChannelRegistry(channelRegistry);
channelMapping.addChannel(fooChannel);
channelMapping.addChannel(barChannel);
router.setChannelMapping(channelMapping);
Message<String> fooMessage = new StringMessage("foo");
Message<String> barMessage = new StringMessage("bar");
Message<String> badMessage = new StringMessage("bad");
endpoint.onMessage(fooMessage);
router.onMessage(fooMessage);
Message<?> result1a = fooChannel.receive(0);
Message<?> result1b = barChannel.receive(0);
assertNotNull(result1a);
assertEquals("foo", result1a.getPayload());
assertNotNull(result1b);
assertEquals("foo", result1b.getPayload());
endpoint.onMessage(barMessage);
router.onMessage(barMessage);
Message<?> result2a = fooChannel.receive(0);
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2a);
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
endpoint.onMessage(badMessage);
router.onMessage(badMessage);
}
@@ -572,22 +550,22 @@ public class MethodInvokingRouterTests {
public static class SingleChannelInstanceRoutingTestBean {
private ChannelRegistry registry;
private ChannelMapping mapping;
public SingleChannelInstanceRoutingTestBean(ChannelRegistry registry) {
this.registry = registry;
public SingleChannelInstanceRoutingTestBean(ChannelMapping mapping) {
this.mapping = mapping;
}
public MessageChannel routePayload(String name) {
return registry.lookupChannel(name + "-channel");
return mapping.getChannel(name + "-channel");
}
public MessageChannel routeMessage(Message<?> message) {
if (message.getPayload().equals("foo")) {
return registry.lookupChannel("foo-channel");
return mapping.getChannel("foo-channel");
}
else if (message.getPayload().equals("bar")) {
return registry.lookupChannel("bar-channel");
return mapping.getChannel("bar-channel");
}
return null;
}
@@ -596,17 +574,17 @@ public class MethodInvokingRouterTests {
public static class MultiChannelInstanceRoutingTestBean {
private ChannelRegistry registry;
private ChannelMapping mapping;
public MultiChannelInstanceRoutingTestBean(ChannelRegistry registry) {
this.registry = registry;
public MultiChannelInstanceRoutingTestBean(ChannelMapping mapping) {
this.mapping = mapping;
}
public List<MessageChannel> routePayload(String name) {
List<MessageChannel> results = new ArrayList<MessageChannel>();
if (name.equals("foo") || name.equals("bar")) {
results.add(registry.lookupChannel("foo-channel"));
results.add(registry.lookupChannel("bar-channel"));
results.add(mapping.getChannel("foo-channel"));
results.add(mapping.getChannel("bar-channel"));
}
return results;
}
@@ -614,8 +592,8 @@ public class MethodInvokingRouterTests {
public List<MessageChannel> routeMessage(Message<?> message) {
List<MessageChannel> results = new ArrayList<MessageChannel>();
if (message.getPayload().equals("foo") || message.getPayload().equals("bar")) {
results.add(registry.lookupChannel("foo-channel"));
results.add(registry.lookupChannel("bar-channel"));
results.add(mapping.getChannel("foo-channel"));
results.add(mapping.getChannel("bar-channel"));
}
return results;
}
@@ -624,8 +602,8 @@ public class MethodInvokingRouterTests {
MessageChannel[] results = null;
if (message.getPayload().equals("foo") || message.getPayload().equals("bar")) {
results = new MessageChannel[2];
results[0] = registry.lookupChannel("foo-channel");
results[1] = registry.lookupChannel("bar-channel");
results[0] = mapping.getChannel("foo-channel");
results[1] = mapping.getChannel("bar-channel");
}
return results;
}

View File

@@ -19,15 +19,10 @@ package org.springframework.integration.router;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelMapping;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
@@ -38,31 +33,8 @@ import org.springframework.integration.message.StringMessage;
public class MultiChannelRouterTests {
@Test
public void routeWithChannelResolver() {
final QueueChannel channel1 = new QueueChannel();
final QueueChannel channel2 = new QueueChannel();
ChannelResolver channelResolver = new ChannelResolver() {
public List<MessageChannel> resolveChannels(Message<?> message) {
List<MessageChannel> channels = new ArrayList<MessageChannel>();
channels.add(channel1);
channels.add(channel2);
return channels;
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
Message<?> result1 = channel1.receive(25);
assertNotNull(result1);
assertEquals("test", result1.getPayload());
Message<?> result2 = channel2.receive(25);
assertNotNull(result2);
assertEquals("test", result2.getPayload());
}
@Test
public void routeWithChannelNameResolver() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
public void routeWithChannelMapping() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] {"channel1", "channel2"};
}
@@ -71,13 +43,12 @@ public class MultiChannelRouterTests {
QueueChannel channel2 = new QueueChannel();
channel1.setBeanName("channel1");
channel2.setBeanName("channel2");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(channel1);
channelRegistry.registerChannel(channel2);
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(channel1);
channelMapping.addChannel(channel2);
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> result1 = channel1.receive(25);
assertNotNull(result1);
assertEquals("test", result1.getPayload());
@@ -88,28 +59,26 @@ public class MultiChannelRouterTests {
@Test(expected = MessagingException.class)
public void channelNameLookupFailure() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] {"noSuchChannel"};
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessagingException.class)
public void channelRegistryNotAvailable() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
public void channelMappingNotAvailable() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] {"noSuchChannel"};
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
}

View File

@@ -24,7 +24,6 @@ import java.util.concurrent.ConcurrentHashMap;
import org.junit.Test;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
@@ -40,15 +39,15 @@ public class PayloadTypeRouterTests {
public void resolveByPayloadType() {
QueueChannel stringChannel = new QueueChannel();
QueueChannel integerChannel = new QueueChannel();
Map<Class<?>, MessageChannel> channelMappings = new ConcurrentHashMap<Class<?>, MessageChannel>();
channelMappings.put(String.class, stringChannel);
channelMappings.put(Integer.class, integerChannel);
PayloadTypeChannelResolver resolver = new PayloadTypeChannelResolver();
resolver.setChannelMappings(channelMappings);
Map<Class<?>, MessageChannel> payloadTypeChannelMap = new ConcurrentHashMap<Class<?>, MessageChannel>();
payloadTypeChannelMap.put(String.class, stringChannel);
payloadTypeChannelMap.put(Integer.class, integerChannel);
PayloadTypeRouter router = new PayloadTypeRouter();
router.setPayloadTypeChannelMap(payloadTypeChannelMap);
Message<String> message1 = new StringMessage("test");
Message<Integer> message2 = new GenericMessage<Integer>(123);
MessageChannel result1 = resolver.resolveChannel(message1);
MessageChannel result2 = resolver.resolveChannel(message2);
MessageChannel result1 = router.resolveChannel(message1);
MessageChannel result2 = router.resolveChannel(message2);
assertEquals(stringChannel, result1);
assertEquals(integerChannel, result2);
}
@@ -59,20 +58,15 @@ public class PayloadTypeRouterTests {
QueueChannel integerChannel = new QueueChannel();
stringChannel.setBeanName("stringChannel");
integerChannel.setBeanName("integerChannel");
Map<Class<?>, MessageChannel> channelMappings = new ConcurrentHashMap<Class<?>, MessageChannel>();
channelMappings.put(String.class, stringChannel);
channelMappings.put(Integer.class, integerChannel);
TestChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(stringChannel);
channelRegistry.registerChannel(integerChannel);
PayloadTypeChannelResolver resolver = new PayloadTypeChannelResolver();
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setChannelRegistry(channelRegistry);
Map<Class<?>, MessageChannel> payloadTypeChannelMap = new ConcurrentHashMap<Class<?>, MessageChannel>();
payloadTypeChannelMap.put(String.class, stringChannel);
payloadTypeChannelMap.put(Integer.class, integerChannel);
PayloadTypeRouter router = new PayloadTypeRouter();
router.setPayloadTypeChannelMap(payloadTypeChannelMap);
Message<String> message1 = new StringMessage("test");
Message<Integer> message2 = new GenericMessage<Integer>(123);
endpoint.onMessage(message1);
endpoint.onMessage(message2);
router.onMessage(message1);
router.onMessage(message2);
Message<?> reply1 = stringChannel.receive(0);
Message<?> reply2 = integerChannel.receive(0);
assertEquals("test", reply1.getPayload());
@@ -85,19 +79,15 @@ public class PayloadTypeRouterTests {
stringChannel.setBeanName("stringChannel");
QueueChannel defaultChannel = new QueueChannel();
defaultChannel.setBeanName("defaultChannel");
TestChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(stringChannel);
channelRegistry.registerChannel(defaultChannel);
Map<Class<?>, MessageChannel> channelMappings = new ConcurrentHashMap<Class<?>, MessageChannel>();
channelMappings.put(String.class, stringChannel);
PayloadTypeChannelResolver resolver = new PayloadTypeChannelResolver();
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
Map<Class<?>, MessageChannel> payloadTypeChannelMap = new ConcurrentHashMap<Class<?>, MessageChannel>();
payloadTypeChannelMap.put(String.class, stringChannel);
PayloadTypeRouter router = new PayloadTypeRouter();
router.setPayloadTypeChannelMap(payloadTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
Message<String> message1 = new StringMessage("test");
Message<Integer> message2 = new GenericMessage<Integer>(123);
endpoint.onMessage(message1);
endpoint.onMessage(message2);
router.onMessage(message1);
router.onMessage(message2);
Message<?> result1 = stringChannel.receive(25);
assertNotNull(result1);
assertEquals("test", result1.getPayload());

View File

@@ -45,11 +45,11 @@ public class RecipientListRouterTests {
List<MessageChannel> channels = new ArrayList<MessageChannel>();
channels.add(channel1);
channels.add(channel2);
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
resolver.setChannels(channels);
resolver.afterPropertiesSet();
RecipientListRouter router = new RecipientListRouter();
router.setChannels(channels);
router.afterPropertiesSet();
Message<String> message = new StringMessage("test");
Collection<MessageChannel> resolved = resolver.resolveChannels(message);
Collection<MessageChannel> resolved = router.resolveChannels(message);
assertEquals(2, resolved.size());
assertTrue(resolved.contains(channel1));
assertTrue(resolved.contains(channel2));
@@ -62,12 +62,11 @@ public class RecipientListRouterTests {
List<MessageChannel> channels = new ArrayList<MessageChannel>();
channels.add(channel1);
channels.add(channel2);
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
resolver.setChannels(channels);
resolver.afterPropertiesSet();
RouterEndpoint endpoint = new RouterEndpoint(resolver);
RecipientListRouter router = new RecipientListRouter();
router.setChannels(channels);
router.afterPropertiesSet();
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> result1 = channel1.receive(25);
assertNotNull(result1);
assertEquals("test", result1.getPayload());
@@ -80,11 +79,10 @@ public class RecipientListRouterTests {
public void routeToSingleChannel() {
QueueChannel channel = new QueueChannel();
channel.setBeanName("channel");
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
resolver.setChannels(Collections.singletonList((MessageChannel) channel));
RouterEndpoint endpoint = new RouterEndpoint(resolver);
RecipientListRouter router = new RecipientListRouter();
router.setChannels(Collections.singletonList((MessageChannel) channel));
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> result1 = channel.receive(25);
assertNotNull(result1);
assertEquals("test", result1.getPayload());
@@ -93,22 +91,24 @@ public class RecipientListRouterTests {
}
@Test(expected = IllegalArgumentException.class)
public void nullChannelListNotSettable() {
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
resolver.setChannels(null);
public void nullChannelListRejected() {
RecipientListRouter router = new RecipientListRouter();
router.setChannels(null);
router.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void emptyChannelListNotSettable() {
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
public void emptyChannelListRejected() {
RecipientListRouter router = new RecipientListRouter();
List<MessageChannel> channels = new ArrayList<MessageChannel>();
resolver.setChannels(channels);
router.setChannels(channels);
router.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void nullChannelListFailsInitialization() {
RecipientListChannelResolver resolver = new RecipientListChannelResolver();
resolver.afterPropertiesSet();
public void noChannelListFailsInitialization() {
RecipientListRouter router = new RecipientListRouter();
router.afterPropertiesSet();
}
}

View File

@@ -50,22 +50,21 @@ public class RootCauseErrorMessageRouterTests {
@Test
public void testMostSpecificCause() {
public void mostSpecificCause() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel);
channelMappings.put(RuntimeException.class, runtimeExceptionChannel);
channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(IllegalArgumentException.class, illegalArgumentChannel);
exceptionTypeChannelMap.put(RuntimeException.class, runtimeExceptionChannel);
exceptionTypeChannelMap.put(MessageHandlingException.class, messageHandlingExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(illegalArgumentChannel.receive(1000));
assertNull(defaultChannel.receive(0));
assertNull(runtimeExceptionChannel.receive(0));
@@ -73,21 +72,20 @@ public class RootCauseErrorMessageRouterTests {
}
@Test
public void testFallbackToNextMostSpecificCause() {
public void fallbackToNextMostSpecificCause() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(RuntimeException.class, runtimeExceptionChannel);
channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(RuntimeException.class, runtimeExceptionChannel);
exceptionTypeChannelMap.put(MessageHandlingException.class, messageHandlingExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(runtimeExceptionChannel.receive(1000));
assertNull(illegalArgumentChannel.receive(0));
assertNull(defaultChannel.receive(0));
@@ -95,20 +93,19 @@ public class RootCauseErrorMessageRouterTests {
}
@Test
public void testFallbackToErrorMessageType() {
public void fallbackToErrorMessageType() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(MessageHandlingException.class, messageHandlingExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(messageHandlingExceptionChannel.receive(1000));
assertNull(runtimeExceptionChannel.receive(0));
assertNull(illegalArgumentChannel.receive(0));
@@ -116,16 +113,15 @@ public class RootCauseErrorMessageRouterTests {
}
@Test
public void testFallbackToDefaultChannel() {
public void fallbackToDefaultChannel() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(defaultChannel.receive(1000));
assertNull(runtimeExceptionChannel.receive(0));
assertNull(illegalArgumentChannel.receive(0));
@@ -133,39 +129,37 @@ public class RootCauseErrorMessageRouterTests {
}
@Test(expected = MessageDeliveryException.class)
public void testNoMatchAndNoDefaultChannel() {
public void noMatchAndNoDefaultChannel() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(MessageDeliveryException.class, messageDeliveryExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setResolutionRequired(true);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(MessageDeliveryException.class, messageDeliveryExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setResolutionRequired(true);
router.onMessage(message);
}
@Test
public void testExceptionPayloadButNotErrorMessage() {
public void exceptionPayloadButNotErrorMessage() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
Message<?> message = new GenericMessage<Exception>(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel);
channelMappings.put(RuntimeException.class, runtimeExceptionChannel);
channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(IllegalArgumentException.class, illegalArgumentChannel);
exceptionTypeChannelMap.put(RuntimeException.class, runtimeExceptionChannel);
exceptionTypeChannelMap.put(MessageHandlingException.class, messageHandlingExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(illegalArgumentChannel.receive(1000));
assertNull(defaultChannel.receive(0));
assertNull(runtimeExceptionChannel.receive(0));
@@ -173,21 +167,20 @@ public class RootCauseErrorMessageRouterTests {
}
@Test
public void testIntermediateCauseHasNoMappingButMostSpecificCauseDoes() {
public void intermediateCauseHasNoMappingButMostSpecificCauseDoes() {
Message<?> failedMessage = new StringMessage("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
RootCauseErrorMessageChannelResolver resolver = new RootCauseErrorMessageChannelResolver();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> exceptionTypeChannelMap =
new HashMap<Class<? extends Throwable>, MessageChannel>();
channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel);
channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel);
resolver.setChannelMappings(channelMappings);
RouterEndpoint endpoint = new RouterEndpoint(resolver);
endpoint.setDefaultOutputChannel(defaultChannel);
endpoint.onMessage(message);
exceptionTypeChannelMap.put(IllegalArgumentException.class, illegalArgumentChannel);
exceptionTypeChannelMap.put(MessageHandlingException.class, messageHandlingExceptionChannel);
router.setExceptionTypeChannelMap(exceptionTypeChannelMap);
router.setDefaultOutputChannel(defaultChannel);
router.onMessage(message);
assertNotNull(illegalArgumentChannel.receive(1000));
assertNull(defaultChannel.receive(0));
assertNull(runtimeExceptionChannel.receive(0));

View File

@@ -16,14 +16,17 @@
package org.springframework.integration.router;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelMapping;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessagingException;
@@ -32,143 +35,159 @@ import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class RouterEndpointTests {
public class RouterTests {
@Test
public void nullChannelIgnoredByDefault() {
ChannelResolver channelResolver = new ChannelResolver() {
AbstractMessageRouter router = new AbstractMessageRouter() {
public List<MessageChannel> resolveChannels(Message<?> message) {
return null;
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void nullChannelThrowsExceptionWhenResolutionRequired() {
ChannelResolver channelResolver = new ChannelResolver() {
AbstractMessageRouter router = new AbstractMessageRouter() {
public List<MessageChannel> resolveChannels(Message<?> message) {
return null;
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
endpoint.setResolutionRequired(true);
router.setResolutionRequired(true);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test
public void emptyChannelListIgnoredByDefault() {
ChannelResolver channelResolver = new ChannelResolver() {
AbstractMessageRouter router = new AbstractMessageRouter() {
public List<MessageChannel> resolveChannels(Message<?> message) {
return Collections.emptyList();
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void emptyChannelListThrowsExceptionWhenResolutionRequired() {
ChannelResolver channelResolver = new ChannelResolver() {
AbstractMessageRouter router = new AbstractMessageRouter() {
public List<MessageChannel> resolveChannels(Message<?> message) {
return Collections.emptyList();
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
endpoint.setResolutionRequired(true);
router.setResolutionRequired(true);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test
public void nullChannelNameArrayIgnoredByDefault() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return null;
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void nullChannelNameArrayThrowsExceptionWhenResolutionRequired() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return null;
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
endpoint.setResolutionRequired(true);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
router.setResolutionRequired(true);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test
public void emptyChannelNameArrayIgnoredByDefault() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] {};
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void emptyChannelNameArrayThrowsExceptionWhenResolutionRequired() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] {};
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
endpoint.setResolutionRequired(true);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
router.setResolutionRequired(true);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessagingException.class)
public void testChannelRegistryIsRequiredWhenUsingChannelNameResolverWithSingleChannelRouter() {
AbstractSingleChannelNameResolver channelNameResolver = new AbstractSingleChannelNameResolver() {
public void channelMappingIsRequiredWhenResolvingChannelNamesWithSingleChannelRouter() {
AbstractSingleChannelNameRouter router = new AbstractSingleChannelNameRouter() {
public String resolveChannelName(Message<?> message) {
return "notImportant";
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.onMessage(new StringMessage("this should fail"));
router.onMessage(new StringMessage("this should fail"));
}
@Test(expected = MessagingException.class)
public void testChannelRegistryIsRequiredWhenUsingChannelNameResolverWithMultiChannelRouter() {
AbstractChannelNameResolver channelNameResolver = new AbstractChannelNameResolver() {
public void channelMappingIsRequiredWhenResolvingChannelNamesWithMultiChannelRouter() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] { "notImportant" };
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.onMessage(new StringMessage("this should fail"));
router.onMessage(new StringMessage("this should fail"));
}
@Test(expected = IllegalArgumentException.class)
public void testChannelResolverMustNotBeNull() {
AbstractSingleChannelNameResolver channelNameResolver = null;
new RouterEndpoint(channelNameResolver);
@Test
public void beanFactoryWithSingleChannelRouter() {
AbstractSingleChannelNameRouter router = new AbstractSingleChannelNameRouter() {
public String resolveChannelName(Message<?> message) {
return "testChannel";
}
};
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testChannel", testChannel);
router.setBeanFactory(context);
router.onMessage(new StringMessage("test"));
Message<?> reply = testChannel.receive(0);
assertEquals("test", reply.getPayload());
}
@Test
public void beanFactoryWithMultiChannelRouter() {
AbstractChannelMappingMessageRouter router = new AbstractChannelMappingMessageRouter() {
public String[] resolveChannelNames(Message<?> message) {
return new String[] { "testChannel" };
}
};
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testChannel", testChannel);
router.setBeanFactory(context);
router.onMessage(new StringMessage("test"));
Message<?> reply = testChannel.receive(0);
assertEquals("test", reply.getPayload());
}
}

View File

@@ -21,10 +21,9 @@ import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.TestChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.TestChannelMapping;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
@@ -37,14 +36,13 @@ public class SingleChannelRouterTests {
@Test
public void routeWithChannelResolver() {
final QueueChannel channel = new QueueChannel();
AbstractSingleChannelResolver channelResolver = new AbstractSingleChannelResolver() {
AbstractSingleChannelRouter router = new AbstractSingleChannelRouter() {
public MessageChannel resolveChannel(Message<?> message) {
return channel;
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> result = channel.receive(25);
assertNotNull(result);
assertEquals("test", result.getPayload());
@@ -52,19 +50,18 @@ public class SingleChannelRouterTests {
@Test
public void routeWithChannelNameResolver() {
AbstractSingleChannelNameResolver channelNameResolver = new AbstractSingleChannelNameResolver() {
AbstractSingleChannelNameRouter router = new AbstractSingleChannelNameRouter() {
public String resolveChannelName(Message<?> message) {
return "testChannel";
}
};
QueueChannel channel = new QueueChannel();
channel.setBeanName("testChannel");
ChannelRegistry channelRegistry = new TestChannelRegistry();
channelRegistry.registerChannel(channel);
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
channelMapping.addChannel(channel);
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
Message<?> result = channel.receive(25);
assertNotNull(result);
assertEquals("test", result.getPayload());
@@ -72,28 +69,26 @@ public class SingleChannelRouterTests {
@Test
public void nullChannelResultIgnored() {
AbstractSingleChannelResolver channelResolver = new AbstractSingleChannelResolver() {
AbstractSingleChannelRouter router = new AbstractSingleChannelRouter() {
public MessageChannel resolveChannel(Message<?> message) {
return null;
}
};
RouterEndpoint endpoint = new RouterEndpoint(channelResolver);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
@Test(expected = MessagingException.class)
public void channelNameResolutionFailure() {
AbstractSingleChannelNameResolver channelNameResolver = new AbstractSingleChannelNameResolver() {
AbstractSingleChannelNameRouter router = new AbstractSingleChannelNameRouter() {
public String resolveChannelName(Message<?> message) {
return "noSuchChannel";
}
};
ChannelRegistry channelRegistry = new TestChannelRegistry();
RouterEndpoint endpoint = new RouterEndpoint(channelNameResolver);
endpoint.setChannelRegistry(channelRegistry);
TestChannelMapping channelMapping = new TestChannelMapping();
router.setChannelMapping(channelMapping);
Message<String> message = new StringMessage("test");
endpoint.onMessage(message);
router.onMessage(message);
}
}