INT-1517, combined Single and Multi channel XPath routers, modified XPathRouterParser to inherit form the base parser for all routers, combine single/multiple xpath router tests into one

This commit is contained in:
Oleg Zhurakousky
2010-10-14 06:45:22 -04:00
parent 3a8444c949
commit 1d0b773f2b
10 changed files with 101 additions and 332 deletions

View File

@@ -43,6 +43,7 @@ import org.w3c.dom.Document;
/**
* @author Jonas Partner
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
@ContextConfiguration
public class XPathRouterParserTests {

View File

@@ -17,7 +17,7 @@
<int-xml:mapping value="channelA" channel="channelB"/>
</int-xml:xpath-router>
<int-xml:xpath-router id="xpathRouterWithMappingMultiChannel" input-channel="multiChannelRouterChannel" multi-channel="true">
<int-xml:xpath-router id="xpathRouterWithMappingMultiChannel" input-channel="multiChannelRouterChannel">
<int-xml:xpath-expression expression="/root/name"/>
<int-xml:mapping value="channelA" channel="channelA"/>
<int-xml:mapping value="channelB" channel="channelA"/>

View File

@@ -18,6 +18,8 @@ package org.springframework.integration.xml.router;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
@@ -31,14 +33,14 @@ import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* @author Jonas Partner
*/
public class XPathMultiChannelRouterTests {
public class XPathRouterTests {
@Test
@SuppressWarnings("unchecked")
public void simpleSingleAttribute() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray();
assertEquals("Wrong number of channels returned", 1, channelNames.length);
assertEquals("Wrong channel name", "one", channelNames[0]);
@@ -49,7 +51,7 @@ public class XPathMultiChannelRouterTests {
public void multipleNodeValues() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray();
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
@@ -60,7 +62,7 @@ public class XPathMultiChannelRouterTests {
@SuppressWarnings("unchecked")
public void multipleNodeValuesAsString() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>")).toArray();
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
@@ -70,17 +72,59 @@ public class XPathMultiChannelRouterTests {
@Test(expected = MessagingException.class)
public void nonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void nodePayload() throws Exception {
XPathMultiChannelRouter router = new XPathMultiChannelRouter("./three/text()");
XPathRouter router = new XPathRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three><three>dave</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob",channelNames[0]);
assertEquals("dave",channelNames[1]);
}
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<Document>(doc)).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test
public void testSimpleStringDoc() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<String>("<doc type='one' />")).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathRouter router = new XPathRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob", channelNames[0]);
}
@Test
public void testEvaluationReturnsEmptyString() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type");
XPathRouter router = new XPathRouter(expression);
List<Object> channelNames = router.getChannelIndicatorList(new GenericMessage<Document>(doc));
assertEquals(0, channelNames.size());
}
}

View File

@@ -1,78 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.router;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.springframework.integration.MessagingException;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* @author Jonas Partner
*/
public class XPathSingleChannelRouterTests {
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<Document>(doc)).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test
public void testSimpleStringDoc() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<String>("<doc type='one' />")).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathSingleChannelRouter router = new XPathSingleChannelRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob", channelNames[0]);
}
@Test
public void testEvaluationReturnsEmptyString() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelNames = router.getChannelIndicatorList(new GenericMessage<Document>(doc));
assertEquals("Wrong channel name", null, channelNames);
}
}