Consistent use of JDK 7 StandardCharsets over Charset.forName

Issue: SPR-14492
This commit is contained in:
Juergen Hoeller
2016-08-26 14:16:19 +02:00
parent 50040e6139
commit 2e4a7480fc
18 changed files with 82 additions and 106 deletions

View File

@@ -88,7 +88,7 @@ public class HttpHeadersTests {
@Test
public void acceptCharsets() {
Charset charset1 = StandardCharsets.UTF_8;
Charset charset2 = Charset.forName("ISO-8859-1");
Charset charset2 = StandardCharsets.ISO_8859_1;
List<Charset> charsets = new ArrayList<>(2);
charsets.add(charset1);
charsets.add(charset2);
@@ -100,7 +100,7 @@ public class HttpHeadersTests {
@Test
public void acceptCharsetWildcard() {
headers.set("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
assertEquals("Invalid Accept header", Arrays.asList(Charset.forName("ISO-8859-1"), StandardCharsets.UTF_8),
assertEquals("Invalid Accept header", Arrays.asList(StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8),
headers.getAcceptCharset());
}

View File

@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.xml.transform.Source;
@@ -81,9 +80,8 @@ public class FormHttpMessageConverterTests {
@Test
public void readForm() throws Exception {
String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
Charset iso88591 = Charset.forName("ISO-8859-1");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(iso88591));
inputMessage.getHeaders().setContentType(new MediaType("application", "x-www-form-urlencoded", iso88591));
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.ISO_8859_1));
inputMessage.getHeaders().setContentType(new MediaType("application", "x-www-form-urlencoded", StandardCharsets.ISO_8859_1));
MultiValueMap<String, String> result = this.converter.read(null, inputMessage);
assertEquals("Invalid result", 3, result.size());

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.
@@ -18,7 +18,7 @@ package org.springframework.http.converter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Locale;
@@ -97,9 +97,8 @@ public class ObjectToStringHttpMessageConverterTests {
@Test
public void defaultCharsetModified() throws IOException {
Charset charset = Charset.forName("UTF-16");
ConversionService cs = new DefaultConversionService();
ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, charset);
ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, StandardCharsets.UTF_16);
converter.write((byte) 31, null, this.response);
assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
@@ -123,28 +122,21 @@ public class ObjectToStringHttpMessageConverterTests {
@Test
public void read() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
Short shortValue = Short.valueOf((short) 781);
request.setContent(shortValue.toString().getBytes(
StringHttpMessageConverter.DEFAULT_CHARSET));
assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
Float floatValue = Float.valueOf(123);
request.setCharacterEncoding("UTF-16");
request.setContent(floatValue.toString().getBytes("UTF-16"));
assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
Long longValue = Long.valueOf(55819182821331L);
request.setCharacterEncoding("UTF-8");
request.setContent(longValue.toString().getBytes("UTF-8"));
assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
}
@@ -160,7 +152,7 @@ public class ObjectToStringHttpMessageConverterTests {
@Test
public void writeUtf16() throws IOException {
MediaType contentType = new MediaType("text", "plain", Charset.forName("UTF-16"));
MediaType contentType = new MediaType("text", "plain", StandardCharsets.UTF_16);
this.converter.write(Integer.valueOf(958), contentType, this.response);
assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());

View File

@@ -17,7 +17,6 @@
package org.springframework.http.converter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
@@ -73,14 +72,13 @@ public class StringHttpMessageConverterTests {
@Test
public void writeDefaultCharset() throws IOException {
Charset iso88591 = Charset.forName("ISO-8859-1");
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, null, this.outputMessage);
HttpHeaders headers = this.outputMessage.getHeaders();
assertEquals(body, this.outputMessage.getBodyAsString(iso88591));
assertEquals(new MediaType("text", "plain", iso88591), headers.getContentType());
assertEquals(body.getBytes(iso88591).length, headers.getContentLength());
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1));
assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
}

View File

@@ -131,12 +131,11 @@ public class GsonHttpMessageConverterTests {
@Test
public void writeUTF16() throws IOException {
Charset utf16 = Charset.forName("UTF-16BE");
MediaType contentType = new MediaType("application", "json", utf16);
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
String body = "H\u00e9llo W\u00f6rld";
this.converter.write(body, contentType, outputMessage);
assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(utf16));
assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE));
assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
}

View File

@@ -18,7 +18,6 @@ package org.springframework.http.converter.json;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
@@ -132,12 +131,11 @@ public class MappingJackson2HttpMessageConverterTests {
@Test
public void writeUTF16() throws IOException {
Charset utf16 = Charset.forName("UTF-16BE");
MediaType contentType = new MediaType("application", "json", utf16);
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
String body = "H\u00e9llo W\u00f6rld";
converter.write(body, contentType, outputMessage);
assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(utf16));
assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE));
assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
}

