Search in sources :

Example 6 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method startBlocksUntilReadyToServe.

@Test
public void startBlocksUntilReadyToServe() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    final Date[] date = new Date[1];
    this.webServer = factory.getWebServer(new ServletContextInitializer() {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            try {
                Thread.sleep(500);
                date[0] = new Date();
            } catch (InterruptedException ex) {
                throw new ServletException(ex);
            }
        }
    });
    this.webServer.start();
    assertThat(date[0]).isNotNull();
}
Also used : ServletException(javax.servlet.ServletException) ServletContext(javax.servlet.ServletContext) Date(java.util.Date) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 7 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method multipleConfigurations.

@Test
public void multipleConfigurations() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    ServletContextInitializer[] initializers = new ServletContextInitializer[6];
    for (int i = 0; i < initializers.length; i++) {
        initializers[i] = mock(ServletContextInitializer.class);
    }
    factory.setInitializers(Arrays.asList(initializers[2], initializers[3]));
    factory.addInitializers(initializers[4], initializers[5]);
    this.webServer = factory.getWebServer(initializers[0], initializers[1]);
    this.webServer.start();
    InOrder ordered = inOrder((Object[]) initializers);
    for (ServletContextInitializer initializer : initializers) {
        ordered.verify(initializer).onStartup((ServletContext) any());
    }
}
Also used : InOrder(org.mockito.InOrder) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 8 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletContextInitializerBeans.

@Test
public void servletContextInitializerBeans() throws Exception {
    addWebServerFactoryBean();
    ServletContextInitializer initializer1 = mock(ServletContextInitializer.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) initializer1).getOrder()).willReturn(1);
    ServletContextInitializer initializer2 = mock(ServletContextInitializer.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) initializer2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("initializerBean2", beanDefinition(initializer2));
    this.context.registerBeanDefinition("initializerBean1", beanDefinition(initializer1));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    InOrder ordered = inOrder(initializer1, initializer2);
    ordered.verify(initializer1).onStartup(servletContext);
    ordered.verify(initializer2).onStartup(servletContext);
}
Also used : InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) ServletContext(javax.servlet.ServletContext) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 9 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletContextInitializerBeansDoesNotSkipServletsAndFilters.

@Test
public void servletContextInitializerBeansDoesNotSkipServletsAndFilters() throws Exception {
    addWebServerFactoryBean();
    ServletContextInitializer initializer = mock(ServletContextInitializer.class);
    Servlet servlet = mock(Servlet.class);
    Filter filter = mock(Filter.class);
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    verify(initializer).onStartup(servletContext);
    verify(servletContext).addServlet(anyString(), (Servlet) any());
    verify(servletContext).addFilter(anyString(), (Filter) any());
}
Also used : Filter(javax.servlet.Filter) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Aggregations

ServletContextInitializer (org.springframework.boot.web.servlet.ServletContextInitializer)9 Test (org.junit.Test)6 ServletContext (javax.servlet.ServletContext)5 ServletException (javax.servlet.ServletException)2 InOrder (org.mockito.InOrder)2 ServletContainerInitializerInfo (io.undertow.servlet.api.ServletContainerInitializerInfo)1 ImmediateInstanceFactory (io.undertow.servlet.util.ImmediateInstanceFactory)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Date (java.util.Date)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Filter (javax.servlet.Filter)1 Servlet (javax.servlet.Servlet)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 WebappLoader (org.apache.catalina.loader.WebappLoader)1 FixContextListener (org.apache.catalina.startup.Tomcat.FixContextListener)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 Ordered (org.springframework.core.Ordered)1