Search in sources :

Example 41 with ServletContext

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");
}
Also used : DelegatingFilterProxyRegistrationBean(org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 42 with ServletContext

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);
}
Also used : ServletContext(jakarta.servlet.ServletContext) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.jupiter.api.Test)

Example 43 with 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);
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 44 with ServletContext

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();
}
Also used : ServletException(jakarta.servlet.ServletException) ServletWebServerFactory(org.springframework.boot.web.servlet.server.ServletWebServerFactory) WebServer(org.springframework.boot.web.server.WebServer) StartupStep(org.springframework.core.metrics.StartupStep) WebServerGracefulShutdownLifecycle(org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle) ServletContext(jakarta.servlet.ServletContext) ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 45 with ServletContext

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletContextListener(jakarta.servlet.ServletContextListener) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContext(jakarta.servlet.ServletContext) Vector(java.util.Vector) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContext (jakarta.servlet.ServletContext)148 Test (org.junit.jupiter.api.Test)63 ServletConfig (jakarta.servlet.ServletConfig)40 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)20 ServletException (jakarta.servlet.ServletException)17 FilterRegistration (jakarta.servlet.FilterRegistration)16 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 Filter (jakarta.servlet.Filter)13 Test (org.junit.Test)13 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 WebApplicationContext (org.springframework.web.context.WebApplicationContext)9 File (java.io.File)8 Context (org.apache.catalina.Context)7 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)7