|
|
|
@ -60,10 +60,21 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
|
|
|
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
|
|
|
|
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
|
|
|
|
import org.springframework.web.servlet.resource.AppCacheManifestTransformer;
|
|
|
|
|
import org.springframework.web.servlet.resource.CachingResourceResolver;
|
|
|
|
|
import org.springframework.web.servlet.resource.CachingResourceTransformer;
|
|
|
|
|
import org.springframework.web.servlet.resource.ContentVersionStrategy;
|
|
|
|
|
import org.springframework.web.servlet.resource.CssLinkResourceTransformer;
|
|
|
|
|
import org.springframework.web.servlet.resource.FixedVersionStrategy;
|
|
|
|
|
import org.springframework.web.servlet.resource.PathResourceResolver;
|
|
|
|
|
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
|
|
|
|
import org.springframework.web.servlet.resource.ResourceResolver;
|
|
|
|
|
import org.springframework.web.servlet.resource.ResourceTransformer;
|
|
|
|
|
import org.springframework.web.servlet.resource.VersionResourceResolver;
|
|
|
|
|
import org.springframework.web.servlet.view.AbstractView;
|
|
|
|
|
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.contains;
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
import static org.hamcrest.Matchers.instanceOf;
|
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
@ -82,6 +93,7 @@ import static org.junit.Assert.assertThat;
|
|
|
|
|
* @author Dave Syer
|
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
|
*/
|
|
|
|
|
public class WebMvcAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
@ -124,6 +136,10 @@ public class WebMvcAutoConfigurationTests {
|
|
|
|
|
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
|
|
|
|
|
assertThat(mappingLocations.get("/webjars/**").get(0),
|
|
|
|
|
equalTo((Resource) new ClassPathResource("/META-INF/resources/webjars/")));
|
|
|
|
|
assertThat(getResourceResolvers("/webjars/**").size(), equalTo(1));
|
|
|
|
|
assertThat(getResourceTransformers("/webjars/**").size(), equalTo(0));
|
|
|
|
|
assertThat(getResourceResolvers("/**").size(), equalTo(1));
|
|
|
|
|
assertThat(getResourceTransformers("/**").size(), equalTo(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -151,6 +167,84 @@ public class WebMvcAutoConfigurationTests {
|
|
|
|
|
assertThat(mappingLocations.size(), equalTo(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void resourceHandlerChainEnabled() throws Exception {
|
|
|
|
|
load("spring.resources.chain.enabled:true");
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/webjars/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceTransformers("/webjars/**").size(), equalTo(1));
|
|
|
|
|
assertThat(getResourceResolvers("/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceTransformers("/**").size(), equalTo(1));
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/**"), contains(instanceOf(CachingResourceResolver.class),
|
|
|
|
|
instanceOf(PathResourceResolver.class)));
|
|
|
|
|
assertThat(getResourceTransformers("/**"), contains(instanceOf(CachingResourceTransformer.class)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void resourceHandlerFixedStrategyEnabled() throws Exception {
|
|
|
|
|
load("spring.resources.chain.strategy.fixed.enabled:true",
|
|
|
|
|
"spring.resources.chain.strategy.fixed.version:test",
|
|
|
|
|
"spring.resources.chain.strategy.fixed.paths:/**/*.js");
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/webjars/**").size(), equalTo(3));
|
|
|
|
|
assertThat(getResourceTransformers("/webjars/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceResolvers("/**").size(), equalTo(3));
|
|
|
|
|
assertThat(getResourceTransformers("/**").size(), equalTo(2));
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/**"), contains(instanceOf(CachingResourceResolver.class),
|
|
|
|
|
instanceOf(VersionResourceResolver.class),
|
|
|
|
|
instanceOf(PathResourceResolver.class)));
|
|
|
|
|
assertThat(getResourceTransformers("/**"), contains(instanceOf(CachingResourceTransformer.class),
|
|
|
|
|
instanceOf(CssLinkResourceTransformer.class)));
|
|
|
|
|
VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers("/**").get(1);
|
|
|
|
|
assertThat(resolver.getStrategyMap().get("/**/*.js"), instanceOf(FixedVersionStrategy.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void resourceHandlerContentStrategyEnabled() throws Exception {
|
|
|
|
|
load("spring.resources.chain.strategy.content.enabled:true",
|
|
|
|
|
"spring.resources.chain.strategy.content.paths:/**,/*.png");
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/webjars/**").size(), equalTo(3));
|
|
|
|
|
assertThat(getResourceTransformers("/webjars/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceResolvers("/**").size(), equalTo(3));
|
|
|
|
|
assertThat(getResourceTransformers("/**").size(), equalTo(2));
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/**"), contains(instanceOf(CachingResourceResolver.class),
|
|
|
|
|
instanceOf(VersionResourceResolver.class),
|
|
|
|
|
instanceOf(PathResourceResolver.class)));
|
|
|
|
|
assertThat(getResourceTransformers("/**"), contains(instanceOf(CachingResourceTransformer.class),
|
|
|
|
|
instanceOf(CssLinkResourceTransformer.class)));
|
|
|
|
|
VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers("/**").get(1);
|
|
|
|
|
assertThat(resolver.getStrategyMap().get("/*.png"), instanceOf(ContentVersionStrategy.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void resourceHandlerChainCustomized() throws Exception {
|
|
|
|
|
load("spring.resources.chain.enabled:true", "spring.resources.chain.cache:false",
|
|
|
|
|
"spring.resources.chain.strategy.content.enabled:true",
|
|
|
|
|
"spring.resources.chain.strategy.content.paths:/**,/*.png",
|
|
|
|
|
"spring.resources.chain.strategy.fixed.enabled:true",
|
|
|
|
|
"spring.resources.chain.strategy.fixed.version:test",
|
|
|
|
|
"spring.resources.chain.strategy.fixed.paths:/**/*.js",
|
|
|
|
|
"spring.resources.chain.html5AppCache:true");
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/webjars/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceTransformers("/webjars/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceResolvers("/**").size(), equalTo(2));
|
|
|
|
|
assertThat(getResourceTransformers("/**").size(), equalTo(2));
|
|
|
|
|
|
|
|
|
|
assertThat(getResourceResolvers("/**"), contains(
|
|
|
|
|
instanceOf(VersionResourceResolver.class), instanceOf(PathResourceResolver.class)));
|
|
|
|
|
assertThat(getResourceTransformers("/**"), contains(instanceOf(CssLinkResourceTransformer.class),
|
|
|
|
|
instanceOf(AppCacheManifestTransformer.class)));
|
|
|
|
|
|
|
|
|
|
VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers("/**").get(0);
|
|
|
|
|
assertThat(resolver.getStrategyMap().get("/*.png"), instanceOf(ContentVersionStrategy.class));
|
|
|
|
|
assertThat(resolver.getStrategyMap().get("/**/*.js"), instanceOf(FixedVersionStrategy.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void noLocaleResolver() throws Exception {
|
|
|
|
|
load(AllResources.class);
|
|
|
|
@ -220,6 +314,18 @@ public class WebMvcAutoConfigurationTests {
|
|
|
|
|
return getMappingLocations(mapping);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<ResourceResolver> getResourceResolvers(String mapping) {
|
|
|
|
|
SimpleUrlHandlerMapping handler = (SimpleUrlHandlerMapping) this.context.getBean("resourceHandlerMapping");
|
|
|
|
|
ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler.getHandlerMap().get(mapping);
|
|
|
|
|
return resourceHandler.getResourceResolvers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<ResourceTransformer> getResourceTransformers(String mapping) {
|
|
|
|
|
SimpleUrlHandlerMapping handler = (SimpleUrlHandlerMapping) this.context.getBean("resourceHandlerMapping");
|
|
|
|
|
ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler.getHandlerMap().get(mapping);
|
|
|
|
|
return resourceHandler.getResourceTransformers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping)
|
|
|
|
|
throws IllegalAccessException {
|
|
|
|
|