|
47 | 47 | import org.springframework.context.annotation.Configuration; |
48 | 48 | import org.springframework.context.support.AbstractApplicationContext; |
49 | 49 | import org.springframework.core.Ordered; |
| 50 | +import org.springframework.core.env.ConfigurableEnvironment; |
50 | 51 | import org.springframework.core.env.PropertySource; |
51 | 52 | import org.springframework.mock.web.MockServletContext; |
52 | 53 | import org.springframework.test.util.ReflectionTestUtils; |
@@ -177,6 +178,29 @@ void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured |
177 | 178 | } |
178 | 179 | } |
179 | 180 |
|
| 181 | + @Test |
| 182 | + void environmentIsConfiguredWithStandardServletEnvironment() { |
| 183 | + ServletContext servletContext = mock(ServletContext.class); |
| 184 | + given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class)); |
| 185 | + given(servletContext.getInitParameterNames()) |
| 186 | + .willReturn(Collections.enumeration(Collections.singletonList("servlet.init.test"))); |
| 187 | + given(servletContext.getInitParameter("servlet.init.test")).willReturn("from-servlet-context"); |
| 188 | + given(servletContext.getAttributeNames()) |
| 189 | + .willReturn(Collections.enumeration(Collections.singletonList("servlet.attribute.test"))); |
| 190 | + given(servletContext.getAttribute("servlet.attribute.test")).willReturn("also-from-servlet-context"); |
| 191 | + try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new RegularSpringBootServletInitializer() |
| 192 | + .createRootApplicationContext(servletContext)) { |
| 193 | + assertThat(context).isNotNull(); |
| 194 | + ConfigurableEnvironment environment = context.getEnvironment(); |
| 195 | + assertThat(environment).isInstanceOf(StandardServletEnvironment.class); |
| 196 | + assertThat(environment.getClass().getName()).endsWith("ApplicationServletEnvironment"); |
| 197 | + assertThat(environment.getPropertySources()).map(PropertySource::getName) |
| 198 | + .contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, |
| 199 | + StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME); |
| 200 | + assertThat(environment.getProperty("servlet.init.test")).isEqualTo("from-servlet-context"); |
| 201 | + } |
| 202 | + } |
| 203 | + |
180 | 204 | @Test |
181 | 205 | void servletContextPropertySourceIsAvailablePriorToRefresh() { |
182 | 206 | ServletContext servletContext = mock(ServletContext.class); |
@@ -334,6 +358,15 @@ public SpringApplication build() { |
334 | 358 |
|
335 | 359 | } |
336 | 360 |
|
| 361 | + static class RegularSpringBootServletInitializer extends SpringBootServletInitializer { |
| 362 | + |
| 363 | + @Override |
| 364 | + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |
| 365 | + return builder.sources(TestApp.class); |
| 366 | + } |
| 367 | + |
| 368 | + } |
| 369 | + |
337 | 370 | private static final class PropertySourceVerifyingApplicationListener |
338 | 371 | implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
339 | 372 |
|
|
0 commit comments