|  | 
| 1 | 1 | /* | 
| 2 |  | - * Copyright 2012-2017 the original author or authors. | 
|  | 2 | + * Copyright 2012-2018 the original author or authors. | 
| 3 | 3 |  * | 
| 4 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
| 5 | 5 |  * you may not use this file except in compliance with the License. | 
|  | 
| 25 | 25 | 
 | 
| 26 | 26 | import org.springframework.beans.BeansException; | 
| 27 | 27 | import org.springframework.beans.factory.config.BeanPostProcessor; | 
|  | 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; | 
| 28 | 29 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | 
| 29 |  | -import org.springframework.boot.test.util.TestPropertyValues; | 
|  | 30 | +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; | 
|  | 31 | +import org.springframework.boot.test.context.runner.ContextConsumer; | 
|  | 32 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; | 
| 30 | 33 | import org.springframework.boot.web.server.WebServerFactoryCustomizer; | 
| 31 | 34 | import org.springframework.boot.web.servlet.ServletRegistrationBean; | 
| 32 | 35 | import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | 
| 33 | 36 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; | 
| 34 | 37 | import org.springframework.boot.web.servlet.server.ServletWebServerFactory; | 
|  | 38 | +import org.springframework.context.ApplicationContext; | 
| 35 | 39 | import org.springframework.context.annotation.Bean; | 
| 36 | 40 | import org.springframework.context.annotation.Configuration; | 
| 37 |  | -import org.springframework.context.annotation.Import; | 
| 38 | 41 | import org.springframework.stereotype.Component; | 
| 39 | 42 | import org.springframework.web.servlet.DispatcherServlet; | 
| 40 | 43 | import org.springframework.web.servlet.FrameworkServlet; | 
|  | 
| 47 | 50 |  * | 
| 48 | 51 |  * @author Dave Syer | 
| 49 | 52 |  * @author Phillip Webb | 
|  | 53 | + * @author Stephane Nicoll | 
| 50 | 54 |  */ | 
| 51 | 55 | public class ServletWebServerFactoryAutoConfigurationTests { | 
| 52 | 56 | 
 | 
| 53 |  | -	private AnnotationConfigServletWebServerApplicationContext context; | 
|  | 57 | +	private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( | 
|  | 58 | +			AnnotationConfigServletWebServerApplicationContext::new).withConfiguration( | 
|  | 59 | +					AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class, | 
|  | 60 | +							DispatcherServletAutoConfiguration.class)) | 
|  | 61 | +					.withUserConfiguration(WebServerConfiguration.class); | 
| 54 | 62 | 
 | 
| 55 | 63 | 	@Test | 
| 56 | 64 | 	public void createFromConfigClass() { | 
| 57 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 58 |  | -				BaseConfiguration.class); | 
| 59 |  | -		verifyContext(); | 
|  | 65 | +		this.contextRunner.run(verifyContext()); | 
| 60 | 66 | 	} | 
| 61 | 67 | 
 | 
| 62 | 68 | 	@Test | 
| 63 | 69 | 	public void contextAlreadyHasDispatcherServletWithDefaultName() { | 
| 64 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 65 |  | -				DispatcherServletConfiguration.class, BaseConfiguration.class); | 
| 66 |  | -		verifyContext(); | 
|  | 70 | +		this.contextRunner.withUserConfiguration(DispatcherServletConfiguration.class) | 
|  | 71 | +				.run(verifyContext()); | 
| 67 | 72 | 	} | 
| 68 | 73 | 
 | 
| 69 | 74 | 	@Test | 
| 70 | 75 | 	public void contextAlreadyHasDispatcherServlet() { | 
| 71 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 72 |  | -				SpringServletConfiguration.class, BaseConfiguration.class); | 
| 73 |  | -		verifyContext(); | 
| 74 |  | -		assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length) | 
| 75 |  | -				.isEqualTo(2); | 
|  | 76 | +		this.contextRunner.withUserConfiguration(SpringServletConfiguration.class) | 
|  | 77 | +				.run((context) -> { | 
|  | 78 | +					verifyContext(context); | 
|  | 79 | +					assertThat(context.getBeanNamesForType(DispatcherServlet.class)) | 
|  | 80 | +							.hasSize(2); | 
|  | 81 | +				}); | 
| 76 | 82 | 	} | 
| 77 | 83 | 
 | 
