@@ -1,300 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.Source;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.castor.xml.XMLProperties;
|
||||
import org.exolab.castor.xml.XercesXMLSerializerFactory;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xmlunit.builder.Input;
|
||||
import org.xmlunit.xpath.JAXPXPathEngine;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.xmlunit.matchers.CompareMatcher.*;
|
||||
|
||||
/**
|
||||
* Tests the {@link CastorMarshaller} class.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@Deprecated
|
||||
public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshaller> {
|
||||
|
||||
/**
|
||||
* 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\"" +
|
||||
" xmlns:java=\"http://java.sun.com\"" +
|
||||
" 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\"" +
|
||||
" xmlns:java=\"http://java.sun.com\"" +
|
||||
" 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\"" +
|
||||
" xmlns:java=\"http://java.sun.com\"" +
|
||||
" xsi:type=\"java:org.springframework.oxm.castor.CastorObject\">" +
|
||||
"<name>test</name><value>8</value></castor-object></objects>";
|
||||
|
||||
|
||||
@Override
|
||||
protected CastorMarshaller createMarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
Map<String, String> props = new HashMap<>(1);
|
||||
props.put(XMLProperties.SERIALIZER_FACTORY, XercesXMLSerializerFactory.class.getName());
|
||||
marshaller.setCastorProperties(props);
|
||||
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 contentHandler = mock(ContentHandler.class);
|
||||
SAXResult result = new SAXResult(contentHandler);
|
||||
marshaller.marshal(flights, result);
|
||||
InOrder ordered = inOrder(contentHandler);
|
||||
ordered.verify(contentHandler).startDocument();
|
||||
ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
|
||||
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
|
||||
eq("flights"), eq("tns:flights"), isA(Attributes.class));
|
||||
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
|
||||
eq("flight"), eq("tns:flight"), isA(Attributes.class));
|
||||
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
|
||||
eq("number"), eq("tns:number"), isA(Attributes.class));
|
||||
ordered.verify(contentHandler).characters(eq(new char[]{'4', '2'}), eq(0), eq(2));
|
||||
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "tns:number");
|
||||
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
|
||||
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
|
||||
ordered.verify(contentHandler).endPrefixMapping("tns");
|
||||
ordered.verify(contentHandler).endDocument();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports() throws Exception {
|
||||
assertTrue("CastorMarshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
assertTrue("CastorMarshaller does not support Flight", marshaller.supports(Flight.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void suppressNamespacesTrue() throws Exception {
|
||||
marshaller.setSuppressNamespaces(true);
|
||||
String result = marshalFlights();
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(SUPPRESSED_NAMESPACE_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void suppressNamespacesFalse() throws Exception {
|
||||
marshaller.setSuppressNamespaces(false);
|
||||
String result = marshalFlights();
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void suppressXsiTypeTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
marshaller.setSuppressXsiType(true);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(SUPPRESSED_XSI_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void suppressXsiTypeFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(XSI_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalAsDocumentTrue() throws Exception {
|
||||
marshaller.setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(DOCUMENT_EXPECTED_STRING));
|
||||
assertTrue("Result doesn't contain xml declaration.",
|
||||
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalAsDocumentFalse() throws Exception {
|
||||
marshaller.setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
|
||||
assertFalse("Result contains xml declaration.", result.matches("<\\?\\s*xml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rootElement() throws Exception {
|
||||
marshaller.setRootElement("canceledFlights");
|
||||
String result = marshalFlights();
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_ELEMENT_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noNamespaceSchemaLocation() throws Exception {
|
||||
String noNamespaceSchemaLocation = "flights.xsd";
|
||||
marshaller.setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
|
||||
String result = marshalFlights();
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void schemaLocation() throws Exception {
|
||||
String schemaLocation = "flights.xsd";
|
||||
marshaller.setSchemaLocation(schemaLocation);
|
||||
String result = marshalFlights();
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useXsiTypeAsRootTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setUseXSITypeAtRoot(true);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_WITH_XSI_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useXsiTypeAsRootFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setUseXSITypeAtRoot(false);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_WITHOUT_XSI_EXPECTED_STRING));
|
||||
}
|
||||
|
||||
|
||||
private String marshal(Object object) throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
marshaller.marshal(object, result);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
private String marshalFlights() throws Exception {
|
||||
return marshal(flights);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert 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<>();
|
||||
namespaces.put("tns", "http://samples.springframework.org/flight");
|
||||
namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
|
||||
|
||||
JAXPXPathEngine engine = new JAXPXPathEngine();
|
||||
engine.setNamespaceContext(namespaces);
|
||||
|
||||
Source source = Input.fromString(xmlDoc).build();
|
||||
Iterable<Node> nodeList = engine.selectNodes(xpath, source);
|
||||
assertEquals(msg, expected, nodeList.iterator().next().getNodeValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of {@link CastorObject} for testing.
|
||||
*/
|
||||
private CastorObject createCastorObject() {
|
||||
CastorObject castorObject = new CastorObject();
|
||||
castorObject.setName("test");
|
||||
castorObject.setValue(8);
|
||||
return castorObject;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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;
|
||||
}
|
||||
}
|
||||
@@ -1,260 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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 java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.MarshallingException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@Deprecated
|
||||
public class CastorUnmarshallerTests extends AbstractUnmarshallerTests<CastorMarshaller> {
|
||||
|
||||
/**
|
||||
* 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 CastorMarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@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);
|
||||
assertThat("Number is invalid", flight.getNumber(), equalTo(42L));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void unmarshalTargetClass() throws Exception {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setTargetClasses(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 setBothTargetClassesAndMapping() throws IOException {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
|
||||
unmarshaller.setTargetClasses(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());
|
||||
assertThat("Invalid items", item.getQuantity(), equalTo(15));
|
||||
item = order.getOrderItem(1);
|
||||
assertEquals("Invalid items", "3", item.getId());
|
||||
assertThat("Invalid items", item.getQuantity(), equalTo(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whitespacePreserveTrue() throws Exception {
|
||||
unmarshaller.setWhitespacePreserve(true);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whitespacePreserveFalse() throws Exception {
|
||||
unmarshaller.setWhitespacePreserve(false);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreExtraAttributesTrue() throws Exception {
|
||||
unmarshaller.setIgnoreExtraAttributes(true);
|
||||
Object result = unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void ignoreExtraAttributesFalse() throws Exception {
|
||||
unmarshaller.setIgnoreExtraAttributes(false);
|
||||
unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Not working yet")
|
||||
public void ignoreExtraElementsTrue() throws Exception {
|
||||
unmarshaller.setIgnoreExtraElements(true);
|
||||
unmarshaller.setValidating(false);
|
||||
Object result = unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void ignoreExtraElementsFalse() throws Exception {
|
||||
unmarshaller.setIgnoreExtraElements(false);
|
||||
unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rootObject() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
unmarshaller.setRootObject(flights);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearCollectionsTrue() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[]{new Flight()});
|
||||
unmarshaller.setRootObject(flights);
|
||||
unmarshaller.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
|
||||
public void clearCollectionsFalse() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[] {new Flight(), null});
|
||||
unmarshaller.setRootObject(flights);
|
||||
unmarshaller.setClearCollections(false);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
assertEquals("Result Flights has incorrect number of Flights.", 3, ((Flights) result).getFlightCount());
|
||||
assertNull("Null Flight was expected.", flights.getFlight()[1]);
|
||||
testFlight(flights.getFlight()[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
|
||||
final AtomicReference<XMLReader> result = new AtomicReference<>();
|
||||
CastorMarshaller marshaller = new CastorMarshaller() {
|
||||
@Override
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
|
||||
result.set(xmlReader);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// 1. external-general-entities and dtd support disabled (default)
|
||||
marshaller.unmarshal(new StreamSource("1"));
|
||||
assertNotNull(result.get());
|
||||
assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
|
||||
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
|
||||
|
||||
// 2. external-general-entities and dtd support enabled
|
||||
result.set(null);
|
||||
marshaller.setSupportDtd(true);
|
||||
marshaller.setProcessExternalEntities(true);
|
||||
marshaller.unmarshal(new StreamSource("1"));
|
||||
assertNotNull(result.get());
|
||||
assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
|
||||
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unmarshalSaxSourceWithXmlOptions() throws Exception {
|
||||
final AtomicReference<XMLReader> result = new AtomicReference<>();
|
||||
CastorMarshaller marshaller = new CastorMarshaller() {
|
||||
@Override
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
|
||||
result.set(xmlReader);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// 1. external-general-entities and dtd support disabled (default)
|
||||
marshaller.unmarshal(new SAXSource(new InputSource("1")));
|
||||
assertNotNull(result.get());
|
||||
assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
|
||||
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
|
||||
|
||||
// 2. external-general-entities and dtd support enabled
|
||||
result.set(null);
|
||||
marshaller.setSupportDtd(true);
|
||||
marshaller.setProcessExternalEntities(true);
|
||||
marshaller.unmarshal(new SAXSource(new InputSource("1")));
|
||||
assertNotNull(result.get());
|
||||
assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
|
||||
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,11 +0,0 @@
|
||||
<?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>
|
||||
@@ -12,15 +12,4 @@
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user