From cb98ee25ff52bf97faebe3f45cdef0ced9b4416e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 17 Jun 2015 09:27:44 +0200 Subject: [PATCH] Polish See gh-2900 --- .../autoconfigure/web/WebMvcProperties.java | 4 +- .../web/WebMvcAutoConfigurationTests.java | 163 +++++------------- .../appendix-application-properties.adoc | 2 +- 3 files changed, 48 insertions(+), 121 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java index 072088aea1..e841dc2f24 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java @@ -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"); * you may not use this file except in compliance with the License. @@ -92,7 +92,7 @@ public class WebMvcProperties { 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 * implementation is used, e.g. 10 seconds on Tomcat with Servlet 3. */ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index 81a3c07514..ed1f184b25 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -17,6 +17,8 @@ package org.springframework.boot.autoconfigure.web; import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; @@ -79,6 +81,7 @@ import static org.junit.Assert.assertThat; * @author Phillip Webb * @author Dave Syer * @author Andy Wilkinson + * @author Stephane Nicoll */ public class WebMvcAutoConfigurationTests { @@ -98,11 +101,7 @@ public class WebMvcAutoConfigurationTests { @Test public void handerAdaptersCreated() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length); assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class) .getMessageConverters().isEmpty()); @@ -113,21 +112,13 @@ public class WebMvcAutoConfigurationTests { @Test public void handerMappingsCreated() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); } @Test public void resourceHandlerMapping() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); Map> mappingLocations = getResourceMappingLocations(); assertThat(mappingLocations.get("/**").size(), equalTo(5)); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); @@ -137,11 +128,7 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerMappingOverrideWebjars() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(WebJars.class); Map> mappingLocations = getResourceMappingLocations(); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); assertThat(mappingLocations.get("/webjars/**").get(0), @@ -149,13 +136,8 @@ public class WebMvcAutoConfigurationTests { } @Test - public void resourceHandlerMappingOverrideAll() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(AllResources.class, Config.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + public void resourceHandlerMappingOverrideAll() throws Exception { + load(AllResources.class); Map> mappingLocations = getResourceMappingLocations(); assertThat(mappingLocations.get("/**").size(), equalTo(1)); assertThat(mappingLocations.get("/**").get(0), @@ -164,39 +146,22 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerMappingDisabled() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.resources.add-mappings:false"); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.resources.add-mappings:false"); Map> mappingLocations = getResourceMappingLocations(); assertThat(mappingLocations.size(), equalTo(0)); } @Test public void noLocaleResolver() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(AllResources.class, Config.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(AllResources.class); this.thrown.expect(NoSuchBeanDefinitionException.class); this.context.getBean(LocaleResolver.class); } @Test public void overrideLocale() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - // 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(); + load(AllResources.class, "spring.mvc.locale:en_UK"); + // mock request and set user preferred locale MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL")); @@ -209,12 +174,7 @@ public class WebMvcAutoConfigurationTests { @Test public void noDateFormat() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(AllResources.class, Config.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(AllResources.class); FormattingConversionService cs = this.context .getBean(FormattingConversionService.class); Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); @@ -224,15 +184,7 @@ public class WebMvcAutoConfigurationTests { @Test public void overrideDateFormat() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - // 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(); + load(AllResources.class, "spring.mvc.dateFormat:dd*MM*yyyy"); FormattingConversionService cs = this.context .getBean(FormattingConversionService.class); Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); @@ -241,26 +193,14 @@ public class WebMvcAutoConfigurationTests { @Test public void noMessageCodesResolver() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(AllResources.class, Config.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(AllResources.class); assertNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class) .getMessageCodesResolver()); } @Test public void overrideMessageCodesFormat() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - 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(); + load(AllResources.class, "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE"); assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class) .getMessageCodesResolver()); } @@ -300,11 +240,7 @@ public class WebMvcAutoConfigurationTests { @Test public void ignoreDefaultModelOnRedirectIsTrue() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); RequestMappingHandlerAdapter adapter = this.context .getBean(RequestMappingHandlerAdapter.class); assertEquals(true, @@ -328,23 +264,13 @@ public class WebMvcAutoConfigurationTests { @Test public void customViewResolver() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, CustomViewResolver.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(CustomViewResolver.class); assertThat(this.context.getBean("viewResolver"), instanceOf(MyViewResolver.class)); } @Test public void customContentNegotiatingViewResolver() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, CustomContentNegotiatingViewResolver.class, - WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(CustomContentNegotiatingViewResolver.class); Map beans = this.context .getBeansOfType(ContentNegotiatingViewResolver.class); assertThat(beans.size(), equalTo(1)); @@ -353,11 +279,7 @@ public class WebMvcAutoConfigurationTests { @Test public void faviconMapping() throws IllegalAccessException { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); assertThat( this.context.getBeansOfType(ResourceHttpRequestHandler.class).get( "faviconRequestHandler"), is(notNullValue())); @@ -370,13 +292,7 @@ public class WebMvcAutoConfigurationTests { @Test public void faviconMappingDisabled() throws IllegalAccessException { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.mvc.favicon.enabled:false"); - this.context.refresh(); + load("spring.mvc.favicon.enabled:false"); assertThat( this.context.getBeansOfType(ResourceHttpRequestHandler.class).get( "faviconRequestHandler"), is(nullValue())); @@ -387,11 +303,7 @@ public class WebMvcAutoConfigurationTests { @Test public void defaultAsyncRequestTimeout() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(); RequestMappingHandlerAdapter adapter = this.context .getBean(RequestMappingHandlerAdapter.class); assertNull(ReflectionTestUtils.getField(adapter, "asyncRequestTimeout")); @@ -399,19 +311,34 @@ public class WebMvcAutoConfigurationTests { @Test public void customAsyncRequestTimeout() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.mvc.async.request-timeout:123456"); - this.context.register(Config.class, WebMvcAutoConfiguration.class, - HttpMessageConvertersAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.mvc.async.request-timeout:123456"); RequestMappingHandlerAdapter adapter = this.context .getBean(RequestMappingHandlerAdapter.class); Object actual = ReflectionTestUtils.getField(adapter, "asyncRequestTimeout"); assertEquals(123456L, actual); } + + @SuppressWarnings("unchecked") + private void load(Class config, String... environment) { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, environment); + List> configClasses = new ArrayList>(); + 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 protected static class ViewConfig { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ecece86eba..d13ec780a2 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -121,7 +121,7 @@ content into your application; rather pick only the properties that you need. spring.mvc.favicon.enabled=true 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.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.suffix= # ... and suffix