Consistent use of Charset.forName over JDK 7 StandardCharsets in 4.x line

This commit is contained in:
Juergen Hoeller
2016-08-26 13:11:39 +02:00
parent 696f687419
commit 2a82b8fed9
5 changed files with 50 additions and 51 deletions
@@ -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.
@@ -21,7 +21,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -145,8 +144,8 @@ import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
import org.springframework.web.util.UrlPathHelper;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
@@ -823,7 +822,7 @@ public class MvcNamespaceTests {
assertNotNull(scriptTemplateConfigurer);
assertEquals("render", scriptTemplateConfigurer.getRenderFunction());
assertEquals(MediaType.TEXT_PLAIN_VALUE, scriptTemplateConfigurer.getContentType());
assertEquals(StandardCharsets.ISO_8859_1, scriptTemplateConfigurer.getCharset());
assertEquals("ISO-8859-1", scriptTemplateConfigurer.getCharset().name());
assertEquals("classpath:", scriptTemplateConfigurer.getResourceLoaderPath());
assertFalse(scriptTemplateConfigurer.isSharedEngine());
String[] scripts = { "org/springframework/web/servlet/view/script/nashorn/render.js" };
@@ -956,18 +955,21 @@ public class MvcNamespaceTests {
public @interface IsoDate {
}
@NumberFormat(style = NumberFormat.Style.PERCENT)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface PercentNumber {
}
@Validated(MyGroup.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyValid {
}
@Controller
public static class TestController {
@@ -978,13 +980,16 @@ public class MvcNamespaceTests {
private boolean recordedValidationError;
@RequestMapping
public void testBind(@RequestParam @IsoDate Date date, @RequestParam(required = false) @PercentNumber Double percent, @MyValid TestBean bean, BindingResult result) {
public void testBind(@RequestParam @IsoDate Date date,
@RequestParam(required = false) @PercentNumber Double percent,
@MyValid TestBean bean, BindingResult result) {
this.date = date;
this.percent = percent;
this.recordedValidationError = (result.getErrorCount() == 1);
}
}
public static class TestValidator implements Validator {
boolean validatorInvoked;
@@ -1000,10 +1005,12 @@ public class MvcNamespaceTests {
}
}
@Retention(RetentionPolicy.RUNTIME)
public @interface MyGroup {
}
private static class TestBean {
@NotNull(groups = MyGroup.class)
@@ -1020,6 +1027,7 @@ public class MvcNamespaceTests {
}
}
private static class TestMockServletContext extends MockServletContext {
@Override
@@ -1033,12 +1041,15 @@ public class MvcNamespaceTests {
}
}
public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {
}
public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter {
}
public static class TestPathMatcher implements PathMatcher {
@Override
@@ -1077,9 +1088,11 @@ public class MvcNamespaceTests {
}
}
public static class TestPathHelper extends UrlPathHelper {
}
public static class TestCacheManager implements CacheManager {
@Override
@@ -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,7 +16,7 @@
package org.springframework.web.servlet.view.script;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -86,7 +86,7 @@ public class ScriptTemplateViewTests {
this.configurer.setRenderObject("Template");
this.configurer.setRenderFunction("render");
this.configurer.setContentType(MediaType.TEXT_PLAIN_VALUE);
this.configurer.setCharset(StandardCharsets.ISO_8859_1);
this.configurer.setCharset(Charset.forName("ISO-8859-1"));
this.configurer.setSharedEngine(true);
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
@@ -95,7 +95,7 @@ public class ScriptTemplateViewTests {
assertEquals("Template", accessor.getPropertyValue("renderObject"));
assertEquals("render", accessor.getPropertyValue("renderFunction"));
assertEquals(MediaType.TEXT_PLAIN_VALUE, accessor.getPropertyValue("contentType"));
assertEquals(StandardCharsets.ISO_8859_1, accessor.getPropertyValue("charset"));
assertEquals(Charset.forName("ISO-8859-1"), accessor.getPropertyValue("charset"));
assertEquals(true, accessor.getPropertyValue("sharedEngine"));
}
@@ -112,7 +112,7 @@ public class ScriptTemplateViewTests {
assertEquals("Template", accessor.getPropertyValue("renderObject"));
assertEquals("render", accessor.getPropertyValue("renderFunction"));
assertEquals(MediaType.TEXT_HTML_VALUE, accessor.getPropertyValue("contentType"));
assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), accessor.getPropertyValue("charset"));
}
@Test
@@ -128,7 +128,7 @@ public class ScriptTemplateViewTests {
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
assertNull(accessor.getPropertyValue("renderObject"));
assertEquals("render", accessor.getPropertyValue("renderFunction"));
assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), accessor.getPropertyValue("charset"));
}
@Test
@@ -252,19 +252,19 @@ public class ScriptTemplateViewTests {
this.view.render(model, request, response);
assertEquals(MediaType.TEXT_HTML_VALUE + ";charset=" +
StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE));
Charset.forName("UTF-8"), response.getHeader(HttpHeaders.CONTENT_TYPE));
response = new MockHttpServletResponse();
this.view.setContentType(MediaType.TEXT_PLAIN_VALUE);
this.view.render(model, request, response);
assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" +
StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE));
Charset.forName("UTF-8"), response.getHeader(HttpHeaders.CONTENT_TYPE));
response = new MockHttpServletResponse();
this.view.setCharset(StandardCharsets.ISO_8859_1);
this.view.setCharset(Charset.forName("ISO-8859-1"));
this.view.render(model, request, response);
assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" +
StandardCharsets.ISO_8859_1, response.getHeader(HttpHeaders.CONTENT_TYPE));
Charset.forName("ISO-8859-1"), response.getHeader(HttpHeaders.CONTENT_TYPE));
}