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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
@@ -33,7 +34,7 @@ import org.springframework.http.HttpOutputMessage;
*/
public class MockHttpOutputMessage implements HttpOutputMessage {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final HttpHeaders headers = new HttpHeaders();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,6 +18,7 @@ package org.springframework.test.web.client;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.http.HttpHeaders;
@@ -50,6 +51,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
this.mockMvc = mockMvc;
}
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) {
@@ -73,7 +75,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
return clientResponse;
}
catch (Exception ex) {
byte[] body = ex.toString().getBytes("UTF-8");
byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.client.response;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
@@ -56,6 +57,7 @@ public class DefaultResponseCreator implements ResponseCreator {
this.statusCode = statusCode;
}
@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
MockClientHttpResponse response;
@@ -74,13 +76,7 @@ public class DefaultResponseCreator implements ResponseCreator {
* Set the body as a UTF-8 String.
*/
public DefaultResponseCreator body(String content) {
try {
this.content = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
// should not happen, UTF-8 is always supported
throw new IllegalStateException(e);
}
this.content = content.getBytes(StandardCharsets.UTF_8);
return this;
}

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
@@ -272,12 +273,7 @@ public class MockHttpServletRequestBuilder
* @param content the body content
*/
public MockHttpServletRequestBuilder content(String content) {
try {
this.content = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
// should never happen
}
this.content = content.getBytes(StandardCharsets.UTF_8);
return this;
}
@@ -286,7 +282,6 @@ public class MockHttpServletRequestBuilder
* @param cookies the cookies to add
*/
public MockHttpServletRequestBuilder cookie(Cookie... cookies) {
Assert.notNull(cookies, "'cookies' must not be null");
Assert.notEmpty(cookies, "'cookies' must not be empty");
this.cookies.addAll(Arrays.asList(cookies));
return this;

View File

@@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.client.match;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Test;
@@ -100,7 +101,7 @@ public class ContentRequestMatchersTests {
String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3";
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
this.request.getBody().write(body.getBytes(Charset.forName("UTF-8")));
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name 1", "value 1");

View File

@@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.servlet.request;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
@@ -279,7 +281,7 @@ public class MockHttpServletRequestBuilderTests {
String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3";
MockHttpServletRequest request = new MockHttpServletRequestBuilder(HttpMethod.POST, "/foo")
.contentType(contentType).content(body.getBytes(Charset.forName("UTF-8")))
.contentType(contentType).content(body.getBytes(StandardCharsets.UTF_8))
.buildRequest(this.servletContext);
assertArrayEquals(new String[] {"value 1"}, request.getParameterMap().get("name 1"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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,10 +16,9 @@
package org.springframework.test.web.servlet.result;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -33,6 +32,9 @@ import org.springframework.util.StreamUtils;
*/
public class XpathResultMatchersTests {
private static final String RESPONSE_CONTENT = "<foo><bar>111</bar><bar>true</bar></foo>";
@Test
public void node() throws Exception {
new XpathResultMatchers("/foo/bar", null).node(Matchers.notNullValue()).match(getStubMvcResult());
@@ -107,7 +109,7 @@ public class XpathResultMatchersTests {
public void stringEncodingDetection() throws Exception {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<person><name>Jürgen</name></person>";
byte[] bytes = content.getBytes(Charset.forName("UTF-8"));
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/xml");
StreamUtils.copy(bytes, response.getOutputStream());
@@ -117,12 +119,10 @@ public class XpathResultMatchersTests {
}
private static final String RESPONSE_CONTENT = "<foo><bar>111</bar><bar>true</bar></foo>";
private StubMvcResult getStubMvcResult() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/xml");
response.getWriter().print(new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
return new StubMvcResult(null, null, null, null, null, null, response);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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,7 +17,7 @@
package org.springframework.test.web.servlet.samples.standalone;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import javax.servlet.Filter;
@@ -40,25 +40,21 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.multipart.MultipartFile;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* @author Rossen Stoyanchev
*/
public class FileUploadControllerTests {
private static final Charset CHARSET = Charset.forName("UTF-8");
@Test
public void multipartRequest() throws Exception {
byte[] fileContent = "bar".getBytes(CHARSET);
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(CHARSET);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
MockMvc mockMvc = standaloneSetup(new MultipartController()).build();
@@ -67,12 +63,9 @@ public class FileUploadControllerTests {
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
// SPR-13317
@Test
@Test // SPR-13317
public void multipartRequestWrapped() throws Exception {
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(CHARSET);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
Filter filter = new RequestWrappingFilter();
@@ -103,6 +96,7 @@ public class FileUploadControllerTests {
}
}
private static class RequestWrappingFilter extends OncePerRequestFilter {
@Override