XML parsing tests pass on non-English locales now, plus a revised exception message and some minor polishing

Issue: SPR-13136
This commit is contained in:
Juergen Hoeller 2015-06-30 16:02:17 +02:00
parent 9c3580d04e
commit 38b8262e1e
11 changed files with 51 additions and 50 deletions

View File

@ -37,7 +37,6 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.xml.XMLConstants;

View File

@ -16,18 +16,10 @@
package org.springframework.oxm.castor;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.concurrent.atomic.AtomicReference;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
@ -41,6 +33,13 @@ import org.springframework.oxm.AbstractUnmarshallerTests;
import org.springframework.oxm.MarshallingException;
import org.springframework.oxm.Unmarshaller;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Jakub Narloch

View File

@ -74,6 +74,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
private Flights flights;
@Override
public Marshaller createMarshaller() throws Exception {
marshaller = new Jaxb2Marshaller();
@ -91,6 +92,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
return flights;
}
@Test
public void marshalSAXResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -205,7 +205,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractHttpM
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
}
}

View File

@ -27,7 +27,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLResolver;

View File

@ -18,7 +18,6 @@ package org.springframework.http.converter.xml;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.MarshalException;

View File

@ -23,7 +23,6 @@ import java.io.OutputStream;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

View File

@ -16,13 +16,10 @@
package org.springframework.http.converter.xml;
import static org.junit.Assert.*;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@ -40,6 +37,8 @@ import org.springframework.core.io.Resource;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import static org.junit.Assert.*;
/**
* Test fixture for {@link Jaxb2CollectionHttpMessageConverter}.
*
@ -71,6 +70,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
typeSetType = new ParameterizedTypeReference<Set<TestType>>() {}.getType();
}
@Test
public void canRead() throws Exception {
assertTrue(converter.canRead(rootElementListType, null, null));
@ -206,6 +206,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
this.converter.read(this.rootElementListType, null, inputMessage);
}
@XmlRootElement
public static class RootElement {
@ -240,6 +241,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
}
}
@XmlType
public static class TestType {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,13 +16,7 @@
package org.springframework.http.converter.xml;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.nio.charset.Charset;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
@ -47,6 +41,11 @@ import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link Jaxb2RootElementHttpMessageConverter}.
*
@ -78,6 +77,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
rootElementCglib = (RootElement) proxy.getProxy();
}
@Test
public void canRead() throws Exception {
assertTrue("Converter does not support reading @XmlRootElement", converter.canRead(RootElement.class, null));
@ -165,7 +165,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
"<rootElement><external>&lol9;</external></rootElement>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
this.thrown.expect(HttpMessageNotReadableException.class);
this.thrown.expectMessage("DOCTYPE is disallowed");
this.thrown.expectMessage("DOCTYPE");
this.converter.read(RootElement.class, inputMessage);
}
@ -210,6 +210,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
assertEquals("b", result.getElement().getField2());
}
@XmlRootElement
public static class RootElement {
@ -228,6 +229,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
}
}
@XmlType
public static class Type {
@ -236,10 +238,11 @@ public class Jaxb2RootElementHttpMessageConverterTests {
}
public static class RootElementSubclass extends RootElement {
public static class RootElementSubclass extends RootElement {
}
public static class MyJaxb2RootElementHttpMessageConverter extends Jaxb2RootElementHttpMessageConverter {
@Override
@ -253,8 +256,11 @@ public class Jaxb2RootElementHttpMessageConverterTests {
}
}
public static class MyCustomElement {
private String field1;
private String field2;
public MyCustomElement() {
@ -282,6 +288,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
}
}
@XmlRootElement
public static class MyRootElement {
@ -305,6 +312,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
}
}
public static class MyCustomElementAdapter extends XmlAdapter<String, MyCustomElement> {
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,10 +26,8 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.xml.sax.SAXException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
@ -45,6 +43,7 @@ import static org.junit.Assert.*;
* Jackson 2.x XML converter tests.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
*/
public class MappingJackson2XmlHttpMessageConverterTests {
@ -70,17 +69,16 @@ public class MappingJackson2XmlHttpMessageConverterTests {
@Test
public void read() throws IOException {
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>";
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>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
assertEquals("Foo", result.getString());
assertEquals(42, result.getNumber());
assertEquals(42F, result.getFraction(), 0F);
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray());
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray());
assertTrue(result.isBool());
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes());
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
}
@Test
@ -149,7 +147,6 @@ public class MappingJackson2XmlHttpMessageConverterTests {
@Test
public void readWithExternalReference() throws IOException {
String body = "<!DOCTYPE MyBean SYSTEM \"http://192.168.28.42/1.jsp\" [" +
" <!ELEMENT root ANY >\n" +
" <!ENTITY ext SYSTEM \"" +
@ -160,14 +157,11 @@ public class MappingJackson2XmlHttpMessageConverterTests {
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
this.thrown.expect(HttpMessageNotReadableException.class);
this.thrown.expectMessage("entity \"ext\"");
this.converter.read(MyBean.class, inputMessage);
}
@Test
public void readWithXmlBomb() throws IOException {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String body = "<?xml version=\"1.0\"?>\n" +
@ -190,15 +184,11 @@ public class MappingJackson2XmlHttpMessageConverterTests {
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
this.thrown.expect(HttpMessageNotReadableException.class);
this.thrown.expectMessage("entity \"lol9\"");
this.converter.read(MyBean.class, inputMessage);
}
private void writeInternal(Object object, HttpOutputMessage outputMessage)
throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
private void writeInternal(Object object, HttpOutputMessage outputMessage) throws Exception {
Method method = AbstractJackson2HttpMessageConverter.class.getDeclaredMethod(
"writeInternal", Object.class, HttpOutputMessage.class);
method.setAccessible(true);
@ -269,9 +259,12 @@ public class MappingJackson2XmlHttpMessageConverterTests {
}
}
private interface MyJacksonView1 {};
private interface MyJacksonView2 {};
@SuppressWarnings("unused")
private static class JacksonViewBean {
@ -308,6 +301,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
}
}
@SuppressWarnings("serial")
private static class MyXmlMapper extends XmlMapper {
}

View File

@ -16,16 +16,10 @@
package org.springframework.http.converter.xml;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.Charset;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
@ -53,6 +47,11 @@ import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.FileCopyUtils;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
// Do NOT statically import org.junit.Assert.*, since XMLAssert extends junit.framework.Assert
/**
@ -80,6 +79,7 @@ public class SourceHttpMessageConverterTests {
" <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>";
}
@Test
public void canRead() {
assertTrue(converter.canRead(Source.class, new MediaType("application", "xml")));
@ -135,7 +135,7 @@ public class SourceHttpMessageConverterTests {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
this.thrown.expect(HttpMessageNotReadableException.class);
this.thrown.expectMessage("DOCTYPE is disallowed");
this.thrown.expectMessage("DOCTYPE");
this.converter.read(DOMSource.class, inputMessage);
}
@ -192,7 +192,7 @@ public class SourceHttpMessageConverterTests {
SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);
this.thrown.expect(SAXException.class);
this.thrown.expectMessage("DOCTYPE is disallowed");
this.thrown.expectMessage("DOCTYPE");
InputSource inputSource = result.getInputSource();
XMLReader reader = result.getXMLReader();