Skip to content

Commit 5bdc8d2

Browse files
committed
Polishing
(cherry picked from commit f933941)
1 parent b7ef047 commit 5bdc8d2

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ protected Object createFlights() {
125125
return flights;
126126
}
127127

128+
128129
@Test
129130
public void marshalSaxResult() throws Exception {
130131
ContentHandler contentHandler = mock(ContentHandler.class);
@@ -133,13 +134,13 @@ public void marshalSaxResult() throws Exception {
133134
InOrder ordered = inOrder(contentHandler);
134135
ordered.verify(contentHandler).startDocument();
135136
ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
136-
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"),
137-
eq("tns:flights"), isA(Attributes.class));
138-
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"),
139-
eq("tns:flight"), isA(Attributes.class));
140-
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"),
141-
eq("tns:number"), isA(Attributes.class));
142-
ordered.verify(contentHandler).characters(eq(new char[] { '4', '2' }), eq(0), eq(2));
137+
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
138+
eq("flights"), eq("tns:flights"), isA(Attributes.class));
139+
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
140+
eq("flight"), eq("tns:flight"), isA(Attributes.class));
141+
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"),
142+
eq("number"), eq("tns:number"), isA(Attributes.class));
143+
ordered.verify(contentHandler).characters(eq(new char[]{'4', '2'}), eq(0), eq(2));
143144
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "tns:number");
144145
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
145146
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
@@ -170,7 +171,6 @@ public void testSuppressNamespacesFalse() throws Exception {
170171
@Test
171172
public void testSuppressXsiTypeTrue() throws Exception {
172173
CastorObject castorObject = createCastorObject();
173-
174174
getCastorMarshaller().setSuppressXsiType(true);
175175
getCastorMarshaller().setRootElement("objects");
176176
String result = marshal(Arrays.asList(castorObject));
@@ -180,7 +180,6 @@ public void testSuppressXsiTypeTrue() throws Exception {
180180
@Test
181181
public void testSuppressXsiTypeFalse() throws Exception {
182182
CastorObject castorObject = createCastorObject();
183-
184183
getCastorMarshaller().setSuppressXsiType(false);
185184
getCastorMarshaller().setRootElement("objects");
186185
String result = marshal(Arrays.asList(castorObject));
@@ -189,17 +188,15 @@ public void testSuppressXsiTypeFalse() throws Exception {
189188

190189
@Test
191190
public void testMarshalAsDocumentTrue() throws Exception {
192-
193191
getCastorMarshaller().setMarshalAsDocument(true);
194192
String result = marshalFlights();
195193
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
196194
assertTrue("Result doesn't contain xml declaration.",
197-
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
195+
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
198196
}
199197

200198
@Test
201199
public void testMarshalAsDocumentFalse() throws Exception {
202-
203200
getCastorMarshaller().setMarshalAsDocument(true);
204201
String result = marshalFlights();
205202
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
@@ -208,7 +205,6 @@ public void testMarshalAsDocumentFalse() throws Exception {
208205

209206
@Test
210207
public void testRootElement() throws Exception {
211-
212208
getCastorMarshaller().setRootElement("canceledFlights");
213209
String result = marshalFlights();
214210
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
@@ -217,31 +213,26 @@ public void testRootElement() throws Exception {
217213
@Test
218214
public void testNoNamespaceSchemaLocation() throws Exception {
219215
String noNamespaceSchemaLocation = "flights.xsd";
220-
221216
getCastorMarshaller().setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
222217
String result = marshalFlights();
223-
224218
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
225-
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
219+
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
226220
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
227221
}
228222

229223
@Test
230224
public void testSchemaLocation() throws Exception {
231225
String schemaLocation = "flights.xsd";
232-
233226
getCastorMarshaller().setSchemaLocation(schemaLocation);
234227
String result = marshalFlights();
235-
236228
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
237-
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
229+
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
238230
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
239231
}
240232

241233
@Test
242234
public void testUseXsiTypeAsRootTrue() throws Exception {
243235
CastorObject castorObject = createCastorObject();
244-
245236
getCastorMarshaller().setSuppressXsiType(false);
246237
getCastorMarshaller().setUseXSITypeAtRoot(true);
247238
getCastorMarshaller().setRootElement("objects");
@@ -252,24 +243,22 @@ public void testUseXsiTypeAsRootTrue() throws Exception {
252243
@Test
253244
public void testUseXsiTypeAsRootFalse() throws Exception {
254245
CastorObject castorObject = createCastorObject();
255-
256246
getCastorMarshaller().setSuppressXsiType(false);
257247
getCastorMarshaller().setUseXSITypeAtRoot(false);
258248
getCastorMarshaller().setRootElement("objects");
259249
String result = marshal(Arrays.asList(castorObject));
260250
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result);
261251
}
262252

253+
263254
private CastorMarshaller getCastorMarshaller() {
264255
return (CastorMarshaller) marshaller;
265256
}
266257

267258
private String marshal(Object object) throws Exception {
268-
269259
StringWriter writer = new StringWriter();
270260
StreamResult result = new StreamResult(writer);
271261
getCastorMarshaller().marshal(object, result);
272-
273262
return writer.toString();
274263
}
275264

@@ -278,7 +267,7 @@ private String marshalFlights() throws Exception {
278267
}
279268

280269
/**
281-
* Asserts the values of xpath expression evaluation is exactly the same as expected value.
270+
* Assert the values of xpath expression evaluation is exactly the same as expected value.
282271
* <p>The xpath may contain the xml namespace prefixes, since namespaces from flight example
283272
* are being registered.
284273
* @param msg the error message that will be used in case of test failure
@@ -302,7 +291,7 @@ private void assertXpathEvaluatesTo(String msg, String expected, String xpath, S
302291
}
303292

304293
/**
305-
* Creates a instance of {@link CastorObject} for testing.
294+
* Create an instance of {@link CastorObject} for testing.
306295
*/
307296
private CastorObject createCastorObject() {
308297
CastorObject castorObject = new CastorObject();

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ protected Unmarshaller createUnmarshaller() throws Exception {
8282
return marshaller;
8383
}
8484

85+
8586
@Test
8687
public void unmarshalTargetClass() throws Exception {
8788
CastorMarshaller unmarshaller = new CastorMarshaller();
@@ -137,7 +138,6 @@ public void testIgnoreExtraAttributesTrue() throws Exception {
137138

138139
@Test(expected = MarshallingException.class)
139140
public void testIgnoreExtraAttributesFalse() throws Exception {
140-
141141
getCastorUnmarshaller().setIgnoreExtraAttributes(false);
142142
unmarshal(EXTRA_ATTRIBUTES_STRING);
143143
}
@@ -153,7 +153,6 @@ public void testIgnoreExtraElementsTrue() throws Exception {
153153

154154
@Test(expected = MarshallingException.class)
155155
public void testIgnoreExtraElementsFalse() throws Exception {
156-
157156
getCastorUnmarshaller().setIgnoreExtraElements(false);
158157
unmarshal(EXTRA_ELEMENTS_STRING);
159158
}
@@ -181,7 +180,7 @@ public void testClearCollectionsTrue() throws Exception {
181180
}
182181

183182
@Test
184-
@Ignore("Fails on the builder server for some reason")
183+
@Ignore("Fails on the build server for some reason")
185184
public void testClearCollectionsFalse() throws Exception {
186185
Flights flights = new Flights();
187186
flights.setFlight(new Flight[]{new Flight(), null});
@@ -196,22 +195,8 @@ public void testClearCollectionsFalse() throws Exception {
196195
testFlight(flights.getFlight()[2]);
197196
}
198197

199-
private CastorMarshaller getCastorUnmarshaller() {
200-
return (CastorMarshaller) unmarshaller;
201-
}
202-
203-
private Object unmarshalFlights() throws Exception {
204-
return unmarshal(INPUT_STRING);
205-
}
206-
207-
private Object unmarshal(String xml) throws Exception {
208-
StreamSource source = new StreamSource(new StringReader(xml));
209-
return unmarshaller.unmarshal(source);
210-
}
211-
212198
@Test
213199
public void unmarshalStreamSourceExternalEntities() throws Exception {
214-
215200
final AtomicReference<XMLReader> result = new AtomicReference<XMLReader>();
216201
CastorMarshaller marshaller = new CastorMarshaller() {
217202
@Override
@@ -222,13 +207,11 @@ protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource
222207
};
223208

224209
// 1. external-general-entities disabled (default)
225-
226210
marshaller.unmarshal(new StreamSource("1"));
227211
assertNotNull(result.get());
228212
assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
229213

230214
// 2. external-general-entities disabled (default)
231-
232215
result.set(null);
233216
marshaller.setProcessExternalEntities(true);
234217
marshaller.unmarshal(new StreamSource("1"));
@@ -260,4 +243,18 @@ protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource
260243
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
261244
}
262245

246+
247+
private CastorMarshaller getCastorUnmarshaller() {
248+
return (CastorMarshaller) unmarshaller;
249+
}
250+
251+
private Object unmarshalFlights() throws Exception {
252+
return unmarshal(INPUT_STRING);
253+
}
254+
255+
private Object unmarshal(String xml) throws Exception {
256+
StreamSource source = new StreamSource(new StringReader(xml));
257+
return unmarshaller.unmarshal(source);
258+
}
259+
263260
}

0 commit comments

Comments
 (0)