Skip to content

Commit

Permalink
XML parsing tests pass on non-English locales now, plus a revised exc…
Browse files Browse the repository at this point in the history
…eption message and some minor polishing

Issue: SPR-13136
  • Loading branch information
jhoeller committed Jun 30, 2015
1 parent 9c3580d commit 38b8262
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.xml.XMLConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,6 +33,13 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {

private Flights flights;


@Override
public Marshaller createMarshaller() throws Exception {
marshaller = new Jaxb2Marshaller();
Expand All @@ -91,6 +92,7 @@ protected Object createFlights() {
return flights;
}


@Test
public void marshalSAXResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -205,7 +205,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLResolver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.io.StringReader;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.MarshalException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +37,8 @@
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;

import static org.junit.Assert.*;

/**
* Test fixture for {@link Jaxb2CollectionHttpMessageConverter}.
*
Expand Down Expand Up @@ -71,6 +70,7 @@ public void setUp() {
typeSetType = new ParameterizedTypeReference<Set<TestType>>() {}.getType();
}


@Test
public void canRead() throws Exception {
assertTrue(converter.canRead(rootElementListType, null, null));
Expand Down Expand Up @@ -206,6 +206,7 @@ public void testXmlBomb() throws Exception {
this.converter.read(this.rootElementListType, null, inputMessage);
}


@XmlRootElement
public static class RootElement {

Expand Down Expand Up @@ -240,6 +241,7 @@ public int hashCode() {
}
}


@XmlType
public static class TestType {

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -47,6 +41,11 @@
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}.
*
Expand Down Expand Up @@ -78,6 +77,7 @@ public void setUp() {
rootElementCglib = (RootElement) proxy.getProxy();
}


@Test
public void canRead() throws Exception {
assertTrue("Converter does not support reading @XmlRootElement", converter.canRead(RootElement.class, null));
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testXmlBomb() throws Exception {
"<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);
}

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


@XmlRootElement
public static class RootElement {

Expand All @@ -228,6 +229,7 @@ public void setType(Type type) {
}
}


@XmlType
public static class Type {

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

}

public static class RootElementSubclass extends RootElement {

public static class RootElementSubclass extends RootElement {
}


public static class MyJaxb2RootElementHttpMessageConverter extends Jaxb2RootElementHttpMessageConverter {

@Override
Expand All @@ -253,8 +256,11 @@ protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
}
}


public static class MyCustomElement {

private String field1;

private String field2;

public MyCustomElement() {
Expand Down Expand Up @@ -282,6 +288,7 @@ public void setField2(String field2) {
}
}


@XmlRootElement
public static class MyRootElement {

Expand All @@ -305,6 +312,7 @@ public void setElement(MyCustomElement element) {
}
}


public static class MyCustomElementAdapter extends XmlAdapter<String, MyCustomElement> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,10 +26,8 @@
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;
Expand All @@ -45,6 +43,7 @@
* Jackson 2.x XML converter tests.
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
*/
public class MappingJackson2XmlHttpMessageConverterTests {

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

@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
Expand Down Expand Up @@ -149,7 +147,6 @@ public void customXmlMapper() {

@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 \"" +
Expand All @@ -160,14 +157,11 @@ public void readWithExternalReference() throws IOException {
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" +
Expand All @@ -190,15 +184,11 @@ public void readWithXmlBomb() throws IOException {
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);
Expand Down Expand Up @@ -269,9 +259,12 @@ public void setArray(String[] array) {
}
}


private interface MyJacksonView1 {};

private interface MyJacksonView2 {};


@SuppressWarnings("unused")
private static class JacksonViewBean {

Expand Down Expand Up @@ -308,6 +301,7 @@ public void setWithoutView(String withoutView) {
}
}


@SuppressWarnings("serial")
private static class MyXmlMapper extends XmlMapper {
}
Expand Down
Loading

0 comments on commit 38b8262

Please sign in to comment.