|
16 | 16 |
|
17 | 17 | package org.springframework.boot.web.servlet.support;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | + |
19 | 22 | import javax.servlet.ServletContext;
|
| 23 | +import javax.servlet.ServletException; |
20 | 24 |
|
21 | 25 | import org.junit.Rule;
|
22 | 26 | import org.junit.Test;
|
|
25 | 29 | import org.springframework.beans.DirectFieldAccessor;
|
26 | 30 | import org.springframework.boot.SpringApplication;
|
27 | 31 | import org.springframework.boot.builder.SpringApplicationBuilder;
|
| 32 | +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; |
28 | 33 | import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
|
29 | 34 | import org.springframework.boot.web.server.WebServer;
|
30 | 35 | import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
| 36 | +import org.springframework.context.ApplicationListener; |
31 | 37 | import org.springframework.context.ConfigurableApplicationContext;
|
32 | 38 | import org.springframework.context.annotation.Bean;
|
33 | 39 | import org.springframework.context.annotation.Configuration;
|
34 | 40 | import org.springframework.context.support.AbstractApplicationContext;
|
| 41 | +import org.springframework.core.env.PropertySource; |
35 | 42 | import org.springframework.mock.web.MockServletContext;
|
36 | 43 | import org.springframework.web.context.WebApplicationContext;
|
| 44 | +import org.springframework.web.context.support.StandardServletEnvironment; |
37 | 45 |
|
38 | 46 | import static org.assertj.core.api.Assertions.assertThat;
|
| 47 | +import static org.mockito.BDDMockito.given; |
| 48 | +import static org.mockito.Mockito.mock; |
39 | 49 |
|
40 | 50 | /**
|
41 | 51 | * Tests for {@link SpringBootServletInitializer}.
|
@@ -120,10 +130,36 @@ public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterCon
|
120 | 130 | }
|
121 | 131 |
|
122 | 132 | @Test
|
123 |
| - public void servletContextApplicationListenerIsAdded() { |
124 |
| - new WithConfiguredSource().createRootApplicationContext(this.servletContext); |
125 |
| - assertThat(this.application.getListeners()) |
126 |
| - .hasAtLeastOneElementOfType(ServletContextApplicationListener.class); |
| 133 | + public void servletContextPropertySourceIsAvailablePriorToRefresh() |
| 134 | + throws ServletException { |
| 135 | + ServletContext servletContext = mock(ServletContext.class); |
| 136 | + given(servletContext.getInitParameterNames()).willReturn( |
| 137 | + Collections.enumeration(Arrays.asList("spring.profiles.active"))); |
| 138 | + given(servletContext.getInitParameter("spring.profiles.active")) |
| 139 | + .willReturn("from-servlet-context"); |
| 140 | + given(servletContext.getAttributeNames()) |
| 141 | + .willReturn(Collections.enumeration(Collections.<String>emptyList())); |
| 142 | + try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new PropertySourceVerifyingSpringBootServletInitializer() |
| 143 | + .createRootApplicationContext(servletContext)) { |
| 144 | + assertThat(context.getEnvironment().getActiveProfiles()) |
| 145 | + .containsExactly("from-servlet-context"); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static class PropertySourceVerifyingSpringBootServletInitializer |
| 150 | + extends SpringBootServletInitializer { |
| 151 | + |
| 152 | + @Override |
| 153 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |
| 154 | + return builder.sources(TestApp.class) |
| 155 | + .listeners(new PropertySourceVerifyingApplicationListener()); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Configuration |
| 161 | + static class TestApp { |
| 162 | + |
127 | 163 | }
|
128 | 164 |
|
129 | 165 | private class MockSpringBootServletInitializer extends SpringBootServletInitializer {
|
@@ -206,4 +242,17 @@ public SpringApplication build() {
|
206 | 242 |
|
207 | 243 | }
|
208 | 244 |
|
| 245 | + private static final class PropertySourceVerifyingApplicationListener |
| 246 | + implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
| 247 | + |
| 248 | + @Override |
| 249 | + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { |
| 250 | + PropertySource<?> propertySource = event.getEnvironment().getPropertySources() |
| 251 | + .get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME); |
| 252 | + assertThat(propertySource.getProperty("spring.profiles.active")) |
| 253 | + .isEqualTo("from-servlet-context"); |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | + |
209 | 258 | }
|
0 commit comments