Stop using Mockito to spy() on JDK I/O streams
When running on JDK 16+, we are not able to spy() on JDK types. To address this, this commit stops using Mockito to spy on JDK I/O streams (such as ByteArrayInputStream and ByteArrayOutputStream).
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -21,8 +21,6 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -31,7 +29,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final ByteArrayOutputStream body = spy(new ByteArrayOutputStream());
|
||||
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
|
||||
|
||||
private boolean headersWritten = false;
|
||||
|
||||
|
||||
@@ -19,11 +19,9 @@ package org.springframework.http.converter;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -31,56 +29,47 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Unit tests for BufferedImageHttpMessageConverter.
|
||||
* Unit tests for {@link BufferedImageHttpMessageConverter}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class BufferedImageHttpMessageConverterTests {
|
||||
class BufferedImageHttpMessageConverterTests {
|
||||
|
||||
private BufferedImageHttpMessageConverter converter;
|
||||
private final BufferedImageHttpMessageConverter converter = new BufferedImageHttpMessageConverter();
|
||||
|
||||
private final Resource logo = new ClassPathResource("logo.jpg", getClass());
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
converter = new BufferedImageHttpMessageConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(BufferedImage.class, null)).as("Image not supported").isTrue();
|
||||
assertThat(converter.canRead(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(BufferedImage.class, null)).as("Image not supported").isTrue();
|
||||
assertThat(converter.canWrite(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
|
||||
assertThat(converter.canWrite(BufferedImage.class, new MediaType("*", "*"))).as("Image not supported").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
|
||||
byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
void read() throws IOException {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(logo.getInputStream());
|
||||
inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg"));
|
||||
BufferedImage result = converter.read(BufferedImage.class, inputMessage);
|
||||
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
|
||||
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
|
||||
void write() throws IOException {
|
||||
BufferedImage body = ImageIO.read(logo.getFile());
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = new MediaType("image", "png");
|
||||
@@ -90,12 +79,10 @@ public class BufferedImageHttpMessageConverterTests {
|
||||
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
|
||||
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
|
||||
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeDefaultContentType() throws IOException {
|
||||
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
|
||||
void writeDefaultContentType() throws IOException {
|
||||
MediaType contentType = new MediaType("image", "png");
|
||||
converter.setDefaultContentType(contentType);
|
||||
BufferedImage body = ImageIO.read(logo.getFile());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -48,8 +48,6 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
|
||||
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
|
||||
import static org.springframework.http.MediaType.MULTIPART_MIXED;
|
||||
@@ -229,7 +227,6 @@ public class FormHttpMessageConverterTests {
|
||||
item = items.get(5);
|
||||
assertThat(item.getFieldName()).isEqualTo("xml");
|
||||
assertThat(item.getContentType()).isEqualTo("text/xml");
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test // SPR-13309
|
||||
|
||||
@@ -38,9 +38,6 @@ import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -74,7 +71,7 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream inputStream = spy(getClass().getResourceAsStream("atom.xml"));
|
||||
InputStream inputStream = getClass().getResourceAsStream("atom.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
|
||||
Feed result = converter.read(Feed.class, inputMessage);
|
||||
@@ -90,7 +87,6 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
Entry entry2 = (Entry) entries.get(1);
|
||||
assertThat(entry2.getId()).isEqualTo("id2");
|
||||
assertThat(entry2.getTitle()).isEqualTo("title2");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,7 +119,6 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
|
||||
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
|
||||
.isSimilarToIgnoringWhitespace(expected, nm);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -34,9 +34,6 @@ import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -59,7 +56,7 @@ public class RssChannelHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream inputStream = spy(getClass().getResourceAsStream("rss.xml"));
|
||||
InputStream inputStream = getClass().getResourceAsStream("rss.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
|
||||
Channel result = converter.read(Channel.class, inputMessage);
|
||||
@@ -75,7 +72,6 @@ public class RssChannelHttpMessageConverterTests {
|
||||
|
||||
Item item2 = (Item) items.get(1);
|
||||
assertThat(item2.getTitle()).isEqualTo("title2");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,7 +105,6 @@ public class RssChannelHttpMessageConverterTests {
|
||||
"</channel></rss>";
|
||||
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
|
||||
.isSimilarToIgnoringWhitespace(expected);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -40,9 +38,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Gson 2.x converter tests.
|
||||
@@ -50,35 +45,34 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Roy Clarkson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class GsonHttpMessageConverterTests {
|
||||
class GsonHttpMessageConverterTests {
|
||||
|
||||
private final GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(this.converter.canRead(Map.class, new MediaType("application", "json"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(this.converter.canWrite(Map.class, new MediaType("application", "json"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canReadAndWriteMicroformats() {
|
||||
void canReadAndWriteMicroformats() {
|
||||
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
|
||||
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readTyped() throws IOException {
|
||||
void readTyped() throws IOException {
|
||||
String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage);
|
||||
|
||||
@@ -89,12 +83,11 @@ public class GsonHttpMessageConverterTests {
|
||||
assertThat(result.getArray()).isEqualTo(new String[] {"Foo", "Bar"});
|
||||
assertThat(result.isBool()).isTrue();
|
||||
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readUntyped() throws IOException {
|
||||
void readUntyped() throws IOException {
|
||||
String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
@@ -120,7 +113,7 @@ public class GsonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
void write() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -140,11 +133,10 @@ public class GsonHttpMessageConverterTests {
|
||||
assertThat(result.contains("\"bytes\":[1,2]")).isTrue();
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWithBaseType() throws IOException {
|
||||
void writeWithBaseType() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -167,7 +159,7 @@ public class GsonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeUTF16() throws IOException {
|
||||
void writeUTF16() throws IOException {
|
||||
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
String body = "H\u00e9llo W\u00f6rld";
|
||||
@@ -177,7 +169,7 @@ public class GsonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readInvalidJson() throws IOException {
|
||||
void readInvalidJson() throws IOException {
|
||||
String body = "FooBar";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
@@ -187,7 +179,7 @@ public class GsonHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readAndWriteGenerics() throws Exception {
|
||||
void readAndWriteGenerics() throws Exception {
|
||||
Field beansList = ListHolder.class.getField("listField");
|
||||
|
||||
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
@@ -214,7 +206,7 @@ public class GsonHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readAndWriteParameterizedType() throws Exception {
|
||||
void readAndWriteParameterizedType() throws Exception {
|
||||
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
|
||||
};
|
||||
|
||||
@@ -241,7 +233,7 @@ public class GsonHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeParameterizedBaseType() throws Exception {
|
||||
void writeParameterizedBaseType() throws Exception {
|
||||
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
|
||||
ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {};
|
||||
|
||||
@@ -267,7 +259,7 @@ public class GsonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixJson() throws IOException {
|
||||
void prefixJson() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setPrefixJson(true);
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
@@ -275,7 +267,7 @@ public class GsonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixJsonCustom() throws IOException {
|
||||
void prefixJsonCustom() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setJsonPrefix(")))");
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
|
||||
@@ -40,9 +40,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Integration tests for the JSON Binding API, running against Apache Johnzon.
|
||||
@@ -77,7 +74,7 @@ public class JsonbHttpMessageConverterTests {
|
||||
public void readTyped() throws IOException {
|
||||
String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
|
||||
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
InputStream inputStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage);
|
||||
@@ -89,7 +86,6 @@ public class JsonbHttpMessageConverterTests {
|
||||
assertThat(result.getArray()).isEqualTo(new String[] {"Foo", "Bar"});
|
||||
assertThat(result.isBool()).isTrue();
|
||||
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -140,7 +136,6 @@ public class JsonbHttpMessageConverterTests {
|
||||
assertThat(result.contains("\"bytes\":[1,2]")).isTrue();
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -49,9 +47,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Jackson 2.x converter tests.
|
||||
@@ -60,7 +55,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Sebastien Deleuze
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class MappingJackson2HttpMessageConverterTests {
|
||||
class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
protected static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator();
|
||||
|
||||
@@ -68,7 +63,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(converter.canRead(Map.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
|
||||
@@ -77,7 +72,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canReadWithObjectMapperRegistrationForType() {
|
||||
void canReadWithObjectMapperRegistrationForType() {
|
||||
MediaType halJsonMediaType = MediaType.parseMediaType("application/hal+json");
|
||||
MediaType halFormsJsonMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
|
||||
|
||||
@@ -98,7 +93,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(converter.canWrite(Map.class, new MediaType("application", "json"))).isTrue();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
|
||||
@@ -107,13 +102,13 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test // SPR-7905
|
||||
public void canReadAndWriteMicroformats() {
|
||||
void canReadAndWriteMicroformats() {
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSupportedMediaTypes() {
|
||||
void getSupportedMediaTypes() {
|
||||
MediaType[] defaultMediaTypes = {MediaType.APPLICATION_JSON, MediaType.parseMediaType("application/*+json")};
|
||||
assertThat(converter.getSupportedMediaTypes()).containsExactly(defaultMediaTypes);
|
||||
assertThat(converter.getSupportedMediaTypes(MyBean.class)).containsExactly(defaultMediaTypes);
|
||||
@@ -129,7 +124,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readTyped() throws IOException {
|
||||
void readTyped() throws IOException {
|
||||
String body = "{" +
|
||||
"\"bytes\":\"AQI=\"," +
|
||||
"\"array\":[\"Foo\",\"Bar\"]," +
|
||||
@@ -137,8 +132,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
"\"string\":\"Foo\"," +
|
||||
"\"bool\":true," +
|
||||
"\"fraction\":42.0}";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
|
||||
assertThat(result.getString()).isEqualTo("Foo");
|
||||
@@ -147,12 +141,11 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
assertThat(result.getArray()).isEqualTo(new String[] {"Foo", "Bar"});
|
||||
assertThat(result.isBool()).isTrue();
|
||||
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readUntyped() throws IOException {
|
||||
void readUntyped() throws IOException {
|
||||
String body = "{" +
|
||||
"\"bytes\":\"AQI=\"," +
|
||||
"\"array\":[\"Foo\",\"Bar\"]," +
|
||||
@@ -175,7 +168,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
void write() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -194,11 +187,10 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue();
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWithBaseType() throws IOException {
|
||||
void writeWithBaseType() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -220,7 +212,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeUTF16() throws IOException {
|
||||
void writeUTF16() throws IOException {
|
||||
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
String body = "H\u00e9llo W\u00f6rld";
|
||||
@@ -230,7 +222,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readInvalidJson() throws IOException {
|
||||
void readInvalidJson() throws IOException {
|
||||
String body = "FooBar";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
@@ -239,7 +231,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readValidJsonWithUnknownProperty() throws IOException {
|
||||
void readValidJsonWithUnknownProperty() throws IOException {
|
||||
String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
@@ -249,7 +241,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readAndWriteGenerics() throws Exception {
|
||||
void readAndWriteGenerics() throws Exception {
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() {
|
||||
@Override
|
||||
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
|
||||
@@ -288,7 +280,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readAndWriteParameterizedType() throws Exception {
|
||||
void readAndWriteParameterizedType() throws Exception {
|
||||
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
|
||||
|
||||
String body = "[{" +
|
||||
@@ -319,7 +311,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeParameterizedBaseType() throws Exception {
|
||||
void writeParameterizedBaseType() throws Exception {
|
||||
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
|
||||
ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {};
|
||||
|
||||
@@ -350,7 +342,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrint() throws Exception {
|
||||
void prettyPrint() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
PrettyPrintBean bean = new PrettyPrintBean();
|
||||
bean.setName("Jason");
|
||||
@@ -364,7 +356,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prettyPrintWithSse() throws Exception {
|
||||
void prettyPrintWithSse() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
outputMessage.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
|
||||
PrettyPrintBean bean = new PrettyPrintBean();
|
||||
@@ -378,7 +370,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixJson() throws Exception {
|
||||
void prefixJson() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setPrefixJson(true);
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
@@ -387,7 +379,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixJsonCustom() throws Exception {
|
||||
void prefixJsonCustom() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
this.converter.setJsonPrefix(")))");
|
||||
this.converter.writeInternal("foo", null, outputMessage);
|
||||
@@ -396,7 +388,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldLevelJsonView() throws Exception {
|
||||
void fieldLevelJsonView() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
@@ -414,7 +406,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classLevelJsonView() throws Exception {
|
||||
void classLevelJsonView() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
@@ -432,7 +424,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filters() throws Exception {
|
||||
void filters() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
JacksonFilteredBean bean = new JacksonFilteredBean();
|
||||
bean.setProperty1("value");
|
||||
@@ -450,7 +442,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13318
|
||||
public void writeSubType() throws Exception {
|
||||
void writeSubType() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean bean = new MyBean();
|
||||
bean.setString("Foo");
|
||||
@@ -464,7 +456,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13318
|
||||
public void writeSubTypeList() throws Exception {
|
||||
void writeSubTypeList() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
List<MyBean> beans = new ArrayList<>();
|
||||
MyBean foo = new MyBean();
|
||||
@@ -488,7 +480,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithNoDefaultConstructor() throws Exception {
|
||||
void readWithNoDefaultConstructor() throws Exception {
|
||||
String body = "{\"property1\":\"foo\",\"property2\":\"bar\"}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
@@ -499,7 +491,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readNonUnicode() throws Exception {
|
||||
void readNonUnicode() throws Exception {
|
||||
String body = "{\"føø\":\"bår\"}";
|
||||
Charset charset = StandardCharsets.ISO_8859_1;
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
|
||||
@@ -511,7 +503,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readAscii() throws Exception {
|
||||
void readAscii() throws Exception {
|
||||
String body = "{\"foo\":\"bar\"}";
|
||||
Charset charset = StandardCharsets.US_ASCII;
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
|
||||
@@ -523,7 +515,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeAscii() throws Exception {
|
||||
void writeAscii() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Map<String,Object> body = new HashMap<>();
|
||||
body.put("foo", "bar");
|
||||
|
||||
@@ -16,15 +16,12 @@
|
||||
|
||||
package org.springframework.http.converter.protobuf;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.google.protobuf.ExtensionRegistry;
|
||||
import com.google.protobuf.Message;
|
||||
import com.google.protobuf.util.JsonFormat;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -36,8 +33,6 @@ import org.springframework.protobuf.SecondMsg;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -48,47 +43,39 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Juergen Hoeller
|
||||
* @author Andreas Ahlenstorf
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ProtobufHttpMessageConverterTests {
|
||||
class ProtobufHttpMessageConverterTests {
|
||||
|
||||
private ProtobufHttpMessageConverter converter;
|
||||
private ExtensionRegistry extensionRegistry = mock(ExtensionRegistry.class);
|
||||
|
||||
private ExtensionRegistry extensionRegistry;
|
||||
private ExtensionRegistryInitializer registryInitializer = mock(ExtensionRegistryInitializer.class);
|
||||
|
||||
private ExtensionRegistryInitializer registryInitializer;
|
||||
private ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter(this.registryInitializer);
|
||||
|
||||
private Msg testMsg;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.registryInitializer = mock(ExtensionRegistryInitializer.class);
|
||||
this.extensionRegistry = mock(ExtensionRegistry.class);
|
||||
this.converter = new ProtobufHttpMessageConverter(this.registryInitializer);
|
||||
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
|
||||
}
|
||||
private Msg testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
|
||||
|
||||
|
||||
@Test
|
||||
public void extensionRegistryInitialized() {
|
||||
void extensionRegistryInitialized() {
|
||||
verify(this.registryInitializer, times(1)).initializeExtensionRegistry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionRegistryInitializerNull() {
|
||||
void extensionRegistryInitializerNull() {
|
||||
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistryInitializer)null);
|
||||
assertThat(converter.extensionRegistry).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionRegistryNull() {
|
||||
void extensionRegistryNull() {
|
||||
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistry)null);
|
||||
assertThat(converter.extensionRegistry).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(this.converter.canRead(Msg.class, null)).isTrue();
|
||||
assertThat(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue();
|
||||
assertThat(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON)).isTrue();
|
||||
@@ -100,7 +87,7 @@ public class ProtobufHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(this.converter.canWrite(Msg.class, null)).isTrue();
|
||||
assertThat(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue();
|
||||
assertThat(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON)).isTrue();
|
||||
@@ -110,18 +97,16 @@ public class ProtobufHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
void read() throws IOException {
|
||||
byte[] body = this.testMsg.toByteArray();
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
|
||||
Message result = this.converter.read(Msg.class, inputMessage);
|
||||
assertThat(result).isEqualTo(this.testMsg);
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readNoContentType() throws IOException {
|
||||
void readNoContentType() throws IOException {
|
||||
byte[] body = this.testMsg.toByteArray();
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
Message result = this.converter.read(Msg.class, inputMessage);
|
||||
@@ -129,7 +114,7 @@ public class ProtobufHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeProtobuf() throws IOException {
|
||||
void writeProtobuf() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
|
||||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
@@ -144,11 +129,10 @@ public class ProtobufHttpMessageConverterTests {
|
||||
String schemaHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
assertThat(schemaHeader).isEqualTo("sample.proto");
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeJsonWithGoogleProtobuf() throws IOException {
|
||||
void writeJsonWithGoogleProtobuf() throws IOException {
|
||||
this.converter = new ProtobufHttpMessageConverter(
|
||||
new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
|
||||
this.extensionRegistry);
|
||||
@@ -172,7 +156,7 @@ public class ProtobufHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeJsonWithJavaFormat() throws IOException {
|
||||
void writeJsonWithJavaFormat() throws IOException {
|
||||
this.converter = new ProtobufHttpMessageConverter(
|
||||
new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
|
||||
this.extensionRegistry);
|
||||
@@ -196,13 +180,13 @@ public class ProtobufHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultContentType() throws Exception {
|
||||
void defaultContentType() throws Exception {
|
||||
assertThat(this.converter.getDefaultContentType(this.testMsg))
|
||||
.isEqualTo(ProtobufHttpMessageConverter.PROTOBUF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentLength() throws Exception {
|
||||
void getContentLength() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
|
||||
this.converter.write(this.testMsg, contentType, outputMessage);
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.protobuf;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.google.protobuf.ExtensionRegistry;
|
||||
import com.google.protobuf.Message;
|
||||
@@ -34,8 +32,6 @@ import org.springframework.protobuf.SecondMsg;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@@ -92,12 +88,10 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
byte[] body = this.testMsg.toByteArray();
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
|
||||
Message result = this.converter.read(Msg.class, inputMessage);
|
||||
assertThat(result).isEqualTo(this.testMsg);
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,7 +118,6 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
|
||||
String schemaHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
assertThat(schemaHeader).isEqualTo("sample.proto");
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -30,37 +30,34 @@ import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Jackson 2.x Smile converter tests.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
class MappingJackson2SmileHttpMessageConverterTests {
|
||||
|
||||
private final MappingJackson2SmileHttpMessageConverter converter = new MappingJackson2SmileHttpMessageConverter();
|
||||
private final ObjectMapper mapper = new ObjectMapper(new SmileFactory());
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "x-jackson-smile"))).isTrue();
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "json"))).isFalse();
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "xml"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "x-jackson-smile"))).isTrue();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "json"))).isFalse();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "xml"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
void read() throws IOException {
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
body.setNumber(42);
|
||||
@@ -68,7 +65,7 @@ public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
body.setArray(new String[]{"Foo", "Bar"});
|
||||
body.setBool(true);
|
||||
body.setBytes(new byte[]{0x1, 0x2});
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(mapper.writeValueAsBytes(body)));
|
||||
InputStream inputStream = new ByteArrayInputStream(mapper.writeValueAsBytes(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "x-jackson-smile"));
|
||||
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
|
||||
@@ -79,11 +76,10 @@ public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
assertThat(result.getArray()).isEqualTo(new String[]{"Foo", "Bar"});
|
||||
assertThat(result.isBool()).isTrue();
|
||||
assertThat(result.getBytes()).isEqualTo(new byte[]{0x1, 0x2});
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
void write() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -96,7 +92,6 @@ public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
assertThat(outputMessage.getBodyAsBytes()).isEqualTo(mapper.writeValueAsBytes(body));
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(new MediaType("application", "x-jackson-smile"));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
@@ -41,9 +39,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link Jaxb2CollectionHttpMessageConverter}.
|
||||
@@ -51,7 +46,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
private Jaxb2CollectionHttpMessageConverter<?> converter;
|
||||
|
||||
@@ -65,7 +60,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
void setup() {
|
||||
converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>();
|
||||
rootElementListType = new ParameterizedTypeReference<List<RootElement>>() {}.getType();
|
||||
rootElementSetType = new ParameterizedTypeReference<Set<RootElement>>() {}.getType();
|
||||
@@ -75,7 +70,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(rootElementListType, null, null)).isTrue();
|
||||
assertThat(converter.canRead(rootElementSetType, null, null)).isTrue();
|
||||
assertThat(converter.canRead(typeSetType, null, null)).isTrue();
|
||||
@@ -83,21 +78,19 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlRootElementList() throws Exception {
|
||||
void readXmlRootElementList() throws Exception {
|
||||
String content = "<list><rootElement><type s=\"1\"/></rootElement><rootElement><type s=\"2\"/></rootElement></list>";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
|
||||
List<RootElement> result = (List<RootElement>) converter.read(rootElementListType, null, inputMessage);
|
||||
|
||||
assertThat(result.size()).as("Invalid result").isEqualTo(2);
|
||||
assertThat(result.get(0).type.s).as("Invalid result").isEqualTo("1");
|
||||
assertThat(result.get(1).type.s).as("Invalid result").isEqualTo("2");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlRootElementSet() throws Exception {
|
||||
void readXmlRootElementSet() throws Exception {
|
||||
String content = "<set><rootElement><type s=\"1\"/></rootElement><rootElement><type s=\"2\"/></rootElement></set>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
|
||||
Set<RootElement> result = (Set<RootElement>) converter.read(rootElementSetType, null, inputMessage);
|
||||
@@ -109,7 +102,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlTypeList() throws Exception {
|
||||
void readXmlTypeList() throws Exception {
|
||||
String content = "<list><foo s=\"1\"/><bar s=\"2\"/></list>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
|
||||
List<TestType> result = (List<TestType>) converter.read(typeListType, null, inputMessage);
|
||||
@@ -121,7 +114,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlTypeSet() throws Exception {
|
||||
void readXmlTypeSet() throws Exception {
|
||||
String content = "<set><foo s=\"1\"/><bar s=\"2\"/></set>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
|
||||
Set<TestType> result = (Set<TestType>) converter.read(typeSetType, null, inputMessage);
|
||||
@@ -133,7 +126,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlRootElementExternalEntityDisabled() throws Exception {
|
||||
void readXmlRootElementExternalEntityDisabled() throws Exception {
|
||||
Resource external = new ClassPathResource("external.txt", getClass());
|
||||
String content = "<!DOCTYPE root [" +
|
||||
" <!ELEMENT external ANY >\n" +
|
||||
@@ -162,7 +155,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlRootElementExternalEntityEnabled() throws Exception {
|
||||
void readXmlRootElementExternalEntityEnabled() throws Exception {
|
||||
Resource external = new ClassPathResource("external.txt", getClass());
|
||||
String content = "<!DOCTYPE root [" +
|
||||
" <!ELEMENT external ANY >\n" +
|
||||
@@ -185,7 +178,7 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlBomb() throws Exception {
|
||||
void testXmlBomb() throws Exception {
|
||||
// https://en.wikipedia.org/wiki/Billion_laughs
|
||||
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
||||
String content = "<?xml version=\"1.0\"?>\n" +
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.bind.Marshaller;
|
||||
@@ -29,7 +27,6 @@ import javax.xml.bind.annotation.XmlType;
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xmlunit.diff.DifferenceEvaluator;
|
||||
|
||||
@@ -46,9 +43,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.xmlunit.diff.ComparisonType.XML_STANDALONE;
|
||||
import static org.xmlunit.diff.DifferenceEvaluators.Default;
|
||||
import static org.xmlunit.diff.DifferenceEvaluators.chain;
|
||||
@@ -60,31 +54,17 @@ import static org.xmlunit.diff.DifferenceEvaluators.downgradeDifferencesToEqual;
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
class Jaxb2RootElementHttpMessageConverterTests {
|
||||
|
||||
private Jaxb2RootElementHttpMessageConverter converter;
|
||||
private final Jaxb2RootElementHttpMessageConverter converter = new Jaxb2RootElementHttpMessageConverter();
|
||||
|
||||
private RootElement rootElement;
|
||||
|
||||
private RootElement rootElementCglib;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
converter = new Jaxb2RootElementHttpMessageConverter();
|
||||
rootElement = new RootElement();
|
||||
DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory();
|
||||
AdvisedSupport advisedSupport = new AdvisedSupport();
|
||||
advisedSupport.setTarget(rootElement);
|
||||
advisedSupport.setProxyTargetClass(true);
|
||||
AopProxy proxy = proxyFactory.createAopProxy(advisedSupport);
|
||||
rootElementCglib = (RootElement) proxy.getProxy();
|
||||
}
|
||||
private final RootElement rootElement = new RootElement();
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(RootElement.class, null))
|
||||
.as("Converter does not support reading @XmlRootElement").isTrue();
|
||||
assertThat(converter.canRead(Type.class, null))
|
||||
@@ -92,29 +72,27 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(RootElement.class, null))
|
||||
.as("Converter does not support writing @XmlRootElement").isTrue();
|
||||
assertThat(converter.canWrite(RootElementSubclass.class, null))
|
||||
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
|
||||
assertThat(converter.canWrite(rootElementCglib.getClass(), null))
|
||||
assertThat(converter.canWrite(createRootElementCglib().getClass(), null))
|
||||
.as("Converter does not support writing @XmlRootElement subclass").isTrue();
|
||||
assertThat(converter.canWrite(Type.class, null))
|
||||
.as("Converter supports writing @XmlType").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readXmlRootElement() throws Exception {
|
||||
void readXmlRootElement() throws Exception {
|
||||
byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes(StandardCharsets.UTF_8);
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
RootElement result = (RootElement) converter.read(RootElement.class, inputMessage);
|
||||
assertThat(result.type.s).as("Invalid result").isEqualTo("Hello World");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readXmlRootElementSubclass() throws Exception {
|
||||
void readXmlRootElementSubclass() throws Exception {
|
||||
byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes(StandardCharsets.UTF_8);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage);
|
||||
@@ -122,7 +100,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readXmlType() throws Exception {
|
||||
void readXmlType() throws Exception {
|
||||
byte[] body = "<foo s=\"Hello World\"/>".getBytes(StandardCharsets.UTF_8);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
Type result = (Type) converter.read(Type.class, inputMessage);
|
||||
@@ -130,7 +108,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readXmlRootElementExternalEntityDisabled() throws Exception {
|
||||
void readXmlRootElementExternalEntityDisabled() throws Exception {
|
||||
Resource external = new ClassPathResource("external.txt", getClass());
|
||||
String content = "<!DOCTYPE root SYSTEM \"https://192.168.28.42/1.jsp\" [" +
|
||||
" <!ELEMENT external ANY >\n" +
|
||||
@@ -144,7 +122,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readXmlRootElementExternalEntityEnabled() throws Exception {
|
||||
void readXmlRootElementExternalEntityEnabled() throws Exception {
|
||||
Resource external = new ClassPathResource("external.txt", getClass());
|
||||
String content = "<!DOCTYPE root [" +
|
||||
" <!ELEMENT external ANY >\n" +
|
||||
@@ -158,7 +136,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlBomb() throws Exception {
|
||||
void testXmlBomb() throws Exception {
|
||||
// https://en.wikipedia.org/wiki/Billion_laughs
|
||||
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
||||
String content = "<?xml version=\"1.0\"?>\n" +
|
||||
@@ -183,7 +161,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeXmlRootElement() throws Exception {
|
||||
void writeXmlRootElement() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(rootElement, null, outputMessage);
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
@@ -191,13 +169,12 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
|
||||
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
|
||||
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeXmlRootElementSubclass() throws Exception {
|
||||
void writeXmlRootElementSubclass() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(rootElementCglib, null, outputMessage);
|
||||
converter.write(createRootElementCglib(), null, outputMessage);
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML);
|
||||
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
|
||||
@@ -208,7 +185,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
// SPR-11488
|
||||
|
||||
@Test
|
||||
public void customizeMarshaller() throws Exception {
|
||||
void customizeMarshaller() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
|
||||
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
|
||||
@@ -218,7 +195,7 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeUnmarshaller() throws Exception {
|
||||
void customizeUnmarshaller() throws Exception {
|
||||
byte[] body = "<myRootElement><element>a|||b</element></myRootElement>".getBytes(StandardCharsets.UTF_8);
|
||||
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
@@ -227,6 +204,15 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
assertThat(result.getElement().getField2()).isEqualTo("b");
|
||||
}
|
||||
|
||||
private RootElement createRootElementCglib() {
|
||||
DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory();
|
||||
AdvisedSupport advisedSupport = new AdvisedSupport();
|
||||
advisedSupport.setTarget(this.rootElement);
|
||||
advisedSupport.setProxyTargetClass(true);
|
||||
AopProxy proxy = proxyFactory.createAopProxy(advisedSupport);
|
||||
return (RootElement) proxy.getProxy();
|
||||
}
|
||||
|
||||
|
||||
@XmlRootElement
|
||||
public static class RootElement {
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -36,9 +34,6 @@ import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Jackson 2.x XML converter tests.
|
||||
@@ -46,13 +41,13 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
class MappingJackson2XmlHttpMessageConverterTests {
|
||||
|
||||
private final MappingJackson2XmlHttpMessageConverter converter = new MappingJackson2XmlHttpMessageConverter();
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "xml"))).isTrue();
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("text", "xml"))).isTrue();
|
||||
assertThat(converter.canRead(MyBean.class, new MediaType("application", "soap+xml"))).isTrue();
|
||||
@@ -61,7 +56,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "xml"))).isTrue();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("text", "xml"))).isTrue();
|
||||
assertThat(converter.canWrite(MyBean.class, new MediaType("application", "soap+xml"))).isTrue();
|
||||
@@ -70,7 +65,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
void read() throws IOException {
|
||||
String body = "<MyBean>" +
|
||||
"<string>Foo</string>" +
|
||||
"<number>42</number>" +
|
||||
@@ -79,8 +74,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
"<array>Bar</array></array>" +
|
||||
"<bool>true</bool>" +
|
||||
"<bytes>AQI=</bytes></MyBean>";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
|
||||
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
|
||||
assertThat(result.getString()).isEqualTo("Foo");
|
||||
@@ -89,11 +83,10 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
assertThat(result.getArray()).isEqualTo(new String[]{"Foo", "Bar"});
|
||||
assertThat(result.isBool()).isTrue();
|
||||
assertThat(result.getBytes()).isEqualTo(new byte[]{0x1, 0x2});
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
void write() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean body = new MyBean();
|
||||
body.setString("Foo");
|
||||
@@ -112,11 +105,10 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
assertThat(result.contains("<bytes>AQI=</bytes>")).isTrue();
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(new MediaType("application", "xml", StandardCharsets.UTF_8));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readInvalidXml() throws IOException {
|
||||
void readInvalidXml() throws IOException {
|
||||
String body = "FooBar";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
@@ -125,7 +117,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readValidXmlWithUnknownProperty() throws IOException {
|
||||
void readValidXmlWithUnknownProperty() throws IOException {
|
||||
String body = "<MyBean><string>string</string><unknownProperty>value</unknownProperty></MyBean>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
@@ -134,7 +126,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonView() throws Exception {
|
||||
void jsonView() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
@@ -152,13 +144,13 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customXmlMapper() {
|
||||
void customXmlMapper() {
|
||||
new MappingJackson2XmlHttpMessageConverter(new MyXmlMapper());
|
||||
// Assert no exception is thrown
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithExternalReference() throws IOException {
|
||||
void readWithExternalReference() throws IOException {
|
||||
String body = "<!DOCTYPE MyBean SYSTEM \"https://192.168.28.42/1.jsp\" [" +
|
||||
" <!ELEMENT root ANY >\n" +
|
||||
" <!ENTITY ext SYSTEM \"" +
|
||||
@@ -173,7 +165,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithXmlBomb() throws IOException {
|
||||
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" +
|
||||
@@ -201,7 +193,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readNonUnicode() throws Exception {
|
||||
void readNonUnicode() throws Exception {
|
||||
String body = "<MyBean>" +
|
||||
"<string>føø bår</string>" +
|
||||
"</MyBean>";
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
@@ -44,19 +42,16 @@ import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willDoNothing;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link MarshallingHttpMessageConverter}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MarshallingHttpMessageConverterTests {
|
||||
class MarshallingHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
Unmarshaller unmarshaller = mock(Unmarshaller.class);
|
||||
|
||||
given(unmarshaller.supports(Integer.class)).willReturn(false);
|
||||
@@ -71,7 +66,7 @@ public class MarshallingHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
Marshaller marshaller = mock(Marshaller.class);
|
||||
|
||||
given(marshaller.supports(Integer.class)).willReturn(false);
|
||||
@@ -86,10 +81,9 @@ public class MarshallingHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
void read() throws Exception {
|
||||
String body = "<root>Hello World</root>";
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Unmarshaller unmarshaller = mock(Unmarshaller.class);
|
||||
given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);
|
||||
@@ -99,11 +93,10 @@ public class MarshallingHttpMessageConverterTests {
|
||||
|
||||
String result = (String) converter.read(Object.class, inputMessage);
|
||||
assertThat(result).as("Invalid result").isEqualTo(body);
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithTypeMismatchException() throws Exception {
|
||||
void readWithTypeMismatchException() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
|
||||
|
||||
Marshaller marshaller = mock(Marshaller.class);
|
||||
@@ -117,7 +110,7 @@ public class MarshallingHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithMarshallingFailureException() throws Exception {
|
||||
void readWithMarshallingFailureException() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
|
||||
UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");
|
||||
|
||||
@@ -132,7 +125,7 @@ public class MarshallingHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws Exception {
|
||||
void write() throws Exception {
|
||||
String body = "<root>Hello World</root>";
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
|
||||
@@ -144,11 +137,10 @@ public class MarshallingHttpMessageConverterTests {
|
||||
|
||||
assertThat(outputMessage.getHeaders().getContentType())
|
||||
.as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWithMarshallingFailureException() throws Exception {
|
||||
void writeWithMarshallingFailureException() throws Exception {
|
||||
String body = "<root>Hello World</root>";
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MarshallingFailureException ex = new MarshallingFailureException("forced");
|
||||
@@ -162,8 +154,9 @@ public class MarshallingHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports() {
|
||||
void supports() {
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
|
||||
new MarshallingHttpMessageConverter().supports(Object.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -32,7 +30,6 @@ import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -52,60 +49,53 @@ import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class SourceHttpMessageConverterTests {
|
||||
class SourceHttpMessageConverterTests {
|
||||
|
||||
private static final String BODY = "<root>Hello World</root>";
|
||||
|
||||
private SourceHttpMessageConverter<Source> converter;
|
||||
private final SourceHttpMessageConverter<Source> converter = new SourceHttpMessageConverter<>();
|
||||
|
||||
private String bodyExternal;
|
||||
private final String bodyExternal;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
converter = new SourceHttpMessageConverter<>();
|
||||
SourceHttpMessageConverterTests() throws IOException {
|
||||
Resource external = new ClassPathResource("external.txt", getClass());
|
||||
|
||||
bodyExternal = "<!DOCTYPE root SYSTEM \"https://192.168.28.42/1.jsp\" [" +
|
||||
this.bodyExternal = "<!DOCTYPE root SYSTEM \"https://192.168.28.42/1.jsp\" [" +
|
||||
" <!ELEMENT root ANY >\n" +
|
||||
" <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>";
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
void canRead() {
|
||||
assertThat(converter.canRead(Source.class, new MediaType("application", "xml"))).isTrue();
|
||||
assertThat(converter.canRead(Source.class, new MediaType("application", "soap+xml"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
void canWrite() {
|
||||
assertThat(converter.canWrite(Source.class, new MediaType("application", "xml"))).isTrue();
|
||||
assertThat(converter.canWrite(Source.class, new MediaType("application", "soap+xml"))).isTrue();
|
||||
assertThat(converter.canWrite(Source.class, MediaType.ALL)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readDOMSource() throws Exception {
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(BODY.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
void readDOMSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
|
||||
Document document = (Document) result.getNode();
|
||||
assertThat(document.getDocumentElement().getLocalName()).as("Invalid result").isEqualTo("root");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readDOMSourceExternal() throws Exception {
|
||||
void readDOMSourceExternal() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
converter.setSupportDtd(true);
|
||||
@@ -116,7 +106,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readDomSourceWithXmlBomb() throws Exception {
|
||||
void readDomSourceWithXmlBomb() throws Exception {
|
||||
// https://en.wikipedia.org/wiki/Billion_laughs
|
||||
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
||||
String content = "<?xml version=\"1.0\"?>\n" +
|
||||
@@ -142,7 +132,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readSAXSource() throws Exception {
|
||||
void readSAXSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
|
||||
@@ -152,7 +142,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readSAXSourceExternal() throws Exception {
|
||||
void readSAXSourceExternal() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
converter.setSupportDtd(true);
|
||||
@@ -170,7 +160,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readSAXSourceWithXmlBomb() throws Exception {
|
||||
void readSAXSourceWithXmlBomb() throws Exception {
|
||||
// https://en.wikipedia.org/wiki/Billion_laughs
|
||||
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
||||
String content = "<?xml version=\"1.0\"?>\n" +
|
||||
@@ -199,7 +189,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readStAXSource() throws Exception {
|
||||
void readStAXSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
|
||||
@@ -214,7 +204,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readStAXSourceExternal() throws Exception {
|
||||
void readStAXSourceExternal() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
converter.setSupportDtd(true);
|
||||
@@ -236,7 +226,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readStAXSourceWithXmlBomb() throws Exception {
|
||||
void readStAXSourceWithXmlBomb() throws Exception {
|
||||
// https://en.wikipedia.org/wiki/Billion_laughs
|
||||
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
|
||||
String content = "<?xml version=\"1.0\"?>\n" +
|
||||
@@ -268,7 +258,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readStreamSource() throws Exception {
|
||||
void readStreamSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage);
|
||||
@@ -277,14 +267,14 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readSource() throws Exception {
|
||||
void readSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
converter.read(Source.class, inputMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeDOMSource() throws Exception {
|
||||
void writeDOMSource() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
|
||||
@@ -301,11 +291,10 @@ public class SourceHttpMessageConverterTests {
|
||||
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML);
|
||||
assertThat(outputMessage.getHeaders().getContentLength())
|
||||
.as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeSAXSource() throws Exception {
|
||||
void writeSAXSource() throws Exception {
|
||||
String xml = "<root>Hello World</root>";
|
||||
SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml)));
|
||||
|
||||
@@ -318,7 +307,7 @@ public class SourceHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeStreamSource() throws Exception {
|
||||
void writeStreamSource() throws Exception {
|
||||
String xml = "<root>Hello World</root>";
|
||||
StreamSource streamSource = new StreamSource(new StringReader(xml));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user