|
16 | 16 |
|
17 | 17 | package org.springframework.boot.web.support;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | + |
19 | 22 | import javax.servlet.ServletContext;
|
20 | 23 | import javax.servlet.ServletException;
|
21 | 24 |
|
|
29 | 32 | import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
30 | 33 | import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
31 | 34 | import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
|
| 35 | +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; |
32 | 36 | import org.springframework.boot.web.servlet.ServletContextInitializer;
|
| 37 | +import org.springframework.context.ApplicationListener; |
33 | 38 | import org.springframework.context.ConfigurableApplicationContext;
|
34 | 39 | import org.springframework.context.annotation.Bean;
|
35 | 40 | import org.springframework.context.annotation.Configuration;
|
36 | 41 | import org.springframework.context.support.AbstractApplicationContext;
|
| 42 | +import org.springframework.core.env.PropertySource; |
37 | 43 | import org.springframework.mock.web.MockServletContext;
|
38 | 44 | import org.springframework.web.context.WebApplicationContext;
|
| 45 | +import org.springframework.web.context.support.StandardServletEnvironment; |
39 | 46 |
|
40 | 47 | import static org.assertj.core.api.Assertions.assertThat;
|
| 48 | +import static org.mockito.BDDMockito.given; |
| 49 | +import static org.mockito.Mockito.mock; |
41 | 50 |
|
42 | 51 | /**
|
43 | 52 | * Tests for {@link SpringBootServletInitializer}.
|
@@ -135,10 +144,43 @@ public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterCon
|
135 | 144 | }
|
136 | 145 |
|
137 | 146 | @Test
|
138 |
| - public void servletContextApplicationListenerIsAdded() { |
139 |
| - new WithConfiguredSource().createRootApplicationContext(this.servletContext); |
140 |
| - assertThat(this.application.getListeners()) |
141 |
| - .hasAtLeastOneElementOfType(ServletContextApplicationListener.class); |
| 147 | + public void servletContextPropertySourceIsAvailablePriorToRefresh() |
| 148 | + throws ServletException { |
| 149 | + ServletContext servletContext = mock(ServletContext.class); |
| 150 | + given(servletContext.getInitParameterNames()).willReturn( |
| 151 | + Collections.enumeration(Arrays.asList("spring.profiles.active"))); |
| 152 | + given(servletContext.getInitParameter("spring.profiles.active")) |
| 153 | + .willReturn("from-servlet-context"); |
| 154 | + given(servletContext.getAttributeNames()) |
| 155 | + .willReturn(Collections.enumeration(Collections.<String>emptyList())); |
| 156 | + WebApplicationContext context = null; |
| 157 | + try { |
| 158 | + context = new PropertySourceVerifyingSpringBootServletInitializer() |
| 159 | + .createRootApplicationContext(servletContext); |
| 160 | + assertThat(context.getEnvironment().getActiveProfiles()) |
| 161 | + .containsExactly("from-servlet-context"); |
| 162 | + } |
| 163 | + finally { |
| 164 | + if (context instanceof ConfigurableApplicationContext) { |
| 165 | + ((ConfigurableApplicationContext) context).close(); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static class PropertySourceVerifyingSpringBootServletInitializer |
| 171 | + extends SpringBootServletInitializer { |
| 172 | + |
| 173 | + @Override |
| 174 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |
| 175 | + return builder.sources(TestApp.class) |
| 176 | + .listeners(new PropertySourceVerifyingApplicationListener()); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + @Configuration |
| 182 | + static class TestApp { |
| 183 | + |
142 | 184 | }
|
143 | 185 |
|
144 | 186 | private class MockSpringBootServletInitializer extends SpringBootServletInitializer {
|
@@ -221,4 +263,17 @@ public SpringApplication build() {
|
221 | 263 |
|
222 | 264 | }
|
223 | 265 |
|
| 266 | + private static final class PropertySourceVerifyingApplicationListener |
| 267 | + implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
| 268 | + |
| 269 | + @Override |
| 270 | + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { |
| 271 | + PropertySource<?> propertySource = event.getEnvironment().getPropertySources() |
| 272 | + .get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME); |
| 273 | + assertThat(propertySource.getProperty("spring.profiles.active")) |
| 274 | + .isEqualTo("from-servlet-context"); |
| 275 | + } |
| 276 | + |
| 277 | + } |
| 278 | + |
224 | 279 | }
|
0 commit comments