added Result transformer INT-286

Refactored OXM transformer creating separate marshalling and unamarshalling transformers and added namespace support INT-109
This commit is contained in:
Jonas Partner
2008-07-06 20:35:31 +00:00
parent b7f31fcbf0
commit 2128c1e21e
27 changed files with 973 additions and 48 deletions

View File

@@ -0,0 +1,48 @@
/*
* 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 java.io.IOException;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.xml.transform.StringSource;
public class StubMarshaller implements Marshaller {
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringSource stringSource = new StringSource("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root>" + graph.toString() + "</root>");
transformer.transform(stringSource, result);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
return true;
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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 java.io.IOException;
import java.util.LinkedList;
import javax.xml.transform.Source;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
public class StubUnmarshaller implements Unmarshaller {
public LinkedList<Source> sourcesPassed = new LinkedList<Source>();
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
return true;
}
public Object unmarshal(Source source) throws XmlMappingException, IOException {
sourcesPassed.addFirst(source);
return "unmarshalled";
}
}

View File

@@ -0,0 +1,25 @@
<?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: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/spring-integration-xml-1.0.xsd">
<si-xml:marshalling-transformer
id="marshallingTransfomerNoResultFactory" marshaller="marshaller" />
<si-xml:marshalling-transformer
id="marshallingTransfomerStringResultFactory" marshaller="marshaller"
result-factory="StringResult" />
<si-xml:marshalling-transformer
id="marshallingTransfomerDOMResultFactory" marshaller="marshaller"
result-factory="DOMResult" />
<bean id="marshaller"
class="org.springframework.integration.xml.config.StubMarshaller" />
</beans>

View File

@@ -0,0 +1,74 @@
/*
* 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 static org.junit.Assert.*;
import javax.xml.transform.dom.DOMResult;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
/**
*
* @author Jonas Partner
*
*/
public class XmlMarshallingTransformerParserTests {
ApplicationContext appContext;
@Before
public void setUp(){
appContext = new ClassPathXmlApplicationContext("XmlMarshallingTransformerParserTests-context.xml", getClass());
}
@Test
public void testDefault() throws Exception{
MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerNoResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
transformer.transform(message);
assertTrue("Wrong payload type ", message.getPayload() instanceof DOMResult);
Document doc = (Document)((DOMResult)message.getPayload()).getNode();
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
}
@Test
public void testDOMResult() throws Exception{
MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerDOMResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
transformer.transform(message);
assertTrue("Wrong payload type ", message.getPayload() instanceof DOMResult);
Document doc = (Document)((DOMResult)message.getPayload()).getNode();
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
}
@Test
public void testStringResult() throws Exception{
MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerStringResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
transformer.transform(message);
assertTrue("Wrong payload type ", message.getPayload() instanceof StringResult);
}
}

View File

@@ -0,0 +1,14 @@
<?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: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/spring-integration-xml-1.0.xsd">
<si-xml:unmarshalling-transformer id="defaultUnmarshaller" unmarshaller="unmarshaller" />
<bean id="unmarshaller"
class="org.springframework.integration.xml.config.StubUnmarshaller" />
</beans>

View File

