AbstractTransformerParser now creates the actual endpoint rather than requiring a "ref" to an object that implements Transformer. Therefore the element being parsed must provide 'input-channel' and 'output-channel'.

This commit is contained in:
Mark Fisher
2008-09-26 17:42:11 +00:00
parent 9ee921ed7c
commit 959b906f4e
13 changed files with 307 additions and 179 deletions

View File

@@ -1,30 +1,54 @@
<?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
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">
<si-xml:marshalling-transformer
id="marshallingTransformerNoResultFactory" marshaller="marshaller" />
<si:message-bus/>
<si:channel id="output">
<si:queue capacity="1"/>
</si:channel>
<si:channel id="marshallingTransformerNoResultFactory"/>
<si-xml:marshalling-transformer
id="marshallingTransformerStringResultFactory" marshaller="marshaller"
input-channel="marshallingTransformerNoResultFactory"
output-channel="output"
marshaller="marshaller" />
<si:channel id="marshallingTransformerStringResultFactory"/>
<si-xml:marshalling-transformer
input-channel="marshallingTransformerStringResultFactory"
output-channel="output"
marshaller="marshaller"
result-type="StringResult" />
<si:channel id="marshallingTransformerDOMResultFactory"/>
<si-xml:marshalling-transformer
id="marshallingTransformerDOMResultFactory" marshaller="marshaller"
input-channel="marshallingTransformerDOMResultFactory"
output-channel="output"
marshaller="marshaller"
result-type="DOMResult" />
<si:channel id="marshallingTransformerCustomResultFactory"/>
<si-xml:marshalling-transformer
id="marshallingTransformerCustomResultFactory" marshaller="marshaller"
input-channel="marshallingTransformerCustomResultFactory"
output-channel="output"
marshaller="marshaller"
result-factory="stubResultFactory" />
<si:channel id="marshallingTransformerWithResultTransformer"/>
<si-xml:marshalling-transformer
id="marshallingTransformerWithResultTransformer"
marshaller="marshaller" result-transformer="resultTransformer" />
input-channel="marshallingTransformerWithResultTransformer"
output-channel="output"
marshaller="marshaller"
result-transformer="resultTransformer" />
<bean id="marshaller" class="org.springframework.integration.xml.config.StubMarshaller" />

View File

