Skip to content

Commit 0411435

Browse files
committed
XML parsing tests pass on non-English locales now, plus a revised exception message and some minor polishing
Issue: SPR-13136 (cherry picked from commit 38b8262)
1 parent d79ec68 commit 0411435

File tree

11 files changed

+45
-49
lines changed

11 files changed

+45
-49
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Date;
3838
import java.util.Map;
3939
import java.util.UUID;
40-
4140
import javax.activation.DataHandler;
4241
import javax.activation.DataSource;
4342
import javax.xml.XMLConstants;

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616

1717
package org.springframework.oxm.castor;
1818

19-
import static junit.framework.Assert.assertEquals;
20-
import static org.hamcrest.CoreMatchers.*;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertSame;
24-
import static org.junit.Assert.*;
25-
2619
import java.io.ByteArrayInputStream;
2720
import java.io.IOException;
2821
import java.io.StringReader;
2922
import java.util.concurrent.atomic.AtomicReference;
30-
3123
import javax.xml.transform.sax.SAXSource;
3224
import javax.xml.transform.stream.StreamSource;
3325

@@ -41,6 +33,9 @@
4133
import org.springframework.oxm.MarshallingException;
4234
import org.springframework.oxm.Unmarshaller;
4335

36+
import static org.hamcrest.CoreMatchers.*;
37+
import static org.junit.Assert.*;
38+
4439
/**
4540
* @author Arjen Poutsma
4641
* @author Jakub Narloch

spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
7474

7575
private Flights flights;
7676

77+
7778
@Override
7879
public Marshaller createMarshaller() throws Exception {
7980
marshaller = new Jaxb2Marshaller();
@@ -91,6 +92,7 @@ protected Object createFlights() {
9192
return flights;
9293
}
9394

95+
9496
@Test
9597
public void marshalSAXResult() throws Exception {
9698
ContentHandler contentHandler = mock(ContentHandler.class);

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
221221
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
222222
}
223223
catch (IOException ex) {
224-
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
224+
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
225225
}
226226
}
227227

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Locale;
2828
import java.util.Map;
2929
import java.util.TimeZone;
30-
3130
import javax.xml.stream.XMLInputFactory;
3231
import javax.xml.stream.XMLResolver;
3332

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.StringReader;
21-
2221
import javax.xml.bind.JAXBElement;
2322
import javax.xml.bind.JAXBException;
2423
import javax.xml.bind.MarshalException;

spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.StringReader;
2424
import java.util.HashSet;
2525
import java.util.Set;
26-
2726
import javax.xml.parsers.DocumentBuilder;
2827
import javax.xml.parsers.DocumentBuilderFactory;
2928
import javax.xml.parsers.ParserConfigurationException;

spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
package org.springframework.http.converter.xml;
1818

19-
import static org.junit.Assert.*;
20-
2119
import java.lang.reflect.Type;
2220
import java.util.Collection;
2321
import java.util.List;
2422
import java.util.Set;
25-
2623
import javax.xml.bind.annotation.XmlAttribute;
2724
import javax.xml.bind.annotation.XmlElement;
2825
import javax.xml.bind.annotation.XmlRootElement;
@@ -40,6 +37,8 @@
4037
import org.springframework.http.MockHttpInputMessage;
4138
import org.springframework.http.converter.HttpMessageNotReadableException;
4239

40+
import static org.junit.Assert.*;
41+
4342
/**
4443
* Test fixture for {@link Jaxb2CollectionHttpMessageConverter}.
4544
*
@@ -71,6 +70,7 @@ public void setUp() {
7170
typeSetType = new ParameterizedTypeReference<Set<TestType>>() {}.getType();
7271
}
7372

73+
7474
@Test
7575
public void canRead() throws Exception {
7676
assertTrue(converter.canRead(rootElementListType, null, null));
@@ -206,6 +206,7 @@ public void testXmlBomb() throws Exception {
206206
this.converter.read(this.rootElementListType, null, inputMessage);
207207
}
208208

209+
209210
@XmlRootElement
210211
public static class RootElement {
211212

@@ -240,6 +241,7 @@ public int hashCode() {
240241
}
241242
}
242243

244+
243245
@XmlType
244246
public static class TestType {
245247

spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,7 @@
1616

1717
package org.springframework.http.converter.xml;
1818

19-
import static org.custommonkey.xmlunit.XMLAssert.*;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertTrue;
23-
2419
import java.nio.charset.Charset;
25-
2620
import javax.xml.bind.Marshaller;
2721
import javax.xml.bind.Unmarshaller;
2822
import javax.xml.bind.annotation.XmlAttribute;
@@ -47,6 +41,11 @@
4741
import org.springframework.http.MockHttpOutputMessage;
4842
import org.springframework.http.converter.HttpMessageNotReadableException;
4943

44+
import static org.custommonkey.xmlunit.XMLAssert.*;
45+
import static org.junit.Assert.assertEquals;
46+
import static org.junit.Assert.assertFalse;
47+
import static org.junit.Assert.assertTrue;
48+
5049
/**
5150
* Tests for {@link Jaxb2RootElementHttpMessageConverter}.
5251
*
@@ -78,6 +77,7 @@ public void setUp() {
7877
rootElementCglib = (RootElement) proxy.getProxy();
7978
}
8079

80+
8181
@Test
8282
public void canRead() throws Exception {
8383
assertTrue("Converter does not support reading @XmlRootElement", converter.canRead(RootElement.class, null));
@@ -165,7 +165,7 @@ public void testXmlBomb() throws Exception {
165165
"<rootElement><external>&lol9;</external></rootElement>";
166166
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
167167
this.thrown.expect(HttpMessageNotReadableException.class);
168-
this.thrown.expectMessage("DOCTYPE is disallowed");
168+
this.thrown.expectMessage("DOCTYPE");
169169
this.converter.read(RootElement.class, inputMessage);
170170
}
171171

@@ -210,6 +210,7 @@ public void customizeUnmarshaller() throws Exception {
210210
assertEquals("b", result.getElement().getField2());
211211
}
212212

213+
213214
@XmlRootElement
214215
public static class RootElement {
215216

@@ -228,6 +229,7 @@ public void setType(Type type) {
228229
}
229230
}
230231

232+
231233
@XmlType
232234
public static class Type {
233235

@@ -236,10 +238,11 @@ public static class Type {
236238

237239
}
238240

239-
public static class RootElementSubclass extends RootElement {
240241

242+
public static class RootElementSubclass extends RootElement {
241243
}
242244

245+
243246
public static class MyJaxb2RootElementHttpMessageConverter extends Jaxb2RootElementHttpMessageConverter {
244247

245248
@Override
@@ -253,8 +256,11 @@ protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
253256
}
254257
}
255258

259+
256260
public static class MyCustomElement {
261+
257262
private String field1;
263+
258264
private String field2;
259265

260266
public MyCustomElement() {
@@ -282,6 +288,7 @@ public void setField2(String field2) {
282288
}
283289
}
284290

291+
285292
@XmlRootElement
286293
public static class MyRootElement {
287294

@@ -305,6 +312,7 @@ public void setElement(MyCustomElement element) {
305312
}
306313
}
307314

315+
308316
public static class MyCustomElementAdapter extends XmlAdapter<String, MyCustomElement> {
309317

310318
@Override

spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.http.converter.xml;
1818

1919
import java.io.IOException;
20-
import java.lang.reflect.InvocationTargetException;
2120
import java.lang.reflect.Method;
2221
import java.nio.charset.Charset;
2322

@@ -26,10 +25,8 @@
2625
import org.junit.Rule;
2726
import org.junit.Test;
2827
import org.junit.rules.ExpectedException;
29-
import org.xml.sax.SAXException;
3028

3129
import org.springframework.core.io.ClassPathResource;
32-
import org.springframework.core.io.Resource;
3330
import org.springframework.http.HttpOutputMessage;
3431
import org.springframework.http.MediaType;
3532
import org.springframework.http.MockHttpInputMessage;
@@ -45,6 +42,7 @@
4542
* Jackson 2.x XML converter tests.
4643
*
4744
* @author Sebastien Deleuze
45+
* @author Rossen Stoyanchev
4846
*/
4947
public class MappingJackson2XmlHttpMessageConverterTests {
5048

@@ -70,17 +68,16 @@ public void canWrite() {
7068

7169
@Test
7270
public void read() throws IOException {
73-
String body =
74-
"<MyBean><string>Foo</string><number>42</number><fraction>42.0</fraction><array><array>Foo</array><array>Bar</array></array><bool>true</bool><bytes>AQI=</bytes></MyBean>";
71+
String body = "<MyBean><string>Foo</string><number>42</number><fraction>42.0</fraction><array><array>Foo</array><array>Bar</array></array><bool>true</bool><bytes>AQI=</bytes></MyBean>";
7572
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
7673
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
7774
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
7875
assertEquals("Foo", result.getString());
7976
assertEquals(42, result.getNumber());
8077
assertEquals(42F, result.getFraction(), 0F);
81-
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
78+
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray());
8279
assertTrue(result.isBool());
83-
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());
80+
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
8481
}
8582

