Rename modules {org.springframework.*=>spring-*}
This renaming more intuitively expresses the relationship between
subprojects and the JAR artifacts they produce.
Tracking history across these renames is possible, but it requires
use of the --follow flag to `git log`, for example
$ git log spring-aop/src/main/java/org/springframework/aop/Advisor.java
will show history up until the renaming event, where
$ git log --follow spring-aop/src/main/java/org/springframework/aop/Advisor.java
will show history for all changes to the file, before and after the
renaming.
See http://chrisbeams.com/git-diff-across-renamed-directories
This commit is contained in:
0
spring-oxm/src/test/castor/castorbuilder.properties
Normal file
0
spring-oxm/src/test/castor/castorbuilder.properties
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.Result;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class AbstractMarshallerTests {
|
||||
|
||||
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>";
|
||||
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
marshaller = createMarshaller();
|
||||
flights = createFlights();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
protected abstract Marshaller createMarshaller() throws Exception;
|
||||
|
||||
protected abstract Object createFlights();
|
||||
|
||||
@Test
|
||||
public void marshalDOMResult() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalEmptyDOMResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||
DOMResult domResult = new DOMResult();
|
||||
marshaller.marshal(flights, domResult);
|
||||
assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document);
|
||||
Document result = (Document) domResult.getNode();
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStreamResultOutputStream() 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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStaxResultStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
Result result = StaxUtils.createStaxResult(streamWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStaxResultEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
Result result = StaxUtils.createStaxResult(eventWriter);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalJaxp14StaxResultStreamWriter() 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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalJaxp14StaxResultEventWriter() 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,156 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.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 static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
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.util.xml.StaxUtils;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public abstract class AbstractUnmarshallerTests {
|
||||
|
||||
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>";
|
||||
|
||||
@Before
|
||||
public 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);
|
||||
|
||||
@Test
|
||||
public void unmarshalDomSource() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalSAXSource() throws Exception {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
Source source = StaxUtils.createStaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStaxSourceXmlEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
||||
Source source = StaxUtils.createStaxSource(eventReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalJaxp14StaxSourceXmlStreamReader() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalJaxp14StaxSourceXmlEventReader() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() 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());
|
||||
Source source = StaxUtils.createStaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.NamespaceContext;
|
||||
import org.custommonkey.xmlunit.SimpleNamespaceContext;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.custommonkey.xmlunit.XpathEngine;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
/**
|
||||
* Tests the {@link CastorMarshaller} class.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
*/
|
||||
public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
/**
|
||||
* Represents the expected result that doesn't contain the xml declaration.
|
||||
*/
|
||||
private static final String DOCUMENT_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
/**
|
||||
* Represents the expected result that doesn't contain the xml namespaces.
|
||||
*/
|
||||
private static final String SUPPRESSED_NAMESPACE_EXPECTED_STRING =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><flights><flight><number>42</number></flight></flights>";
|
||||
|
||||
/**
|
||||
* Represents the expected result with modified root element name.
|
||||
*/
|
||||
private static final String ROOT_ELEMENT_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<tns:canceledFlights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:canceledFlights>";
|
||||
|
||||
/**
|
||||
* Represents the expected result with 'xsi:type' attribute.
|
||||
*/
|
||||
private static final String XSI_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<objects><castor-object xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
|
||||
" xsi:type=\"java:org.springframework.oxm.castor.CastorObject\">" +
|
||||
"<name>test</name><value>8</value></castor-object></objects>";
|
||||
|
||||
/**
|
||||
* Represents the expected result with suppressed 'xsi:type' attribute.
|
||||
*/
|
||||
private static final String SUPPRESSED_XSI_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<objects><castor-object><name>test</name><value>8</value></castor-object></objects>";
|
||||
|
||||
/**
|
||||
* Represents the expected result with 'xsi:type' attribute for root element.
|
||||
*/
|
||||
private static final String ROOT_WITH_XSI_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<objects xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
|
||||
" xsi:type=\"java:java.util.Arrays$ArrayList\">" +
|
||||
"<castor-object xsi:type=\"java:org.springframework.oxm.castor.CastorObject\">" +
|
||||
"<name>test</name><value>8</value></castor-object></objects>";
|
||||
|
||||
/**
|
||||
* Represents the expected result without 'xsi:type' attribute for root element.
|
||||
*/
|
||||
private static final String ROOT_WITHOUT_XSI_EXPECTED_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<objects><castor-object xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
|
||||
" xsi:type=\"java:org.springframework.oxm.castor.CastorObject\">" +
|
||||
"<name>test</name><value>8</value></castor-object></objects>";
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createFlights() {
|
||||
Flight flight = new Flight();
|
||||
flight.setNumber(42L);
|
||||
Flights flights = new Flights();
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalSaxResult() throws Exception {
|
||||
ContentHandler handlerMock = createMock(ContentHandler.class);
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startPrefixMapping("tns", "http://samples.springframework.org/flight");
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("tns:flights"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("tns:flight"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("tns:number"),
|
||||
isA(Attributes.class));
|
||||
handlerMock.characters(aryEq(new char[]{'4', '2'}), eq(0), eq(2));
|
||||
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();
|
||||
|
||||
replay(handlerMock);
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flights, result);
|
||||
verify(handlerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports() throws Exception {
|
||||
Assert.assertTrue("CastorMarshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
Assert.assertTrue("CastorMarshaller does not support Flight", marshaller.supports(Flight.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressNamespacesTrue() throws Exception {
|
||||
getCastorMarshaller().setSuppressNamespaces(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_NAMESPACE_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressNamespacesFalse() throws Exception {
|
||||
getCastorMarshaller().setSuppressNamespaces(false);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressXsiTypeTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
|
||||
getCastorMarshaller().setSuppressXsiType(true);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressXsiTypeFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarshalAsDocumentTrue() throws Exception {
|
||||
|
||||
getCastorMarshaller().setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
|
||||
Assert.assertTrue("Result doesn't contain xml declaration.",
|
||||
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarshalAsDocumentFalse() throws Exception {
|
||||
|
||||
getCastorMarshaller().setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
Assert.assertFalse("Result contains xml declaration.", result.matches("<\\?\\s*xml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootElement() throws Exception {
|
||||
|
||||
getCastorMarshaller().setRootElement("canceledFlights");
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoNamespaceSchemaLocation() throws Exception {
|
||||
String noNamespaceSchemaLocation = "flights.xsd";
|
||||
|
||||
getCastorMarshaller().setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
|
||||
String result = marshalFlights();
|
||||
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchemaLocation() throws Exception {
|
||||
String schemaLocation = "flights.xsd";
|
||||
|
||||
getCastorMarshaller().setSchemaLocation(schemaLocation);
|
||||
String result = marshalFlights();
|
||||
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseXsiTypeAsRootTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setUseXSITypeAtRoot(true);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITH_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseXsiTypeAsRootFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setUseXSITypeAtRoot(false);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
|
||||
private CastorMarshaller getCastorMarshaller() {
|
||||
return (CastorMarshaller) marshaller;
|
||||
}
|
||||
|
||||
private String marshal(Object object) throws Exception {
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
getCastorMarshaller().marshal(object, result);
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
private String marshalFlights() throws Exception {
|
||||
return marshal(flights);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the values of xpath expression evaluation is exactly the same as expected value. </p> The xpath may contain
|
||||
* the xml namespace prefixes, since namespaces from flight example are being registered.
|
||||
*
|
||||
* @param msg the error message that will be used in case of test failure
|
||||
* @param expected the expected value
|
||||
* @param xpath the xpath to evaluate
|
||||
* @param xmlDoc the xml to use
|
||||
* @throws Exception if any error occurs during xpath evaluation
|
||||
*/
|
||||
private void assertXpathEvaluatesTo(String msg, String expected, String xpath, String xmlDoc) throws Exception {
|
||||
Map<String, String> namespaces = new HashMap<String, String>();
|
||||
namespaces.put("tns", "http://samples.springframework.org/flight");
|
||||
namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
|
||||
|
||||
NamespaceContext ctx = new SimpleNamespaceContext(namespaces);
|
||||
XpathEngine engine = XMLUnit.newXpathEngine();
|
||||
engine.setNamespaceContext(ctx);
|
||||
|
||||
Document doc = XMLUnit.buildControlDocument(xmlDoc);
|
||||
NodeList node = engine.getMatchingNodes(xpath, doc);
|
||||
|
||||
assertEquals(msg, expected, node.item(0).getNodeValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a instance of {@link CastorObject} for testing.
|
||||
*
|
||||
* @return a instance of {@link CastorObject}
|
||||
*/
|
||||
private CastorObject createCastorObject() {
|
||||
CastorObject castorObject = new CastorObject();
|
||||
castorObject.setName("test");
|
||||
castorObject.setValue(8);
|
||||
return castorObject;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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;
|
||||
|
||||
/**
|
||||
* Represents a POJO used by {@link CastorMarshallerTests} for testing the marshaller output.
|
||||
*
|
||||
* @author Jakub Narloch
|
||||
*/
|
||||
public class CastorObject {
|
||||
|
||||
private String name;
|
||||
|
||||
private Integer value;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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 java.io.StringReader;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.MarshallingException;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests the {@link CastorMarshaller} class.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
*/
|
||||
public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
/**
|
||||
* Represents the xml with additional attribute that is not mapped in Castor config.
|
||||
*/
|
||||
protected static final String EXTRA_ATTRIBUTES_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight status=\"canceled\"><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
/**
|
||||
* Represents the xml with additional element that is not mapped in Castor config.
|
||||
*/
|
||||
protected static final String EXTRA_ELEMENTS_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number><tns:date>2011-06-14</tns:date>" +
|
||||
"</tns:flight></tns:flights>";
|
||||
|
||||
@Override
|
||||
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]);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlight(Object o) {
|
||||
Flight flight = (Flight) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, (long) flight.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalTargetClass() throws Exception {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setTargetClasses(new Class[] { Flights.class } );
|
||||
unmarshaller.afterPropertiesSet();
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBothTargetClassesAndMapping() throws IOException {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
|
||||
unmarshaller.setTargetClasses(new Class[] { Order.class } );
|
||||
unmarshaller.afterPropertiesSet();
|
||||
|
||||
String xml = "<order>" +
|
||||
"<order-item id=\"1\" quantity=\"15\"/>" +
|
||||
"<order-item id=\"3\" quantity=\"20\"/>" +
|
||||
"</order>";
|
||||
|
||||
Order order = (Order) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
|
||||
assertEquals("Invalid amount of items", 2, order.getOrderItemCount());
|
||||
OrderItem item = order.getOrderItem(0);
|
||||
assertEquals("Invalid items", "1", item.getId());
|
||||
assertEquals("Invalid items", 15, (int)item.getQuantity());
|
||||
item = order.getOrderItem(1);
|
||||
assertEquals("Invalid items", "3", item.getId());
|
||||
assertEquals("Invalid items", 20, (int)item.getQuantity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespacePreserveTrue() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setWhitespacePreserve(true);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespacePreserveFalse() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setWhitespacePreserve(false);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreExtraAttributesTrue() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setIgnoreExtraAttributes(true);
|
||||
Object result = unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void testIgnoreExtraAttributesFalse() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setIgnoreExtraAttributes(false);
|
||||
unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Not working yet")
|
||||
public void testIgnoreExtraElementsTrue() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setIgnoreExtraElements(true);
|
||||
getCastorUnmarshaller().setValidating(false);
|
||||
Object result = unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void testIgnoreExtraElementsFalse() throws Exception {
|
||||
|
||||
getCastorUnmarshaller().setIgnoreExtraElements(false);
|
||||
unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObject() throws Exception {
|
||||
|
||||
Flights flights = new Flights();
|
||||
getCastorUnmarshaller().setObject(flights);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
testFlights(result);
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearCollectionsTrue() throws Exception {
|
||||
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[]{new Flight()});
|
||||
getCastorUnmarshaller().setObject(flights);
|
||||
getCastorUnmarshaller().setClearCollections(true);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
assertEquals("Result Flights has incorrect number of Flight.", 1, ((Flights) result).getFlightCount());
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Fails on the builder server for some reason")
|
||||
public void testClearCollectionsFalse() throws Exception {
|
||||
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[]{new Flight(), null});
|
||||
getCastorUnmarshaller().setObject(flights);
|
||||
getCastorUnmarshaller().setClearCollections(false);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
assertEquals("Result Flights has incorrect number of Flight.", 3, ((Flights) result).getFlightCount());
|
||||
assertNull("Flight shouldn't have number.", flights.getFlight(0).getNumber());
|
||||
assertNull("Null Flight was expected.", flights.getFlight()[1]);
|
||||
testFlight(flights.getFlight()[2]);
|
||||
}
|
||||
|
||||
private CastorMarshaller getCastorUnmarshaller() {
|
||||
return (CastorMarshaller) unmarshaller;
|
||||
}
|
||||
|
||||
private Object unmarshalFlights() throws Exception {
|
||||
return unmarshal(INPUT_STRING);
|
||||
}
|
||||
|
||||
private Object unmarshal(String xml) throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(xml));
|
||||
return unmarshaller.unmarshal(source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.apache.xmlbeans.XmlOptions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.oxm.castor.CastorMarshaller;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
import org.springframework.oxm.xmlbeans.XmlBeansMarshaller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests the {@link OxmNamespaceHandler} class.
|
||||
*
|
||||
* @author Arjen Poustma
|
||||
* @author Jakub Narloch
|
||||
*/
|
||||
public class OxmNamespaceHandlerTests {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void createAppContext() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlBeansMarshaller() throws Exception {
|
||||
XmlBeansMarshaller marshaller = 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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jaxb2ContextPathMarshaller() throws Exception {
|
||||
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class);
|
||||
assertNotNull(jaxb2Marshaller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jaxb2ClassesToBeBoundMarshaller() throws Exception {
|
||||
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class);
|
||||
assertNotNull(jaxb2Marshaller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castorEncodingMarshaller() throws Exception {
|
||||
CastorMarshaller castorMarshaller = applicationContext.getBean("castorEncodingMarshaller", CastorMarshaller.class);
|
||||
assertNotNull(castorMarshaller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castorTargetClassMarshaller() throws Exception {
|
||||
CastorMarshaller castorMarshaller = applicationContext.getBean("castorTargetClassMarshaller", CastorMarshaller.class);
|
||||
assertNotNull(castorMarshaller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castorTargetPackageMarshaller() throws Exception {
|
||||
CastorMarshaller castorMarshaller = applicationContext.getBean("castorTargetPackageMarshaller", CastorMarshaller.class);
|
||||
assertNotNull(castorMarshaller);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castorMappingLocationMarshaller() throws Exception {
|
||||
CastorMarshaller castorMarshaller = applicationContext.getBean("castorMappingLocationMarshaller", CastorMarshaller.class);
|
||||
assertNotNull(castorMarshaller);
|
||||
}
|
||||
}
|
||||
@@ -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,310 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.junit.Test;
|
||||
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.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.UncategorizedMappingException;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb.test.FlightType;
|
||||
import org.springframework.oxm.jaxb.test.Flights;
|
||||
import org.springframework.oxm.jaxb.test.ObjectFactory;
|
||||
import org.springframework.oxm.mime.MimeContainer;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertFalse;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.fail;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
|
||||
|
||||
private Jaxb2Marshaller marshaller;
|
||||
|
||||
private Flights flights;
|
||||
|
||||
@Override
|
||||
public Marshaller createMarshaller() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createFlights() {
|
||||
FlightType flight = new FlightType();
|
||||
flight.setNumber(42L);
|
||||
flights = new Flights();
|
||||
flights.getFlight().add(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalSAXResult() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lazyInit() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.setLazyInit(true);
|
||||
marshaller.afterPropertiesSet();
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void properties() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.setMarshallerProperties(
|
||||
Collections.<String, Object>singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,
|
||||
Boolean.TRUE));
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void noContextPathOrClassesToBeBound() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = UncategorizedMappingException.class)
|
||||
public void testInvalidContextPath() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath("ab");
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = XmlMappingException.class)
|
||||
public void marshalInvalidClass() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(FlightType.class);
|
||||
marshaller.afterPropertiesSet();
|
||||
Result result = new StreamResult(new StringWriter());
|
||||
Flights flights = new Flights();
|
||||
marshaller.marshal(flights, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsContextPath() throws Exception {
|
||||
testSupports();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsClassesToBeBound() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(Flights.class, FlightType.class);
|
||||
marshaller.afterPropertiesSet();
|
||||
testSupports();
|
||||
}
|
||||
|
||||
private void testSupports() throws Exception {
|
||||
assertTrue("Jaxb2Marshaller does not support Flights class", marshaller.supports(Flights.class));
|
||||
assertTrue("Jaxb2Marshaller does not support Flights generic type", marshaller.supports((Type)Flights.class));
|
||||
|
||||
assertFalse("Jaxb2Marshaller supports FlightType class", marshaller.supports(FlightType.class));
|
||||
assertFalse("Jaxb2Marshaller supports FlightType type", marshaller.supports((Type)FlightType.class));
|
||||
|
||||
Method method = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
||||
marshaller.supports(method.getGenericReturnType()));
|
||||
|
||||
marshaller.setSupportJaxbElementClass(true);
|
||||
JAXBElement<FlightType> flightTypeJAXBElement = new JAXBElement<FlightType>(new QName("http://springframework.org", "flight"), FlightType.class,
|
||||
new FlightType());
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(flightTypeJAXBElement.getClass()));
|
||||
|
||||
assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyRootElement.class));
|
||||
assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type)DummyRootElement.class));
|
||||
method = getClass().getDeclaredMethod("createDummyRootElement");
|
||||
assertFalse("Jaxb2Marshaller supports JAXBElement not in context path",
|
||||
marshaller.supports(method.getGenericReturnType()));
|
||||
|
||||
assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyType.class));
|
||||
assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type)DummyType.class));
|
||||
method = getClass().getDeclaredMethod("createDummyType");
|
||||
assertFalse("Jaxb2Marshaller supports JAXBElement not in context path",
|
||||
marshaller.supports(method.getGenericReturnType()));
|
||||
|
||||
testSupportsPrimitives();
|
||||
testSupportsStandardClasses();
|
||||
}
|
||||
|
||||
private void testSupportsPrimitives() {
|
||||
final Primitives primitives = new Primitives();
|
||||
ReflectionUtils.doWithMethods(Primitives.class, new ReflectionUtils.MethodCallback() {
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
Type returnType = method.getGenericReturnType();
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">",
|
||||
marshaller.supports(returnType));
|
||||
try {
|
||||
// make sure the marshalling does not result in errors
|
||||
Object returnValue = method.invoke(primitives);
|
||||
marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}, new ReflectionUtils.MethodFilter() {
|
||||
public boolean matches(Method method) {
|
||||
return method.getName().startsWith("primitive");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void testSupportsStandardClasses() throws Exception {
|
||||
final StandardClasses standardClasses = new StandardClasses();
|
||||
ReflectionUtils.doWithMethods(StandardClasses.class, new ReflectionUtils.MethodCallback() {
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
Type returnType = method.getGenericReturnType();
|
||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">",
|
||||
marshaller.supports(returnType));
|
||||
try {
|
||||
// make sure the marshalling does not result in errors
|
||||
Object returnValue = method.invoke(standardClasses);
|
||||
marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}, new ReflectionUtils.MethodFilter() {
|
||||
public boolean matches(Method method) {
|
||||
return method.getName().startsWith("standardClass");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsXmlRootElement() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class);
|
||||
marshaller.afterPropertiesSet();
|
||||
assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class));
|
||||
assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class));
|
||||
|
||||
assertFalse("Jaxb2Marshaller supports DummyType class", marshaller.supports(DummyType.class));
|
||||
assertFalse("Jaxb2Marshaller supports DummyType type", marshaller.supports((Type)DummyType.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void marshalAttachments() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setClassesToBeBound(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);
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(object, new StreamResult(writer), mimeContainer);
|
||||
verify(mimeContainer);
|
||||
assertTrue("No XML written", writer.toString().length() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsPackagesToScan() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setPackagesToScan(new String[] {CONTEXT_PATH});
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@XmlRootElement
|
||||
public static class DummyRootElement {
|
||||
|
||||
private DummyType t = new DummyType();
|
||||
|
||||
}
|
||||
|
||||
@XmlType
|
||||
public static class DummyType {
|
||||
|
||||
private String s = "Hello";
|
||||
}
|
||||
|
||||
private JAXBElement<DummyRootElement> createDummyRootElement() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private JAXBElement<DummyType> createDummyType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.StringReader;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.jaxb.test.FlightType;
|
||||
import org.springframework.oxm.jaxb.test.Flights;
|
||||
import org.springframework.oxm.mime.MimeContainer;
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
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;
|
||||
|
||||
@Override
|
||||
public Unmarshaller createUnmarshaller() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setContextPath("org.springframework.oxm.jaxb.test");
|
||||
unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
|
||||
unmarshaller.afterPropertiesSet();
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalAttachments() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setClassesToBeBound(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>";
|
||||
|
||||
StringReader reader = new StringReader(content);
|
||||
Object result = unmarshaller.unmarshal(new StreamSource(reader), 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());
|
||||
}
|
||||
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
streamReader.nextTag(); // skip to flight
|
||||
Source source = StaxUtils.createStaxSource(streamReader);
|
||||
JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source);
|
||||
FlightType flight = element.getValue();
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.JAXBElement;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
/**
|
||||
* Used by {@link org.springframework.oxm.jaxb.Jaxb2MarshallerTests}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class Primitives {
|
||||
|
||||
private static final QName NAME = new QName("http://springframework.org/oxm-test", "primitives");
|
||||
|
||||
// following methods are used to test support for primitives
|
||||
public JAXBElement<Boolean> primitiveBoolean() {
|
||||
return new JAXBElement<Boolean>(NAME, Boolean.class, true);
|
||||
}
|
||||
|
||||
public JAXBElement<Byte> primitiveByte() {
|
||||
return new JAXBElement<Byte>(NAME, Byte.class, (byte)42);
|
||||
}
|
||||
|
||||
public JAXBElement<Short> primitiveShort() {
|
||||
return new JAXBElement<Short>(NAME, Short.class, (short)42);
|
||||
}
|
||||
|
||||
public JAXBElement<Integer> primitiveInteger() {
|
||||
return new JAXBElement<Integer>(NAME, Integer.class, 42);
|
||||
}
|
||||
|
||||
public JAXBElement<Long> primitiveLong() {
|
||||
return new JAXBElement<Long>(NAME, Long.class, 42L);
|
||||
}
|
||||
|
||||
public JAXBElement<Double> primitiveDouble() {
|
||||
return new JAXBElement<Double>(NAME, Double.class, 42D);
|
||||
}
|
||||
|
||||
public JAXBElement<byte[]> primitiveByteArray() {
|
||||
return new JAXBElement<byte[]>(NAME, byte[].class, new byte[]{42});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.Image;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.DataSource;
|
||||
import javax.activation.URLDataSource;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.Duration;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
/**
|
||||
* Used by {@link org.springframework.oxm.jaxb.Jaxb2MarshallerTests}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class StandardClasses {
|
||||
|
||||
private static final QName NAME = new QName("http://springframework.org/oxm-test", "standard-classes");
|
||||
|
||||
private DatatypeFactory factory;
|
||||
|
||||
public StandardClasses() throws DatatypeConfigurationException {
|
||||
factory = DatatypeFactory.newInstance();
|
||||
}
|
||||
|
||||
/*
|
||||
java.net.URI
|
||||
javax.xml.datatype.XMLGregorianCalendarxs:anySimpleType
|
||||
javax.xml.datatype.Duration
|
||||
java.lang.Object
|
||||
java.awt.Image
|
||||
javax.activation.DataHandler
|
||||
javax.xml.transform.Source
|
||||
java.util.UUID
|
||||
*/
|
||||
public JAXBElement<String> standardClassString() {
|
||||
return new JAXBElement<String>(NAME, String.class, "42");
|
||||
}
|
||||
|
||||
public JAXBElement<BigInteger> standardClassBigInteger() {
|
||||
return new JAXBElement<BigInteger>(NAME, BigInteger.class, new BigInteger("42"));
|
||||
}
|
||||
|
||||
public JAXBElement<BigDecimal> standardClassBigDecimal() {
|
||||
return new JAXBElement<BigDecimal>(NAME, BigDecimal.class, new BigDecimal("42.0"));
|
||||
}
|
||||
|
||||
public JAXBElement<Calendar> standardClassCalendar() {
|
||||
return new JAXBElement<Calendar>(NAME, Calendar.class, Calendar.getInstance());
|
||||
}
|
||||
|
||||
public JAXBElement<GregorianCalendar> standardClassGregorianCalendar() {
|
||||
return new JAXBElement<GregorianCalendar>(NAME, GregorianCalendar.class, (GregorianCalendar)Calendar.getInstance());
|
||||
}
|
||||
|
||||
public JAXBElement<Date> standardClassDate() {
|
||||
return new JAXBElement<Date>(NAME, Date.class, new Date());
|
||||
}
|
||||
|
||||
public JAXBElement<QName> standardClassQName() {
|
||||
return new JAXBElement<QName>(NAME, QName.class, NAME);
|
||||
}
|
||||
|
||||
public JAXBElement<URI> standardClassURI() {
|
||||
return new JAXBElement<URI>(NAME, URI.class, URI.create("http://springframework.org"));
|
||||
}
|
||||
|
||||
public JAXBElement<XMLGregorianCalendar> standardClassXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar calendar =
|
||||
factory.newXMLGregorianCalendar((GregorianCalendar) Calendar.getInstance());
|
||||
return new JAXBElement<XMLGregorianCalendar>(NAME, XMLGregorianCalendar.class, calendar);
|
||||
}
|
||||
|
||||
public JAXBElement<Duration> standardClassDuration() {
|
||||
Duration duration = factory.newDuration(42000);
|
||||
return new JAXBElement<Duration>(NAME, Duration.class, duration);
|
||||
}
|
||||
|
||||
public JAXBElement<Image> standardClassImage() throws IOException {
|
||||
Image image = ImageIO.read(getClass().getResourceAsStream("spring-ws.png"));
|
||||
return new JAXBElement<Image>(NAME, Image.class, image);
|
||||
}
|
||||
|
||||
public JAXBElement<DataHandler> standardClassDataHandler() {
|
||||
DataSource dataSource = new URLDataSource(getClass().getResource("spring-ws.png"));
|
||||
DataHandler dataHandler = new DataHandler(dataSource);
|
||||
return new JAXBElement<DataHandler>(NAME, DataHandler.class, dataHandler);
|
||||
}
|
||||
|
||||
/* The following should work according to the spec, but doesn't on the JAXB2 implementation including in JDK 1.6.0_17
|
||||
public JAXBElement<Source> standardClassSource() {
|
||||
StringReader reader = new StringReader("<foo/>");
|
||||
return new JAXBElement<Source>(NAME, Source.class, new StreamSource(reader));
|
||||
}
|
||||
*/
|
||||
|
||||
public JAXBElement<UUID> standardClassUUID() {
|
||||
return new JAXBElement<UUID>(NAME, UUID.class, UUID.randomUUID());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,103 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.io.StringWriter;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*
|
||||
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does
|
||||
* not occur by default. The Gradle build should succeed, however.
|
||||
*/
|
||||
public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.setTargetPackage("org.springframework.oxm.jibx");
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createFlights() {
|
||||
Flights flights = new Flights();
|
||||
FlightType flight = new FlightType();
|
||||
flight.setNumber(42L);
|
||||
flights.addFlight(flight);
|
||||
return flights;
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void afterPropertiesSetNoContextPath() throws Exception {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void indentation() throws Exception {
|
||||
((JibxMarshaller) marshaller).setIndent(4);
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
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, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodingAndStandalone() throws Exception {
|
||||
((JibxMarshaller) marshaller).setEncoding("ISO-8859-1");
|
||||
((JibxMarshaller) marshaller).setStandalone(Boolean.TRUE);
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
assertTrue("Encoding and standalone not set",
|
||||
writer.toString().startsWith("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dtd() throws Exception {
|
||||
((JibxMarshaller) marshaller).setDocTypeRootElementName("flights");
|
||||
((JibxMarshaller) marshaller).setDocTypeSystemId("flights.dtd");
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
assertTrue("doc type not written",
|
||||
writer.toString().contains("<!DOCTYPE flights SYSTEM \"flights.dtd\">"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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,63 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import org.junit.Ignore;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*
|
||||
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does
|
||||
* not occur by default. The Gradle build should succeed, however.
|
||||
*/
|
||||
public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
JibxMarshaller unmarshaller = new JibxMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Ignore
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
// JiBX does not support reading XML fragments, hence the override here
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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 static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlBeansMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createFlights() {
|
||||
FlightsDocument flightsDocument = FlightsDocument.Factory.newInstance();
|
||||
FlightsDocument.Flights flights = flightsDocument.addNewFlights();
|
||||
FlightType flightType = flights.addNewFlight();
|
||||
flightType.setNumber(42L);
|
||||
return flightsDocument;
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void testMarshalNonXmlObject() throws Exception {
|
||||
marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports() 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(FlightsDocument.Flights.class));
|
||||
assertTrue("XmlBeansMarshaller does not support FlightType", marshaller.supports(FlightType.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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 javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
import org.springframework.samples.flight.FlightDocument;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlBeansUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlights(Object o) {
|
||||
FlightsDocument flightsDocument = (FlightsDocument) o;
|
||||
assertNotNull("FlightsDocument is null", flightsDocument);
|
||||
FlightsDocument.Flights flights = flightsDocument.getFlights();
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeOfFlightArray());
|
||||
testFlight(flights.getFlightArray(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() 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());
|
||||
Source source = StaxUtils.createStaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
@Test(expected = ValidationFailureException.class)
|
||||
public void testValidate() throws Exception {
|
||||
((XmlBeansMarshaller) unmarshaller).setValidating(true);
|
||||
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 StreamSource(new StringReader(invalidInput)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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 org.apache.xmlbeans.XmlOptions;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlOptionsFactoryBeanTests {
|
||||
|
||||
private XmlOptionsFactoryBean factoryBean = new XmlOptionsFactoryBean();
|
||||
|
||||
@Test
|
||||
public void xmlOptionsFactoryBean() throws Exception {
|
||||
factoryBean.setOptions(Collections.singletonMap(XmlOptions.SAVE_PRETTY_PRINT, Boolean.TRUE));
|
||||
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,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,24 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class FlightSubclass extends Flight {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.springframework.oxm.xstream;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Flights {
|
||||
|
||||
private List<Flight> flights = new ArrayList<Flight>();
|
||||
|
||||
private List<String> strings = new ArrayList<String>();
|
||||
|
||||
public List<Flight> getFlights() {
|
||||
return flights;
|
||||
}
|
||||
|
||||
public void setFlights(List<Flight> flights) {
|
||||
this.flights = flights;
|
||||
}
|
||||
|
||||
public List<String> getStrings() {
|
||||
return strings;
|
||||
}
|
||||
|
||||
public void setStrings(List<String> strings) {
|
||||
this.strings = strings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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.dom.DOMResult;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import com.thoughtworks.xstream.converters.Converter;
|
||||
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
|
||||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
|
||||
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
|
||||
import com.thoughtworks.xstream.io.json.JsonWriter;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
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.springframework.util.xml.StaxUtils;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XStreamMarshallerTests {
|
||||
|
||||
private static final String EXPECTED_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller marshaller;
|
||||
|
||||
private Flight flight;
|
||||
|
||||
@Before
|
||||
public void createMarshaller() throws Exception {
|
||||
marshaller = new XStreamMarshaller();
|
||||
Map<String, String> aliases = new HashMap<String, String>();
|
||||
aliases.put("flight", Flight.class.getName());
|
||||
marshaller.setAliases(aliases);
|
||||
flight = new Flight();
|
||||
flight.setFlightNumber(42L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalDOMResult() 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
|
||||
@Test
|
||||
public void marshalDOMResultToExistentDocument() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStreamResultWriter() throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStreamResultOutputStream() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalSaxResult() throws Exception {
|
||||
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
|
||||
handlerMock.startDocument();
|
||||
handlerMock.startElement(eq(""), eq("flight"), eq("flight"), isA(Attributes.class));
|
||||
handlerMock.startElement(eq(""), eq("flightNumber"), eq("flightNumber"), isA(Attributes.class));
|
||||
handlerMock.characters(isA(char[].class), eq(0), eq(2));
|
||||
handlerMock.endElement("", "flightNumber", "flightNumber");
|
||||
handlerMock.endElement("", "flight", "flight");
|
||||
handlerMock.endDocument();
|
||||
|
||||
replay(handlerMock);
|
||||
SAXResult result = new SAXResult(handlerMock);
|
||||
marshaller.marshal(flight, result);
|
||||
verify(handlerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStaxResultXMLStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
||||
Result result = StaxUtils.createStaxResult(streamWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalStaxResultXMLEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
StringWriter writer = new StringWriter();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
||||
Result result = StaxUtils.createStaxResult(eventWriter);
|
||||
marshaller.marshal(flight, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converters() throws Exception {
|
||||
marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()});
|
||||
byte[] buf = new byte[]{0x1, 0x2};
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(buf, new StreamResult(writer));
|
||||
assertXMLEqual("<byte-array>AQI=</byte-array>", writer.toString());
|
||||
Reader reader = new StringReader(writer.toString());
|
||||
byte[] bufResult = (byte[]) marshaller.unmarshal(new StreamSource(reader));
|
||||
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useAttributesFor() throws Exception {
|
||||
marshaller.setUseAttributeForTypes(new Class[]{Long.TYPE});
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useAttributesForStringClassMap() throws Exception {
|
||||
marshaller.setUseAttributeFor(Collections.singletonMap("flightNumber", Long.TYPE));
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useAttributesForClassStringMap() throws Exception {
|
||||
marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, "flightNumber"));
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useAttributesForClassStringListMap() throws Exception {
|
||||
marshaller
|
||||
.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("flightNumber")));
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
String expected = "<flight flightNumber=\"42\" />";
|
||||
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aliasesByTypeStringClassMap() throws Exception {
|
||||
Map<String, Class<?>> aliases = new HashMap<String, Class<?>>();
|
||||
aliases.put("flight", Flight.class);
|
||||
FlightSubclass flight = new FlightSubclass();
|
||||
flight.setFlightNumber(42);
|
||||
marshaller.setAliasesByType(aliases);
|
||||
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aliasesByTypeStringStringMap() throws Exception {
|
||||
Map<String, String> aliases = new HashMap<String, String>();
|
||||
aliases.put("flight", Flight.class.getName());
|
||||
FlightSubclass flight = new FlightSubclass();
|
||||
flight.setFlightNumber(42);
|
||||
marshaller.setAliasesByType(aliases);
|
||||
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldAliases() throws Exception {
|
||||
marshaller.setFieldAliases(Collections.singletonMap("org.springframework.oxm.xstream.Flight.flightNumber", "flightNo"));
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
String expected = "<flight><flightNo>42</flightNo></flight>";
|
||||
assertXMLEqual("Marshaller does not use aliases", expected, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void omitFields() throws Exception {
|
||||
Map omittedFieldsMap = Collections.singletonMap(Flight.class, "flightNumber");
|
||||
marshaller.setOmittedFields(omittedFieldsMap);
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
assertXpathNotExists("/flight/flightNumber", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void implicitCollections() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
flights.getFlights().add(flight);
|
||||
flights.getStrings().add("42");
|
||||
|
||||
Map<String, Class> aliases = new HashMap<String, Class>();
|
||||
aliases.put("flight", Flight.class);
|
||||
aliases.put("flights", Flights.class);
|
||||
marshaller.setAliases(aliases);
|
||||
|
||||
Map implicitCollections = Collections.singletonMap(Flights.class, "flights,strings");
|
||||
marshaller.setImplicitCollections(implicitCollections);
|
||||
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
String result = writer.toString();
|
||||
assertXpathNotExists("/flights/flights", result);
|
||||
assertXpathExists("/flights/flight", result);
|
||||
assertXpathNotExists("/flights/strings", result);
|
||||
assertXpathExists("/flights/string", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jettisonDriver() throws Exception {
|
||||
marshaller.setStreamDriver(new JettisonMappedXmlDriver());
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
|
||||
Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonDriver() throws Exception {
|
||||
marshaller.setStreamDriver(new JsonHierarchicalStreamDriver() {
|
||||
@Override
|
||||
public HierarchicalStreamWriter createWriter(Writer writer) {
|
||||
return new JsonWriter(writer, new char[0], "", JsonWriter.DROP_ROOT_MODE);
|
||||
}
|
||||
});
|
||||
|
||||
Writer writer = new StringWriter();
|
||||
marshaller.marshal(flight, new StreamResult(writer));
|
||||
assertEquals("Invalid result", "{\"flightNumber\": 42}", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void annotatedMarshalStreamResultWriter() throws Exception {
|
||||
marshaller.setAnnotatedClass(Flight.class);
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
Flight flight = new Flight();
|
||||
flight.setFlightNumber(42);
|
||||
marshaller.marshal(flight, result);
|
||||
String expected = "<flight><number>42</number></flight>";
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", expected, writer.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
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.stream.StreamSource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XStreamUnmarshallerTests {
|
||||
|
||||
protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
|
||||
|
||||
private XStreamMarshaller unmarshaller;
|
||||
|
||||
@Before
|
||||
public void creteUnmarshaller() throws Exception {
|
||||
unmarshaller = new XStreamMarshaller();
|
||||
Map<String, Class> aliases = new HashMap<String, Class>();
|
||||
aliases.put("flight", Flight.class);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalDomSource() 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
Source source = StaxUtils.createStaxSource(streamReader);
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStreamSourceInputStream() throws Exception {
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStreamSourceReader() throws Exception {
|
||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlight(flights);
|
||||
}
|
||||
}
|
||||
|
||||
6
spring-oxm/src/test/resources/log4j.properties
Normal file
6
spring-oxm/src/test/resources/log4j.properties
Normal file
@@ -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" standalone="yes"?>
|
||||
<mapping>
|
||||
<class name="org.springframework.oxm.castor.OrderItem">
|
||||
<field name="Id" type="java.lang.String">
|
||||
<bind-xml name="id" node="attribute" />
|
||||
</field>
|
||||
<field name="Quantity" type="java.lang.Integer">
|
||||
<bind-xml name="quantity" node="attribute" />
|
||||
</field>
|
||||
</class>
|
||||
</mapping>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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-3.1.xsd">
|
||||
|
||||
<!-- XMLBeans -->
|
||||
<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>
|
||||
|
||||
<!-- JAXB2 -->
|
||||
<oxm:jaxb2-marshaller id="jaxb2ContextPathMarshaller"
|
||||
contextPath="org.springframework.oxm.jaxb.test" />
|
||||
|
||||
<oxm:jaxb2-marshaller id="jaxb2ClassesMarshaller">
|
||||
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.Flights" />
|
||||
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.FlightType" />
|
||||
</oxm:jaxb2-marshaller>
|
||||
|
||||
<!-- Castor -->
|
||||
|
||||
<oxm:castor-marshaller id="castorEncodingMarshaller" encoding="ISO-8859-1" />
|
||||
|
||||
<oxm:castor-marshaller id="castorTargetClassMarshaller" target-class="org.springframework.oxm.castor.Flight" />
|
||||
|
||||
<oxm:castor-marshaller id="castorTargetPackageMarshaller" target-package="org.springframework.oxm.castor" />
|
||||
|
||||
<oxm:castor-marshaller id="castorMappingLocationMarshaller"
|
||||
mapping-location="classpath:org/springframework/oxm/castor/mapping.xml" />
|
||||
</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>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
|
||||
targetNamespace="http://samples.springframework.org/order"
|
||||
xmlns:tns="http://samples.springframework.org/order">
|
||||
<element name="order">
|
||||
<complexType>
|
||||
<sequence>
|
||||
<element name="order-item" type="tns:orderItemType"
|
||||
maxOccurs="unbounded">
|
||||
</element>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</element>
|
||||
<complexType name="orderItemType">
|
||||
<attribute name="id" type="string" />
|
||||
<attribute name="quantity" type="int" />
|
||||
</complexType>
|
||||
</schema>
|
||||
Reference in New Issue
Block a user