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 38b8262e1e)
This commit is contained in:
@@ -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 {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@@ -26,10 +25,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 +42,7 @@ import static org.junit.Assert.*;
|
||||
* Jackson 2.x XML converter tests.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
|
||||
@@ -70,17 +68,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 +146,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 +156,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 +183,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 = AbstractHttpMessageConverter.class.getDeclaredMethod(
|
||||
"writeInternal", Object.class, HttpOutputMessage.class);
|
||||
method.setAccessible(true);
|
||||
@@ -269,9 +258,12 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private interface MyJacksonView1 {};
|
||||
|
||||
private interface MyJacksonView2 {};
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class JacksonViewBean {
|
||||
|
||||
@@ -308,6 +300,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class MyXmlMapper extends XmlMapper {
|
||||
}
|
||||
|
||||
@@ -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.assertNotEquals;
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user