Ensure all converters don't close InputStream
Closes gh-27969
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.
|
||||
@@ -19,6 +19,7 @@ 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;
|
||||
|
||||
@@ -33,6 +34,9 @@ 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.
|
||||
@@ -65,11 +69,13 @@ public class BufferedImageHttpMessageConverterTests {
|
||||
public void read() throws IOException {
|
||||
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
|
||||
byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
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
|
||||
@@ -84,6 +90,7 @@ 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
|
||||
|
||||
@@ -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.
|
||||
@@ -38,6 +38,9 @@ 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
|
||||
@@ -71,8 +74,8 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream is = getClass().getResourceAsStream("atom.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
|
||||
InputStream inputStream = spy(getClass().getResourceAsStream("atom.xml"));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
|
||||
Feed result = converter.read(Feed.class, inputMessage);
|
||||
assertThat(result.getTitle()).isEqualTo("title");
|
||||
@@ -87,6 +90,7 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
Entry entry2 = (Entry) entries.get(1);
|
||||
assertThat(entry2.getId()).isEqualTo("id2");
|
||||
assertThat(entry2.getTitle()).isEqualTo("title2");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,6 +123,7 @@ 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
|
||||
|
||||
@@ -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.
|
||||
@@ -34,6 +34,9 @@ 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
|
||||
@@ -56,8 +59,8 @@ public class RssChannelHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
InputStream is = getClass().getResourceAsStream("rss.xml");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
|
||||
InputStream inputStream = spy(getClass().getResourceAsStream("rss.xml"));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
|
||||
Channel result = converter.read(Channel.class, inputMessage);
|
||||
assertThat(result.getTitle()).isEqualTo("title");
|
||||
@@ -72,6 +75,7 @@ public class RssChannelHttpMessageConverterTests {
|
||||
|
||||
Item item2 = (Item) items.get(1);
|
||||
assertThat(item2.getTitle()).isEqualTo("title2");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -105,6 +109,7 @@ public class RssChannelHttpMessageConverterTests {
|
||||
"</channel></rss>";
|
||||
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
|
||||
.isSimilarToIgnoringWhitespace(expected);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
@@ -38,6 +40,9 @@ 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.
|
||||
@@ -72,7 +77,8 @@ public class GsonHttpMessageConverterTests {
|
||||
public void readTyped() 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"));
|
||||
InputStream inputStream = spy(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);
|
||||
|
||||
@@ -83,6 +89,7 @@ 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
|
||||
@@ -132,6 +139,7 @@ public class GsonHttpMessageConverterTests {
|
||||
assertThat(result.contains("\"bool\":true")).isTrue();
|
||||
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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
@@ -38,6 +40,9 @@ 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.
|
||||
@@ -72,7 +77,8 @@ 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}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
InputStream inputStream = spy(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);
|
||||
|
||||
@@ -83,6 +89,7 @@ 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
|
||||
@@ -132,6 +139,7 @@ public class JsonbHttpMessageConverterTests {
|
||||
assertThat(result.contains("\"bool\":true")).isTrue();
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
@@ -48,6 +50,7 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -134,7 +137,8 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
"\"string\":\"Foo\"," +
|
||||
"\"bool\":true," +
|
||||
"\"fraction\":42.0}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
|
||||
assertThat(result.getString()).isEqualTo("Foo");
|
||||
@@ -143,6 +147,7 @@ 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
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.http.converter.protobuf;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import com.google.protobuf.ExtensionRegistry;
|
||||
@@ -34,6 +36,8 @@ 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;
|
||||
|
||||
@@ -108,10 +112,12 @@ public class ProtobufHttpMessageConverterTests {
|
||||
@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
|
||||
@@ -138,6 +144,7 @@ public class ProtobufHttpMessageConverterTests {
|
||||
String schemaHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
assertThat(schemaHeader).isEqualTo("sample.proto");
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
@@ -32,6 +34,8 @@ 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;
|
||||
|
||||
@@ -88,10 +92,12 @@ 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
|
||||
@@ -118,6 +124,7 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
|
||||
String schemaHeader =
|
||||
outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
|
||||
assertThat(schemaHeader).isEqualTo("sample.proto");
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.http.converter.smile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
|
||||
@@ -28,6 +30,9 @@ 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.
|
||||
@@ -63,7 +68,8 @@ public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
body.setArray(new String[]{"Foo", "Bar"});
|
||||
body.setBool(true);
|
||||
body.setBytes(new byte[]{0x1, 0x2});
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(mapper.writeValueAsBytes(body));
|
||||
InputStream inputStream = spy(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);
|
||||
assertThat(result.getString()).isEqualTo("Foo");
|
||||
@@ -73,6 +79,7 @@ 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
|
||||
@@ -88,6 +95,7 @@ public class MappingJackson2SmileHttpMessageConverterTests {
|
||||
converter.write(body, null, outputMessage);
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
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;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -38,6 +41,9 @@ 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}.
|
||||
@@ -79,12 +85,14 @@ public class Jaxb2CollectionHttpMessageConverterTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readXmlRootElementList() throws Exception {
|
||||
String content = "<list><rootElement><type s=\"1\"/></rootElement><rootElement><type s=\"2\"/></rootElement></list>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.bind.Marshaller;
|
||||
@@ -44,6 +46,9 @@ 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;
|
||||
@@ -95,9 +100,11 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
@Test
|
||||
public void readXmlRootElement() throws Exception {
|
||||
byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes("UTF-8");
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
RootElement result = (RootElement) converter.read(RootElement.class, inputMessage);
|
||||
assertThat(result.type.s).as("Invalid result").isEqualTo("Hello World");
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -177,6 +184,7 @@ 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
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
|
||||
@@ -34,6 +36,9 @@ 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.
|
||||
@@ -74,7 +79,8 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
"<array>Bar</array></array>" +
|
||||
"<bool>true</bool>" +
|
||||
"<bytes>AQI=</bytes></MyBean>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
|
||||
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
|
||||
assertThat(result.getString()).isEqualTo("Foo");
|
||||
@@ -83,6 +89,7 @@ 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
|
||||
@@ -104,6 +111,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
|
||||
assertThat(result.contains("<bool>true</bool>")).isTrue();
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
@@ -40,6 +44,9 @@ 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}.
|
||||
@@ -81,7 +88,8 @@ public class MarshallingHttpMessageConverterTests {
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
String body = "<root>Hello World</root>";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
|
||||
Unmarshaller unmarshaller = mock(Unmarshaller.class);
|
||||
given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);
|
||||
@@ -91,6 +99,7 @@ public class MarshallingHttpMessageConverterTests {
|
||||
|
||||
String result = (String) converter.read(Object.class, inputMessage);
|
||||
assertThat(result).as("Invalid result").isEqualTo(body);
|
||||
verify(inputStream, never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -135,6 +144,7 @@ public class MarshallingHttpMessageConverterTests {
|
||||
converter.write(body, null, outputMessage);
|
||||
|
||||
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
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;
|
||||
@@ -50,6 +52,9 @@ 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
|
||||
@@ -90,11 +95,13 @@ public class SourceHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void readDOMSource() throws Exception {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
|
||||
InputStream inputStream = spy(new ByteArrayInputStream(BODY.getBytes("UTF-8")));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
|
||||
inputMessage.getHeaders().setContentType(new 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
|
||||
@@ -294,6 +301,7 @@ public class SourceHttpMessageConverterTests {
|
||||
.isSimilarTo("<root>Hello World</root>");
|
||||
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml"));
|
||||
assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length);
|
||||
verify(outputMessage.getBody(), never()).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user