Revise encoding steps towards use of JDK Charset and StandardCharsets

Issue: SPR-14492
This commit is contained in:
Juergen Hoeller
2016-07-19 23:43:05 +02:00
parent 79d30d8c8a
commit 99be15f58b
95 changed files with 480 additions and 569 deletions

View File

@@ -16,13 +16,9 @@
package org.springframework.web.servlet.mvc.method.annotation;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.web.servlet.HandlerMapping.*;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
@@ -56,6 +52,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.web.servlet.HandlerMapping.*;
/**
* Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock
* {@link HttpMessageConverter}.
@@ -158,7 +158,7 @@ public class HttpEntityMethodProcessorMockTests {
MediaType contentType = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Content-Type", contentType.toString());
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8")));
servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8));
given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true);
given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
@@ -180,7 +180,7 @@ public class HttpEntityMethodProcessorMockTests {
servletRequest.setServerName("www.example.com");
servletRequest.setServerPort(80);
servletRequest.setRequestURI("/path");
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8")));
servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8));
given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true);
given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
@@ -213,7 +213,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test(expected = HttpMediaTypeNotSupportedException.class)
public void resolveArgumentNoContentType() throws Exception {
servletRequest.setMethod("POST");
servletRequest.setContent("some content".getBytes(Charset.forName("UTF-8")));
servletRequest.setContent("some content".getBytes(StandardCharsets.UTF_8));
processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null);
fail("Expected exception");
}
@@ -522,7 +522,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test
public void handleReturnTypeResource() throws Exception {
ResponseEntity<Resource> returnValue = ResponseEntity
.ok(new ByteArrayResource("Content".getBytes(Charset.forName("UTF-8"))));
.ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));

View File

@@ -17,7 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -131,7 +131,7 @@ public class RequestPartMethodArgumentResolverTests {
resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
reset(messageConverter);
byte[] content = "doesn't matter as long as not empty".getBytes(Charset.forName("UTF-8"));
byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);

View File

@@ -16,15 +16,11 @@
package org.springframework.web.servlet.mvc.method.annotation;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@@ -54,6 +50,9 @@ import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerMapping;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* Test fixture for {@link RequestResponseBodyMethodProcessor} delegating to a
* mock HttpMessageConverter.
@@ -136,7 +135,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
servletRequest.addHeader("Content-Type", contentType.toString());
String body = "Foo";
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8")));
servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8));
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(true);
given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
@@ -168,7 +167,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception {
MediaType contentType = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Content-Type", contentType.toString());
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
@SuppressWarnings("unchecked")
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
@@ -184,7 +183,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
public void resolveArgumentCannotRead() throws Exception {
MediaType contentType = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Content-Type", contentType.toString());
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(false);
@@ -193,7 +192,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
@Test(expected = HttpMediaTypeNotSupportedException.class)
public void resolveArgumentNoContentType() throws Exception {
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
}
@@ -201,7 +200,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
@Test(expected = HttpMediaTypeNotSupportedException.class)
public void resolveArgumentInvalidContentType() throws Exception {
this.servletRequest.setContentType("bad");
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8));
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
}
@@ -299,7 +298,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
@Test
public void handleReturnTypeResource() throws Exception {
Resource returnValue = new ByteArrayResource("Content".getBytes(Charset.forName("UTF-8")));
Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8));
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));

View File

@@ -17,6 +17,7 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -62,7 +63,6 @@ public class StreamingResponseBodyReturnValueHandlerTests {
@Before
public void setUp() throws Exception {
this.handler = new StreamingResponseBodyReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();
@@ -86,12 +86,11 @@ public class StreamingResponseBodyReturnValueHandlerTests {
@Test
public void streamingResponseBody() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
MethodParameter returnType = returnType(TestController.class, "handle");
StreamingResponseBody streamingBody = outputStream -> {
outputStream.write("foo".getBytes(Charset.forName("UTF-8")));
outputStream.write("foo".getBytes(StandardCharsets.UTF_8));
latch.countDown();
};
this.handler.handleReturnValue(streamingBody, returnType, this.mavContainer, this.webRequest);
@@ -104,13 +103,12 @@ public class StreamingResponseBodyReturnValueHandlerTests {
@Test
public void responseEntity() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
ResponseEntity<StreamingResponseBody> emitter = ResponseEntity.ok().header("foo", "bar")
.body(outputStream -> {
outputStream.write("foo".getBytes(Charset.forName("UTF-8")));
outputStream.write("foo".getBytes(StandardCharsets.UTF_8));
latch.countDown();
});
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);