Separated the OXM marshalling and unmarshalling transformer

Added source and result factories
This commit is contained in:
Jonas Partner
2008-07-03 19:54:55 +00:00
parent 25bcd05293
commit 36343b4e1d
10 changed files with 328 additions and 96 deletions

View File

@@ -19,20 +19,19 @@ package org.springframework.integration.xml.transformer;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.xml.result.StringResultFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
/**
* @author Mark Fisher
@@ -41,26 +40,29 @@ public class XmlPayloadMarshallingTransformerTests {
@Test
public void testStringToStringResult() {
Marshaller marshaller = new TestMarshaller();
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
transformer.setResultFactory(new StringResultFactory());
Message<?> message = new StringMessage("world");
transformer.transform(message);
assertEquals(StringResult.class, message.getPayload().getClass());
assertEquals("hello world", message.getPayload().toString());
assertEquals("world", marshaller.payloads.get(0));
}
@Test
public void testStringSourceToString() {
Marshaller marshaller = new TestMarshaller();
public void testDefaultResultFactory() {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
Message<?> message = new GenericMessage<StringSource>(new StringSource("world"));
Message<?> message = new StringMessage("world");
transformer.transform(message);
assertEquals(String.class, message.getPayload().getClass());
assertEquals("hello world", message.getPayload().toString());
assertEquals(DOMResult.class, message.getPayload().getClass());
assertEquals("world", marshaller.payloads.get(0));
}
private static class TestMarshaller implements Marshaller {
private static class TestMarshaller implements Marshaller, Unmarshaller {
List<Object> payloads = new ArrayList<Object>();
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
@@ -68,19 +70,13 @@ public class XmlPayloadMarshallingTransformerTests {
}
public void marshal(Object originalPayload, Result result) throws XmlMappingException, IOException {
payloads.add(originalPayload);
if (result instanceof StringResult) {
((StringResult) result).getWriter().write("hello " + originalPayload);
((StringResult) result).getWriter().write("hello world");
}
}
public Object unmarshal(Source source) throws XmlMappingException, IOException {
if (source instanceof StringSource) {
char[] chars = new char[8];
((StringSource) source).getReader().read(chars);
return "hello " + new String(chars).trim();
}
return null;
}
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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.transformer;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import javax.xml.transform.Source;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.xml.transform.StringSource;
/**
*
* @author Jonas Partner
*
*/
public class XmlPayloadUnmarshallingTransfomerTests {
@Test
public void testStringSourceToString() {
Unmarshaller unmarshaller = new TestUnmarshaller();
XmlPayloadUnmarshallingTransfomer transformer = new XmlPayloadUnmarshallingTransfomer(unmarshaller);
Message<?> message = new GenericMessage<StringSource>(new StringSource("world"));
transformer.transform(message);
assertEquals(String.class, message.getPayload().getClass());
assertEquals("hello world", message.getPayload().toString());
}
private static class TestUnmarshaller implements Unmarshaller {
public Object unmarshal(Source source) throws XmlMappingException, IOException {
if (source instanceof StringSource) {
char[] chars = new char[8];
((StringSource) source).getReader().read(chars);
return "hello " + new String(chars).trim();
}
return null;
}
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
return true;
}
}
}

View File

@@ -38,35 +38,36 @@ import org.xml.sax.InputSource;
/**
*
* @author Jonas Partner
*
*
*/
public class XsltPayloadTransformerTest {
XsltPayloadTransformer transformer;
@Before
public void setUp() throws Exception{
public void setUp() throws Exception {
transformer = new XsltPayloadTransformer(getXslResource());
}
@Test
public void testDocumentAsPayload() 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();
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();
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
public void testSourceAsPayload() throws Exception {
GenericMessage<Source> message = new GenericMessage<Source>(new StringSource(getInputString()));
@@ -74,29 +75,25 @@ public class XsltPayloadTransformerTest {
DOMResult result = new DOMResult();
TransformerFactory.newInstance().newTransformer().transform(message.getPayload(), result);
String rootNodeName = ((Document)result.getNode()).getDocumentElement().getNodeName();
String rootNodeName = ((Document) result.getNode()).getDocumentElement().getNodeName();
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
}
@Test(expected = MessagingException.class)
public void testNonXmlString() throws Exception {
transformer.transform(new StringMessage("test"));
}
@Test(expected = MessagingException.class)
public void testUnsupportedPayloadType() throws Exception {
transformer.transform(new GenericMessage<Long>(new Long(12)));
}
public String getInputString() {
return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
}
public Resource getXslResource() throws Exception{
public 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"));
}