Browse Source

Polishing

(cherry picked from commit f933941)
pull/753/head
Juergen Hoeller 10 years ago
parent
commit
5bdc8d269b
  1. 39
      spring-oxm/src/test/java/org/springframework/oxm/castor/CastorMarshallerTests.java
  2. 35
      spring-oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java

39
spring-oxm/src/test/java/org/springframework/oxm/castor/CastorMarshallerTests.java

@ -125,6 +125,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -125,6 +125,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
return flights;
}
@Test
public void marshalSaxResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);
@ -133,13 +134,13 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -133,13 +134,13 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
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).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");
@ -170,7 +171,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -170,7 +171,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void testSuppressXsiTypeTrue() throws Exception {
CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(true);
getCastorMarshaller().setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
@ -180,7 +180,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -180,7 +180,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void testSuppressXsiTypeFalse() throws Exception {
CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(false);
getCastorMarshaller().setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
@ -189,17 +188,15 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -189,17 +188,15 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void testMarshalAsDocumentTrue() throws Exception {
getCastorMarshaller().setMarshalAsDocument(true);
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
assertTrue("Result doesn't contain xml declaration.",
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
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);
@ -208,7 +205,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -208,7 +205,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void testRootElement() throws Exception {
getCastorMarshaller().setRootElement("canceledFlights");
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
@ -217,31 +213,26 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -217,31 +213,26 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@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);
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);
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");
@ -252,7 +243,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -252,7 +243,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void testUseXsiTypeAsRootFalse() throws Exception {
CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(false);
getCastorMarshaller().setUseXSITypeAtRoot(false);
getCastorMarshaller().setRootElement("objects");
@ -260,16 +250,15 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -260,16 +250,15 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
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();
}
@ -278,7 +267,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -278,7 +267,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
}
/**
* Asserts the values of xpath expression evaluation is exactly the same as expected value.
* 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
@ -302,7 +291,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests { @@ -302,7 +291,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
}
/**
* Creates a instance of {@link CastorObject} for testing.
* Create an instance of {@link CastorObject} for testing.
*/
private CastorObject createCastorObject() {
CastorObject castorObject = new CastorObject();

35
spring-oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java

@ -82,6 +82,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -82,6 +82,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
return marshaller;
}
@Test
public void unmarshalTargetClass() throws Exception {
CastorMarshaller unmarshaller = new CastorMarshaller();
@ -137,7 +138,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -137,7 +138,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
@Test(expected = MarshallingException.class)
public void testIgnoreExtraAttributesFalse() throws Exception {
getCastorUnmarshaller().setIgnoreExtraAttributes(false);
unmarshal(EXTRA_ATTRIBUTES_STRING);
}
@ -153,7 +153,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -153,7 +153,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
@Test(expected = MarshallingException.class)
public void testIgnoreExtraElementsFalse() throws Exception {
getCastorUnmarshaller().setIgnoreExtraElements(false);
unmarshal(EXTRA_ELEMENTS_STRING);
}
@ -181,7 +180,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -181,7 +180,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
}
@Test
@Ignore("Fails on the builder server for some reason")
@Ignore("Fails on the build server for some reason")
public void testClearCollectionsFalse() throws Exception {
Flights flights = new Flights();
flights.setFlight(new Flight[]{new Flight(), null});
@ -196,22 +195,8 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -196,22 +195,8 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
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);
}
@Test
public void unmarshalStreamSourceExternalEntities() throws Exception {
final AtomicReference<XMLReader> result = new AtomicReference<XMLReader>();
CastorMarshaller marshaller = new CastorMarshaller() {
@Override
@ -222,13 +207,11 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -222,13 +207,11 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
};
// 1. external-general-entities disabled (default)
marshaller.unmarshal(new StreamSource("1"));
assertNotNull(result.get());
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
// 2. external-general-entities disabled (default)
result.set(null);
marshaller.setProcessExternalEntities(true);
marshaller.unmarshal(new StreamSource("1"));
@ -260,4 +243,18 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests { @@ -260,4 +243,18 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
}
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);
}
}

Loading…
Cancel
Save