modified unmarshalling transformer to support payloads consistently with xslt transformer INT-311

This commit is contained in:
Jonas Partner
2008-08-19 12:33:46 +00:00
parent 94746fb6a4
commit 6c76f5b3bd
16 changed files with 241 additions and 70 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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 javax.xml.transform.Result;
import org.springframework.integration.xml.transformer.ResultTransformer;
public class StubResultTransformer implements ResultTransformer {
Object toReturn;
public StubResultTransformer(Object toReturn){
this.toReturn = toReturn;
}
public Object transformResult(Result res) {
return toReturn;
}
}

View File

@@ -19,7 +19,18 @@
result-factory="DOMResult" />
<si-xml:marshalling-transformer
id="marshallingTransfomerWithResultTransformer"
marshaller="marshaller" result-transformer="resultTransformer" />
<bean id="marshaller"
class="org.springframework.integration.xml.config.StubMarshaller" />
<bean id="resultTransformer"
class="org.springframework.integration.xml.config.StubResultTransformer">
<constructor-arg value="testReturn" />
</bean>
</beans>

View File

@@ -56,6 +56,16 @@ public class XmlMarshallingTransformerParserTests {
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
}
@Test
public void testDefaultWithResultTransformer() throws Exception {
MessageHandler transformer = (MessageHandler) appContext.getBean("marshallingTransfomerWithResultTransformer");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
Message<?> result = transformer.handle(message);
assertTrue("Wrong payload type", result.getPayload() instanceof String);
String resultPayload = (String)result.getPayload();
assertEquals("Wrong palyoad", "testReturn", resultPayload);
}
@Test
public void testDOMResult() throws Exception {

View File

@@ -68,7 +68,7 @@ public class XmlUnmarshallingTransformerParserTests {
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>");
Message<?> result = transformer.handle(message);
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
}
@Test

View File

@@ -12,7 +12,18 @@
<si-xml:xslt-transformer id="xsltTransformerWithTemplates"
xsl-templates="templates" />
<si-xml:xslt-transformer id="xsltTransformerWithTemplatesAndResultTransformer"
xsl-templates="templates" result-transformer="resultTransformer" />
<bean id="templates"
class="org.springframework.integration.xml.config.TestTemplatesFactory" />
<bean id="resultTransformer"
class="org.springframework.integration.xml.config.StubResultTransformer">
<constructor-arg value="testReturn" />
</bean>
</beans>

View File

@@ -70,5 +70,17 @@ public class XsltPayloadTransformerParserTests {
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
assertEquals("Wrong payload", "test", doc.getDocumentElement().getTextContent());
}
@Test
public void testWithTemplatesAndResultTransformer() throws Exception {
MessageHandler messageTransformer = (MessageHandler) applicationContext
.getBean("xsltTransformerWithTemplatesAndResultTransformer");
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
Message<?> result = messageTransformer.handle(message);
assertEquals("Wrong payload type", String.class, result.getPayload().getClass());
String strResult = (String)result.getPayload();
assertEquals("Wrong payload", "testReturn", strResult);
}
}

View File

@@ -97,8 +97,7 @@ public class XsltPayloadTransformerTest {
@Test
public void testSourceWithResultTransformer() throws Exception {
Integer returnValue = new Integer(13);
transformer
.setResultTransformer(new StubResultTransformer(returnValue));
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue));
Object transformed = transformer
.transform(new StringSource(docAsString));
assertEquals("Wrong value from result conversion", returnValue,