JAXB integration test for OXM based marshallig and unmrshalling transformers

This commit is contained in:
Jonas Partner
2008-10-22 07:38:40 +00:00
parent 4daddefaa4
commit c08cb7a437
6 changed files with 177 additions and 1 deletions

View File

@@ -48,7 +48,8 @@
input-channel="marshallingTransformerWithResultTransformer"
output-channel="output"
marshaller="marshaller"
result-transformer="resultTransformer" />
result-transformer="resultTransformer">
</si-xml:marshalling-transformer>
<bean id="marshaller" class="org.springframework.integration.xml.config.StubMarshaller" />

View File

@@ -0,0 +1,39 @@
/*
* 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.jaxbmarshaling;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType @XmlRootElement(name="person")
public class JaxbAnnotatedPerson {
@XmlElement(name="firstname")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
private String firstName;
}

View File

@@ -0,0 +1,50 @@
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<si:message-bus />
<si:channel id="marshallIn"/>
<si:channel id="unmarshallIn"/>
<si:channel id="unmarshallOut">
<si:queue capacity="10" />
</si:channel>
<si:channel id="marshallOut">
<si:queue capacity="10" />
</si:channel>
<si-xml:marshalling-transformer id="marshaller" input-channel="marshallIn" output-channel="marshallOut" marshaller="marshallerUnmarshaller" />
<si-xml:unmarshalling-transformer id="unmarshaller" input-channel="unmarshallIn" output-channel="unmarshallOut" unmarshaller="marshallerUnmarshaller" />
<bean id="marshallerUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>org.springframework.integration.xml.transformer.jaxbmarshaling.JaxbAnnotatedPerson</value>
</list>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,80 @@
/*
* 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.jaxbmarshaling;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
@ContextConfiguration
public class JaxbMarshallingIntegrationTests extends AbstractJUnit4SpringContextTests {
@Autowired @Qualifier("marshallIn")
MessageChannel marshallIn;
@Autowired @Qualifier("marshallOut")
PollableChannel marshalledOut;
@Autowired @Qualifier("unmarshallIn")
MessageChannel unmarshallIn;
@Autowired @Qualifier("unmarshallOut")
PollableChannel unmarshallOut;
@Test
public void testMarshalling() throws Exception{
JaxbAnnotatedPerson person = new JaxbAnnotatedPerson();
person.setFirstName("john");
marshallIn.send(new GenericMessage<Object>(person));
GenericMessage<Result> res = (GenericMessage<Result>) marshalledOut.receive(2000);
assertNotNull("No response recevied" ,res);
assertTrue("payload was not a DOMResult" , res.getPayload() instanceof DOMResult);
Document doc = (Document)((DOMResult)res.getPayload()).getNode();
assertEquals("Wrong name for root element ", "person",doc.getDocumentElement().getLocalName());
}
@Test
public void testUnmarshalling() throws Exception{
StringSource source = new StringSource("<person><firstname>bob</firstname></person>");
unmarshallIn.send(new GenericMessage<Source>(source));
GenericMessage<Object> res = (GenericMessage<Object>) unmarshallOut.receive(2000);
assertNotNull("No response", res);
assertTrue("Not a Person ", res.getPayload() instanceof JaxbAnnotatedPerson);
JaxbAnnotatedPerson person = (JaxbAnnotatedPerson)res.getPayload();
assertEquals("Worng firstname", "bob", person.getFirstName());
}
}