8683
@Test
@@ -149,7 +146,6 @@ public void customXmlMapper() {
149146

150147
@Test
151148
public void readWithExternalReference() throws IOException {
152-
153149
String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" +
154150
" <!ELEMENT root ANY >\n" +
155151
" <!ENTITY ext SYSTEM \"" +
@@ -160,14 +156,11 @@ public void readWithExternalReference() throws IOException {
160156
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
161157

162158
this.thrown.expect(HttpMessageNotReadableException.class);
163-
this.thrown.expectMessage("entity \"ext\"");
164-
165159
this.converter.read(MyBean.class, inputMessage);
166160
}
167161

168162
@Test
169163
public void readWithXmlBomb() throws IOException {
170-
171164
// https://en.wikipedia.org/wiki/Billion_laughs
172165
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
173166
String body = "<?xml version=\"1.0\"?>\n" +
@@ -190,15 +183,11 @@ public void readWithXmlBomb() throws IOException {
190183
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
191184

192185
this.thrown.expect(HttpMessageNotReadableException.class);
193-
this.thrown.expectMessage("entity \"lol9\"");
194-
195186
this.converter.read(MyBean.class, inputMessage);
196187
}
197188

198189

199-
private void writeInternal(Object object, HttpOutputMessage outputMessage)
200-
throws NoSuchMethodException, InvocationTargetException,
201-
IllegalAccessException {
190+
private void writeInternal(Object object, HttpOutputMessage outputMessage) throws Exception {
202191
Method method = AbstractHttpMessageConverter.class.getDeclaredMethod(
203192
"writeInternal", Object.class, HttpOutputMessage.class);
204193
method.setAccessible(true);
@@ -269,9 +258,12 @@ public void setArray(String[] array) {
269258
}
270259
}
271260

261+
272262
private interface MyJacksonView1 {};
263+
273264
private interface MyJacksonView2 {};
274265

266+
275267
@SuppressWarnings("unused")
276268
private static class JacksonViewBean {
277269

@@ -308,6 +300,7 @@ public void setWithoutView(String withoutView) {
308300
}
309301
}
310302

303+
311304
@SuppressWarnings("serial")
312305
private static class MyXmlMapper extends XmlMapper {
313306
}

0 commit comments

Comments
 (0)