added namespace support for setting resolution required attribute and providing a custom ChannelResolver

SingleChannelRouter also now returns null when the xpath expression evaluates to empty String
INT-360
This commit is contained in:
Jonas Partner
2008-11-13 20:14:56 +00:00
parent 6b468da46d
commit b730a0ddc5
6 changed files with 106 additions and 16 deletions

View File

@@ -0,0 +1,30 @@
/*
* 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 org.springframework.integration.channel.ChannelResolver;
import org.springframework.integration.core.MessageChannel;
class StubChannelResolver implements ChannelResolver{
public MessageChannel resolveChannelName(String channelName) {
return null;
}
}

View File

@@ -18,10 +18,9 @@ package org.springframework.integration.xml.config;
import static org.junit.Assert.assertEquals;
import org.w3c.dom.Document;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -32,6 +31,7 @@ import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
@@ -114,5 +114,50 @@ public class XPathRouterParserTests {
inputChannel.send(docMessage);
assertEquals("Wrong number of messages", 1, outputChannel.getMesssageCount());
}
@Test
public void testSetChannelResolver() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer contextBuffer = new StringBuffer("<si-xml:xpath-router id='router' channel-resolver='stubResolver' input-channel='test-input'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>");
contextBuffer.append("<bean id='stubResolver' class='").append(StubChannelResolver.class.getName()).append("'/>");
EventDrivenConsumer consumer = buildContext(contextBuffer.toString());
DirectFieldAccessor accessor = new DirectFieldAccessor(consumer);
Object handler = accessor.getPropertyValue("handler");
accessor = new DirectFieldAccessor(handler);
Object resolver = accessor.getPropertyValue("channelResolver");
assertEquals("Wrong channel resolver ",StubChannelResolver.class, resolver.getClass());
}
@Test
public void testSetResolutionRequiredFalse() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer contextBuffer = new StringBuffer("<si-xml:xpath-router id='router' resolution-required='false' input-channel='test-input'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>");
EventDrivenConsumer consumer = buildContext(contextBuffer.toString());
DirectFieldAccessor accessor = new DirectFieldAccessor(consumer);
Object handler = accessor.getPropertyValue("handler");
accessor = new DirectFieldAccessor(handler);
Object resolutionRequired = accessor.getPropertyValue("resolutionRequired");
assertEquals("Resolution required not set to false ", false, resolutionRequired);
}
@Test
public void testSetResolutionRequiredTrue() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
StringBuffer contextBuffer = new StringBuffer("<si-xml:xpath-router id='router' resolution-required='true' input-channel='test-input'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>");
EventDrivenConsumer consumer = buildContext(contextBuffer.toString());
DirectFieldAccessor accessor = new DirectFieldAccessor(consumer);
Object handler = accessor.getPropertyValue("handler");
accessor = new DirectFieldAccessor(handler);
Object resolutionRequired = accessor.getPropertyValue("resolutionRequired");
assertEquals("Resolution required not set to true ", true, resolutionRequired);
}
}

View File

@@ -19,15 +19,14 @@ 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.core.MessagingException;
import org.springframework.integration.message.GenericMessage;
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
@@ -62,16 +61,18 @@ public class XPathSingleChannelRouterTests {
public void testNodePayload() throws Exception {
XPathSingleChannelRouter router = new XPathSingleChannelRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
String[] channelNames = router.determineTargetChannelNames(new GenericMessage<Node>(
testDocument.getElementsByTagName("two").item(0)));
String[] channelNames = router.determineTargetChannelNames(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0)));
assertEquals("bob", channelNames[0]);
}
@Test(expected=MessagingException.class)
@Test
public void testEvaluationReturnsEmptyString() throws Exception {
XPathSingleChannelRouter router = new XPathSingleChannelRouter("/yellow");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
router.determineTargetChannelNames(new GenericMessage<Node>(testDocument));
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelNames = router.determineTargetChannelNames(new GenericMessage<Document>(doc));
assertEquals("Wrong channel name", null, channelNames);
}
}