Consistent configurer access in WebMvcConfigurationSupport

Issue: SPR-16017

(cherry picked from commit 40ba95f)
This commit is contained in:
Juergen Hoeller
2017-09-27 19:00:51 +02:00
parent 00c0d7847f
commit a1a7c62127
12 changed files with 104 additions and 97 deletions

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.config.annotation;
import java.util.Arrays;
@@ -28,7 +29,7 @@ import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Test fixture for {@link ContentNegotiationConfigurer} tests.
@@ -42,6 +43,7 @@ public class ContentNegotiationConfigurerTests {
private MockHttpServletRequest servletRequest;
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest();
@@ -49,9 +51,10 @@ public class ContentNegotiationConfigurerTests {
this.configurer = new ContentNegotiationConfigurer(this.servletRequest.getServletContext());
}
@Test
public void defaultSettings() throws Exception {
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
this.servletRequest.setRequestURI("/flower.gif");
@@ -74,7 +77,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void addMediaTypes() throws Exception {
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
this.servletRequest.setRequestURI("/flower.json");
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
@@ -85,7 +88,7 @@ public class ContentNegotiationConfigurerTests {
this.configurer.favorParameter(true);
this.configurer.parameterName("f");
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addParameter("f", "json");
@@ -96,7 +99,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void ignoreAcceptHeader() throws Exception {
this.configurer.ignoreAcceptHeader(true);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
@@ -107,7 +110,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void setDefaultContentType() throws Exception {
this.configurer.defaultContentType(MediaType.APPLICATION_JSON);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
}
@@ -115,7 +118,7 @@ public class ContentNegotiationConfigurerTests {
@Test
public void setMultipleDefaultContentTypes() throws Exception {
this.configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.ALL);
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL), manager.resolveMediaTypes(this.webRequest));
}
@@ -123,8 +126,9 @@ public class ContentNegotiationConfigurerTests {
@Test
public void setDefaultContentTypeStrategy() throws Exception {
this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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.
@@ -43,23 +43,25 @@ public class DefaultServletHandlerConfigurerTests {
private MockHttpServletResponse response;
@Before
public void setUp() {
public void setup() {
response = new MockHttpServletResponse();
servletContext = new DispatchingMockServletContext();
configurer = new DefaultServletHandlerConfigurer(servletContext);
}
@Test
public void notEnabled() {
assertNull(configurer.getHandlerMapping());
assertNull(configurer.buildHandlerMapping());
}
@Test
public void enable() throws Exception {
configurer.enable();
SimpleUrlHandlerMapping getHandlerMapping = getHandlerMapping();
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping;
SimpleUrlHandlerMapping getHandlerMapping = configurer.buildHandlerMapping();
SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
@@ -75,7 +77,7 @@ public class DefaultServletHandlerConfigurerTests {
@Test
public void enableWithServletName() throws Exception {
configurer.enable("defaultServlet");
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
@@ -88,6 +90,7 @@ public class DefaultServletHandlerConfigurerTests {
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
private static class DispatchingMockServletContext extends MockServletContext {
private String url;
@@ -99,8 +102,4 @@ public class DefaultServletHandlerConfigurerTests {
}
}
private SimpleUrlHandlerMapping getHandlerMapping() {
return (SimpleUrlHandlerMapping) configurer.getHandlerMapping();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -47,16 +47,16 @@ public class ViewControllerRegistryTests {
@Before
public void setUp() {
this.registry = new ViewControllerRegistry();
this.registry.setApplicationContext(new StaticApplicationContext());
public void setup() {
this.registry = new ViewControllerRegistry(new StaticApplicationContext());
this.request = new MockHttpServletRequest("GET", "/");
this.response = new MockHttpServletResponse();
}
@Test
public void noViewControllers() throws Exception {
assertNull(this.registry.getHandlerMapping());
public void noViewControllers() {
assertNull(this.registry.buildHandlerMapping());
}
@Test
@@ -125,17 +125,17 @@ public class ViewControllerRegistryTests {
@Test
public void order() {
this.registry.addViewController("/path");
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
SimpleUrlHandlerMapping handlerMapping = this.registry.buildHandlerMapping();
assertEquals(1, handlerMapping.getOrder());
this.registry.setOrder(2);
handlerMapping = getHandlerMapping();
handlerMapping = this.registry.buildHandlerMapping();
assertEquals(2, handlerMapping.getOrder());
}
private ParameterizableViewController getController(String path) {
Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
Map<String, ?> urlMap = this.registry.buildHandlerMapping().getUrlMap();
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get(path);
assertNotNull(controller);
return controller;
@@ -149,8 +149,4 @@ public class ViewControllerRegistryTests {
return (RedirectView) controller.getView();
}
private SimpleUrlHandlerMapping getHandlerMapping() {
return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -54,15 +54,14 @@ public class ViewResolverRegistryTests {
@Before
public void setUp() {
public void setup() {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
this.registry = new ViewResolverRegistry();
this.registry.setApplicationContext(context);
this.registry.setContentNegotiationManager(new ContentNegotiationManager());
this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context);
}