See gh-2900
pull/2592/merge
Stephane Nicoll 10 years ago
parent 8b9c38085a
commit cb98ee25ff

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -92,7 +92,7 @@ public class WebMvcProperties {
public static class Async { public static class Async {
/** /**
* The amount of time (in milliseconds) before asynchronous request handling times * Amount of time (in milliseconds) before asynchronous request handling times
* out. If this value is not set, the default timeout of the underlying * out. If this value is not set, the default timeout of the underlying
* implementation is used, e.g. 10 seconds on Tomcat with Servlet 3. * implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
*/ */

@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -79,6 +81,7 @@ import static org.junit.Assert.assertThat;
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
public class WebMvcAutoConfigurationTests { public class WebMvcAutoConfigurationTests {
@ -98,11 +101,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void handerAdaptersCreated() throws Exception { public void handerAdaptersCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length); assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length);
assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class) assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class)
.getMessageConverters().isEmpty()); .getMessageConverters().isEmpty());
@ -113,21 +112,13 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void handerMappingsCreated() throws Exception { public void handerMappingsCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length);
} }
@Test @Test
public void resourceHandlerMapping() throws Exception { public void resourceHandlerMapping() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations(); Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(5)); assertThat(mappingLocations.get("/**").size(), equalTo(5));
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
@ -137,11 +128,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void resourceHandlerMappingOverrideWebjars() throws Exception { public void resourceHandlerMappingOverrideWebjars() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(WebJars.class);
this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations(); Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
assertThat(mappingLocations.get("/webjars/**").get(0), assertThat(mappingLocations.get("/webjars/**").get(0),
@ -150,12 +137,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void resourceHandlerMappingOverrideAll() throws Exception { public void resourceHandlerMappingOverrideAll() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class);
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations(); Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(1)); assertThat(mappingLocations.get("/**").size(), equalTo(1));
assertThat(mappingLocations.get("/**").get(0), assertThat(mappingLocations.get("/**").get(0),
@ -164,39 +146,22 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void resourceHandlerMappingDisabled() throws Exception { public void resourceHandlerMappingDisabled() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load("spring.resources.add-mappings:false");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.resources.add-mappings:false");
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations(); Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.size(), equalTo(0)); assertThat(mappingLocations.size(), equalTo(0));
} }
@Test @Test
public void noLocaleResolver() throws Exception { public void noLocaleResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class);
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
this.thrown.expect(NoSuchBeanDefinitionException.class); this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(LocaleResolver.class); this.context.getBean(LocaleResolver.class);
} }
@Test @Test
public void overrideLocale() throws Exception { public void overrideLocale() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class, "spring.mvc.locale:en_UK");
// set fixed locale
EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.locale:en_UK");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
// mock request and set user preferred locale // mock request and set user preferred locale
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL")); request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
@ -209,12 +174,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void noDateFormat() throws Exception { public void noDateFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class);
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
FormattingConversionService cs = this.context FormattingConversionService cs = this.context
.getBean(FormattingConversionService.class); .getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
@ -224,15 +184,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void overrideDateFormat() throws Exception { public void overrideDateFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class, "spring.mvc.dateFormat:dd*MM*yyyy");
// set fixed date format
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.dateFormat:dd*MM*yyyy");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
FormattingConversionService cs = this.context FormattingConversionService cs = this.context
.getBean(FormattingConversionService.class); .getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
@ -241,26 +193,14 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void noMessageCodesResolver() throws Exception { public void noMessageCodesResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class);
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class) assertNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
.getMessageCodesResolver()); .getMessageCodesResolver());
} }
@Test @Test
public void overrideMessageCodesFormat() throws Exception { public void overrideMessageCodesFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(AllResources.class, "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class) assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
.getMessageCodesResolver()); .getMessageCodesResolver());
} }
@ -300,11 +240,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void ignoreDefaultModelOnRedirectIsTrue() throws Exception { public void ignoreDefaultModelOnRedirectIsTrue() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerAdapter adapter = this.context RequestMappingHandlerAdapter adapter = this.context
.getBean(RequestMappingHandlerAdapter.class); .getBean(RequestMappingHandlerAdapter.class);
assertEquals(true, assertEquals(true,
@ -328,23 +264,13 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void customViewResolver() throws Exception { public void customViewResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(CustomViewResolver.class);
this.context.register(Config.class, CustomViewResolver.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean("viewResolver"), instanceOf(MyViewResolver.class)); assertThat(this.context.getBean("viewResolver"), instanceOf(MyViewResolver.class));
} }
@Test @Test
public void customContentNegotiatingViewResolver() throws Exception { public void customContentNegotiatingViewResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load(CustomContentNegotiatingViewResolver.class);
this.context.register(Config.class, CustomContentNegotiatingViewResolver.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Map<String, ContentNegotiatingViewResolver> beans = this.context Map<String, ContentNegotiatingViewResolver> beans = this.context
.getBeansOfType(ContentNegotiatingViewResolver.class); .getBeansOfType(ContentNegotiatingViewResolver.class);
assertThat(beans.size(), equalTo(1)); assertThat(beans.size(), equalTo(1));
@ -353,11 +279,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void faviconMapping() throws IllegalAccessException { public void faviconMapping() throws IllegalAccessException {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat( assertThat(
this.context.getBeansOfType(ResourceHttpRequestHandler.class).get( this.context.getBeansOfType(ResourceHttpRequestHandler.class).get(
"faviconRequestHandler"), is(notNullValue())); "faviconRequestHandler"), is(notNullValue()));
@ -370,13 +292,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void faviconMappingDisabled() throws IllegalAccessException { public void faviconMappingDisabled() throws IllegalAccessException {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load("spring.mvc.favicon.enabled:false");
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.favicon.enabled:false");
this.context.refresh();
assertThat( assertThat(
this.context.getBeansOfType(ResourceHttpRequestHandler.class).get( this.context.getBeansOfType(ResourceHttpRequestHandler.class).get(
"faviconRequestHandler"), is(nullValue())); "faviconRequestHandler"), is(nullValue()));
@ -387,11 +303,7 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void defaultAsyncRequestTimeout() throws Exception { public void defaultAsyncRequestTimeout() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerAdapter adapter = this.context RequestMappingHandlerAdapter adapter = this.context
.getBean(RequestMappingHandlerAdapter.class); .getBean(RequestMappingHandlerAdapter.class);
assertNull(ReflectionTestUtils.getField(adapter, "asyncRequestTimeout")); assertNull(ReflectionTestUtils.getField(adapter, "asyncRequestTimeout"));
@ -399,19 +311,34 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void customAsyncRequestTimeout() throws Exception { public void customAsyncRequestTimeout() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); load("spring.mvc.async.request-timeout:123456");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.async.request-timeout:123456");
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerAdapter adapter = this.context RequestMappingHandlerAdapter adapter = this.context
.getBean(RequestMappingHandlerAdapter.class); .getBean(RequestMappingHandlerAdapter.class);
Object actual = ReflectionTestUtils.getField(adapter, "asyncRequestTimeout"); Object actual = ReflectionTestUtils.getField(adapter, "asyncRequestTimeout");
assertEquals(123456L, actual); assertEquals(123456L, actual);
} }
@SuppressWarnings("unchecked")
private void load(Class<?> config, String... environment) {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
List<Class<?>> configClasses = new ArrayList<Class<?>>();
if (config != null) {
configClasses.add(config);
}
configClasses.addAll(Arrays.asList(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class));
this.context.register(configClasses.toArray(new Class<?>[configClasses.size()]));
this.context.refresh();
}
private void load(String... environment) {
load(null, environment);
}
@Configuration @Configuration
protected static class ViewConfig { protected static class ViewConfig {

@ -121,7 +121,7 @@ content into your application; rather pick only the properties that you need.
spring.mvc.favicon.enabled=true spring.mvc.favicon.enabled=true
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.mvc.ignore-default-model-on-redirect=true # if the the content of the "default" model should be ignored redirects spring.mvc.ignore-default-model-on-redirect=true # if the the content of the "default" model should be ignored redirects
spring.mvc.async.request-timeout= # async timeout in milliseconds spring.mvc.async.request-timeout= # async request timeout in milliseconds
spring.view.prefix= # MVC view prefix spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix spring.view.suffix= # ... and suffix

Loading…
Cancel
Save