INT-1156 completed namespace support for XPath Transformer
This commit is contained in:
@@ -24,4 +24,16 @@
|
||||
|
||||
<xpath-transformer input-channel="nodeListInput" expression="/person/@*" evaluation-type="NODE_LIST_RESULT" output-channel="output"/>
|
||||
|
||||
<xpath-transformer input-channel="nodeMapperInput" expression="/person/@age" node-mapper="testNodeMapper" output-channel="output"/>
|
||||
|
||||
<xpath-transformer input-channel="customConverterInput" expression="/test/@type" converter="testXmlPayloadConverter" output-channel="output"/>
|
||||
|
||||
<xpath-transformer input-channel="expressionRefInput" expression-ref="testExpression" evaluation-type="NUMBER_RESULT" output-channel="output"/>
|
||||
|
||||
<xpath-expression id="testExpression" expression="/person/@age * 2"/>
|
||||
|
||||
<beans:bean id="testNodeMapper" class="org.springframework.integration.xml.config.XPathTransformerParserTests$TestNodeMapper"/>
|
||||
|
||||
<beans:bean id="testXmlPayloadConverter" class="org.springframework.integration.xml.config.XPathTransformerParserTests$TestXmlPayloadConverter"/>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -19,19 +19,28 @@ package org.springframework.integration.xml.config;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.xml.xpath.NodeMapper;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -56,6 +65,15 @@ public class XPathTransformerParserTests {
|
||||
@Autowired
|
||||
private MessageChannel nodeListInput;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel nodeMapperInput;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel customConverterInput;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel expressionRefInput;
|
||||
|
||||
@Autowired
|
||||
private PollableChannel output;
|
||||
|
||||
@@ -99,4 +117,55 @@ public class XPathTransformerParserTests {
|
||||
assertEquals(3, nodeList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nodeMapper() {
|
||||
this.nodeMapperInput.send(message);
|
||||
assertEquals("42-mapped", output.receive(0).getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customConverter() {
|
||||
this.customConverterInput.send(message);
|
||||
assertEquals("custom", output.receive(0).getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionRef() {
|
||||
this.expressionRefInput.send(message);
|
||||
assertEquals(new Double(84), output.receive(0).getPayload());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestNodeMapper implements NodeMapper {
|
||||
|
||||
@Override
|
||||
public Object mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getTextContent() + "-mapped";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestXmlPayloadConverter implements XmlPayloadConverter {
|
||||
|
||||
public Source convertToSource(Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Node convertToNode(Object object) {
|
||||
try {
|
||||
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
|
||||
new InputSource(new StringReader("<test type='custom'/>")));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Document convertToDocument(Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,17 +19,26 @@ package org.springframework.integration.xml.transformer;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.xml.xpath.NodeMapper;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -55,6 +64,14 @@ public class XPathTransformerTests {
|
||||
assertEquals("test", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xpathExpressionReferenceConstructorInsteadOfString() throws Exception {
|
||||
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/parent/child/@name");
|
||||
XPathTransformer transformer = new XPathTransformer(expression);
|
||||
Object result = transformer.doTransform(message);
|
||||
assertEquals("test", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void numberResult() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/parent/child/@age");
|
||||
@@ -111,6 +128,14 @@ public class XPathTransformerTests {
|
||||
assertEquals("test-mapped", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customConverter() throws Exception {
|
||||
XPathTransformer transformer = new XPathTransformer("/test/@type");
|
||||
transformer.setConverter(new TestXmlPayloadConverter());
|
||||
Object result = transformer.doTransform(message);
|
||||
assertEquals("custom", result);
|
||||
}
|
||||
|
||||
|
||||
private static class TestNodeMapper implements NodeMapper {
|
||||
|
||||
@@ -120,4 +145,26 @@ public class XPathTransformerTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestXmlPayloadConverter implements XmlPayloadConverter {
|
||||
|
||||
public Source convertToSource(Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Node convertToNode(Object object) {
|
||||
try {
|
||||
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
|
||||
new InputSource(new StringReader("<test type='custom'/>")));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Document convertToDocument(Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user