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

@@ -17,7 +17,7 @@ package org.springframework.web.reactive.result.method.annotation;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@@ -56,12 +56,8 @@ import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
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.assertTrue;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
/**
* Unit tests for {@link HttpEntityArgumentResolver}.When adding a test also
@@ -288,7 +284,7 @@ public class HttpEntityArgumentResolverTests {
}
private DataBuffer dataBuffer(String body) {
byte[] bytes = body.getBytes(Charset.forName("UTF-8"));
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new DefaultDataBufferFactory().wrap(byteBuffer);
}

View File

@@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method.annotation;
import java.io.Serializable;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -64,12 +65,8 @@ import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
/**
* Unit tests for {@link AbstractMessageReaderArgumentResolver}.
@@ -289,7 +286,7 @@ public class MessageReaderArgumentResolverTests {
}
private DataBuffer dataBuffer(String body) {
byte[] bytes = body.getBytes(Charset.forName("UTF-8"));
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new DefaultDataBufferFactory().wrap(byteBuffer);
}

View File

@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method.annotation;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -63,11 +64,9 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
import static org.junit.Assert.*;
import static org.springframework.http.MediaType.*;
import static org.springframework.web.reactive.HandlerMapping.*;
/**
* Unit tests for {@link AbstractMessageWriterResultHandler}.
@@ -90,7 +89,7 @@ public class MessageWriterResultHandlerTests {
}
@Test // SPR-12894
@Test // SPR-12894
public void useDefaultContentType() throws Exception {
Resource body = new ClassPathResource("logo.png", getClass());
ResolvableType type = ResolvableType.forType(Resource.class);
@@ -99,7 +98,7 @@ public class MessageWriterResultHandlerTests {
assertEquals("image/x-png", this.response.getHeaders().getFirst("Content-Type"));
}
@Test // SPR-13631
@Test // SPR-13631
public void useDefaultCharset() throws Exception {
this.exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE,
Collections.singleton(APPLICATION_JSON));
@@ -126,7 +125,7 @@ public class MessageWriterResultHandlerTests {
assertNull(this.response.getBody());
}
@Test // SPR-13135
@Test // SPR-13135
public void unsupportedReturnType() throws Exception {
ByteArrayOutputStream body = new ByteArrayOutputStream();
ResolvableType type = ResolvableType.forType(OutputStream.class);
@@ -137,7 +136,7 @@ public class MessageWriterResultHandlerTests {
TestSubscriber.subscribe(mono).assertError(IllegalStateException.class);
}
@Test // SPR-12811
@Test // SPR-12811
public void jacksonTypeOfListElement() throws Exception {
List<ParentClass> body = Arrays.asList(new Foo("foo"), new Bar("bar"));
ResolvableType type = ResolvableType.forClassWithGenerics(List.class, ParentClass.class);
@@ -148,7 +147,7 @@ public class MessageWriterResultHandlerTests {
"{\"type\":\"bar\",\"parentProperty\":\"bar\"}]");
}
@Test // SPR-13318
@Test // SPR-13318
@Ignore
public void jacksonTypeWithSubType() throws Exception {
SimpleBean body = new SimpleBean(123L, "foo");
@@ -159,7 +158,7 @@ public class MessageWriterResultHandlerTests {
assertResponseBody("{\"id\":123,\"name\":\"foo\"}");
}
@Test // SPR-13318
@Test // SPR-13318
@Ignore
public void jacksonTypeWithSubTypeOfListElement() throws Exception {
List<SimpleBean> body = Arrays.asList(new SimpleBean(123L, "foo"), new SimpleBean(456L, "bar"));
@@ -201,7 +200,7 @@ public class MessageWriterResultHandlerTests {
private void assertResponseBody(String responseBody) {
TestSubscriber.subscribe(this.response.getBody())
.assertValuesWith(buf -> assertEquals(responseBody,
DataBufferTestUtils.dumpString(buf, Charset.forName("UTF-8"))));
DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8)));
}

View File

@@ -19,6 +19,7 @@ package org.springframework.web.reactive.result.method.annotation;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@@ -246,7 +247,7 @@ public class RequestBodyArgumentResolverTests {
}
private DataBuffer dataBuffer(String body) {
byte[] bytes = body.getBytes(Charset.forName("UTF-8"));
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return new DefaultDataBufferFactory().wrap(byteBuffer);
}

View File

@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.method.annotation;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -58,18 +59,15 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.*;
/**
* Unit tests for {@link ResponseEntityResultHandler}. When adding a test also
* consider whether the logic under test is in a parent class, then see:
* <ul>
* <li>{@code MessageWriterResultHandlerTests},
* <li>{@code ContentNegotiatingResultHandlerSupportTests}
* <li>{@code MessageWriterResultHandlerTests},
* <li>{@code ContentNegotiatingResultHandlerSupportTests}
* </ul>
* @author Rossen Stoyanchev
*/
@@ -206,7 +204,7 @@ public class ResponseEntityResultHandlerTests {
private void assertResponseBody(String responseBody) {
TestSubscriber.subscribe(this.response.getBody())
.assertValuesWith(buf -> assertEquals(responseBody,
DataBufferTestUtils.dumpString(buf, Charset.forName("UTF-8"))));
DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8)));
}