View File

@@ -19,7 +19,7 @@ package org.springframework.web.client;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
@@ -50,14 +50,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
@@ -185,7 +178,7 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
@Test
public void postForLocation() throws Exception {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
Future<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");
URI location = locationFuture.get();
@@ -195,7 +188,7 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
@Test
public void postForLocationCallback() throws Exception {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
final URI expected = new URI(baseUrl + "/post/1");
ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");
@@ -215,7 +208,7 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
@Test
public void postForLocationCallbackWithLambdas() throws Exception {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
final URI expected = new URI(baseUrl + "/post/1");
ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");

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.
@@ -21,7 +21,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
@@ -40,12 +40,12 @@ public class HttpStatusCodeExceptionTests {
/**
* Corners bug SPR-9273, which reported the fact that following the changes made in
* SPR-7591, {@link HttpStatusCodeException} and subtypes became no longer
* serializable due to the addition of a non-serializable {@link Charset} field.
* serializable due to the addition of a non-serializable {@code Charset} field.
*/
@Test
public void testSerializability() throws IOException, ClassNotFoundException {
HttpStatusCodeException ex1 = new HttpClientErrorException(
HttpStatus.BAD_REQUEST, null, null, Charset.forName("US-ASCII"));
HttpStatus.BAD_REQUEST, null, null, StandardCharsets.US_ASCII);
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(ex1);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

View File

@@ -19,7 +19,6 @@ package org.springframework.web.client;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.EnumSet;
@@ -112,7 +111,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
@Test
public void postForLocationEntity() throws URISyntaxException {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
URI location = template.postForLocation(baseUrl + "/{method}", entity, "post");
assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location);
@@ -264,9 +263,12 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
assertTrue(content.contains("\"type\":\"bar\""));
}
public interface MyJacksonView1 {};
public interface MyJacksonView2 {};
public static class MySampleBean {
@JsonView(MyJacksonView1.class)
@@ -311,6 +313,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public static class ParentClass {
@@ -332,6 +335,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
}
}
@JsonTypeName("foo")
public static class Foo extends ParentClass {
@@ -343,6 +347,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
}
}
@JsonTypeName("bar")
public static class Bar extends ParentClass {

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,8 +17,7 @@
package org.springframework.web.multipart.support;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
@@ -33,9 +32,7 @@ import org.springframework.mock.web.test.MockMultipartHttpServletRequest;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
/**
* @author Rossen Stoyanchev
@@ -112,12 +109,12 @@ public class RequestPartServletServerHttpRequestTests {
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "octet-stream", Charset.forName("iso-8859-1")));
headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1));
return headers;
}
};
byte[] bytes = {(byte) 0xC4};
mockRequest.setParameter("part", new String(bytes, Charset.forName("iso-8859-1")));
mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
@@ -136,7 +133,7 @@ public class RequestPartServletServerHttpRequestTests {
}
};
byte[] bytes = {(byte) 0xC4};
mockRequest.setParameter("part", new String(bytes, Charset.forName("iso-8859-1")));
mockRequest.setParameter("part", new String(bytes, StandardCharsets.ISO_8859_1));
mockRequest.setCharacterEncoding("iso-8859-1");
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");