| 78 | 84 | 	@Test | 
| 79 | 85 | 	public void contextAlreadyHasNonDispatcherServlet() { | 
| 80 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 81 |  | -				NonSpringServletConfiguration.class, BaseConfiguration.class); | 
| 82 |  | -		verifyContext(); // the non default servlet is still registered | 
| 83 |  | -		assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length) | 
| 84 |  | -				.isEqualTo(0); | 
|  | 86 | +		this.contextRunner.withUserConfiguration(NonSpringServletConfiguration.class) | 
|  | 87 | +				.run((context) -> { | 
|  | 88 | +					verifyContext(context); // the non default servlet is still registered | 
|  | 89 | +					assertThat(context).doesNotHaveBean(DispatcherServlet.class); | 
|  | 90 | +				}); | 
| 85 | 91 | 	} | 
| 86 | 92 | 
 | 
| 87 | 93 | 	@Test | 
| 88 | 94 | 	public void contextAlreadyHasNonServlet() { | 
| 89 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 90 |  | -				NonServletConfiguration.class, BaseConfiguration.class); | 
| 91 |  | -		assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length) | 
| 92 |  | -				.isEqualTo(0); | 
| 93 |  | -		assertThat(this.context.getBeanNamesForType(Servlet.class).length).isEqualTo(0); | 
|  | 95 | +		this.contextRunner.withUserConfiguration(NonServletConfiguration.class) | 
|  | 96 | +				.run((context) -> { | 
|  | 97 | +					assertThat(context).doesNotHaveBean(DispatcherServlet.class); | 
|  | 98 | +					assertThat(context).doesNotHaveBean(Servlet.class); | 
|  | 99 | +				}); | 
| 94 | 100 | 	} | 
| 95 | 101 | 
 | 
| 96 | 102 | 	@Test | 
| 97 | 103 | 	public void contextAlreadyHasDispatcherServletAndRegistration() { | 
| 98 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 99 |  | -				DispatcherServletWithRegistrationConfiguration.class, | 
| 100 |  | -				BaseConfiguration.class); | 
| 101 |  | -		verifyContext(); | 
| 102 |  | -		assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length) | 
| 103 |  | -				.isEqualTo(1); | 
|  | 104 | +		this.contextRunner | 
|  | 105 | +				.withUserConfiguration( | 
|  | 106 | +						DispatcherServletWithRegistrationConfiguration.class) | 
|  | 107 | +				.run((context) -> { | 
|  | 108 | +					verifyContext(context); | 
|  | 109 | +					assertThat(context).hasSingleBean(DispatcherServlet.class); | 
|  | 110 | +				}); | 
| 104 | 111 | 	} | 
| 105 | 112 | 
 | 
| 106 | 113 | 	@Test | 
| 107 | 114 | 	public void webServerHasNoServletContext() { | 
| 108 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 109 |  | -				EnsureWebServerHasNoServletContext.class, BaseConfiguration.class); | 
| 110 |  | -		verifyContext(); | 
|  | 115 | +		this.contextRunner.withUserConfiguration(EnsureWebServerHasNoServletContext.class) | 
|  | 116 | +				.run(verifyContext()); | 
| 111 | 117 | 	} | 
| 112 | 118 | 
 | 