@@ -20,38 +20,44 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.transformer.Transformer;
import org.springframework.integration.xml.config.StubResultFactory.StubStringResult;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class XmlMarshallingTransformerParserTests {
private ApplicationContext appContext;
private PollableChannel output;
@Before
public void setUp() {
this.appContext = new ClassPathXmlApplicationContext("XmlMarshallingTransformerParserTests-context.xml", getClass());
this.output = (PollableChannel) appContext.getBean("output");
}
@Test
public void testDefault() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerNoResultFactory");
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerNoResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Wrong payload type", result.getPayload() instanceof DOMResult);
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
@@ -59,9 +65,10 @@ public class XmlMarshallingTransformerParserTests {
@Test
public void testDefaultWithResultTransformer() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerWithResultTransformer");
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerWithResultTransformer");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Wrong payload type", result.getPayload() instanceof String);
String resultPayload = (String)result.getPayload();
assertEquals("Wrong palyoad", "testReturn", resultPayload);
@@ -69,9 +76,10 @@ public class XmlMarshallingTransformerParserTests {
@Test
public void testDOMResult() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerDOMResultFactory");
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerDOMResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Wrong payload type ", result.getPayload() instanceof DOMResult);
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
@@ -79,17 +87,19 @@ public class XmlMarshallingTransformerParserTests {
@Test
public void testStringResult() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerStringResultFactory");
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerStringResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Wrong payload type", result.getPayload() instanceof StringResult);
}
@Test
public void testCustomResultFactory() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerCustomResultFactory");
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerCustomResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Wrong payload type", result.getPayload() instanceof StubStringResult);
}

View File

@@ -1,14 +1,28 @@
<?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/xml/spring-integration-xml-1.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si-xml:unmarshalling-transformer id="defaultUnmarshaller" unmarshaller="unmarshaller" />
<si:message-bus/>
<bean id="unmarshaller" class="org.springframework.integration.xml.config.StubUnmarshaller" />
<si:channel id="input"/>
<si:channel id="output">
<si:queue capacity="1"/>
</si:channel>
<si-xml:unmarshalling-transformer id="defaultUnmarshaller"
input-channel="input"
output-channel="output"
unmarshaller="unmarshaller"/>
<bean id="unmarshaller" class="org.springframework.integration.xml.config.StubUnmarshaller"/>
</beans>

View File

@@ -26,15 +26,17 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.Transformer;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringSource;
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class XmlUnmarshallingTransformerParserTests {
@@ -45,49 +47,54 @@ public class XmlUnmarshallingTransformerParserTests {
@Before
public void setUp() {
appContext = new ClassPathXmlApplicationContext("XmlUnmarshallingTransformerParserTests-context.xml",
getClass());
appContext = new ClassPathXmlApplicationContext(
"XmlUnmarshallingTransformerParserTests-context.xml", this.getClass());
unmarshaller = (StubUnmarshaller) appContext.getBean("unmarshaller");
}
@Test
public void testDefaultUnmarshall() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("defaultUnmarshaller");
MessageChannel input = (MessageChannel) appContext.getBean("input");
PollableChannel output = (PollableChannel) appContext.getBean("output");
GenericMessage<Object> message = new GenericMessage<Object>(new StringSource(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
@Test
public void testUnmarshallString() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("defaultUnmarshaller");
MessageChannel input = (MessageChannel) appContext.getBean("input");
PollableChannel output = (PollableChannel) appContext.getBean("output");
GenericMessage<Object> message = new GenericMessage<Object>(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>");
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
@Test
public void testUnmarshallDocument() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("defaultUnmarshaller");
MessageChannel input = (MessageChannel) appContext.getBean("input");
PollableChannel output = (PollableChannel) appContext.getBean("output");
GenericMessage<Object> message = new GenericMessage<Object>(
XmlTestUtil
.getDocumentForString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
Message<?> result = transformer.transform(message);
XmlTestUtil.getDocumentForString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
input.send(message);
Message<?> result = output.receive(0);
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
}
@Test(expected = MessagingException.class)
public void testUnmarshallUnsupported() throws Exception {
Transformer transformer = (Transformer) appContext.getBean("defaultUnmarshaller");
MessageChannel input = (MessageChannel) appContext.getBean("input");
GenericMessage<Object> message = new GenericMessage<Object>(new StringBuffer(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
transformer.transform(message);
input.send(message);
}
}

View File

@@ -1,32 +1,59 @@
<?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
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">
<si:message-bus/>
<si:channel id="output">
<si:queue capacity="1"/>
</si:channel>
<si:channel id="withResourceIn"/>
<si-xml:xslt-transformer id="xsltTransformerWithResource"
xsl-resource="org/springframework/integration/xml/config/test.xsl" />
input-channel="withResourceIn"
output-channel="output"
xsl-resource="org/springframework/integration/xml/config/test.xsl"/>
<si:channel id="withTemplatesIn"/>
<si-xml:xslt-transformer id="xsltTransformerWithTemplates"
xsl-templates="templates" />
input-channel="withTemplatesIn"
output-channel="output"
xsl-templates="templates"/>
<si:channel id="withTemplatesAndResultTransformerIn"/>
<si-xml:xslt-transformer id="xsltTransformerWithTemplatesAndResultTransformer"
xsl-templates="templates" result-transformer="resultTransformer" />
input-channel="withTemplatesAndResultTransformerIn"
output-channel="output"
xsl-templates="templates"
result-transformer="resultTransformer"/>
<si:channel id="withTemplatesAndResultFactoryIn"/>
<si-xml:xslt-transformer id="xsltTransformerWithTemplatesAndResultFactory"
xsl-templates="templates" result-factory="stubResultFactory" />
input-channel="withTemplatesAndResultFactoryIn"
output-channel="output"
xsl-templates="templates"
result-factory="stubResultFactory"/>
<si-xml:xslt-transformer id="xsltTransformerWithTemplatesAndStringResulType"
xsl-templates="templates" result-type="StringResult" />
<si:channel id="withTemplatesAndStringResultTypeIn"/>
<si-xml:xslt-transformer id="xsltTransformerWithTemplatesAndStringResultType"
input-channel="withTemplatesAndStringResultTypeIn"
output-channel="output"
xsl-templates="templates"
result-type="StringResult"/>
<bean id="templates" class="org.springframework.integration.xml.config.TestTemplatesFactory" />
<bean id="templates" class="org.springframework.integration.xml.config.TestTemplatesFactory"/>
<bean id="resultTransformer" class="org.springframework.integration.xml.config.StubResultTransformer">
<constructor-arg value="testReturn" />
<constructor-arg value="testReturn"/>
</bean>
<bean id="stubResultFactory" class="org.springframework.integration.xml.config.StubResultFactory"/>

View File

@@ -23,20 +23,21 @@ import javax.xml.transform.dom.DOMResult;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.transformer.Transformer;
import org.springframework.integration.xml.config.StubResultFactory.StubStringResult;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringResult;
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class XsltPayloadTransformerParserTests {
@@ -44,19 +45,22 @@ public class XsltPayloadTransformerParserTests {
private ApplicationContext applicationContext;
private PollableChannel output;
@Before
public void setUp() {
applicationContext = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
output = (PollableChannel) applicationContext.getBean("output");
}
@Test
public void testWithResourceProvided() throws Exception {
Transformer transformer = (Transformer) applicationContext
.getBean("xsltTransformerWithResource");
MessageChannel input = (MessageChannel) applicationContext.getBean("withResourceIn");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Payload was not a DOMResult", result.getPayload() instanceof DOMResult);
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong payload", "test", doc.getDocumentElement().getTextContent());
@@ -64,10 +68,10 @@ public class XsltPayloadTransformerParserTests {
@Test
public void testWithTemplatesProvided() throws Exception {
Transformer transformer = (Transformer) applicationContext
.getBean("xsltTransformerWithTemplates");
MessageChannel input = (MessageChannel) applicationContext.getBean("withTemplatesIn");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Payload was not a DOMResult", result.getPayload() instanceof DOMResult);
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong payload", "test", doc.getDocumentElement().getTextContent());
@@ -75,30 +79,30 @@ public class XsltPayloadTransformerParserTests {
@Test
public void testWithTemplatesAndResultTransformer() throws Exception {
Transformer transformer = (Transformer) applicationContext
.getBean("xsltTransformerWithTemplatesAndResultTransformer");
MessageChannel input = (MessageChannel) applicationContext.getBean("withTemplatesAndResultTransformerIn");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertEquals("Wrong payload type", String.class, result.getPayload().getClass());
String strResult = (String)result.getPayload();
assertEquals("Wrong payload", "testReturn", strResult);
}
@Test
public void testWithResourceProvidedndStubResultFactory() throws Exception {
Transformer transformer = (Transformer) applicationContext
.getBean("xsltTransformerWithTemplatesAndResultFactory");
MessageChannel input = (MessageChannel) applicationContext.getBean("withTemplatesAndResultFactoryIn");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Payload was not a StubStringResult", result.getPayload() instanceof StubStringResult);
}
@Test
public void testWithResourceAndStringResultType() throws Exception {
Transformer transformer = (Transformer) applicationContext
.getBean("xsltTransformerWithTemplatesAndStringResulType");
MessageChannel input = (MessageChannel) applicationContext.getBean("withTemplatesAndStringResultTypeIn");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = transformer.transform(message);
input.send(message);
Message<?> result = output.receive(0);
assertTrue("Payload was not a StringResult", result.getPayload() instanceof StringResult);
}