View File

@@ -172,7 +172,7 @@ public class HttpMessageWriterViewTests {
TestSubscriber
.subscribe(response.getBody())
.assertValuesWith(buf -> assertEquals("{\"foo\":\"f\",\"bar\":\"b\"}",
DataBufferTestUtils.dumpString(buf, Charset.forName("UTF-8"))));
DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8)));
}

View File

@@ -19,7 +19,7 @@ package org.springframework.web.reactive.result.view;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
@@ -62,13 +62,13 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.DefaultWebSessionManager;
import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.http.MediaType.*;
/**
* Unit tests for {@link ViewResolutionResultHandler}.
*
* @author Rossen Stoyanchev
*/
public class ViewResolutionResultHandlerTests {
@@ -289,7 +289,7 @@ public class ViewResolutionResultHandlerTests {
private void assertResponseBody(String responseBody) {
TestSubscriber.subscribe(this.response.getBody())
.assertValuesWith(buf -> assertEquals(responseBody,
DataBufferTestUtils.dumpString(buf, Charset.forName("UTF-8"))));
DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8)));
}
@@ -299,7 +299,6 @@ public class ViewResolutionResultHandlerTests {
private int order = Ordered.LOWEST_PRECEDENCE;
TestViewResolver(String... viewNames) {
Arrays.stream(viewNames).forEach(name -> this.views.put(name, new TestView(name)));
}
@@ -318,9 +317,9 @@ public class ViewResolutionResultHandlerTests {
View view = this.views.get(viewName);
return Mono.justOrEmpty(view);
}
}
private static final class TestView implements View {
private final String name;
@@ -355,12 +354,13 @@ public class ViewResolutionResultHandlerTests {
if (mediaType != null) {
response.getHeaders().setContentType(mediaType);
}
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(Charset.forName("UTF-8")));
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8));
DataBuffer dataBuffer = new DefaultDataBufferFactory().wrap(byteBuffer);
return response.writeWith(Flux.just(dataBuffer));
}
}
private static class TestBean {
private final String name;
@@ -379,6 +379,7 @@ public class ViewResolutionResultHandlerTests {
}
}
@SuppressWarnings("unused")
private static class TestController {
@@ -406,4 +407,4 @@ public class ViewResolutionResultHandlerTests {
Long longAttribute() { return null; }
}
}
}

View File

@@ -17,7 +17,7 @@ package org.springframework.web.reactive.result.view.freemarker;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import freemarker.template.Configuration;
@@ -42,8 +42,7 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.DefaultWebSessionManager;
import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* @author Rossen Stoyanchev
@@ -52,8 +51,6 @@ public class FreeMarkerViewTests {
public static final String TEMPLATE_PATH = "classpath*:org/springframework/web/reactive/view/freemarker/";
private static final Charset UTF_8 = Charset.forName("UTF-8");
private ServerWebExchange exchange;
@@ -140,7 +137,7 @@ public class FreeMarkerViewTests {
ByteBuffer byteBuffer = dataBuffer.asByteBuffer();
final byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
return new String(bytes, UTF_8);
return new String(bytes, StandardCharsets.UTF_8);
}