Revise charset by location support for static resources

This commit is contained in:
Rossen Stoyanchev
2017-11-17 12:55:11 -05:00
parent 03fef655ff
commit 2a309a031a
8 changed files with 108 additions and 141 deletions
@@ -21,8 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -47,14 +47,13 @@ import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.annotation.NumberFormat;
@@ -86,7 +85,7 @@ import org.springframework.web.context.request.async.CallableProcessingIntercept
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptorAdapter;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.CompositeUriComponentsContributor;
@@ -174,7 +173,7 @@ public class MvcNamespaceTests {
"org.springframework.web.servlet.config.viewControllerHandlerMapping";
private GenericWebApplicationContext appContext;
private XmlWebApplicationContext appContext;
private TestController handler;
@@ -184,7 +183,7 @@ public class MvcNamespaceTests {
@Before
public void setUp() throws Exception {
TestMockServletContext servletContext = new TestMockServletContext();
appContext = new GenericWebApplicationContext();
appContext = new XmlWebApplicationContext();
appContext.setServletContext(servletContext);
LocaleContextHolder.setLocale(Locale.US);
@@ -435,8 +434,6 @@ public class MvcNamespaceTests {
assertNotNull(handler);
assertNotNull(handler.getUrlPathHelper());
assertEquals(1, handler.getLocationCharsets().size());
assertEquals(StandardCharsets.ISO_8859_1, handler.getLocationCharsets().values().iterator().next());
List<ResourceResolver> resolvers = handler.getResourceResolvers();
assertThat(resolvers, Matchers.hasSize(4));
@@ -456,8 +453,9 @@ public class MvcNamespaceTests {
Matchers.instanceOf(ContentVersionStrategy.class));
PathResourceResolver pathResolver = (PathResourceResolver) resolvers.get(3);
assertEquals(1, pathResolver.getLocationCharsets().size());
assertEquals(StandardCharsets.ISO_8859_1, handler.getLocationCharsets().values().iterator().next());
Map<Resource, Charset> locationCharsets = pathResolver.getLocationCharsets();
assertEquals(1, locationCharsets.size());
assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());
List<ResourceTransformer> transformers = handler.getResourceTransformers();
assertThat(transformers, Matchers.hasSize(3));
@@ -967,11 +965,8 @@ public class MvcNamespaceTests {
private void loadBeanDefinitions(String fileName) {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
ClassPathResource resource = new ClassPathResource(fileName, AnnotationDrivenBeanDefinitionParserTests.class);
reader.loadBeanDefinitions(resource);
String names = Arrays.toString(this.appContext.getBeanDefinitionNames());
appContext.refresh();
this.appContext.setConfigLocation("classpath:org/springframework/web/servlet/config/" + fileName);
this.appContext.refresh();
}
@@ -16,8 +16,10 @@
package org.springframework.web.servlet.config.annotation;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Before;
@@ -25,11 +27,12 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.CacheControl;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.http.CacheControl;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.HandlerMapping;
@@ -46,7 +49,12 @@ import org.springframework.web.servlet.resource.VersionResourceResolver;
import org.springframework.web.servlet.resource.WebJarsResourceResolver;
import org.springframework.web.util.UrlPathHelper;
import static org.junit.Assert.*;
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.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link ResourceHandlerRegistry}.
@@ -64,8 +72,11 @@ public class ResourceHandlerRegistryTests {
@Before
public void setUp() {
this.registry = new ResourceHandlerRegistry(new GenericWebApplicationContext(),
new MockServletContext(), new ContentNegotiationManager(), new UrlPathHelper());
GenericWebApplicationContext appContext = new GenericWebApplicationContext();
appContext.refresh();
this.registry = new ResourceHandlerRegistry(appContext, new MockServletContext(),
new ContentNegotiationManager(), new UrlPathHelper());
this.registration = this.registry.addResourceHandler("/resources/**");
this.registration.addResourceLocations("classpath:org/springframework/web/servlet/config/annotation/");
@@ -226,13 +237,12 @@ public class ResourceHandlerRegistryTests {
UrlResource resource = (UrlResource) handler.getLocations().get(1);
assertEquals("file:/tmp", resource.getURL().toString());
assertNotNull(handler.getUrlPathHelper());
assertEquals(1, handler.getLocationCharsets().size());
assertEquals(StandardCharsets.ISO_8859_1, handler.getLocationCharsets().get(resource));
List<ResourceResolver> resolvers = handler.getResourceResolvers();
PathResourceResolver resolver = (PathResourceResolver) resolvers.get(resolvers.size()-1);
assertEquals(1, resolver.getLocationCharsets().size());
assertEquals(StandardCharsets.ISO_8859_1, handler.getLocationCharsets().values().iterator().next());
Map<Resource, Charset> locationCharsets = resolver.getLocationCharsets();
assertEquals(1, locationCharsets.size());
assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());
}
private ResourceHttpRequestHandler getHandler(String pathPattern) {