Browse Source

Polishing

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

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

@ -125,6 +125,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
return flights; return flights;
} }
@Test @Test
public void marshalSaxResult() throws Exception { public void marshalSaxResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class); ContentHandler contentHandler = mock(ContentHandler.class);
@ -133,12 +134,12 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
InOrder ordered = inOrder(contentHandler); InOrder ordered = inOrder(contentHandler);
ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startDocument();
ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight"); ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
eq("tns:flights"), isA(Attributes.class)); eq("flights"), eq("tns:flights"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
eq("tns:flight"), isA(Attributes.class)); eq("flight"), eq("tns:flight"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
eq("tns:number"), isA(Attributes.class)); eq("number"), eq("tns:number"), isA(Attributes.class));
ordered.verify(contentHandler).characters(eq(new char[]{'4', '2'}), eq(0), eq(2)); 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", "number", "tns:number");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
@ -170,7 +171,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testSuppressXsiTypeTrue() throws Exception { public void testSuppressXsiTypeTrue() throws Exception {
CastorObject castorObject = createCastorObject(); CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(true); getCastorMarshaller().setSuppressXsiType(true);
getCastorMarshaller().setRootElement("objects"); getCastorMarshaller().setRootElement("objects");
String result = marshal(Arrays.asList(castorObject)); String result = marshal(Arrays.asList(castorObject));
@ -180,7 +180,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testSuppressXsiTypeFalse() throws Exception { public void testSuppressXsiTypeFalse() throws Exception {
CastorObject castorObject = createCastorObject(); CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(false); getCastorMarshaller().setSuppressXsiType(false);
getCastorMarshaller().setRootElement("objects"); getCastorMarshaller().setRootElement("objects");
String result = marshal(Arrays.asList(castorObject)); String result = marshal(Arrays.asList(castorObject));
@ -189,7 +188,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testMarshalAsDocumentTrue() throws Exception { public void testMarshalAsDocumentTrue() throws Exception {
getCastorMarshaller().setMarshalAsDocument(true); getCastorMarshaller().setMarshalAsDocument(true);
String result = marshalFlights(); String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result); assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
@ -199,7 +197,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testMarshalAsDocumentFalse() throws Exception { public void testMarshalAsDocumentFalse() throws Exception {
getCastorMarshaller().setMarshalAsDocument(true); getCastorMarshaller().setMarshalAsDocument(true);
String result = marshalFlights(); String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result); assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
@ -208,7 +205,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testRootElement() throws Exception { public void testRootElement() throws Exception {
getCastorMarshaller().setRootElement("canceledFlights"); getCastorMarshaller().setRootElement("canceledFlights");
String result = marshalFlights(); String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result); assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
@ -217,10 +213,8 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testNoNamespaceSchemaLocation() throws Exception { public void testNoNamespaceSchemaLocation() throws Exception {
String noNamespaceSchemaLocation = "flights.xsd"; String noNamespaceSchemaLocation = "flights.xsd";
getCastorMarshaller().setNoNamespaceSchemaLocation(noNamespaceSchemaLocation); getCastorMarshaller().setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
String result = marshalFlights(); String result = marshalFlights();
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.", 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); assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
@ -229,10 +223,8 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testSchemaLocation() throws Exception { public void testSchemaLocation() throws Exception {
String schemaLocation = "flights.xsd"; String schemaLocation = "flights.xsd";
getCastorMarshaller().setSchemaLocation(schemaLocation); getCastorMarshaller().setSchemaLocation(schemaLocation);
String result = marshalFlights(); String result = marshalFlights();
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.", 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); assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
@ -241,7 +233,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testUseXsiTypeAsRootTrue() throws Exception { public void testUseXsiTypeAsRootTrue() throws Exception {
CastorObject castorObject = createCastorObject(); CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(false); getCastorMarshaller().setSuppressXsiType(false);
getCastorMarshaller().setUseXSITypeAtRoot(true); getCastorMarshaller().setUseXSITypeAtRoot(true);
getCastorMarshaller().setRootElement("objects"); getCastorMarshaller().setRootElement("objects");
@ -252,7 +243,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void testUseXsiTypeAsRootFalse() throws Exception { public void testUseXsiTypeAsRootFalse() throws Exception {
CastorObject castorObject = createCastorObject(); CastorObject castorObject = createCastorObject();
getCastorMarshaller().setSuppressXsiType(false); getCastorMarshaller().setSuppressXsiType(false);
getCastorMarshaller().setUseXSITypeAtRoot(false); getCastorMarshaller().setUseXSITypeAtRoot(false);
getCastorMarshaller().setRootElement("objects"); getCastorMarshaller().setRootElement("objects");
@ -260,16 +250,15 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result); assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result);
} }
private CastorMarshaller getCastorMarshaller() { private CastorMarshaller getCastorMarshaller() {
return (CastorMarshaller) marshaller; return (CastorMarshaller) marshaller;
} }
private String marshal(Object object) throws Exception { private String marshal(Object object) throws Exception {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer); StreamResult result = new StreamResult(writer);
getCastorMarshaller().marshal(object, result); getCastorMarshaller().marshal(object, result);
return writer.toString(); return writer.toString();
} }
@ -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 * <p>The xpath may contain the xml namespace prefixes, since namespaces from flight example
* are being registered. * are being registered.
* @param msg the error message that will be used in case of test failure * @param msg the error message that will be used in case of test failure
@ -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() { private CastorObject createCastorObject() {
CastorObject castorObject = new CastorObject(); 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 {
return marshaller; return marshaller;
} }
@Test @Test
public void unmarshalTargetClass() throws Exception { public void unmarshalTargetClass() throws Exception {
CastorMarshaller unmarshaller = new CastorMarshaller(); CastorMarshaller unmarshaller = new CastorMarshaller();
@ -137,7 +138,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
@Test(expected = MarshallingException.class) @Test(expected = MarshallingException.class)
public void testIgnoreExtraAttributesFalse() throws Exception { public void testIgnoreExtraAttributesFalse() throws Exception {
getCastorUnmarshaller().setIgnoreExtraAttributes(false); getCastorUnmarshaller().setIgnoreExtraAttributes(false);
unmarshal(EXTRA_ATTRIBUTES_STRING); unmarshal(EXTRA_ATTRIBUTES_STRING);
} }
@ -153,7 +153,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
@Test(expected = MarshallingException.class) @Test(expected = MarshallingException.class)
public void testIgnoreExtraElementsFalse() throws Exception { public void testIgnoreExtraElementsFalse() throws Exception {
getCastorUnmarshaller().setIgnoreExtraElements(false); getCastorUnmarshaller().setIgnoreExtraElements(false);
unmarshal(EXTRA_ELEMENTS_STRING); unmarshal(EXTRA_ELEMENTS_STRING);
} }
@ -181,7 +180,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
} }
@Test @Test
@Ignore("Fails on the builder server for some reason") @Ignore("Fails on the build server for some reason")
public void testClearCollectionsFalse() throws Exception { public void testClearCollectionsFalse() throws Exception {
Flights flights = new Flights(); Flights flights = new Flights();
flights.setFlight(new Flight[]{new Flight(), null}); flights.setFlight(new Flight[]{new Flight(), null});
@ -196,22 +195,8 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
testFlight(flights.getFlight()[2]); 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 @Test
public void unmarshalStreamSourceExternalEntities() throws Exception { public void unmarshalStreamSourceExternalEntities() throws Exception {
final AtomicReference<XMLReader> result = new AtomicReference<XMLReader>(); final AtomicReference<XMLReader> result = new AtomicReference<XMLReader>();
CastorMarshaller marshaller = new CastorMarshaller() { CastorMarshaller marshaller = new CastorMarshaller() {
@Override @Override
@ -222,13 +207,11 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
}; };
// 1. external-general-entities disabled (default) // 1. external-general-entities disabled (default)
marshaller.unmarshal(new StreamSource("1")); marshaller.unmarshal(new StreamSource("1"));
assertNotNull(result.get()); assertNotNull(result.get());
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities")); assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
// 2. external-general-entities disabled (default) // 2. external-general-entities disabled (default)
result.set(null); result.set(null);
marshaller.setProcessExternalEntities(true); marshaller.setProcessExternalEntities(true);
marshaller.unmarshal(new StreamSource("1")); marshaller.unmarshal(new StreamSource("1"));
@ -260,4 +243,18 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities")); 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