@@ -0,0 +1,90 @@
/*
* 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 static org.junit.Assert.*;
import javax.xml.transform.dom.DOMSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringSource;
/**
*
* @author Jonas Partner
*
*/
public class XmlUnmarshallingTransformerParserTests {
ApplicationContext appContext;
StubUnmarshaller unmarshaller;
@Before
public void setUp() {
appContext = new ClassPathXmlApplicationContext("XmlUnmarshallingTransformerParserTests-context.xml",
getClass());
unmarshaller = (StubUnmarshaller) appContext.getBean("unmarshaller");
}
@Test
public void testDefaultUnmarshall() throws Exception {
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
GenericMessage<Object> message = new GenericMessage<Object>(new StringSource(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
transformer.transform(message);
assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
@Test
public void testUnmarshallString() throws Exception {
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
GenericMessage<Object> message = new GenericMessage<Object>(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>");
transformer.transform(message);
assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
}
@Test
public void testUnmarshallDocument() throws Exception {
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
GenericMessage<Object> message = new GenericMessage<Object>(
XmlTestUtil
.getDocumentForString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
transformer.transform(message);
assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
}
@Test(expected = MessagingException.class)
public void testUnmarshallUnsupported() throws Exception {
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
GenericMessage<Object> message = new GenericMessage<Object>(new StringBuffer(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
transformer.transform(message);
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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.source;
import static org.junit.Assert.*;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessagingException;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
*
* @author Jonas Partner
*
*/
public class DomSourceFactoryTests {
Document doc;
DomSourceFactory sourceFactory;
String docContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>testValue</root>";
@Before
public void setUp() throws Exception {
StringReader reader = new StringReader(docContent);
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader));
sourceFactory = new DomSourceFactory();
}
@Test
public void testWithDocumentPayload() throws Exception {
GenericMessage<Object> message = new GenericMessage<Object>(doc);
Source source = sourceFactory.getSourceForMessage(message);
assertNotNull("Returned source was null", source);
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
assertEquals("Wrong content in source ", docContent, getAsString(source));
}
@Test
public void testWithStringPayload() throws Exception {
GenericMessage<Object> message = new GenericMessage<Object>(docContent);
Source source = sourceFactory.getSourceForMessage(message);
assertNotNull("Returned source was null", source);
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
assertEquals("Wrong content in source ", docContent, getAsString(source));
}
@Test(expected = MessagingException.class)
public void testWithUnsupportedPayload() throws Exception {
GenericMessage<Object> message = new GenericMessage<Object>(12);
sourceFactory.getSourceForMessage(message);
}
String getAsString(Source source) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringResult res = new StringResult();
transformer.transform(source, res);
return res.toString();
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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.transformer;
import static org.junit.Assert.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
public class ResultToDocumentTransformerTests {
String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
ResultToDocumentTransformer resToDocTransformer;
@Before
public void setUp() {
resToDocTransformer = new ResultToDocumentTransformer();
}
@Test
public void testWithDomResult() throws Exception {
DOMResult result = XmlTestUtil.getDomResultForString(doc);
GenericMessage<Object> message = new GenericMessage<Object>(result);
resToDocTransformer.transform(message);
assertTrue("Wrong payload type expected Document", message.getPayload() instanceof Document);
Document doc = (Document) message.getPayload();
assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName());
}
@Test
public void testWithStringResult() throws Exception {
StringResult result = XmlTestUtil.getStringResultForString(doc);
GenericMessage<Object> message = new GenericMessage<Object>(result);
resToDocTransformer.transform(message);
assertTrue("Wrong payload type expected Document", message.getPayload() instanceof Document);
Document doc = (Document) message.getPayload();
assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName());
}
@Test(expected = MessagingException.class)
public void testWithUnsupportedSaxResult() throws Exception {
SAXResult result = new SAXResult();
GenericMessage<Object> message = new GenericMessage<Object>(result);
resToDocTransformer.transform(message);
}
}

View File

@@ -49,23 +49,12 @@ public class XsltPayloadTransformerTest {
transformer = new XsltPayloadTransformer(getXslResource());
}
@Test
public void testDocumentAsPayload() throws Exception {
@Test(expected = MessagingException.class)
public void testDocumentAsPayloadShouldBeRejected() throws Exception {
Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(getInputString())));
GenericMessage<Document> documentMessage = new GenericMessage<Document>(input);
transformer.transform(documentMessage);
String rootNodeName = ((Document) documentMessage.getPayload()).getDocumentElement().getNodeName();
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
}
@Test
public void testXmlAsStringPayload() throws Exception {
StringMessage message = new StringMessage(getInputString());
transformer.transform(message);
String rootNodeName = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(message.getPayload()))).getDocumentElement().getNodeName();
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
}
@Test

View File

@@ -0,0 +1,66 @@
/*
* 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.util;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* Utill class for XML related testing
* @author Jonas Partner
*
*/
public class XmlTestUtil {
public static Document getDocumentForString(String strDoc) throws Exception {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new InputSource(new StringReader(strDoc)));
}
public static DOMSource getDomSourceForString(String strDoc) throws Exception {
DOMSource domSource = new DOMSource();
domSource.setNode(getDocumentForString(strDoc));
return domSource;
}
public static DOMResult getDomResultForString(String strDoc) throws Exception {
DOMResult res = new DOMResult();
transform(getDomSourceForString(strDoc), res);
return res;
}
public static StringResult getStringResultForString(String strDoc) throws Exception {
StringResult res = new StringResult();
transform(getDomSourceForString(strDoc), res);
return res;
}
public static void transform(Source source, Result res) throws Exception {
TransformerFactory.newInstance().newTransformer().transform(source, res);
}
}