SpringBootServletInitializerTests Class — spring-boot Architecture
Architecture documentation for the SpringBootServletInitializerTests class in SpringBootServletInitializerTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java lines 70–384
@ExtendWith(OutputCaptureExtension.class)
class SpringBootServletInitializerTests {
private final ServletContext servletContext = new MockServletContext();
private @Nullable SpringApplication application;
@AfterEach
void verifyLoggingOutput(CapturedOutput output) {
assertThat(output).doesNotContain(StandardServletEnvironment.class.getSimpleName());
}
@Test
void failsWithoutConfigure() {
assertThatIllegalStateException()
.isThrownBy(() -> new MockSpringBootServletInitializer().createRootApplicationContext(this.servletContext))
.withMessageContaining("No SpringApplication sources have been defined");
}
@Test
void withConfigurationAnnotation() {
new WithConfigurationAnnotation().createRootApplicationContext(this.servletContext);
assertThat(this.application).isNotNull();
assertThat(this.application.getAllSources()).containsOnly(WithConfigurationAnnotation.class,
ErrorPageFilterConfiguration.class);
}
@Test
void withConfiguredSource() {
new WithConfiguredSource().createRootApplicationContext(this.servletContext);
assertThat(this.application).isNotNull();
assertThat(this.application.getAllSources()).containsOnly(Config.class, ErrorPageFilterConfiguration.class);
}
@Test
void applicationBuilderCanBeCustomized() {
CustomSpringBootServletInitializer servletInitializer = new CustomSpringBootServletInitializer();
servletInitializer.createRootApplicationContext(this.servletContext);
assertThat(servletInitializer.applicationBuilder.built).isTrue();
}
@Test
void mainClassHasSensibleDefault() {
new WithConfigurationAnnotation().createRootApplicationContext(this.servletContext);
assertThat(this.application).hasFieldOrPropertyWithValue("mainApplicationClass",
WithConfigurationAnnotation.class);
}
@Test
void shutdownHooksAreNotRegistered() throws ServletException {
new WithConfigurationAnnotation().onStartup(this.servletContext);
assertThat(this.servletContext.getAttribute(LoggingApplicationListener.REGISTER_SHUTDOWN_HOOK_PROPERTY))
.isEqualTo(false);
assertThat(this.application).isNotNull();
Object properties = ReflectionTestUtils.getField(this.application, "properties");
assertThat(properties).hasFieldOrPropertyWithValue("registerShutdownHook", false);
}
@Test
void errorPageFilterRegistrationCanBeDisabled() {
try (AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilterNotRegistered()
.createRootApplicationContext(this.servletContext)) {
assertThat(context).isNotNull();
Map<String, ErrorPageFilter> errorPageFilterBeans = context.getBeansOfType(ErrorPageFilter.class);
assertThat(errorPageFilterBeans).isEmpty();
}
}
@Test
@SuppressWarnings("rawtypes")
void errorPageFilterIsRegisteredWithNearHighestPrecedence() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames()).willReturn(Collections.emptyEnumeration());
given(servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
try (AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilter()
.createRootApplicationContext(servletContext)) {
assertThat(context).isNotNull();
Map<String, FilterRegistrationBean> registrations = context.getBeansOfType(FilterRegistrationBean.class);
assertThat(registrations).hasSize(1);
FilterRegistrationBean errorPageFilterRegistration = registrations.get("errorPageFilterRegistration");
assertThat(errorPageFilterRegistration).isNotNull();
assertThat(errorPageFilterRegistration.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE + 1);
}
}
@Test
@SuppressWarnings("rawtypes")
void errorPageFilterIsRegisteredForRequestAndAsyncDispatch() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames()).willReturn(Collections.emptyEnumeration());
given(servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
try (AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilter()
.createRootApplicationContext(servletContext)) {
assertThat(context).isNotNull();
Map<String, FilterRegistrationBean> registrations = context.getBeansOfType(FilterRegistrationBean.class);
assertThat(registrations).hasSize(1);
FilterRegistrationBean errorPageFilterRegistration = registrations.get("errorPageFilterRegistration");
assertThat(errorPageFilterRegistration).hasFieldOrPropertyWithValue("dispatcherTypes",
EnumSet.of(DispatcherType.ASYNC, DispatcherType.REQUEST));
}
}
@Test
void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured() {
try (ConfigurableApplicationContext context = new SpringApplication(ExecutableWar.class).run()) {
assertThat(context.getBeansOfType(ErrorPageFilter.class)).isEmpty();
}
}
@Test
void environmentIsConfiguredWithStandardServletEnvironment() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames())
.willReturn(Collections.enumeration(Collections.singletonList("servlet.init.test")));
given(servletContext.getInitParameter("servlet.init.test")).willReturn("from-servlet-context");
given(servletContext.getAttributeNames())
.willReturn(Collections.enumeration(Collections.singletonList("servlet.attribute.test")));
given(servletContext.getAttribute("servlet.attribute.test")).willReturn("also-from-servlet-context");
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new RegularSpringBootServletInitializer()
.createRootApplicationContext(servletContext)) {
assertThat(context).isNotNull();
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment).isInstanceOf(StandardServletEnvironment.class);
assertThat(environment.getClass().getName()).endsWith("ApplicationServletEnvironment");
assertThat(environment.getPropertySources()).map(PropertySource::getName)
.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
assertThat(environment.getProperty("servlet.init.test")).isEqualTo("from-servlet-context");
}
}
@Test
void servletContextPropertySourceIsAvailablePriorToRefresh() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames())
.willReturn(Collections.enumeration(Collections.singletonList("spring.profiles.active")));
given(servletContext.getInitParameter("spring.profiles.active")).willReturn("from-servlet-context");
given(servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new PropertySourceVerifyingSpringBootServletInitializer()
.createRootApplicationContext(servletContext)) {
assertThat(context).isNotNull();
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("from-servlet-context");
}
}
@Test
void whenServletContextIsDestroyedThenJdbcDriversAreDeregistered() throws ServletException {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames()).willReturn(new Vector<String>().elements());
given(servletContext.getAttributeNames()).willReturn(new Vector<String>().elements());
AtomicBoolean driversDeregistered = new AtomicBoolean();
new SpringBootServletInitializer() {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Config.class);
}
@Override
protected void deregisterJdbcDrivers(ServletContext servletContext) {
driversDeregistered.set(true);
}
}.onStartup(servletContext);
ArgumentCaptor<ServletContextListener> captor = ArgumentCaptor.forClass(ServletContextListener.class);
then(servletContext).should().addListener(captor.capture());
captor.getValue().contextDestroyed(new ServletContextEvent(servletContext));
assertThat(driversDeregistered).isTrue();
}
@Test
void whenServletContextIsDestroyedThenReactorSchedulersAreShutDown() throws ServletException {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
given(servletContext.getInitParameterNames()).willReturn(new Vector<String>().elements());
given(servletContext.getAttributeNames()).willReturn(new Vector<String>().elements());
AtomicBoolean schedulersShutDown = new AtomicBoolean();
new SpringBootServletInitializer() {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Config.class);
}
@Override
protected void shutDownSharedReactorSchedulers(ServletContext servletContext) {
schedulersShutDown.set(true);
}
}.onStartup(servletContext);
ArgumentCaptor<ServletContextListener> captor = ArgumentCaptor.forClass(ServletContextListener.class);
then(servletContext).should().addListener(captor.capture());
captor.getValue().contextDestroyed(new ServletContextEvent(servletContext));
assertThat(schedulersShutDown).isTrue();
}
static class PropertySourceVerifyingSpringBootServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestApp.class).listeners(new PropertySourceVerifyingApplicationListener());
}
}
@Configuration(proxyBeanMethods = false)
static class TestApp {
}
private class MockSpringBootServletInitializer extends SpringBootServletInitializer {
@Override
protected @Nullable WebApplicationContext run(SpringApplication application) {
SpringBootServletInitializerTests.this.application = application;
return null;
}
}
private final class CustomSpringBootServletInitializer extends MockSpringBootServletInitializer {
private final CustomSpringApplicationBuilder applicationBuilder = new CustomSpringApplicationBuilder();
@Override
protected SpringApplicationBuilder createSpringApplicationBuilder() {
return this.applicationBuilder;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Config.class);
}
}
@Configuration(proxyBeanMethods = false)
public class WithConfigurationAnnotation extends MockSpringBootServletInitializer {
}
public class WithConfiguredSource extends MockSpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Config.class);
}
}
@Configuration(proxyBeanMethods = false)
static class WithErrorPageFilterNotRegistered extends SpringBootServletInitializer {
WithErrorPageFilterNotRegistered() {
setRegisterErrorPageFilter(false);
}
}
@Configuration(proxyBeanMethods = false)
static class WithErrorPageFilter extends SpringBootServletInitializer {
}
@Configuration(proxyBeanMethods = false)
static class ExecutableWar extends SpringBootServletInitializer {
}
@Configuration(proxyBeanMethods = false)
static class Config {
}
static class CustomSpringApplicationBuilder extends SpringApplicationBuilder {
private boolean built;
@Override
public SpringApplication build() {
this.built = true;
return super.build();
}
}
static class RegularSpringBootServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestApp.class);
}
}
private static final class PropertySourceVerifyingApplicationListener
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
PropertySource<?> propertySource = event.getEnvironment()
.getPropertySources()
.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getProperty("spring.profiles.active")).isEqualTo("from-servlet-context");
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free