Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Jun 17, 2015
1 parent 8b9c380 commit cb98ee2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -92,7 +92,7 @@ public Async getAsync() {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +81,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public class WebMvcAutoConfigurationTests {

Expand All @@ -98,11 +101,7 @@ public void close() {

@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());
Expand All @@ -113,21 +112,13 @@ public void handerAdaptersCreated() throws Exception {

@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<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(5));
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
Expand All @@ -137,25 +128,16 @@ public void resourceHandlerMapping() throws Exception {

@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<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
assertThat(mappingLocations.get("/webjars/**").get(0),
equalTo((Resource) new ClassPathResource("/foo/")));
}

@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<String, List<Resource>> mappingLocations = getResourceMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(1));
assertThat(mappingLocations.get("/**").get(0),
Expand All @@ -164,39 +146,22 @@ public void resourceHandlerMappingOverrideAll() throws Exception {

@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<String, List<Resource>> 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"));
Expand All @@ -209,12 +174,7 @@ public void overrideLocale() throws Exception {

@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();
Expand All @@ -224,15 +184,7 @@ public void noDateFormat() throws Exception {

@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();
Expand All @@ -241,26 +193,14 @@ public void overrideDateFormat() throws Exception {

@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());
}
Expand Down Expand Up @@ -300,11 +240,7 @@ protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping

@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,
Expand All @@ -328,23 +264,13 @@ public void overrideIgnoreDefaultModelOnRedirect() throws Exception {

@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<String, ContentNegotiatingViewResolver> beans = this.context
.getBeansOfType(ContentNegotiatingViewResolver.class);
assertThat(beans.size(), equalTo(1));
Expand All @@ -353,11 +279,7 @@ public void customContentNegotiatingViewResolver() throws Exception {

@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()));
Expand All @@ -370,13 +292,7 @@ public void faviconMapping() throws IllegalAccessException {

@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()));
Expand All @@ -387,31 +303,42 @@ public void faviconMappingDisabled() throws IllegalAccessException {

@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"));
}

@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<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
protected static class ViewConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb98ee2

Please sign in to comment.