Added support for String payloads to XPath Routers
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.xml;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class DefaultXmlPayloadConverterTests {
|
||||
|
||||
DefaultXmlPayloadConverter converter;
|
||||
|
||||
Document testDocument;
|
||||
|
||||
String testDocumentAsString = "<test>hello</test>";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception{
|
||||
converter = new DefaultXmlPayloadConverter();
|
||||
testDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
|
||||
new InputSource(new StringReader(testDocumentAsString)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithString() {
|
||||
Document doc = converter.convertToDocument("<test>hello</test>");
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithDocument() {
|
||||
Document doc = converter.convertToDocument(testDocument);
|
||||
Assert.assertTrue(doc == testDocument);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,15 +19,13 @@ package org.springframework.integration.xml.router;
|
||||
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.message.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.xml.router.XPathMultiChannelNameResolver;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
@@ -56,6 +54,17 @@ public class XPathMultiChannelNameResolverTests {
|
||||
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);
|
||||
assertEquals("Wrong channel name", "bOne", channelNames[0]);
|
||||
assertEquals("Wrong channel name", "bTwo", channelNames[1]);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testNonNodePayload() throws Exception {
|
||||
|
||||
@@ -36,12 +36,22 @@ public class XPathSingleChannelNameResolverTests {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testSimpleDocType() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
|
||||
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
|
||||
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
|
||||
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
|
||||
String channelName = resolver.resolveChannelName(new GenericMessage(doc));
|
||||
assertEquals("Wrong channel name", "one", channelName);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testSimpleStringDoc() throws Exception {
|
||||
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
|
||||
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
|
||||
String channelName = resolver.resolveChannelName(new GenericMessage("<doc type='one' />"));
|
||||
assertEquals("Wrong channel name", "one", channelName);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testNonNodePayload() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user