Skip to content

Commit 11d9a7c

Browse files
committed
Merge branch '2.0.x'
2 parents 14a0064 + fc0a687 commit 11d9a7c

File tree

1 file changed

+62
-64
lines changed

1 file changed

+62
-64
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,16 +25,19 @@
2525

2626
import org.springframework.beans.BeansException;
2727
import org.springframework.beans.factory.config.BeanPostProcessor;
28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2829
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;
3033
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3134
import org.springframework.boot.web.servlet.ServletRegistrationBean;
3235
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
3336
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
3437
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
38+
import org.springframework.context.ApplicationContext;
3539
import org.springframework.context.annotation.Bean;
3640
import org.springframework.context.annotation.Configuration;
37-
import org.springframework.context.annotation.Import;
3841
import org.springframework.stereotype.Component;
3942
import org.springframework.web.servlet.DispatcherServlet;
4043
import org.springframework.web.servlet.FrameworkServlet;
@@ -47,112 +50,107 @@
4750
*
4851
* @author Dave Syer
4952
* @author Phillip Webb
53+
* @author Stephane Nicoll
5054
*/
5155
public class ServletWebServerFactoryAutoConfigurationTests {
5256

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);
5462

5563
@Test
5664
public void createFromConfigClass() {
57-
this.context = new AnnotationConfigServletWebServerApplicationContext(
58-
BaseConfiguration.class);
59-
verifyContext();
65+
this.contextRunner.run(verifyContext());
6066
}
6167

6268
@Test
6369
public void contextAlreadyHasDispatcherServletWithDefaultName() {
64-
this.context = new AnnotationConfigServletWebServerApplicationContext(
65-
DispatcherServletConfiguration.class, BaseConfiguration.class);
66-
verifyContext();
70+
this.contextRunner.withUserConfiguration(DispatcherServletConfiguration.class)
71+
.run(verifyContext());
6772
}
6873

6974
@Test
7075
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+
});
7682
}
7783

7884
@Test
7985
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+
});
8591
}
8692

8793
@Test
8894
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+
});
94100
}
95101

96102
@Test
97103
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+
});
104111
}
105112

106113
@Test
107114
public void webServerHasNoServletContext() {
108-
this.context = new AnnotationConfigServletWebServerApplicationContext(
109-
EnsureWebServerHasNoServletContext.class, BaseConfiguration.class);
110-
verifyContext();
115+
this.contextRunner.withUserConfiguration(EnsureWebServerHasNoServletContext.class)
116+
.run(verifyContext());
111117
}
112118

113119
@Test
114120
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+
});
119129
}
120130

121131
@Test
122132
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+
});
134139
}
135140

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(
139149
DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME,
140150
Servlet.class);
141151
verify(factory.getServletContext()).addServlet("dispatcherServlet", servlet);
142152
}
143153

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-
156154
@Configuration
157155
@ConditionalOnExpression("true")
158156
public static class WebServerConfiguration {

0 commit comments

Comments
 (0)