use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames.
@Test
void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() {
addWebServerFactoryBean();
DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean("filterBean");
this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
BeanDefinition filterBeanDefinition = beanDefinition(new IllegalStateException("Create FilterBean Failure"));
filterBeanDefinition.setLazyInit(true);
this.context.registerBeanDefinition("filterBean", filterBeanDefinition);
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
then(servletContext).should(atMost(1)).addFilter(anyString(), this.filterCaptor.capture());
// Up to this point the filterBean should not have been created, calling
// the delegate proxy will trigger creation and an exception
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
this.filterCaptor.getValue().init(new MockFilterConfig());
this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
}).withMessageContaining("Create FilterBean Failure");
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method unorderedServletContextInitializerBeans.
@Test
void unorderedServletContextInitializerBeans() throws Exception {
addWebServerFactoryBean();
ServletContextInitializer initializer1 = mock(ServletContextInitializer.class);
ServletContextInitializer initializer2 = mock(ServletContextInitializer.class);
this.context.registerBeanDefinition("initializerBean2", beanDefinition(initializer2));
this.context.registerBeanDefinition("initializerBean1", beanDefinition(initializer1));
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
then(initializer1).should().onStartup(servletContext);
then(initializer2).should().onStartup(servletContext);
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method servletContextListenerBeans.
@Test
void servletContextListenerBeans() {
addWebServerFactoryBean();
ServletContextListener initializer = mock(ServletContextListener.class);
this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
then(servletContext).should().addListener(initializer);
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContext method createWebServer.
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");
ServletWebServerFactory factory = getWebServerFactory();
createWebServer.tag("factory", factory.getClass().toString());
this.webServer = factory.getWebServer(getSelfInitializer());
createWebServer.end();
getBeanFactory().registerSingleton("webServerGracefulShutdown", new WebServerGracefulShutdownLifecycle(this.webServer));
getBeanFactory().registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this, this.webServer));
} else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
} catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class SpringBootServletInitializerTests method whenServletContextIsDestroyedThenJdbcDriversAreDeregistered.
@Test
void whenServletContextIsDestroyedThenJdbcDriversAreDeregistered() throws ServletException {
ServletContext servletContext = mock(ServletContext.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();
}
Aggregations