Move test to ApplicationContextRunner

Move `DispatcherServletAutoConfigurationTests` to use the
`ApplicationContextRunner`

See gh-12477
This commit is contained in:
dreis2211 2018-03-14 11:30:47 +01:00 committed by Phillip Webb
parent 8a53631066
commit 3246496e2c
1 changed files with 81 additions and 105 deletions

View File

@ -19,18 +19,16 @@ package org.springframework.boot.autoconfigure.web.servlet;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
@ -47,146 +45,124 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class DispatcherServletAutoConfigurationTests { public class DispatcherServletAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
@After DispatcherServletAutoConfiguration.class));
public void closeContext() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void registrationProperties() { public void registrationProperties() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.run(context -> {
this.context.register(DispatcherServletAutoConfiguration.class); assertThat(context.getBean(DispatcherServlet.class)).isNotNull();
this.context.setServletContext(new MockServletContext()); ServletRegistrationBean<?> registration = context
this.context.refresh(); .getBean(ServletRegistrationBean.class);
assertThat(this.context.getBean(DispatcherServlet.class)).isNotNull(); assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]");
ServletRegistrationBean<?> registration = this.context });
.getBean(ServletRegistrationBean.class);
assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]");
} }
@Test @Test
public void registrationNonServletBean() { public void registrationNonServletBean() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withUserConfiguration(NonServletConfiguration.class)
this.context.register(NonServletConfiguration.class, .run(context -> {
DispatcherServletAutoConfiguration.class); assertThat(context.getBeanNamesForType(ServletRegistrationBean.class))
this.context.setServletContext(new MockServletContext()); .isEmpty();
this.context.refresh(); assertThat(context.getBeanNamesForType(DispatcherServlet.class))
assertThat(this.context.getBeanNamesForType(ServletRegistrationBean.class).length) .isEmpty();
.isEqualTo(0); });
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
.isEqualTo(0);
} }
// If a DispatcherServlet instance is registered with a name different // If a DispatcherServlet instance is registered with a name different
// from the default one, we're registering one anyway // from the default one, we're registering one anyway
@Test @Test
public void registrationOverrideWithDispatcherServletWrongName() { public void registrationOverrideWithDispatcherServletWrongName() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withUserConfiguration(CustomDispatcherServletWrongName.class)
this.context.register(CustomDispatcherServletWrongName.class, .run(context -> {
DispatcherServletAutoConfiguration.class); ServletRegistrationBean<?> registration = context
this.context.setServletContext(new MockServletContext()); .getBean(ServletRegistrationBean.class);
this.context.refresh(); assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]");
ServletRegistrationBean<?> registration = this.context assertThat(registration.getServletName()).isEqualTo("dispatcherServlet");
.getBean(ServletRegistrationBean.class); assertThat(context.getBeanNamesForType(DispatcherServlet.class))
assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]"); .hasSize(2);
assertThat(registration.getServletName()).isEqualTo("dispatcherServlet"); });
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
.isEqualTo(2);
} }
@Test @Test
public void registrationOverrideWithAutowiredServlet() { public void registrationOverrideWithAutowiredServlet() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withUserConfiguration(CustomAutowiredRegistration.class)
this.context.register(CustomAutowiredRegistration.class, .run(context -> {
DispatcherServletAutoConfiguration.class); ServletRegistrationBean<?> registration = context
this.context.setServletContext(new MockServletContext()); .getBean(ServletRegistrationBean.class);
this.context.refresh(); assertThat(registration.getUrlMappings().toString()).isEqualTo("[/foo]");
ServletRegistrationBean<?> registration = this.context assertThat(registration.getServletName()).isEqualTo("customDispatcher");
.getBean(ServletRegistrationBean.class); assertThat(context.getBeanNamesForType(DispatcherServlet.class))
assertThat(registration.getUrlMappings().toString()).isEqualTo("[/foo]"); .hasSize(1);
assertThat(registration.getServletName()).isEqualTo("customDispatcher"); });
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
.isEqualTo(1);
} }
@Test @Test
public void servletPath() { public void servletPath() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withPropertyValues("server.servlet.path:/spring")
this.context.setServletContext(new MockServletContext()); .run(context -> {
this.context.register(DispatcherServletAutoConfiguration.class); assertThat(context.getBean(DispatcherServlet.class)).isNotNull();
TestPropertyValues.of("server.servlet.path:/spring").applyTo(this.context); ServletRegistrationBean<?> registration = context
this.context.refresh(); .getBean(ServletRegistrationBean.class);
assertThat(this.context.getBean(DispatcherServlet.class)).isNotNull(); assertThat(registration.getUrlMappings().toString()).isEqualTo("[/spring/*]");
ServletRegistrationBean<?> registration = this.context assertThat(registration.getMultipartConfig()).isNull();
.getBean(ServletRegistrationBean.class); });
assertThat(registration.getUrlMappings().toString()).isEqualTo("[/spring/*]");
assertThat(registration.getMultipartConfig()).isNull();
} }
@Test @Test
public void multipartConfig() { public void multipartConfig() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withUserConfiguration(MultipartConfiguration.class)
this.context.setServletContext(new MockServletContext()); .run(context -> {
this.context.register(MultipartConfiguration.class, ServletRegistrationBean<?> registration = context
DispatcherServletAutoConfiguration.class); .getBean(ServletRegistrationBean.class);
this.context.refresh(); assertThat(registration.getMultipartConfig()).isNotNull();
ServletRegistrationBean<?> registration = this.context });
.getBean(ServletRegistrationBean.class);
assertThat(registration.getMultipartConfig()).isNotNull();
} }
@Test @Test
public void renamesMultipartResolver() { public void renamesMultipartResolver() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withUserConfiguration(MultipartResolverConfiguration.class)
this.context.setServletContext(new MockServletContext()); .run(context -> {
this.context.register(MultipartResolverConfiguration.class, DispatcherServlet dispatcherServlet = context
DispatcherServletAutoConfiguration.class); .getBean(DispatcherServlet.class);
this.context.refresh(); dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(context));
DispatcherServlet dispatcherServlet = this.context assertThat(dispatcherServlet.getMultipartResolver())
.getBean(DispatcherServlet.class); .isInstanceOf(MockMultipartResolver.class);
dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(this.context)); });
assertThat(dispatcherServlet.getMultipartResolver())
.isInstanceOf(MockMultipartResolver.class);
} }
@Test @Test
public void dispatcherServletDefaultConfig() { public void dispatcherServletDefaultConfig() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.run(context -> {
this.context.setServletContext(new MockServletContext()); DispatcherServlet bean = context.getBean(DispatcherServlet.class);
this.context.register(DispatcherServletAutoConfiguration.class); assertThat(bean).extracting("throwExceptionIfNoHandlerFound")
this.context.refresh(); .containsExactly(false);
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true);
assertThat(bean).extracting("throwExceptionIfNoHandlerFound") assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false);
.containsExactly(false); assertThat(new DirectFieldAccessor(
assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true); context.getBean("dispatcherServletRegistration"))
assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false); .getPropertyValue("loadOnStartup")).isEqualTo(-1);
assertThat(new DirectFieldAccessor( });
this.context.getBean("dispatcherServletRegistration"))
.getPropertyValue("loadOnStartup")).isEqualTo(-1);
} }
@Test @Test
public void dispatcherServletCustomConfig() { public void dispatcherServletCustomConfig() {
this.context = new AnnotationConfigWebApplicationContext(); this.contextRunner.withPropertyValues(
this.context.setServletContext(new MockServletContext()); "spring.mvc.throw-exception-if-no-handler-found:true",
this.context.register(DispatcherServletAutoConfiguration.class);
TestPropertyValues.of("spring.mvc.throw-exception-if-no-handler-found:true",
"spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-options-request:false",
"spring.mvc.dispatch-trace-request:true", "spring.mvc.dispatch-trace-request:true",
"spring.mvc.servlet.load-on-startup=5").applyTo(this.context); "spring.mvc.servlet.load-on-startup=5")
this.context.refresh(); .run(context -> {
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); DispatcherServlet bean = context.getBean(DispatcherServlet.class);
assertThat(bean).extracting("throwExceptionIfNoHandlerFound") assertThat(bean).extracting("throwExceptionIfNoHandlerFound")
.containsExactly(true); .containsExactly(true);
assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(false); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(false);
assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true); assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true);
assertThat(new DirectFieldAccessor( assertThat(new DirectFieldAccessor(
this.context.getBean("dispatcherServletRegistration")) context.getBean("dispatcherServletRegistration"))
.getPropertyValue("loadOnStartup")).isEqualTo(5); .getPropertyValue("loadOnStartup")).isEqualTo(5);
});
} }
@Configuration @Configuration