|
50 | 50 | import org.springframework.context.annotation.Configuration; |
51 | 51 | import org.springframework.context.support.AbstractApplicationContext; |
52 | 52 | import org.springframework.core.Ordered; |
| 53 | +import org.springframework.core.env.ConfigurableEnvironment; |
53 | 54 | import org.springframework.core.env.PropertySource; |
54 | 55 | import org.springframework.mock.web.MockServletContext; |
55 | 56 | import org.springframework.test.util.ReflectionTestUtils; |
@@ -190,6 +191,29 @@ void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured |
190 | 191 | } |
191 | 192 | } |
192 | 193 |
|
| 194 | + @Test |
| 195 | + void environmentIsConfiguredWithStandardServletEnvironment() { |
| 196 | + ServletContext servletContext = mock(ServletContext.class); |
| 197 | + given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class)); |
| 198 | + given(servletContext.getInitParameterNames()) |
| 199 | + .willReturn(Collections.enumeration(Collections.singletonList("servlet.init.test"))); |
| 200 | + given(servletContext.getInitParameter("servlet.init.test")).willReturn("from-servlet-context"); |
| 201 | + given(servletContext.getAttributeNames()) |
| 202 | + .willReturn(Collections.enumeration(Collections.singletonList("servlet.attribute.test"))); |
| 203 | + given(servletContext.getAttribute("servlet.attribute.test")).willReturn("also-from-servlet-context"); |
| 204 | + try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new RegularSpringBootServletInitializer() |
| 205 | + .createRootApplicationContext(servletContext)) { |
| 206 | + assertThat(context).isNotNull(); |
| 207 | + ConfigurableEnvironment environment = context.getEnvironment(); |
| 208 | + assertThat(environment).isInstanceOf(StandardServletEnvironment.class); |
| 209 | + assertThat(environment.getClass().getName()).endsWith("ApplicationServletEnvironment"); |
| 210 | + assertThat(environment.getPropertySources()).map(PropertySource::getName) |
| 211 | + .contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, |
| 212 | + StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME); |
| 213 | + assertThat(environment.getProperty("servlet.init.test")).isEqualTo("from-servlet-context"); |
| 214 | + } |
| 215 | + } |
| 216 | + |
193 | 217 | @Test |
194 | 218 | void servletContextPropertySourceIsAvailablePriorToRefresh() { |
195 | 219 | ServletContext servletContext = mock(ServletContext.class); |
@@ -351,6 +375,15 @@ public SpringApplication build() { |
351 | 375 |
|
352 | 376 | } |
353 | 377 |
|
| 378 | + static class RegularSpringBootServletInitializer extends SpringBootServletInitializer { |
| 379 | + |
| 380 | + @Override |
| 381 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |
| 382 | + return builder.sources(TestApp.class); |
| 383 | + } |
| 384 | + |
| 385 | + } |
| 386 | + |
354 | 387 | private static final class PropertySourceVerifyingApplicationListener |
355 | 388 | implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
356 | 389 |
|
|
0 commit comments