fix for INT-491
This commit is contained in:
@@ -49,10 +49,14 @@ public class XPathMessageSplitterParser extends AbstractConsumerEndpointParser {
|
||||
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XPathMessageSplitter.class);
|
||||
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
|
||||
String documentBuilderFactoryRef = element.getAttribute("doc-builder-factory");
|
||||
String createDocuments = element.getAttribute("create-documents");
|
||||
|
||||
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
|
||||
Assert.isTrue(xPathExpressionNodes.getLength() <= 1, "only one xpath-expression child can be specified");
|
||||
boolean hasChild = xPathExpressionNodes.getLength() == 1;
|
||||
boolean hasReference = StringUtils.hasText(xPathExpressionRef);
|
||||
|
||||
Assert.isTrue(hasChild ^ hasReference, "Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
|
||||
if (hasChild) {
|
||||
BeanDefinition beanDefinition = this.xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
|
||||
@@ -61,6 +65,14 @@ public class XPathMessageSplitterParser extends AbstractConsumerEndpointParser {
|
||||
else {
|
||||
builder.addConstructorArgReference(xPathExpressionRef);
|
||||
}
|
||||
|
||||
if(StringUtils.hasText(documentBuilderFactoryRef)){
|
||||
builder.addPropertyReference("documentBuilder", documentBuilderFactoryRef);
|
||||
}
|
||||
if(StringUtils.hasText("create-documents")){
|
||||
builder.addPropertyValue("createDocuments", Boolean.valueOf(createDocuments));
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
public class StubDocumentBuilderFactory extends DocumentBuilderFactory{
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String name) throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getFeature(String name) throws ParserConfigurationException {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, Object value) throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFeature(String name, boolean value) throws ParserConfigurationException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,19 +17,23 @@
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
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;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
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
|
||||
@@ -39,23 +43,77 @@ public class XPathMessageSplitterParserTests {
|
||||
|
||||
String channelDefinitions = "<si:channel id='test-input' /><si:channel id='test-output'><si:queue capacity='10'/></si:channel>";
|
||||
|
||||
@Autowired @Qualifier("test-input")
|
||||
@Autowired
|
||||
@Qualifier("test-input")
|
||||
MessageChannel inputChannel;
|
||||
|
||||
@Autowired @Qualifier("test-output")
|
||||
@Autowired
|
||||
@Qualifier("test-output")
|
||||
QueueChannel outputChannel;
|
||||
|
||||
@Test
|
||||
public void testSimpleStringExpression() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper.getTestAppContext(
|
||||
channelDefinitions + "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>");
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer)ctx.getBean("splitter");
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper
|
||||
.getTestAppContext(channelDefinitions
|
||||
+ "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>");
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter");
|
||||
consumer.start();
|
||||
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
|
||||
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,
|
||||
false);
|
||||
inputChannel.send(docMessage);
|
||||
assertEquals("Wrong number of split messages ", 2, outputChannel.getMesssageCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleStringExpressionWithCreateDocuments() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper
|
||||
.getTestAppContext(channelDefinitions
|
||||
+ "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output' create-documents='true'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>");
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter");
|
||||
consumer.start();
|
||||
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,
|
||||
false);
|
||||
inputChannel.send(docMessage);
|
||||
assertEquals("Wrong number of split messages ", 2, outputChannel.getMesssageCount());
|
||||
assertTrue("Splitter failed to create documents ",
|
||||
((Message) outputChannel.receive(1000)).getPayload() instanceof Document);
|
||||
assertTrue("Splitter failed to create documents ",
|
||||
((Message) outputChannel.receive(1000)).getPayload() instanceof Document);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProvideDocumentBuilder() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>");
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper
|
||||
.getTestAppContext("<bean id='docBuilderFactory' class='org.springframework.integration.xml.config.StubDocumentBuilderFactory' />"
|
||||
+ channelDefinitions
|
||||
+ "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output' doc-builder-factory='docBuilderFactory'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>");
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter");
|
||||
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(consumer);
|
||||
Object handler = fieldAccessor.getPropertyValue("handler");
|
||||
fieldAccessor = new DirectFieldAccessor(handler);
|
||||
Object documnetBuilderFactory = fieldAccessor.getPropertyValue("documentBuilderFactory");
|
||||
assertTrue("DocumnetBuilderFactory was not expected stub ", documnetBuilderFactory instanceof DocumentBuilderFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXPathExpressionRef() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>");
|
||||
TestXmlApplicationContext ctx = TestXmlApplicationContextHelper
|
||||
.getTestAppContext(
|
||||
channelDefinitions +
|
||||
"<si-xml:xpath-expression id='xpathOne' expression='//name'/>" +
|
||||
"<si-xml:xpath-splitter id='splitter' xpath-expression-ref='xpathOne' input-channel='test-input' output-channel='test-output' />");
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter");
|
||||
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(consumer);
|
||||
Object handler = fieldAccessor.getPropertyValue("handler");
|
||||
fieldAccessor = new DirectFieldAccessor(handler);
|
||||
Object documnetBuilderFactory = fieldAccessor.getPropertyValue("documentBuilderFactory");
|
||||
assertTrue("DocumnetBuilderFactory was not expected stub ", documnetBuilderFactory instanceof DocumentBuilderFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user