From 4effca35b500fda26f1f7d8322bc622468e5e93a Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 31 Jan 2022 13:51:40 +0100 Subject: [PATCH 1/3] Ignore Content-Type that is invalid (not concrete) Closes gh-27957 --- .../http/server/ServletServerHttpRequest.java | 6 ++++-- .../http/server/ServletServerHttpRequestTests.java | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index b136e3fbcf..82f722310c 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -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. @@ -158,7 +158,9 @@ public class ServletServerHttpRequest implements ServerHttpRequest { String requestContentType = this.servletRequest.getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); - this.headers.setContentType(contentType); + if (contentType.isConcrete()) { + this.headers.setContentType(contentType); + } } } if (contentType != null && contentType.getCharset() == null) { diff --git a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java index de0779a4c3..6b24f91613 100644 --- a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java @@ -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. @@ -151,6 +151,13 @@ public class ServletServerHttpRequestTests { assertThat(headers.getContentType()).isNull(); } + @Test // gh-27957 + void getHeadersWithWildcardContentType() { + mockRequest.setContentType("*/*"); + mockRequest.removeHeader("Content-Type"); + assertThat(request.getHeaders()).as("Invalid content-type should not raise exception").hasSize(0); + } + @Test void getBody() throws IOException { byte[] content = "Hello World".getBytes(StandardCharsets.UTF_8); From 8d5a6520ce033c38c57a9ec026102e17bd598acc Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 2 Feb 2022 15:32:21 +0000 Subject: [PATCH 2/3] Ensure all converters don't close InputStream Closes gh-27969 --- .../BufferedImageHttpMessageConverter.java | 2 ++ .../feed/AbstractWireFeedHttpMessageConverter.java | 7 +++++-- .../json/AbstractJackson2HttpMessageConverter.java | 12 +++++++----- .../xml/AbstractXmlHttpMessageConverter.java | 7 +++++-- .../converter/xml/SourceHttpMessageConverter.java | 4 ++-- .../BufferedImageHttpMessageConverterTests.java | 11 +++++++++-- .../feed/AtomFeedHttpMessageConverterTests.java | 11 ++++++++--- .../feed/RssChannelHttpMessageConverterTests.java | 11 ++++++++--- .../json/GsonHttpMessageConverterTests.java | 12 ++++++++++-- .../json/JsonbHttpMessageConverterTests.java | 12 ++++++++++-- .../MappingJackson2HttpMessageConverterTests.java | 9 +++++++-- .../ProtobufHttpMessageConverterTests.java | 9 ++++++++- ...rotobufJsonFormatHttpMessageConverterTests.java | 9 ++++++++- ...pingJackson2SmileHttpMessageConverterTests.java | 12 ++++++++++-- .../Jaxb2CollectionHttpMessageConverterTests.java | 12 ++++++++++-- .../Jaxb2RootElementHttpMessageConverterTests.java | 12 ++++++++++-- ...appingJackson2XmlHttpMessageConverterTests.java | 12 ++++++++++-- .../xml/MarshallingHttpMessageConverterTests.java | 14 ++++++++++++-- .../xml/SourceHttpMessageConverterTests.java | 12 ++++++++++-- 19 files changed, 151 insertions(+), 39 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java index 0dbfc02f09..62a899f9b3 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/BufferedImageHttpMessageConverter.java @@ -45,6 +45,7 @@ import org.springframework.http.MediaType; import org.springframework.http.StreamingHttpOutputMessage; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; /** @@ -207,6 +208,7 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET); try { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); + Reader reader = new InputStreamReader(inputStream, charset); return (T) feedInput.build(reader); } catch (FeedException ex) { diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index 19b22fef2c..b5273919e4 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -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. @@ -17,6 +17,7 @@ package org.springframework.http.converter.json; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; @@ -361,24 +362,25 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener "UTF-16".equals(charset.name()) || "UTF-32".equals(charset.name()); try { + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); if (inputMessage instanceof MappingJacksonInputMessage) { Class deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView(); if (deserializationView != null) { ObjectReader objectReader = objectMapper.readerWithView(deserializationView).forType(javaType); if (isUnicode) { - return objectReader.readValue(inputMessage.getBody()); + return objectReader.readValue(inputStream); } else { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + Reader reader = new InputStreamReader(inputStream, charset); return objectReader.readValue(reader); } } } if (isUnicode) { - return objectMapper.readValue(inputMessage.getBody(), javaType); + return objectMapper.readValue(inputStream, javaType); } else { - Reader reader = new InputStreamReader(inputMessage.getBody(), charset); + Reader reader = new InputStreamReader(inputStream, charset); return objectMapper.readValue(reader, javaType); } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java index 9da6bcb42c..19d95e4bef 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -17,6 +17,7 @@ package org.springframework.http.converter.xml; import java.io.IOException; +import java.io.InputStream; import javax.xml.transform.Result; import javax.xml.transform.Source; @@ -33,6 +34,7 @@ import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.util.StreamUtils; /** * Abstract base class for {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverters} @@ -66,7 +68,8 @@ public abstract class AbstractXmlHttpMessageConverter extends AbstractHttpMes throws IOException, HttpMessageNotReadableException { try { - return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody())); + InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody()); + return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputStream)); } catch (IOException | HttpMessageConversionException ex) { throw ex; diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java index 63c70e30c6..468152e2ce 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java @@ -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. @@ -146,7 +146,7 @@ public class SourceHttpMessageConverter extends AbstractHttpMe protected T readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - InputStream body = inputMessage.getBody(); + InputStream body = StreamUtils.nonClosing(inputMessage.getBody()); if (DOMSource.class == clazz) { return (T) readDOMSource(body, inputMessage); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java index b91f6b429e..477cd7138c 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java index 856dcd7870..cd71fa9f39 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java index 5d1e01f328..b537604ad3 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java @@ -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 { ""; assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarToIgnoringWhitespace(expected); + verify(outputMessage.getBody(), never()).close(); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java index aa2c777ae4..60d7306c47 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java index 44e14ac576..d2aad2f424 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java index cea6e8c7cf..f67a56433e 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index 26106e13d3..f0b83166da 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java index 296efbd8b3..c933c42622 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufJsonFormatHttpMessageConverterTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java index 2cbb22bdef..9d3e8470a7 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java @@ -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(); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java index 0b02ce6ae1..9c0c08646f 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java @@ -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 = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + InputStream inputStream = spy(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream); List result = (List) 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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java index cf5fa05d46..202776f685 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java @@ -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 = "".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("", ev); + verify(outputMessage.getBody(), never()).close(); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java index ab8c617b6f..631ff4848e 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java @@ -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 { "Bar" + "true" + "AQI="; - 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("true")).isTrue(); assertThat(result.contains("AQI=")).isTrue(); assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml", StandardCharsets.UTF_8)); + verify(outputMessage.getBody(), never()).close(); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java index b3f95c9c81..d79f9dc5ac 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java @@ -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 = "Hello World"; - 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 diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java index a54e6895ef..c2341a9bed 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java @@ -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("Hello World"); 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 From c4e362500be18bd2e277535d568f45e8a059b0db Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 2 Feb 2022 17:09:04 +0000 Subject: [PATCH 3/3] Polishing tests --- .../ByteArrayHttpMessageConverterTests.java | 6 +- .../FormHttpMessageConverterTests.java | 9 ++- .../ResourceHttpMessageConverterTests.java | 6 +- .../json/GsonHttpMessageConverterTests.java | 8 ++- .../json/JsonbHttpMessageConverterTests.java | 6 +- ...pingJackson2HttpMessageConverterTests.java | 34 ++++++----- .../ProtobufHttpMessageConverterTests.java | 9 +-- ...ackson2SmileHttpMessageConverterTests.java | 3 +- ...b2CollectionHttpMessageConverterTests.java | 18 +++--- ...2RootElementHttpMessageConverterTests.java | 38 +++++++----- ...gJackson2XmlHttpMessageConverterTests.java | 19 +++--- .../MarshallingHttpMessageConverterTests.java | 23 ++++--- .../xml/SourceHttpMessageConverterTests.java | 60 ++++++++++--------- 13 files changed, 132 insertions(+), 107 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java index d868e2666e..63c9b5aa32 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java @@ -63,8 +63,10 @@ public class ByteArrayHttpMessageConverterTests { byte[] body = new byte[]{0x1, 0x2}; converter.write(body, null, outputMessage); assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "octet-stream")); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(2); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_OCTET_STREAM); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(2); } } diff --git a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java index 05669acc99..1c3b8729ab 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java @@ -147,9 +147,12 @@ public class FormHttpMessageConverterTests { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.write(body, APPLICATION_FORM_URLENCODED, outputMessage); - assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).as("Invalid result").isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"); - assertThat(outputMessage.getHeaders().getContentType().toString()).as("Invalid content-type").isEqualTo("application/x-www-form-urlencoded;charset=UTF-8"); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); + assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)) + .as("Invalid result").isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"); + assertThat(outputMessage.getHeaders().getContentType().toString()) + .as("Invalid content-type").isEqualTo("application/x-www-form-urlencoded;charset=UTF-8"); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(outputMessage.getBodyAsBytes().length); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java index 3c98fe538d..3bf1b9d3e3 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java @@ -106,8 +106,10 @@ public class ResourceHttpMessageConverterTests { Resource body = new ClassPathResource("logo.jpg", getClass()); converter.write(body, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.IMAGE_JPEG); - assertThat(outputMessage.getHeaders().getContentLength()).as("Invalid content-length").isEqualTo(body.getFile().length()); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.IMAGE_JPEG); + assertThat(outputMessage.getHeaders().getContentLength()) + .as("Invalid content-length").isEqualTo(body.getFile().length()); } @Test // SPR-10848 diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java index 60d7306c47..6d5cf1453d 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java @@ -138,7 +138,8 @@ public class GsonHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); 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)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); verify(outputMessage.getBody(), never()).close(); } @@ -161,7 +162,8 @@ public class GsonHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); 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)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test @@ -177,7 +179,7 @@ public class GsonHttpMessageConverterTests { @Test public void readInvalidJson() throws IOException { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java index d2aad2f424..2519005f66 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/JsonbHttpMessageConverterTests.java @@ -138,7 +138,8 @@ public class JsonbHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); 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)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); verify(outputMessage.getBody(), never()).close(); } @@ -161,7 +162,8 @@ public class JsonbHttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); 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)); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8)); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java index f67a56433e..b06544ac23 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java @@ -160,7 +160,7 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); HashMap result = (HashMap) converter.read(HashMap.class, inputMessage); assertThat(result.get("string")).isEqualTo("Foo"); @@ -192,7 +192,8 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); verify(outputMessage.getBody(), never()).close(); } @@ -214,7 +215,8 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.contains("\"array\":[\"Foo\",\"Bar\"]")).isTrue(); assertThat(result.contains("\"bool\":true")).isTrue(); assertThat(result.contains("\"bytes\":\"AQI=\"")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_JSON); } @Test @@ -230,16 +232,16 @@ public class MappingJackson2HttpMessageConverterTests { @Test public void readInvalidJson() throws IOException { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(MyBean.class, inputMessage)); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(MyBean.class, inputMessage)); } @Test public void readValidJsonWithUnknownProperty() throws IOException { String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); converter.read(MyBean.class, inputMessage); // Assert no HttpMessageNotReadableException is thrown @@ -266,7 +268,7 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); inputMessage.getHeaders().setContentType(new MediaType("application", "json")); List results = (List) converter.read(List.class, inputMessage); @@ -280,7 +282,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, new MediaType("application", "json"), outputMessage); + converter.write(results, MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -296,8 +298,8 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); List results = (List) converter.read(beansList.getType(), null, inputMessage); @@ -311,7 +313,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage); + converter.write(results, beansList.getType(), MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -328,8 +330,8 @@ public class MappingJackson2HttpMessageConverterTests { "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); List results = (List) converter.read(beansList.getType(), null, inputMessage); @@ -343,7 +345,7 @@ public class MappingJackson2HttpMessageConverterTests { assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2}); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage); + converter.write(results, baseList.getType(), MediaType.APPLICATION_JSON, outputMessage); JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); } @@ -489,7 +491,7 @@ public class MappingJackson2HttpMessageConverterTests { public void readWithNoDefaultConstructor() throws Exception { String body = "{\"property1\":\"foo\",\"property2\":\"bar\"}"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); - inputMessage.getHeaders().setContentType(new MediaType("application", "json")); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); assertThatExceptionOfType(HttpMessageConversionException.class).isThrownBy(() -> converter.read(BeanWithNoDefaultConstructor.class, inputMessage)) .withMessageStartingWith("Type definition error:"); diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index f0b83166da..01dc97d84f 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java @@ -19,7 +19,7 @@ package org.springframework.http.converter.protobuf; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import com.google.protobuf.ExtensionRegistry; import com.google.protobuf.Message; @@ -158,7 +158,7 @@ public class ProtobufHttpMessageConverterTests { assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(contentType); - final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); + final String body = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(body.isEmpty()).as("body is empty").isFalse(); Msg.Builder builder = Msg.newBuilder(); @@ -182,7 +182,7 @@ public class ProtobufHttpMessageConverterTests { assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(contentType); - final String body = outputMessage.getBodyAsString(Charset.forName("UTF-8")); + final String body = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertThat(body.isEmpty()).as("body is empty").isFalse(); Msg.Builder builder = Msg.newBuilder(); @@ -197,7 +197,8 @@ public class ProtobufHttpMessageConverterTests { @Test public void defaultContentType() throws Exception { - assertThat(this.converter.getDefaultContentType(this.testMsg)).isEqualTo(ProtobufHttpMessageConverter.PROTOBUF); + assertThat(this.converter.getDefaultContentType(this.testMsg)) + .isEqualTo(ProtobufHttpMessageConverter.PROTOBUF); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java index 9d3e8470a7..231d003060 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java @@ -94,7 +94,8 @@ public class MappingJackson2SmileHttpMessageConverterTests { body.setBytes(new byte[]{0x1, 0x2}); 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")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "x-jackson-smile")); verify(outputMessage.getBody(), never()).close(); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java index 9c0c08646f..399e2e0b4b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverterTests.java @@ -99,7 +99,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlRootElementSet() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Set result = (Set) converter.read(rootElementSetType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -111,7 +111,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlTypeList() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); List result = (List) converter.read(typeListType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -123,7 +123,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { @SuppressWarnings("unchecked") public void readXmlTypeSet() throws Exception { String content = ""; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Set result = (Set) converter.read(typeSetType, null, inputMessage); assertThat(result.size()).as("Invalid result").isEqualTo(2); @@ -139,7 +139,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); converter = new Jaxb2CollectionHttpMessageConverter>() { @Override @@ -168,7 +168,7 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); Jaxb2CollectionHttpMessageConverter c = new Jaxb2CollectionHttpMessageConverter>() { @Override @@ -203,10 +203,10 @@ public class Jaxb2CollectionHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - this.converter.read(this.rootElementListType, null, inputMessage)) - .withMessageContaining("\"lol9\""); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> this.converter.read(this.rootElementListType, null, inputMessage)) + .withMessageContaining("\"lol9\""); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java index 202776f685..0324edcc10 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java @@ -85,21 +85,27 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void canRead() { - assertThat(converter.canRead(RootElement.class, null)).as("Converter does not support reading @XmlRootElement").isTrue(); - assertThat(converter.canRead(Type.class, null)).as("Converter does not support reading @XmlType").isTrue(); + assertThat(converter.canRead(RootElement.class, null)) + .as("Converter does not support reading @XmlRootElement").isTrue(); + assertThat(converter.canRead(Type.class, null)) + .as("Converter does not support reading @XmlType").isTrue(); } @Test public 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)).as("Converter does not support writing @XmlRootElement subclass").isTrue(); - assertThat(converter.canWrite(Type.class, null)).as("Converter supports writing @XmlType").isFalse(); + 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)) + .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 { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); InputStream inputStream = spy(new ByteArrayInputStream(body)); MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream); RootElement result = (RootElement) converter.read(RootElement.class, inputMessage); @@ -109,7 +115,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void readXmlRootElementSubclass() throws Exception { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage); assertThat(result.getType().s).as("Invalid result").isEqualTo("Hello World"); @@ -117,7 +123,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void readXmlType() throws Exception { - byte[] body = "".getBytes("UTF-8"); + byte[] body = "".getBytes(StandardCharsets.UTF_8); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); Type result = (Type) converter.read(Type.class, inputMessage); assertThat(result.s).as("Invalid result").isEqualTo("Hello World"); @@ -130,7 +136,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); converter.setSupportDtd(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); @@ -144,7 +150,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + " ]>" + " &ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); this.converter.setProcessExternalEntities(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); @@ -170,7 +176,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(RootElement.class, inputMessage)) .withMessageContaining("DOCTYPE"); @@ -180,7 +186,8 @@ public class Jaxb2RootElementHttpMessageConverterTests { public void writeXmlRootElement() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElement, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("", ev); @@ -191,7 +198,8 @@ public class Jaxb2RootElementHttpMessageConverterTests { public void writeXmlRootElementSubclass() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElementCglib, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("", ev); @@ -211,7 +219,7 @@ public class Jaxb2RootElementHttpMessageConverterTests { @Test public void customizeUnmarshaller() throws Exception { - byte[] body = "a|||b".getBytes("UTF-8"); + byte[] body = "a|||b".getBytes(StandardCharsets.UTF_8); MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter(); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); MyRootElement result = (MyRootElement) myConverter.read(MyRootElement.class, inputMessage); diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java index 631ff4848e..1deac303b7 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java @@ -110,15 +110,16 @@ public class MappingJackson2XmlHttpMessageConverterTests { assertThat(result.contains("FooBar")).isTrue(); assertThat(result.contains("true")).isTrue(); assertThat(result.contains("AQI=")).isTrue(); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml", StandardCharsets.UTF_8)); + 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 { String body = "FooBar"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(MyBean.class, inputMessage)); } @@ -126,8 +127,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { @Test public void readValidXmlWithUnknownProperty() throws IOException { String body = "stringvalue"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.read(MyBean.class, inputMessage); // Assert no HttpMessageNotReadableException is thrown } @@ -164,8 +165,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { new ClassPathResource("external.txt", getClass()).getURI() + "\" >]>&ext;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); @@ -191,8 +192,8 @@ public class MappingJackson2XmlHttpMessageConverterTests { "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage)); diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java index d79f9dc5ac..3bb9c6f887 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java @@ -108,12 +108,12 @@ public class MarshallingHttpMessageConverterTests { Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); - given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); + given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(3); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(String.class, inputMessage)) - .withCauseInstanceOf(TypeMismatchException.class); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(String.class, inputMessage)) + .withCauseInstanceOf(TypeMismatchException.class); } @Test @@ -127,9 +127,8 @@ public class MarshallingHttpMessageConverterTests { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); - assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> - converter.read(Object.class, inputMessage)) - .withCause(ex); + assertThatExceptionOfType(HttpMessageNotReadableException.class) + .isThrownBy(() -> converter.read(Object.class, inputMessage)).withCause(ex); } @Test @@ -143,7 +142,8 @@ public class MarshallingHttpMessageConverterTests { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); verify(outputMessage.getBody(), never()).close(); } @@ -157,13 +157,12 @@ public class MarshallingHttpMessageConverterTests { willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.class)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); - assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> - converter.write(body, null, outputMessage)) - .withCause(ex); + assertThatExceptionOfType(HttpMessageNotWritableException.class) + .isThrownBy(() -> converter.write(body, null, outputMessage)).withCause(ex); } @Test - public void supports() throws Exception { + public void supports() { assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> new MarshallingHttpMessageConverter().supports(Object.class)); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java index c2341a9bed..54f2fd1322 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/SourceHttpMessageConverterTests.java @@ -95,9 +95,9 @@ public class SourceHttpMessageConverterTests { @Test public void readDOMSource() throws Exception { - InputStream inputStream = spy(new ByteArrayInputStream(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")); + 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"); @@ -106,8 +106,8 @@ public class SourceHttpMessageConverterTests { @Test public void readDOMSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage); Document document = (Document) result.getNode(); @@ -134,7 +134,7 @@ public class SourceHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(DOMSource.class, inputMessage)) @@ -143,8 +143,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSAXSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream())); @@ -153,8 +153,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSAXSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); @@ -189,20 +189,19 @@ public class SourceHttpMessageConverterTests { "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); XMLReader reader = result.getXMLReader(); - assertThatExceptionOfType(SAXException.class).isThrownBy(() -> - reader.parse(inputSource)) - .withMessageContaining("DOCTYPE"); + assertThatExceptionOfType(SAXException.class) + .isThrownBy(() -> reader.parse(inputSource)).withMessageContaining("DOCTYPE"); } @Test public void readStAXSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); assertThat(streamReader.hasNext()).isTrue(); @@ -216,8 +215,8 @@ public class SourceHttpMessageConverterTests { @Test public void readStAXSourceExternal() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.setSupportDtd(true); StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); @@ -255,7 +254,7 @@ public class SourceHttpMessageConverterTests { " \n" + "]>\n" + "&lol9;"; - MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8)); StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage); XMLStreamReader streamReader = result.getXMLStreamReader(); @@ -264,15 +263,14 @@ public class SourceHttpMessageConverterTests { streamReader.next(); String s = streamReader.getLocalName(); assertThat(s).isEqualTo("root"); - assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() -> - streamReader.getElementText()) - .withMessageContaining("\"lol9\""); + assertThatExceptionOfType(XMLStreamException.class) + .isThrownBy(streamReader::getElementText).withMessageContaining("\"lol9\""); } @Test public void readStreamSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage); String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream())); assertThat(XmlContent.of(s)).isSimilarTo(BODY); @@ -280,8 +278,8 @@ public class SourceHttpMessageConverterTests { @Test public void readSource() throws Exception { - MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); - inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); + MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8)); + inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML); converter.read(Source.class, inputMessage); } @@ -299,8 +297,10 @@ public class SourceHttpMessageConverterTests { converter.write(domSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - 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); + assertThat(outputMessage.getHeaders().getContentType()) + .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(); } @@ -313,7 +313,8 @@ public class SourceHttpMessageConverterTests { converter.write(saxSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); } @Test @@ -325,7 +326,8 @@ public class SourceHttpMessageConverterTests { converter.write(streamSource, null, outputMessage); assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8))) .isSimilarTo("Hello World"); - assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(new MediaType("application", "xml")); + assertThat(outputMessage.getHeaders().getContentType()) + .as("Invalid content-type").isEqualTo(MediaType.APPLICATION_XML); } }