Skip to content

Commit 38b8262

Browse files
committed
XML parsing tests pass on non-English locales now, plus a revised exception message and some minor polishing
Issue: SPR-13136
1 parent 9c3580d commit 38b8262

File tree

11 files changed

+51
-50
lines changed

11 files changed

+51
-50
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: 7 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,13 @@
4133
import org.springframework.oxm.MarshallingException;
4234
import org.springframework.oxm.Unmarshaller;
4335

36+
import static junit.framework.Assert.assertEquals;
37+
import static org.hamcrest.CoreMatchers.*;
38+
import static org.junit.Assert.assertNotNull;
39+
import static org.junit.Assert.assertNull;
40+
import static org.junit.Assert.assertSame;
41+
import static org.junit.Assert.*;
42+
4443
/**
4544
* @author Arjen Poutsma
4645
* @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: 2 additions & 2 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.
@@ -205,7 +205,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
205205
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
206206
}
207207
catch (IOException ex) {
208-
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
208+
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
209209
}
210210
}
211211

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: 10 additions & 16 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.
@@ -26,10 +26,8 @@
2626
import org.junit.Rule;
2727
import org.junit.Test;
2828
import org.junit.rules.ExpectedException;
29-
import org.xml.sax.SAXException;
3029

3130
import org.springframework.core.io.ClassPathResource;
32-
import org.springframework.core.io.Resource;
3331
import org.springframework.http.HttpOutputMessage;
3432
import org.springframework.http.MediaType;
3533
import org.springframework.http.MockHttpInputMessage;
@@ -45,6 +43,7 @@
4543
* Jackson 2.x XML converter tests.
4644
*
4745
* @author Sebastien Deleuze
46+
* @author Rossen Stoyanchev
4847
*/
4948
public class MappingJackson2XmlHttpMessageConverterTests {
5049

@@ -70,17 +69,16 @@ public void canWrite() {
7069

7170
@Test
7271
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>";
72+
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>";
7573
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
7674
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
7775
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
7876
assertEquals("Foo", result.getString());
7977
assertEquals(42, result.getNumber());
8078
assertEquals(42F, result.getFraction(), 0F);
81-
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
79+
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray());
8280
assertTrue(result.isBool());
83-
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());
81+
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
8482
}
8583

8684
@Test
@@ -149,7 +147,6 @@ public void customXmlMapper() {
149147

150148
@Test
151149
public void readWithExternalReference() throws IOException {
152-
153150
String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" +
154151
" <!ELEMENT root ANY >\n" +
155152
" <!ENTITY ext SYSTEM \"" +
@@ -160,14 +157,11 @@ public void readWithExternalReference() throws IOException {
160157
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
161158

162159
this.thrown.expect(HttpMessageNotReadableException.class);
163-
this.thrown.expectMessage("entity \"ext\"");
164-
165160
this.converter.read(MyBean.class, inputMessage);
166161
}
167162

168163
@Test
169164
public void readWithXmlBomb() throws IOException {
170-
171165
// https://en.wikipedia.org/wiki/Billion_laughs
172166
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
173167
String body = "<?xml version=\"1.0\"?>\n" +
@@ -190,15 +184,11 @@ public void readWithXmlBomb() throws IOException {
190184
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
191185

192186
this.thrown.expect(HttpMessageNotReadableException.class);
193-
this.thrown.expectMessage("entity \"lol9\"");
194-
195187
this.converter.read(MyBean.class, inputMessage);
196188
}
197189

198190

199-
private void writeInternal(Object object, HttpOutputMessage outputMessage)
200-
throws NoSuchMethodException, InvocationTargetException,
201-
IllegalAccessException {
191+
private void writeInternal(Object object, HttpOutputMessage outputMessage) throws Exception {
202192
Method method = AbstractJackson2HttpMessageConverter.class.getDeclaredMethod(
203193
"writeInternal", Object.class, HttpOutputMessage.class);
204194
method.setAccessible(true);
@@ -269,9 +259,12 @@ public void setArray(String[] array) {
269259
}
270260
}
271261

262+
272263
private interface MyJacksonView1 {};
264+
273265
private interface MyJacksonView2 {};
274266

267+
275268
@SuppressWarnings("unused")
276269
private static class JacksonViewBean {
277270

@@ -308,6 +301,7 @@ public void setWithoutView(String withoutView) {
308301
}
309302
}
310303

304+
311305
@SuppressWarnings("serial")
312306
private static class MyXmlMapper extends XmlMapper {
313307
}

0 commit comments

Comments
 (0)