Initial import of OXM module
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
|
||||
public abstract class AbstractMarshallerTestCase extends XMLTestCase {
|
||||
|
||||
protected Marshaller marshaller;
|
||||
|
||||
protected Object flights;
|
||||
|
||||
protected static final String EXPECTED_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
protected final void setUp() throws Exception {
|
||||
marshaller = createMarshaller();
|
||||
flights = createFlights();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
protected abstract Marshaller createMarshaller() throws Exception;
|
||||
|
||||
protected abstract Object createFlights();
|
||||
|
||||
public void testMarshalDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document result = builder.newDocument();
|
||||
DOMResult domResult = new DOMResult(result);
|
||||
marshaller.marshal(flights, domResult);
|
||||
Document expected = builder.newDocument();
|
||||
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
|
||||
namespace.setNodeValue("http://samples.springframework.org/flight");
|
||||
flightsElement.setAttributeNode(namespace);
|
||||
expected.appendChild(flightsElement);
|
||||
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultOutputStream() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(os);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
|
||||
new String(os.toByteArray(), "UTF-8"));
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StaxResult result = new StaxResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StaxResult result = new StaxResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalJaxp14StaxResultStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StAXResult result = new StAXResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalJaxp14StaxResultEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StAXResult result = new StAXResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
|
||||
public abstract class AbstractUnmarshallerTestCase extends TestCase {
|
||||
|
||||
protected Unmarshaller unmarshaller;
|
||||
|
||||
protected static final String INPUT_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
protected final void setUp() throws Exception {
|
||||
unmarshaller = createUnmarshaller();
|
||||
}
|
||||
|
||||
protected abstract Unmarshaller createUnmarshaller() throws Exception;
|
||||
|
||||
protected abstract void testFlights(Object o);
|
||||
|
||||
protected abstract void testFlight(Object o);
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
document.appendChild(flightsElement);
|
||||
Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = document.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
DOMSource source = new DOMSource(document);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalSAXSource() throws Exception {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StAXSource source = new StAXSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
StAXSource source = new StAXSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
|
||||
streamReader.getName());
|
||||
streamReader.nextTag(); // skip to flight
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
|
||||
streamReader.getName());
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.castor;
|
||||
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
public class CastorMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
Flight flight = new Flight();
|
||||
flight.setNumber(42L);
|
||||
Flights flights = new Flights();
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createControl(ContentHandler.class);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("tns", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flights", "tns:flights", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flight", "tns:flight", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "number", "tns:number", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerControl.setMatcher(MockControl.ARRAY_MATCHER);
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "tns:number");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
|
||||
handlerMock.endPrefixMapping("tns");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
public void testSupports() throws Exception {
|
||||
assertTrue("CastorMarshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
assertTrue("CastorMarshaller does not support Flight", marshaller.supports(Flight.class));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.castor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
public class CastorUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlightCount());
|
||||
testFlight(flights.getFlight()[0]);
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
Flight flight = (Flight) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
public void testUnmarshalTargetClass() throws Exception {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testSetBothTargetClassAndMapping() throws IOException {
|
||||
try {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
marshaller.setMappingLocation(new ClassPathResource("mapping.xml", CastorMarshaller.class));
|
||||
marshaller.setTargetClass(getClass());
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.castor;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.exolab.castor.xml.MarshalException;
|
||||
import org.exolab.castor.xml.ValidationException;
|
||||
import org.exolab.castor.xml.XMLException;
|
||||
|
||||
public class CastorUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertMarshalException() {
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new MarshalException(""), true) instanceof CastorMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new MarshalException(""), false) instanceof CastorUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertValidationException() {
|
||||
assertTrue("Invalid exception conversion", CastorUtils
|
||||
.convertXmlException(new ValidationException(""), false) instanceof CastorValidationFailureException);
|
||||
}
|
||||
|
||||
public void testConvertXMLException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
CastorUtils.convertXmlException(new XMLException(""), false) instanceof CastorSystemException);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright ${YEAR} 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.oxm.config;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class Jaxb2OxmNamespaceHandlerTest extends TestCase {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("jaxb2OxmNamespaceHandlerTest.xml", getClass());
|
||||
}
|
||||
|
||||
public void testContextPathMarshaller() throws Exception {
|
||||
applicationContext.getBean("contextPathMarshaller", Jaxb2Marshaller.class);
|
||||
}
|
||||
|
||||
public void testClassesToBeBoundMarshaller() throws Exception {
|
||||
applicationContext.getBean("classesMarshaller", Jaxb2Marshaller.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright ${YEAR} 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.oxm.config;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.oxm.jaxb.Jaxb1Marshaller;
|
||||
import org.springframework.oxm.jibx.JibxMarshaller;
|
||||
import org.springframework.oxm.xmlbeans.XmlBeansMarshaller;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
|
||||
public class OxmNamespaceHandlerTest extends TestCase {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass());
|
||||
}
|
||||
|
||||
public void testJaxb1Marshaller() throws Exception {
|
||||
applicationContext.getBean("jaxb1Marshaller", Jaxb1Marshaller.class);
|
||||
}
|
||||
|
||||
public void testJibxMarshaller() throws Exception {
|
||||
applicationContext.getBean("jibxMarshaller", JibxMarshaller.class);
|
||||
}
|
||||
|
||||
public void testXmlBeansMarshaller() throws Exception {
|
||||
XmlBeansMarshaller marshaller =
|
||||
(XmlBeansMarshaller) applicationContext.getBean("xmlBeansMarshaller", XmlBeansMarshaller.class);
|
||||
XmlOptions options = marshaller.getXmlOptions();
|
||||
assertNotNull("Options not set", options);
|
||||
assertTrue("option not set", options.hasOption("SAVE_PRETTY_PRINT"));
|
||||
assertEquals("option not set", "true", options.get("SAVE_PRETTY_PRINT"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jaxb;
|
||||
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
|
||||
public abstract class AbstractJaxbMarshallerTestCase extends AbstractMarshallerTestCase {
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.setDocumentLocator(null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flights", "flights", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "flight", "flight", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.startElement("http://samples.springframework.org/flight", "number", "number", null);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
||||
handlerMock.endPrefixMapping("");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 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.oxm.jaxb;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.xml.bind.annotation.XmlAttachmentRef;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(namespace = "http://springframework.org/spring-ws")
|
||||
public class BinaryObject {
|
||||
|
||||
@XmlElement(namespace = "http://springframework.org/spring-ws")
|
||||
private byte[] bytes;
|
||||
|
||||
@XmlElement(namespace = "http://springframework.org/spring-ws")
|
||||
private DataHandler dataHandler;
|
||||
|
||||
@XmlElement(namespace = "http://springframework.org/spring-ws")
|
||||
@XmlAttachmentRef
|
||||
private DataHandler swaDataHandler;
|
||||
|
||||
public BinaryObject() {
|
||||
}
|
||||
|
||||
public BinaryObject(byte[] bytes, DataHandler dataHandler) {
|
||||
this.bytes = bytes;
|
||||
this.dataHandler = dataHandler;
|
||||
swaDataHandler = dataHandler;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public DataHandler getDataHandler() {
|
||||
return dataHandler;
|
||||
}
|
||||
|
||||
public DataHandler getSwaDataHandler() {
|
||||
return swaDataHandler;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.jaxb;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb1.FlightType;
|
||||
import org.springframework.oxm.jaxb1.Flights;
|
||||
import org.springframework.oxm.jaxb1.FlightsType;
|
||||
import org.springframework.oxm.jaxb1.impl.FlightTypeImpl;
|
||||
import org.springframework.oxm.jaxb1.impl.FlightsImpl;
|
||||
|
||||
public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
|
||||
|
||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb1";
|
||||
|
||||
protected final Marshaller createMarshaller() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPaths(new String[]{CONTEXT_PATH});
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
FlightType flight = new FlightTypeImpl();
|
||||
flight.setNumber(42L);
|
||||
Flights flights = new FlightsImpl();
|
||||
flights.getFlight().add(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testProperties() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.setMarshallerProperties(
|
||||
Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testNoContextPath() throws Exception {
|
||||
try {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidContextPath() throws Exception {
|
||||
try {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath("ab");
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an XmlMappingException");
|
||||
}
|
||||
catch (XmlMappingException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testSupports() throws Exception {
|
||||
assertTrue("Jaxb1Marshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
assertFalse("Jaxb1Marshaller supports FlightsType", marshaller.supports(FlightsType.class));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.jaxb;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.jaxb1.FlightType;
|
||||
import org.springframework.oxm.jaxb1.Flights;
|
||||
|
||||
public class Jaxb1UnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
|
||||
marshaller.setContextPath("org.springframework.oxm.jaxb1");
|
||||
marshaller.setValidating(true);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
testFlight(flights.getFlight().get(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jaxb;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.datatype.Duration;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.Locator;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb2.FlightType;
|
||||
import org.springframework.oxm.jaxb2.Flights;
|
||||
import org.springframework.oxm.jaxb2.ObjectFactory;
|
||||
import org.springframework.oxm.mime.MimeContainer;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
public class Jaxb2MarshallerTest extends XMLTestCase {
|
||||
|
||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb2";
|
||||
|
||||
private static final String EXPECTED_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
private Jaxb2Marshaller marshaller;
|
||||
|
||||
private Flights flights;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.afterPropertiesSet();
|
||||
FlightType flight = new FlightType();
|
||||
flight.setNumber(42L);
|
||||
flights = new Flights();
|
||||
flights.getFlight().add(flight);
|
||||
}
|
||||
|
||||
public void testMarshalDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
DOMResult domResult = new DOMResult(document);
|
||||
marshaller.marshal(flights, domResult);
|
||||
Document expected = builder.newDocument();
|
||||
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
expected.appendChild(flightsElement);
|
||||
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultOutputStream() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(os);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
|
||||
new String(os.toByteArray(), "UTF-8"));
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StaxResult result = new StaxResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StaxResult result = new StaxResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriterJaxp14() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StAXResult result = new StAXResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLEventWriterJaxp14() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StAXResult result = new StAXResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testProperties() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.setMarshallerProperties(
|
||||
Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testNoContextPathOrClassesToBeBound() throws Exception {
|
||||
try {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidContextPath() throws Exception {
|
||||
try {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath("ab");
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an XmlMappingException");
|
||||
}
|
||||
catch (XmlMappingException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testMarshalInvalidClass() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(new Class[]{FlightType.class});
|
||||
marshaller.afterPropertiesSet();
|
||||
Result result = new StreamResult(new StringWriter());
|
||||
Flights flights = new Flights();
|
||||
try {
|
||||
marshaller.marshal(flights, result);
|
||||
fail("Should have thrown an MarshallingFailureException");
|
||||
}
|
||||
catch (XmlMappingException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
|
||||
handlerMock.setDocumentLocator(isA(Locator.class));
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.characters(isA(char[].class), eq(0), eq(2));
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
||||
handlerMock.endPrefixMapping("");
|
||||
handlerMock.endDocument();
|
||||
replay(handlerMock);
|
||||
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
verify(handlerMock);
|
||||
}
|
||||
|
||||
public void testSupportsContextPath() throws Exception {
|
||||
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
||||
assertTrue("Jaxb2Marshaller does not support Flights",
|
||||
marshaller.supports(createFlights.getGenericReturnType()));
|
||||
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
||||
marshaller.supports(createFlight.getGenericReturnType()));
|
||||
assertFalse("Jaxb2Marshaller supports non-parameterized JAXBElement", marshaller.supports(JAXBElement.class));
|
||||
JAXBElement<Jaxb2MarshallerTest> testElement =
|
||||
new JAXBElement<Jaxb2MarshallerTest>(new QName("something"), Jaxb2MarshallerTest.class, null, this);
|
||||
assertFalse("Jaxb2Marshaller supports wrong JAXBElement", marshaller.supports(testElement.getClass()));
|
||||
}
|
||||
|
||||
public void testSupportsClassesToBeBound() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class});
|
||||
marshaller.afterPropertiesSet();
|
||||
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
||||
assertTrue("Jaxb2Marshaller does not support Flights",
|
||||
marshaller.supports(createFlights.getGenericReturnType()));
|
||||
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
||||
marshaller.supports(createFlight.getGenericReturnType()));
|
||||
assertFalse("Jaxb2Marshaller supports non-parameterized JAXBElement", marshaller.supports(JAXBElement.class));
|
||||
JAXBElement<Jaxb2MarshallerTest> testElement =
|
||||
new JAXBElement<Jaxb2MarshallerTest>(new QName("something"), Jaxb2MarshallerTest.class, null, this);
|
||||
assertFalse("Jaxb2Marshaller supports wrong JAXBElement", marshaller.supports(testElement.getClass()));
|
||||
}
|
||||
|
||||
public void testSupportsPrimitives() throws Exception {
|
||||
Method primitives = getClass().getDeclaredMethod("primitives", JAXBElement.class, JAXBElement.class,
|
||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
||||
JAXBElement.class);
|
||||
Type[] types = primitives.getGenericParameterTypes();
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
ParameterizedType type = (ParameterizedType) types[i];
|
||||
assertTrue("Jaxb2Marshaller does not support " + type, marshaller.supports(types[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSupportsStandards() throws Exception {
|
||||
Method standards = getClass().getDeclaredMethod("standards", JAXBElement.class, JAXBElement.class,
|
||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
||||
JAXBElement.class, JAXBElement.class);
|
||||
Type[] types = standards.getGenericParameterTypes();
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
ParameterizedType type = (ParameterizedType) types[i];
|
||||
assertTrue("Jaxb2Marshaller does not support " + type, marshaller.supports(types[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public void testMarshalAttachments() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(new Class[]{BinaryObject.class});
|
||||
marshaller.setMtomEnabled(true);
|
||||
marshaller.afterPropertiesSet();
|
||||
MimeContainer mimeContainer = createMock(MimeContainer.class);
|
||||
|
||||
Resource logo = new ClassPathResource("spring-ws.png", getClass());
|
||||
DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
|
||||
|
||||
expect(mimeContainer.convertToXopPackage()).andReturn(true);
|
||||
mimeContainer.addAttachment(isA(String.class), isA(DataHandler.class));
|
||||
expectLastCall().times(3);
|
||||
|
||||
replay(mimeContainer);
|
||||
byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
|
||||
BinaryObject object = new BinaryObject(bytes, dataHandler);
|
||||
Result result = new StringResult();
|
||||
marshaller.marshal(object, result, mimeContainer);
|
||||
verify(mimeContainer);
|
||||
assertTrue("No XML written", result.toString().length() > 0);
|
||||
}
|
||||
|
||||
private void primitives(JAXBElement<Boolean> bool,
|
||||
JAXBElement<Byte> aByte,
|
||||
JAXBElement<Short> aShort,
|
||||
JAXBElement<Integer> anInteger,
|
||||
JAXBElement<Long> aLong,
|
||||
JAXBElement<Float> aFloat,
|
||||
JAXBElement<Double> aDouble,
|
||||
JAXBElement<byte[]> byteArray) {
|
||||
}
|
||||
|
||||
private void standards(JAXBElement<String> string,
|
||||
JAXBElement<BigInteger> integer,
|
||||
JAXBElement<BigDecimal> decimal,
|
||||
JAXBElement<Calendar> calendar,
|
||||
JAXBElement<Date> date,
|
||||
JAXBElement<QName> qName,
|
||||
JAXBElement<URI> uri,
|
||||
JAXBElement<XMLGregorianCalendar> xmlGregorianCalendar,
|
||||
JAXBElement<Duration> duration,
|
||||
JAXBElement<Object> object,
|
||||
JAXBElement<Image> image,
|
||||
JAXBElement<DataHandler> dataHandler,
|
||||
JAXBElement<Source> source,
|
||||
JAXBElement<UUID> uuid) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jaxb;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.jaxb2.FlightType;
|
||||
import org.springframework.oxm.jaxb2.Flights;
|
||||
import org.springframework.oxm.mime.MimeContainer;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class Jaxb2UnmarshallerTest extends TestCase {
|
||||
|
||||
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
private Jaxb2Marshaller unmarshaller;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setContextPath("org.springframework.oxm.jaxb2");
|
||||
unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
|
||||
unmarshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
||||
document.appendChild(flightsElement);
|
||||
Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
||||
flightsElement.appendChild(flightElement);
|
||||
Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = document.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
DOMSource source = new DOMSource(document);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalSAXSource() throws Exception {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReaderJaxp14() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StAXSource source = new StAXSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlEventReaderJaxp14() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
StAXSource source = new StAXSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testMarshalAttachments() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class});
|
||||
unmarshaller.setMtomEnabled(true);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
MimeContainer mimeContainer = createMock(MimeContainer.class);
|
||||
|
||||
Resource logo = new ClassPathResource("spring-ws.png", getClass());
|
||||
DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
|
||||
|
||||
expect(mimeContainer.isXopPackage()).andReturn(true);
|
||||
expect(mimeContainer.getAttachment(
|
||||
"<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>"))
|
||||
.andReturn(dataHandler);
|
||||
expect(mimeContainer.getAttachment(
|
||||
"<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>"))
|
||||
.andReturn(dataHandler);
|
||||
expect(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png"))
|
||||
.andReturn(dataHandler);
|
||||
replay(mimeContainer);
|
||||
String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
|
||||
"<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
|
||||
"</bytes>" + "<dataHandler>" +
|
||||
"<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
|
||||
"</dataHandler>" +
|
||||
"<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" +
|
||||
"</binaryObject>";
|
||||
|
||||
Source source = new StringSource(content);
|
||||
Object result = unmarshaller.unmarshal(source, mimeContainer);
|
||||
assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
|
||||
verify(mimeContainer);
|
||||
BinaryObject object = (BinaryObject) result;
|
||||
assertNotNull("bytes property not set", object.getBytes());
|
||||
assertTrue("bytes property not set", object.getBytes().length > 0);
|
||||
assertNotNull("datahandler property not set", object.getSwaDataHandler());
|
||||
}
|
||||
|
||||
private void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
testFlight(flights.getFlight().get(0));
|
||||
}
|
||||
|
||||
private void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
streamReader.nextTag(); // skip to flight
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source);
|
||||
FlightType flight = element.getValue();
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jaxb;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.MarshalException;
|
||||
import javax.xml.bind.UnmarshalException;
|
||||
import javax.xml.bind.ValidationException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class JaxbUtilsTest extends TestCase {
|
||||
|
||||
public void testGetJaxbVersion() throws Exception {
|
||||
assertEquals("Invalid JAXB version", JaxbUtils.JAXB_2, JaxbUtils.getJaxbVersion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jibx;
|
||||
|
||||
public class FlightType {
|
||||
|
||||
protected long number;
|
||||
|
||||
public long getNumber() {
|
||||
return this.number;
|
||||
}
|
||||
|
||||
public void setNumber(long number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jibx;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Flights {
|
||||
|
||||
protected ArrayList flightList = new ArrayList();
|
||||
|
||||
public void addFlight(FlightType flight) {
|
||||
flightList.add(flight);
|
||||
}
|
||||
|
||||
public FlightType getFlight(int index) {
|
||||
return (FlightType) flightList.get(index);
|
||||
}
|
||||
|
||||
public int sizeFlightList() {
|
||||
return flightList.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jibx;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
public class JibxMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.setTargetClass(Flights.class);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
Flights flights = new Flights();
|
||||
FlightType flight = new FlightType();
|
||||
flight.setNumber(42L);
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void testAfterPropertiesSetNoContextPath() throws Exception {
|
||||
try {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testIndentation() throws Exception {
|
||||
((JibxMarshaller) marshaller).setIndent(4);
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flights, result);
|
||||
XMLUnit.setIgnoreWhitespace(false);
|
||||
String expected = "<?xml version=\"1.0\"?>\n" +
|
||||
"<flights xmlns=\"http://samples.springframework.org/flight\">\n" + " <flight>\n" +
|
||||
" <number>42</number>\n" + " </flight>\n" + "</flights>";
|
||||
assertXMLEqual(expected, result.toString());
|
||||
}
|
||||
|
||||
public void testEncodingAndStandalone() throws Exception {
|
||||
((JibxMarshaller) marshaller).setEncoding("ISO-8859-1");
|
||||
((JibxMarshaller) marshaller).setStandalone(Boolean.TRUE);
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flights, result);
|
||||
assertTrue("Encoding and standalone not set",
|
||||
result.toString().startsWith("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>"));
|
||||
}
|
||||
|
||||
public void testSupports() throws Exception {
|
||||
assertTrue("JibxMarshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
assertTrue("JibxMarshaller does not support FlightType", marshaller.supports(FlightType.class));
|
||||
assertFalse("JibxMarshaller supports illegal type", marshaller.supports(getClass()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.jibx;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
public class JibxUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
JibxMarshaller unmarshaller = new JibxMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeFlightList());
|
||||
testFlight(flights.getFlight(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
// JiBX does not support reading XML fragments, hence the override here
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.jibx;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.jibx.runtime.ValidationException;
|
||||
|
||||
public class JibxUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertMarshallingException() throws Exception {
|
||||
assertTrue("Invalid exception conversion",
|
||||
JibxUtils.convertJibxException(new JiBXException(""), true) instanceof JibxMarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertUnmarshallingException() throws Exception {
|
||||
assertTrue("Invalid exception conversion", JibxUtils
|
||||
.convertJibxException(new JiBXException(""), false) instanceof JibxUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertValidationException() throws Exception {
|
||||
assertTrue("Invalid exception conversion", JibxUtils
|
||||
.convertJibxException(new ValidationException(""), true) instanceof JibxValidationFailureException);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 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.oxm.support;
|
||||
|
||||
import javax.jms.BytesMessage;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class MarshallingMessageConverterTest extends TestCase {
|
||||
|
||||
private MarshallingMessageConverter converter;
|
||||
|
||||
private MockControl marshallerControl;
|
||||
|
||||
private Marshaller marshallerMock;
|
||||
|
||||
private MockControl unmarshallerControl;
|
||||
|
||||
private Unmarshaller unmarshallerMock;
|
||||
|
||||
private MockControl sessionControl;
|
||||
|
||||
private Session sessionMock;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
marshallerControl = MockControl.createControl(Marshaller.class);
|
||||
marshallerMock = (Marshaller) marshallerControl.getMock();
|
||||
unmarshallerControl = MockControl.createControl(Unmarshaller.class);
|
||||
unmarshallerMock = (Unmarshaller) unmarshallerControl.getMock();
|
||||
converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
|
||||
sessionControl = MockControl.createControl(Session.class);
|
||||
sessionMock = (Session) sessionControl.getMock();
|
||||
|
||||
}
|
||||
|
||||
public void testToBytesMessage() throws Exception {
|
||||
MockControl bytesMessageControl = MockControl.createControl(BytesMessage.class);
|
||||
BytesMessage bytesMessageMock = (BytesMessage) bytesMessageControl.getMock();
|
||||
Object toBeMarshalled = new Object();
|
||||
|
||||
sessionControl.expectAndReturn(sessionMock.createBytesMessage(), bytesMessageMock);
|
||||
marshallerMock.marshal(toBeMarshalled, new StringResult());
|
||||
marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
bytesMessageMock.writeBytes(new byte[0]);
|
||||
bytesMessageControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
|
||||
marshallerControl.replay();
|
||||
unmarshallerControl.replay();
|
||||
sessionControl.replay();
|
||||
bytesMessageControl.replay();
|
||||
|
||||
converter.toMessage(toBeMarshalled, sessionMock);
|
||||
|
||||
marshallerControl.verify();
|
||||
unmarshallerControl.verify();
|
||||
sessionControl.verify();
|
||||
bytesMessageControl.verify();
|
||||
}
|
||||
|
||||
public void testFromBytesMessage() throws Exception {
|
||||
MockControl bytesMessageControl = MockControl.createControl(BytesMessage.class);
|
||||
BytesMessage bytesMessageMock = (BytesMessage) bytesMessageControl.getMock();
|
||||
Object unmarshalled = new Object();
|
||||
|
||||
bytesMessageControl.expectAndReturn(bytesMessageMock.getBodyLength(), 10);
|
||||
bytesMessageMock.readBytes(new byte[0]);
|
||||
bytesMessageControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
bytesMessageControl.setReturnValue(0);
|
||||
unmarshallerMock.unmarshal(new StringSource(""));
|
||||
unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
unmarshallerControl.setReturnValue(unmarshalled);
|
||||
|
||||
marshallerControl.replay();
|
||||
unmarshallerControl.replay();
|
||||
sessionControl.replay();
|
||||
bytesMessageControl.replay();
|
||||
|
||||
Object result = converter.fromMessage(bytesMessageMock);
|
||||
assertEquals("Invalid result", result, unmarshalled);
|
||||
|
||||
marshallerControl.verify();
|
||||
unmarshallerControl.verify();
|
||||
sessionControl.verify();
|
||||
bytesMessageControl.verify();
|
||||
}
|
||||
|
||||
public void testToTextMessage() throws Exception {
|
||||
converter.setMarshalTo(MarshallingMessageConverter.MARSHAL_TO_TEXT_MESSAGE);
|
||||
MockControl textMessageControl = MockControl.createControl(TextMessage.class);
|
||||
TextMessage textMessageMock = (TextMessage) textMessageControl.getMock();
|
||||
Object toBeMarshalled = new Object();
|
||||
|
||||
sessionControl.expectAndReturn(sessionMock.createTextMessage(""), textMessageMock);
|
||||
marshallerMock.marshal(toBeMarshalled, new StringResult());
|
||||
marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
|
||||
marshallerControl.replay();
|
||||
unmarshallerControl.replay();
|
||||
sessionControl.replay();
|
||||
textMessageControl.replay();
|
||||
|
||||
converter.toMessage(toBeMarshalled, sessionMock);
|
||||
|
||||
marshallerControl.verify();
|
||||
unmarshallerControl.verify();
|
||||
sessionControl.verify();
|
||||
textMessageControl.verify();
|
||||
}
|
||||
|
||||
public void testFromTextMessage() throws Exception {
|
||||
MockControl textMessageControl = MockControl.createControl(TextMessage.class);
|
||||
TextMessage textMessageMock = (TextMessage) textMessageControl.getMock();
|
||||
Object unmarshalled = new Object();
|
||||
|
||||
unmarshallerMock.unmarshal(new StringSource(""));
|
||||
unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
unmarshallerControl.setReturnValue(unmarshalled);
|
||||
textMessageControl.expectAndReturn(textMessageMock.getText(), "");
|
||||
|
||||
marshallerControl.replay();
|
||||
unmarshallerControl.replay();
|
||||
sessionControl.replay();
|
||||
textMessageControl.replay();
|
||||
|
||||
Object result = converter.fromMessage(textMessageMock);
|
||||
assertEquals("Invalid result", result, unmarshalled);
|
||||
|
||||
marshallerControl.verify();
|
||||
unmarshallerControl.verify();
|
||||
sessionControl.verify();
|
||||
textMessageControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 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.oxm.support;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
public class MarshallingViewTest extends TestCase {
|
||||
|
||||
private MarshallingView view;
|
||||
|
||||
private MockControl control;
|
||||
|
||||
private Marshaller marshallerMock;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
control = MockControl.createControl(Marshaller.class);
|
||||
marshallerMock = (Marshaller) control.getMock();
|
||||
view = new MarshallingView(marshallerMock);
|
||||
}
|
||||
|
||||
public void testGetContentType() {
|
||||
Assert.assertEquals("Invalid content type", "application/xml", view.getContentType());
|
||||
}
|
||||
|
||||
public void testRenderModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey(modelKey);
|
||||
Map model = new HashMap();
|
||||
model.put(modelKey, toBeMarshalled);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
control.expectAndReturn(marshallerMock.supports(Object.class), true);
|
||||
marshallerMock.marshal(toBeMarshalled, new StreamResult(response.getOutputStream()));
|
||||
control.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
|
||||
control.replay();
|
||||
view.render(model, request, response);
|
||||
Assert.assertEquals("Invalid content type", "application/xml", response.getContentType());
|
||||
Assert.assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
control.verify();
|
||||
}
|
||||
|
||||
public void testRenderModelKeyUnsupported() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey(modelKey);
|
||||
Map model = new HashMap();
|
||||
model.put(modelKey, toBeMarshalled);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
control.expectAndReturn(marshallerMock.supports(Object.class), false);
|
||||
|
||||
control.replay();
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
control.verify();
|
||||
}
|
||||
|
||||
public void testRenderNoModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
Map model = new HashMap();
|
||||
model.put(modelKey, toBeMarshalled);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
control.expectAndReturn(marshallerMock.supports(Object.class), true);
|
||||
marshallerMock.marshal(toBeMarshalled, new StreamResult(response.getOutputStream()));
|
||||
control.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
|
||||
control.replay();
|
||||
view.render(model, request, response);
|
||||
Assert.assertEquals("Invalid content type", "application/xml", response.getContentType());
|
||||
Assert.assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
control.verify();
|
||||
}
|
||||
|
||||
public void testRenderUnsupportedModel() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
Map model = new HashMap();
|
||||
model.put(modelKey, toBeMarshalled);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
control.expectAndReturn(marshallerMock.supports(Object.class), false);
|
||||
|
||||
control.replay();
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
control.verify();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.xmlbeans;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.samples.flight.FlightsDocument.Flights;
|
||||
|
||||
public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase {
|
||||
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
public void testMarshalNonXmlObject() throws Exception {
|
||||
try {
|
||||
marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream()));
|
||||
fail("XmlBeansMarshaller did not throw ClassCastException for non-XmlObject");
|
||||
}
|
||||
catch (ClassCastException e) {
|
||||
// Expected behavior
|
||||
}
|
||||
}
|
||||
|
||||
protected Object createFlights() {
|
||||
FlightsDocument flightsDocument = FlightsDocument.Factory.newInstance();
|
||||
Flights flights = flightsDocument.addNewFlights();
|
||||
FlightType flightType = flights.addNewFlight();
|
||||
flightType.setNumber(42L);
|
||||
return flightsDocument;
|
||||
}
|
||||
|
||||
public void testSupports() throws Exception {
|
||||
assertTrue("XmlBeansMarshaller does not support XmlObject", marshaller.supports(XmlObject.class));
|
||||
assertFalse("XmlBeansMarshaller supports other objects", marshaller.supports(Object.class));
|
||||
assertTrue("XmlBeansMarshaller does not support FlightsDocument", marshaller.supports(FlightsDocument.class));
|
||||
assertTrue("XmlBeansMarshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
assertTrue("XmlBeansMarshaller does not support FlightType", marshaller.supports(FlightType.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.xmlbeans;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.samples.flight.FlightDocument;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.samples.flight.FlightsDocument.Flights;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
protected void testFlights(Object o) {
|
||||
FlightsDocument flightsDocument = (FlightsDocument) o;
|
||||
assertNotNull("FlightsDocument is null", flightsDocument);
|
||||
Flights flights = flightsDocument.getFlights();
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeOfFlightArray());
|
||||
testFlight(flights.getFlightArray(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = null;
|
||||
if (o instanceof FlightType) {
|
||||
flight = (FlightType) o;
|
||||
}
|
||||
else if (o instanceof FlightDocument) {
|
||||
FlightDocument flightDocument = (FlightDocument) o;
|
||||
flight = flightDocument.getFlight();
|
||||
}
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
|
||||
streamReader.getName());
|
||||
streamReader.nextTag(); // skip to flight
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
|
||||
streamReader.getName());
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
public void testValidate() throws Exception {
|
||||
((XmlBeansMarshaller) unmarshaller).setValidating(true);
|
||||
|
||||
try {
|
||||
String invalidInput = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>abc</tns:number></tns:flight></tns:flights>";
|
||||
unmarshaller.unmarshal(new StringSource(invalidInput));
|
||||
fail("Expected a XmlBeansValidationFailureException");
|
||||
}
|
||||
catch (XmlBeansValidationFailureException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2005 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.oxm.xmlbeans;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.xmlbeans.XMLStreamValidationException;
|
||||
import org.apache.xmlbeans.XmlError;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class XmlBeansUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertXMLStreamValidationException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(
|
||||
new XMLStreamValidationException(XmlError.forMessage("")),
|
||||
true) instanceof XmlBeansValidationFailureException);
|
||||
|
||||
}
|
||||
|
||||
public void testConvertXmlException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils
|
||||
.convertXmlBeansException(new XmlException(""), true) instanceof XmlBeansMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(new XmlException(""),
|
||||
false) instanceof XmlBeansUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertSAXException() {
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils
|
||||
.convertXmlBeansException(new SAXException(""), true) instanceof XmlBeansMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XmlBeansUtils.convertXmlBeansException(new SAXException(""),
|
||||
false) instanceof XmlBeansUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testFallbackException() {
|
||||
assertTrue("Invalid exception conversion",
|
||||
XmlBeansUtils.convertXmlBeansException(new Exception(""), false) instanceof XmlBeansSystemException);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.xmlbeans;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
|
||||
public class XmlOptionsFactoryBeanTest extends TestCase {
|
||||
|
||||
private XmlOptionsFactoryBean factoryBean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
factoryBean = new XmlOptionsFactoryBean();
|
||||
}
|
||||
|
||||
public void testXmlOptionsFactoryBean() throws Exception {
|
||||
factoryBean.setOptions(Collections.singletonMap(XmlOptions.SAVE_PRETTY_PRINT, Boolean.TRUE));
|
||||
factoryBean.afterPropertiesSet();
|
||||
XmlOptions xmlOptions = (XmlOptions) factoryBean.getObject();
|
||||
assertNotNull("No XmlOptions returned", xmlOptions);
|
||||
assertTrue("Option not set", xmlOptions.hasOption(XmlOptions.SAVE_PRETTY_PRINT));
|
||||
assertFalse("Invalid option set", xmlOptions.hasOption(XmlOptions.LOAD_LINE_NUMBERS));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 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.oxm.xstream;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
|
||||
public class AnnotationXStreamMarshallerTest extends XMLTestCase {
|
||||
|
||||
private AnnotationXStreamMarshaller marshaller;
|
||||
|
||||
private static final String EXPECTED_STRING = "<flight><number>42</number></flight>";
|
||||
|
||||
private Flight flight;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
marshaller = new AnnotationXStreamMarshaller();
|
||||
marshaller.setAnnotatedClass(Flight.class);
|
||||
flight = new Flight();
|
||||
flight.setFlightNumber(42L);
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 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.oxm.xstream;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("flight")
|
||||
public class Flight {
|
||||
|
||||
@XStreamAlias("number")
|
||||
private long flightNumber;
|
||||
|
||||
public long getFlightNumber() {
|
||||
return flightNumber;
|
||||
}
|
||||
|
||||
public void setFlightNumber(long number) {
|
||||
this.flightNumber = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.xstream;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import com.thoughtworks.xstream.converters.Converter;
|
||||
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
|
||||
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.xml.transform.StaxResult;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class XStreamMarshallerTest extends XMLTestCase {
|
||||
|
||||
private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller marshaller;
|
||||
|
||||
private Flight flight;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
marshaller = new XStreamMarshaller();
|
||||
Properties aliases = new Properties();
|
||||
aliases.setProperty("flight", Flight.class.getName());
|
||||
marshaller.setAliases(aliases);
|
||||
flight = new Flight();
|
||||
flight.setFlightNumber(42L);
|
||||
}
|
||||
|
||||
public void testMarshalDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
DOMResult domResult = new DOMResult(document);
|
||||
marshaller.marshal(flight, domResult);
|
||||
Document expected = builder.newDocument();
|
||||
Element flightElement = expected.createElement("flight");
|
||||
expected.appendChild(flightElement);
|
||||
Element numberElement = expected.createElement("flightNumber");
|
||||
flightElement.appendChild(numberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
numberElement.appendChild(text);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
|
||||
}
|
||||
|
||||
// see SWS-392
|
||||
public void testMarshalDOMResultToExistentDocument() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document existent = builder.newDocument();
|
||||
Element rootElement = existent.createElement("root");
|
||||
Element flightsElement = existent.createElement("flights");
|
||||
rootElement.appendChild(flightsElement);
|
||||
existent.appendChild(rootElement);
|
||||
|
||||
// marshall into the existent document
|
||||
DOMResult domResult = new DOMResult(flightsElement);
|
||||
marshaller.marshal(flight, domResult);
|
||||
|
||||
Document expected = builder.newDocument();
|
||||
Element eRootElement = expected.createElement("root");
|
||||
Element eFlightsElement = expected.createElement("flights");
|
||||
Element eFlightElement = expected.createElement("flight");
|
||||
eRootElement.appendChild(eFlightsElement);
|
||||
eFlightsElement.appendChild(eFlightElement);
|
||||
expected.appendChild(eRootElement);
|
||||
Element eNumberElement = expected.createElement("flightNumber");
|
||||
eFlightElement.appendChild(eNumberElement);
|
||||
Text text = expected.createTextNode("42");
|
||||
eNumberElement.appendChild(text);
|
||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, existent);
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStreamResultOutputStream() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
StreamResult result = new StreamResult(os);
|
||||
marshaller.marshal(flight, result);
|
||||
String s = new String(os.toByteArray(), "UTF-8");
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, s);
|
||||
}
|
||||
|
||||
public void testMarshalSaxResult() throws Exception {
|
||||
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
handlerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
|
||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startElement("", "flight", "flight", null);
|
||||
handlerMock.startElement("", "number", "number", null);
|
||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
||||
handlerMock.endElement("", "number", "number");
|
||||
handlerMock.endElement("", "flight", "flight");
|
||||
handlerMock.endDocument();
|
||||
|
||||
handlerControl.replay();
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flight, result);
|
||||
handlerControl.verify();
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
StaxResult result = new StaxResult(streamWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testMarshalStaxResultXMLEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
StaxResult result = new StaxResult(eventWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
public void testConverters() throws Exception {
|
||||
marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()});
|
||||
byte[] buf = new byte[]{0x1, 0x2};
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(buf, result);
|
||||
assertXMLEqual("<byte-array>AQI=</byte-array>", result.toString());
|
||||
StringSource source = new StringSource(result.toString());
|
||||
byte[] bufResult = (byte[]) marshaller.unmarshal(source);
|
||||
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
|
||||
}
|
||||
|
||||
public void testUseAttributesFor() throws Exception {
|
||||
marshaller.setUseAttributeForTypes(new Class[]{Long.TYPE});
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
|
||||
}
|
||||
|
||||
public void testUseAttributesForStringClassMap() throws Exception {
|
||||
marshaller.setUseAttributeFor(Collections.singletonMap("flightNumber", Long.TYPE));
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
|
||||
}
|
||||
|
||||
public void testUseAttributesForClassStringMap() throws Exception {
|
||||
marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, "flightNumber"));
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, result.toString());
|
||||
}
|
||||
|
||||
public void testOmitField() throws Exception {
|
||||
marshaller.addOmittedField(Flight.class, "flightNumber");
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
assertXpathNotExists("/flight/flightNumber", result.toString());
|
||||
}
|
||||
|
||||
public void testOmitFields() throws Exception {
|
||||
Map omittedFieldsMap = Collections.singletonMap(Flight.class, "flightNumber");
|
||||
marshaller.setOmittedFields(omittedFieldsMap);
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
assertXpathNotExists("/flight/flightNumber", result.toString());
|
||||
}
|
||||
|
||||
public void testDriver() throws Exception {
|
||||
marshaller.setStreamDriver(new JettisonMappedXmlDriver());
|
||||
StringResult result = new StringResult();
|
||||
marshaller.marshal(flight, result);
|
||||
assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":\"42\"}}", result.toString());
|
||||
Object o = marshaller.unmarshal(new StringSource(result.toString()));
|
||||
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
|
||||
Flight unflight = (Flight) o;
|
||||
assertNotNull("Flight is null", unflight);
|
||||
assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.xstream;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
|
||||
public class XStreamUnmarshallerTest extends TestCase {
|
||||
|
||||
protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller unmarshaller;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
unmarshaller = new XStreamMarshaller();
|
||||
Properties aliases = new Properties();
|
||||
aliases.setProperty("flight", Flight.class.getName());
|
||||
unmarshaller.setAliases(aliases);
|
||||
}
|
||||
|
||||
private void testFlight(Object o) {
|
||||
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
|
||||
Flight flight = (Flight) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getFlightNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
|
||||
DOMSource source = new DOMSource(document);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
public void testUnmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2006 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.oxm.xstream;
|
||||
|
||||
import com.thoughtworks.xstream.io.StreamException;
|
||||
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class XStreamUtilsTest extends TestCase {
|
||||
|
||||
public void testConvertStreamException() {
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new StreamException(new Exception()), true) instanceof XStreamMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new StreamException(new Exception()), false) instanceof XStreamUnmarshallingFailureException);
|
||||
}
|
||||
|
||||
public void testConvertCannotResolveClassException() {
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new CannotResolveClassException(""), true) instanceof XStreamMarshallingFailureException);
|
||||
assertTrue("Invalid exception conversion", XStreamUtils.convertXStreamException(
|
||||
new CannotResolveClassException(""), false) instanceof XStreamUnmarshallingFailureException);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
log4j.logger.org.springframework.oxm=DEBUG
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd">
|
||||
<mapping>
|
||||
<description>Castor generated mapping file</description>
|
||||
<class name="org.springframework.oxm.castor.Flights">
|
||||
<description>
|
||||
Default mapping for class
|
||||
org.springframework.oxm.castor.Flights
|
||||
</description>
|
||||
<map-to xml="flights"
|
||||
ns-uri="http://samples.springframework.org/flight" ns-prefix="tns"/>
|
||||
<field name="flight"
|
||||
type="org.springframework.oxm.castor.Flight"
|
||||
required="true" collection="array">
|
||||
<bind-xml name="tns:flight" node="element" QName-prefix="tns"
|
||||
xmlns:tns="http://samples.springframework.org/flight"/>
|
||||
</field>
|
||||
</class>
|
||||
<class name="org.springframework.oxm.castor.Flight">
|
||||
<description>
|
||||
Default mapping for class
|
||||
org.springframework.oxm.castor.Flight
|
||||
</description>
|
||||
<map-to xml="flight"
|
||||
ns-uri="http://samples.springframework.org/flight" ns-prefix="tns"/>
|
||||
<field name="number" type="long" required="true">
|
||||
<bind-xml name="tns:number" node="element"
|
||||
xmlns:tns="http://samples.springframework.org/flight"/>
|
||||
</field>
|
||||
</class>
|
||||
</mapping>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
|
||||
<oxm:jaxb2-marshaller id="contextPathMarshaller" contextPath="org.springframework.oxm.jaxb2"/>
|
||||
<oxm:jaxb2-marshaller id="classesMarshaller">
|
||||
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb2.Flights"/>
|
||||
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb2.FlightType"/>
|
||||
</oxm:jaxb2-marshaller>
|
||||
</beans>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
|
||||
<oxm:jaxb1-marshaller id="jaxb1Marshaller" contextPath="org.springframework.oxm.jaxb1"/>
|
||||
<oxm:jibx-marshaller id="jibxMarshaller" target-class="org.springframework.oxm.jibx.Flights"/>
|
||||
<oxm:xmlbeans-marshaller id="xmlBeansMarshaller" options="xmlBeansOptions"/>
|
||||
|
||||
<bean id="xmlBeansOptions" class="org.springframework.oxm.xmlbeans.XmlOptionsFactoryBean">
|
||||
<property name="options">
|
||||
<props>
|
||||
<prop key="SAVE_PRETTY_PRINT">true</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
|
||||
targetNamespace="http://samples.springframework.org/flight"
|
||||
xmlns:tns="http://samples.springframework.org/flight">
|
||||
<element name="flights">
|
||||
<complexType>
|
||||
<sequence>
|
||||
<element name="flight" type="tns:flightType"
|
||||
maxOccurs="unbounded">
|
||||
</element>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</element>
|
||||
<element name="flight" type="tns:flightType"/>
|
||||
<complexType name="flightType">
|
||||
<sequence>
|
||||
<element name="number" type="long"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</schema>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding>
|
||||
<mapping name="flights" class="org.springframework.oxm.jibx.Flights">
|
||||
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
|
||||
<collection field="flightList">
|
||||
<structure map-as="org.springframework.oxm.jibx.FlightType"/>
|
||||
</collection>
|
||||
</mapping>
|
||||
<mapping name="flight" class="org.springframework.oxm.jibx.FlightType">
|
||||
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
|
||||
<value name="number" field="number" usage="required"/>
|
||||
</mapping>
|
||||
</binding>
|
||||
Reference in New Issue
Block a user