| 113 | 119 | 	@Test | 
| 114 | 120 | 	public void customizeWebServerFactoryThroughCallback() { | 
| 115 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext( | 
| 116 |  | -				CallbackEmbeddedServerFactoryCustomizer.class, BaseConfiguration.class); | 
| 117 |  | -		verifyContext(); | 
| 118 |  | -		assertThat(getWebServerFactory().getPort()).isEqualTo(9000); | 
|  | 121 | +		this.contextRunner | 
|  | 122 | +				.withUserConfiguration(CallbackEmbeddedServerFactoryCustomizer.class) | 
|  | 123 | +				.run((context) -> { | 
|  | 124 | +					verifyContext(context); | 
|  | 125 | +					assertThat( | 
|  | 126 | +							context.getBean(MockServletWebServerFactory.class).getPort()) | 
|  | 127 | +									.isEqualTo(9000); | 
|  | 128 | +				}); | 
| 119 | 129 | 	} | 
| 120 | 130 | 
 | 
| 121 | 131 | 	@Test | 
| 122 | 132 | 	public void initParametersAreConfiguredOnTheServletContext() { | 
| 123 |  | -		this.context = new AnnotationConfigServletWebServerApplicationContext(); | 
| 124 |  | -		TestPropertyValues | 
| 125 |  | -				.of("server.servlet.context-parameters.a:alpha", | 
| 126 |  | -						"server.servlet.context-parameters.b:bravo") | 
| 127 |  | -				.applyTo(this.context); | 
| 128 |  | -		this.context.register(BaseConfiguration.class); | 
| 129 |  | -		this.context.refresh(); | 
| 130 |  | - | 
| 131 |  | -		ServletContext servletContext = this.context.getServletContext(); | 
| 132 |  | -		assertThat(servletContext.getInitParameter("a")).isEqualTo("alpha"); | 
| 133 |  | -		assertThat(servletContext.getInitParameter("b")).isEqualTo("bravo"); | 
|  | 133 | +		this.contextRunner.withPropertyValues("server.servlet.context-parameters.a:alpha", | 
|  | 134 | +				"server.servlet.context-parameters.b:bravo").run((context) -> { | 
|  | 135 | +					ServletContext servletContext = context.getServletContext(); | 
|  | 136 | +					assertThat(servletContext.getInitParameter("a")).isEqualTo("alpha"); | 
|  | 137 | +					assertThat(servletContext.getInitParameter("b")).isEqualTo("bravo"); | 
|  | 138 | +				}); | 
| 134 | 139 | 	} | 
| 135 | 140 | 
 | 
| 136 |  | -	private void verifyContext() { | 
| 137 |  | -		MockServletWebServerFactory factory = getWebServerFactory(); | 
| 138 |  | -		Servlet servlet = this.context.getBean( | 
|  | 141 | +	private ContextConsumer<AssertableWebApplicationContext> verifyContext() { | 
|  | 142 | +		return this::verifyContext; | 
|  | 143 | +	} | 
|  | 144 | + | 
|  | 145 | +	private void verifyContext(ApplicationContext context) { | 
|  | 146 | +		MockServletWebServerFactory factory = context | 
|  | 147 | +				.getBean(MockServletWebServerFactory.class); | 
|  | 148 | +		Servlet servlet = context.getBean( | 
| 139 | 149 | 				DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME, | 
| 140 | 150 | 				Servlet.class); | 
| 141 | 151 | 		verify(factory.getServletContext()).addServlet("dispatcherServlet", servlet); | 
| 142 | 152 | 	} | 
| 143 | 153 | 
 | 
| 144 |  | -	private MockServletWebServerFactory getWebServerFactory() { | 
| 145 |  | -		return this.context.getBean(MockServletWebServerFactory.class); | 
| 146 |  | -	} | 
| 147 |  | - | 
| 148 |  | -	@Configuration | 
| 149 |  | -	@Import({ WebServerConfiguration.class, | 
| 150 |  | -			ServletWebServerFactoryAutoConfiguration.class, | 
| 151 |  | -			DispatcherServletAutoConfiguration.class }) | 
| 152 |  | -	protected static class BaseConfiguration { | 
| 153 |  | - | 
| 154 |  | -	} | 
| 155 |  | - | 
| 156 | 154 | 	@Configuration | 
| 157 | 155 | 	@ConditionalOnExpression("true") | 
| 158 | 156 | 	public static class WebServerConfiguration { | 
|  | 
0 commit comments