|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2012-2017 the original author or authors.
|
|
|
|
|
* Copyright 2012-2018 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.
|
|
|
|
@ -25,16 +25,19 @@ import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
|
|
|
import org.springframework.boot.test.util.TestPropertyValues;
|
|
|
|
|
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
|
|
|
|
import org.springframework.boot.test.context.runner.ContextConsumer;
|
|
|
|
|
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
|
|
|
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
|
|
|
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
|
|
|
|
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
|
|
|
|
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
|
|
|
|
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.servlet.DispatcherServlet;
|
|
|
|
|
import org.springframework.web.servlet.FrameworkServlet;
|
|
|
|
@ -47,112 +50,107 @@ import static org.mockito.Mockito.verify;
|
|
|
|
|
*
|
|
|
|
|
* @author Dave Syer
|
|
|
|
|
* @author Phillip Webb
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
*/
|
|
|
|
|
public class ServletWebServerFactoryAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
private AnnotationConfigServletWebServerApplicationContext context;
|
|
|
|
|
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
|
|
|
|
|
AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(
|
|
|
|
|
AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class,
|
|
|
|
|
DispatcherServletAutoConfiguration.class))
|
|
|
|
|
.withUserConfiguration(WebServerConfiguration.class);
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void createFromConfigClass() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
this.contextRunner.run(verifyContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextAlreadyHasDispatcherServletWithDefaultName() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
DispatcherServletConfiguration.class, BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
this.contextRunner.withUserConfiguration(DispatcherServletConfiguration.class)
|
|
|
|
|
.run(verifyContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextAlreadyHasDispatcherServlet() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
SpringServletConfiguration.class, BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
|
|
|
|
|
.isEqualTo(2);
|
|
|
|
|
this.contextRunner.withUserConfiguration(SpringServletConfiguration.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
verifyContext(context);
|
|
|
|
|
assertThat(context.getBeanNamesForType(DispatcherServlet.class))
|
|
|
|
|
.hasSize(2);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextAlreadyHasNonDispatcherServlet() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
NonSpringServletConfiguration.class, BaseConfiguration.class);
|
|
|
|
|
verifyContext(); // the non default servlet is still registered
|
|
|
|
|
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
|
|
|
|
|
.isEqualTo(0);
|
|
|
|
|
this.contextRunner.withUserConfiguration(NonSpringServletConfiguration.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
verifyContext(context); // the non default servlet is still registered
|
|
|
|
|
assertThat(context).doesNotHaveBean(DispatcherServlet.class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextAlreadyHasNonServlet() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
NonServletConfiguration.class, BaseConfiguration.class);
|
|
|
|
|
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
|
|
|
|
|
.isEqualTo(0);
|
|
|
|
|
assertThat(this.context.getBeanNamesForType(Servlet.class).length).isEqualTo(0);
|
|
|
|
|
this.contextRunner.withUserConfiguration(NonServletConfiguration.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
assertThat(context).doesNotHaveBean(DispatcherServlet.class);
|
|
|
|
|
assertThat(context).doesNotHaveBean(Servlet.class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextAlreadyHasDispatcherServletAndRegistration() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
DispatcherServletWithRegistrationConfiguration.class,
|
|
|
|
|
BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length)
|
|
|
|
|
.isEqualTo(1);
|
|
|
|
|
this.contextRunner
|
|
|
|
|
.withUserConfiguration(
|
|
|
|
|
DispatcherServletWithRegistrationConfiguration.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
verifyContext(context);
|
|
|
|
|
assertThat(context).hasSingleBean(DispatcherServlet.class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void webServerHasNoServletContext() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
EnsureWebServerHasNoServletContext.class, BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
this.contextRunner.withUserConfiguration(EnsureWebServerHasNoServletContext.class)
|
|
|
|
|
.run(verifyContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void customizeWebServerFactoryThroughCallback() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
CallbackEmbeddedServerFactoryCustomizer.class, BaseConfiguration.class);
|
|
|
|
|
verifyContext();
|
|
|
|
|
assertThat(getWebServerFactory().getPort()).isEqualTo(9000);
|
|
|
|
|
this.contextRunner
|
|
|
|
|
.withUserConfiguration(CallbackEmbeddedServerFactoryCustomizer.class)
|
|
|
|
|
.run((context) -> {
|
|
|
|
|
verifyContext(context);
|
|
|
|
|
assertThat(
|
|
|
|
|
context.getBean(MockServletWebServerFactory.class).getPort())
|
|
|
|
|
.isEqualTo(9000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void initParametersAreConfiguredOnTheServletContext() {
|
|
|
|
|
this.context = new AnnotationConfigServletWebServerApplicationContext();
|
|
|
|
|
TestPropertyValues
|
|
|
|
|
.of("server.servlet.context-parameters.a:alpha",
|
|
|
|
|
"server.servlet.context-parameters.b:bravo")
|
|
|
|
|
.applyTo(this.context);
|
|
|
|
|
this.context.register(BaseConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
|
|
|
|
|
ServletContext servletContext = this.context.getServletContext();
|
|
|
|
|
assertThat(servletContext.getInitParameter("a")).isEqualTo("alpha");
|
|
|
|
|
assertThat(servletContext.getInitParameter("b")).isEqualTo("bravo");
|
|
|
|
|
this.contextRunner.withPropertyValues("server.servlet.context-parameters.a:alpha",
|
|
|
|
|
"server.servlet.context-parameters.b:bravo").run((context) -> {
|
|
|
|
|
ServletContext servletContext = context.getServletContext();
|
|
|
|
|
assertThat(servletContext.getInitParameter("a")).isEqualTo("alpha");
|
|
|
|
|
assertThat(servletContext.getInitParameter("b")).isEqualTo("bravo");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void verifyContext() {
|
|
|
|
|
MockServletWebServerFactory factory = getWebServerFactory();
|
|
|
|
|
Servlet servlet = this.context.getBean(
|
|
|
|
|
private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
|
|
|
|
|
return this::verifyContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void verifyContext(ApplicationContext context) {
|
|
|
|
|
MockServletWebServerFactory factory = context
|
|
|
|
|
.getBean(MockServletWebServerFactory.class);
|
|
|
|
|
Servlet servlet = context.getBean(
|
|
|
|
|
DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME,
|
|
|
|
|
Servlet.class);
|
|
|
|
|
verify(factory.getServletContext()).addServlet("dispatcherServlet", servlet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MockServletWebServerFactory getWebServerFactory() {
|
|
|
|
|
return this.context.getBean(MockServletWebServerFactory.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@Import({ WebServerConfiguration.class,
|
|
|
|
|
ServletWebServerFactoryAutoConfiguration.class,
|
|
|
|
|
DispatcherServletAutoConfiguration.class })
|
|
|
|
|
protected static class BaseConfiguration {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@ConditionalOnExpression("true")
|
|
|
|
|
public static class WebServerConfiguration {
|
|
|
|
|