support for setting parameters and output properties on transformer instances used by the XsltPayloadTransformer
resolves INT-897
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package org.springframework.integration.xml.transformer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.xml.transform.ErrorListener;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.URIResolver;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
public class DefaultTransformerConfiguerTests {
|
||||
|
||||
StubTransformer transformer;
|
||||
|
||||
DefaultTransformerConfigurer transformerConfigurer;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
this.transformer = new StubTransformer();
|
||||
this.transformerConfigurer = new DefaultTransformerConfigurer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testSettingParametersAndOutputProperties(){
|
||||
Message<String> testMessage = MessageBuilder.withPayload("test")
|
||||
.setHeader("xslt_parameter_headerOne",1)
|
||||
.setHeader("xslt_parameter_headerTwo", "string")
|
||||
.setHeader("xslt_output_property_outOne","1")
|
||||
.setHeader("xslt_output_property_outTwo","2")
|
||||
.build();
|
||||
transformerConfigurer.configureTransfomer(testMessage, transformer);
|
||||
|
||||
Object paramOne = transformer.getParameter("headerOne");
|
||||
assertEquals("Wrong value for headerOne parameter",1, paramOne);
|
||||
|
||||
Object paramTwo = transformer.getParameter("headerTwo");
|
||||
assertEquals("Wrong value for headerTwo parameter","string", paramTwo);
|
||||
|
||||
String outPropertyOne = transformer.getOutputProperty("outOne");
|
||||
assertEquals("Wrong value for headerOne parameter","1", outPropertyOne);
|
||||
|
||||
String outPropertyTwo = transformer.getOutputProperty("outTwo");
|
||||
assertEquals("Wrong value for headerTwo parameter","2", outPropertyTwo);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNonStringOutputPropertyHeader(){
|
||||
Message<String> testMessage = MessageBuilder.withPayload("test")
|
||||
.setHeader("xslt_output_property_outOne",12)
|
||||
.build();
|
||||
transformerConfigurer.configureTransfomer(testMessage, transformer);
|
||||
}
|
||||
|
||||
private static class StubTransformer extends Transformer{
|
||||
|
||||
Map<String,Object> paramterMap = new HashMap<String, Object>();
|
||||
|
||||
Map<String, String> outputProperties = new HashMap<String, String>();
|
||||
|
||||
@Override
|
||||
public void clearParameters() {
|
||||
paramterMap.clear();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorListener getErrorListener() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getOutputProperties() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOutputProperty(String name)
|
||||
throws IllegalArgumentException {
|
||||
return outputProperties.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(String name) {
|
||||
return paramterMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URIResolver getURIResolver() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorListener(ErrorListener listener)
|
||||
throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOutputProperties(Properties oformat) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOutputProperty(String name, String value)
|
||||
throws IllegalArgumentException {
|
||||
outputProperties.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setParameter(String name, Object value) {
|
||||
paramterMap.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setURIResolver(URIResolver resolver) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(Source xmlSource, Result outputTarget)
|
||||
throws TransformerException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,7 +32,9 @@ import org.w3c.dom.Document;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessagingException;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
@@ -48,88 +50,92 @@ public class XsltPayloadTransformerTests {
|
||||
|
||||
private String outputAsString = "<bob>test</bob>";
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDocumentAsPayload() throws Exception {
|
||||
Object transformed = transformer.transformPayload(
|
||||
XmlTestUtil.getDocumentForString(docAsString));
|
||||
assertTrue("Wrong return type for document payload",
|
||||
Document.class.isAssignableFrom(transformed.getClass()));
|
||||
Object transformed = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertTrue("Wrong return type for document payload", Document.class
|
||||
.isAssignableFrom(transformed.getClass()));
|
||||
Document transformedDocument = (Document) transformed;
|
||||
assertXMLEqual(outputAsString, XmlTestUtil.docToString(transformedDocument));
|
||||
assertXMLEqual(outputAsString, XmlTestUtil
|
||||
.docToString(transformedDocument));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceAsPayload() throws Exception {
|
||||
Object transformed = transformer.transformPayload(new StringSource(docAsString));
|
||||
assertEquals("Wrong return type for source payload",
|
||||
DOMResult.class, transformed.getClass());
|
||||
Object transformed = transformer.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
assertEquals("Wrong return type for source payload", DOMResult.class,
|
||||
transformed.getClass());
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
assertXMLEqual("Document incorrect after transformation",
|
||||
XmlTestUtil.getDocumentForString(outputAsString),
|
||||
(Document) result.getNode());
|
||||
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
|
||||
.getDocumentForString(outputAsString), (Document) result
|
||||
.getNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayload() throws Exception {
|
||||
Object transformed = transformer.transformPayload(docAsString);
|
||||
assertEquals("Wrong return type for string payload",
|
||||
String.class, transformed.getClass());
|
||||
Object transformed = transformer.doTransform(buildMessage(docAsString));
|
||||
assertEquals("Wrong return type for string payload", String.class,
|
||||
transformed.getClass());
|
||||
String transformedString = (String) transformed;
|
||||
assertXMLEqual("String incorrect after transform",
|
||||
outputAsString, transformedString);
|
||||
assertXMLEqual("String incorrect after transform", outputAsString,
|
||||
transformedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayloadUseFactoriesTrue() throws Exception {
|
||||
transformer.setAlwaysUseSourceResultFactories(true);
|
||||
Object transformed = transformer.transformPayload(docAsString);
|
||||
Object transformed = transformer.doTransform(buildMessage(docAsString));
|
||||
assertEquals("Wrong return type for useFactories true",
|
||||
DOMResult.class, transformed.getClass());
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
assertXMLEqual("Document incorrect after transformation",
|
||||
XmlTestUtil.getDocumentForString(outputAsString),
|
||||
(Document) result.getNode());
|
||||
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
|
||||
.getDocumentForString(outputAsString), (Document) result
|
||||
.getNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceWithResultTransformer() throws Exception {
|
||||
Integer returnValue = new Integer(13);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue));
|
||||
Object transformed = transformer.transformPayload(new StringSource(docAsString));
|
||||
assertEquals("Wrong value from result conversion", returnValue, transformed);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(),
|
||||
new StubResultTransformer(returnValue));
|
||||
Object transformed = transformer.doTransform(buildMessage(new StringSource(
|
||||
docAsString)));
|
||||
assertEquals("Wrong value from result conversion", returnValue,
|
||||
transformed);
|
||||
}
|
||||
|
||||
@Test(expected = TransformerException.class)
|
||||
public void testNonXmlString() throws Exception {
|
||||
transformer.transformPayload("test");
|
||||
transformer.doTransform(buildMessage("test"));
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testUnsupportedPayloadType() throws Exception {
|
||||
transformer.transformPayload(new Long(12));
|
||||
transformer.doTransform(buildMessage(new Long(12)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXsltWithImports() throws Exception {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl", this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
assertThat(transformer.transformString(docAsString), is(outputAsString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXsltWithImports() throws Exception {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl",
|
||||
this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
assertEquals(transformer.doTransform(buildMessage(docAsString)), outputAsString);
|
||||
}
|
||||
|
||||
protected Message<?> buildMessage(Object payload){
|
||||
return MessageBuilder.withPayload(payload).build();
|
||||
}
|
||||
|
||||
private Resource getXslResource() throws Exception {
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"order\"><bob>test</bob></xsl:template></xsl:stylesheet>";
|
||||
return new ByteArrayResource(xsl.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
|
||||
public static class StubResultTransformer implements ResultTransformer {
|
||||
|
||||
private Object objectToReturn;
|
||||
|
||||
Reference in New Issue
Block a user