OPEN - issue INT-309: XPath Message Selector
http://jira.springframework.org/browse/INT-309 namespace support
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.context.support.AbstractXmlApplicationContext;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
public class TestXmlApplicationContext extends AbstractXmlApplicationContext {
|
||||
|
||||
private final Resource[] resources;
|
||||
|
||||
public TestXmlApplicationContext(String ... xmlStrings){
|
||||
resources = new Resource[xmlStrings.length];
|
||||
for (int i = 0 ; i < xmlStrings.length; i++) {
|
||||
resources[i] = new TestResource(xmlStrings[i]);
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Resource[] getConfigResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
private static class TestResource extends AbstractResource{
|
||||
|
||||
String xmlString;
|
||||
|
||||
TestResource(String xmlString){
|
||||
this.xmlString = xmlString;
|
||||
}
|
||||
|
||||
|
||||
public String getDescription() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
public class TestXmlApplicationContextHelper {
|
||||
|
||||
public static TestXmlApplicationContext getTestAppContext(String xmlFragment) {
|
||||
String xml = header + xmlFragment + footer;
|
||||
TestXmlApplicationContext ctx = new TestXmlApplicationContext(xml);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private final static String header = "<?xml version='1.0' encoding='UTF-8'?>"
|
||||
+ "<beans xmlns='http://www.springframework.org/schema/beans' "
|
||||
+ "xmlns:si-xml='http://www.springframework.org/schema/integration/xml' "
|
||||
+ "xmlns:si='http://www.springframework.org/schema/integration' "
|
||||
+ "xmlns:util='http://www.springframework.org/schema/util' "
|
||||
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xsi:schemaLocation="
|
||||
+ "'http://www.springframework.org/schema/beans "
|
||||
+ "http://www.springframework.org/schema/beans/spring-beans.xsd "
|
||||
+ "http://www.springframework.org/schema/integration "
|
||||
+ "http://www.springframework.org/schema/integration/spring-integration-1.0.xsd "
|
||||
+ "http://www.springframework.org/schema/integration/xml "
|
||||
+ "http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd "
|
||||
+ "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd' >";
|
||||
|
||||
private final static String footer = "</beans>";
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/xml
|
||||
http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<bean id="messageChannel" class="org.springframework.integration.channel.QueueChannel" />
|
||||
|
||||
<si:message-bus/>
|
||||
|
||||
<si:channel id="inputOne" />
|
||||
<si:channel id="inputTwo" />
|
||||
|
||||
<si:channel id="outputOne">
|
||||
<si:queue capacity="10"/>
|
||||
</si:channel>
|
||||
<si:channel id="outputTwo">
|
||||
<si:queue capacity="10"/>
|
||||
</si:channel>
|
||||
|
||||
<si:channel id="errorChannel">
|
||||
<si:queue capacity="10"/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-router id="routerOne"
|
||||
xpath-expression-ref="xpathExpression" />
|
||||
<si:router ref="routerOne" input-channel="inputOne" error-handler="errorHandler"/>
|
||||
|
||||
<si-xml:xpath-router id="routerTwo"
|
||||
xpath-expression-ref="xpathExpressionMulti" multi-channel="true" />
|
||||
<si:router ref="routerTwo" input-channel="inputTwo" error-handler="errorHandler"/>
|
||||
|
||||
|
||||
<bean id="xpathExpression"
|
||||
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
|
||||
<property name="expression" value="/name" />
|
||||
</bean>
|
||||
|
||||
<bean id="xpathExpressionMulti"
|
||||
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
|
||||
<property name="expression" value="//name" />
|
||||
</bean>
|
||||
|
||||
<bean id="errorHandler" class="org.springframework.integration.channel.MessagePublishingErrorHandler">
|
||||
<property name="errorChannel" ref="errorChannel"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -16,100 +16,96 @@
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class XPathRouterParserTests extends AbstractJUnit4SpringContextTests{
|
||||
public class XPathRouterParserTests {
|
||||
|
||||
@Autowired @Qualifier("inputOne")
|
||||
MessageChannel inputOne;
|
||||
|
||||
@Autowired @Qualifier("inputTwo")
|
||||
MessageChannel inputTwo;
|
||||
|
||||
@Autowired @Qualifier("outputOne")
|
||||
PollableChannel outputOne;
|
||||
|
||||
@Autowired @Qualifier("outputTwo")
|
||||
PollableChannel outputTwo;
|
||||
|
||||
@Autowired @Qualifier("errorChannel")
|
||||
PollableChannel errorChannel;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputOne() throws Exception{
|
||||
public void testSimpleStringExpression() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
inputOne.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) outputOne.receive(1000);
|
||||
assertNotNull("Did not receive message from outputOne", received);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputTwo() throws Exception{
|
||||
Document doc = XmlTestUtil.getDocumentForString("<name>outputTwo</name>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
inputOne.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) outputTwo.receive(1000);
|
||||
assertNotNull("Did not receive message from two", received);
|
||||
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/name' />");
|
||||
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
|
||||
|
||||
String[] channelNames = router.resolveChannelNames(docMessage);
|
||||
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
|
||||
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputThree() throws Exception{
|
||||
Document doc = XmlTestUtil.getDocumentForString("<name>outputThree</name>");
|
||||
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);
|
||||
inputOne.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) errorChannel.receive(1000);
|
||||
assertNotNull("Did not receive message on errors", received);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputOneMulti() throws Exception{
|
||||
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
inputTwo.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) outputOne.receive(1000);
|
||||
assertNotNull("Did not receive message from outputOne", received);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputOneAndTwoMulti() throws Exception{
|
||||
Document doc = XmlTestUtil.getDocumentForString("<doc><name>outputOne</name><name>outputTwo</name></doc>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
inputTwo.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) outputTwo.receive(1000);
|
||||
assertNotNull("Did not receive message from two", received);
|
||||
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' ns-prefix='ns2' ns-uri='www.example.org' />");
|
||||
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
|
||||
|
||||
String[] channelNames = router.resolveChannelNames(docMessage);
|
||||
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
|
||||
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testOutputThreeMulti() throws Exception{
|
||||
Document doc = XmlTestUtil.getDocumentForString("<name>outputThree</name>");
|
||||
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>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
inputTwo.send(docMessage);
|
||||
GenericMessage<Document> received = (GenericMessage<Document>) errorChannel.receive(1000);
|
||||
assertNotNull("Did not receive message on errors", received);
|
||||
|
||||
StringBuffer buffer = new StringBuffer(
|
||||
"<si-xml:xpath-router id='router' xpath-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("</si-xml:xpath-router>");
|
||||
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
|
||||
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
|
||||
|
||||
String[] channelNames = router.resolveChannelNames(docMessage);
|
||||
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
|
||||
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithReferenceToNamespaceMap() throws Exception {
|
||||
Document doc = XmlTestUtil
|
||||
.getDocumentForString("<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' xpath-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>");
|
||||
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(buffer.toString());
|
||||
XPathSingleChannelNameResolver router = (XPathSingleChannelNameResolver) ctx.getBean("router");
|
||||
|
||||
String[] channelNames = router.resolveChannelNames(docMessage);
|
||||
assertEquals("Wrong number of channel names returned", 1, channelNames.length);
|
||||
assertEquals("Wrong channel name", "outputOne", channelNames[0]);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void testNamespacedWithNoPrefixStringExpression() throws Exception {
|
||||
TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' xpath-namespace='www.example.org' />");
|
||||
}
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void testPrefixWithNoNamespaceStringExpression() throws Exception {
|
||||
TestXmlApplicationContextHelper.getTestAppContext("<si-xml:xpath-router id='router' xpath-expression='/ns2:name' xpath-prefix='ns2' />");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class XPathSelectorParserTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleStringExpressionBoolean() throws Exception {
|
||||
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/name' evaluation-result-type='boolean' />";
|
||||
MessageSelector selector =getSelector( contextXml);
|
||||
|
||||
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
|
||||
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<other>outputOne</other>"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithNamespaceBoolean() throws Exception {
|
||||
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/ns:name' ns-prefix='ns' ns-uri='www.example.org' evaluation-result-type='boolean' />";
|
||||
MessageSelector selector = getSelector(contextXml);
|
||||
|
||||
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>"))));
|
||||
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringExpressionWithNestedMap() throws Exception {
|
||||
String contextXml = "<si-xml:xpath-selector id='selector' xpath-expression='/ns:name' ns-prefix='ns' ns-uri='www.example.org' evaluation-result-type='boolean' />";
|
||||
MessageSelector selector = getSelector(contextXml);
|
||||
|
||||
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>"))));
|
||||
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<name>outputOne</name>"))));
|
||||
|
||||
}
|
||||
|
||||
public MessageSelector getSelector( String testcontextXml) throws Exception{
|
||||
TestXmlApplicationContext ctx =
|
||||
TestXmlApplicationContextHelper.getTestAppContext(testcontextXml);
|
||||
return (MessageSelector) ctx.getBean("selector");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user