Search in sources :

Example 56 with Servlet

use of javax.servlet.Servlet in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method singleServletBean.

@Test
public void singleServletBean() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    verify(factory.getServletContext()).addServlet("servletBean", servlet);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 57 with Servlet

use of javax.servlet.Servlet in project spring-boot by spring-projects.

the class ServletContextInitializerBeans method addServletContextInitializerBean.

private void addServletContextInitializerBean(String beanName, ServletContextInitializer initializer, ListableBeanFactory beanFactory) {
    if (initializer instanceof ServletRegistrationBean) {
        Servlet source = ((ServletRegistrationBean<?>) initializer).getServlet();
        addServletContextInitializerBean(Servlet.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof FilterRegistrationBean) {
        Filter source = ((FilterRegistrationBean<?>) initializer).getFilter();
        addServletContextInitializerBean(Filter.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof DelegatingFilterProxyRegistrationBean) {
        String source = ((DelegatingFilterProxyRegistrationBean) initializer).getTargetBeanName();
        addServletContextInitializerBean(Filter.class, beanName, initializer, beanFactory, source);
    } else if (initializer instanceof ServletListenerRegistrationBean) {
        EventListener source = ((ServletListenerRegistrationBean<?>) initializer).getListener();
        addServletContextInitializerBean(EventListener.class, beanName, initializer, beanFactory, source);
    } else {
        addServletContextInitializerBean(ServletContextInitializer.class, beanName, initializer, beanFactory, initializer);
    }
}
Also used : Filter(javax.servlet.Filter) Servlet(javax.servlet.Servlet) EventListener(java.util.EventListener)

Example 58 with Servlet

use of javax.servlet.Servlet 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)

Example 59 with Servlet

use of javax.servlet.Servlet in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
public void servletAndFilterBeans() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter1 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter1).getOrder()).willReturn(1);
    Filter filter2 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean2", beanDefinition(filter2));
    this.context.registerBeanDefinition("filterBean1", beanDefinition(filter1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    verify(factory.getServletContext()).addServlet("servletBean", servlet);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
    ordered.verify(factory.getServletContext()).addFilter("filterBean1", filter1);
    ordered.verify(factory.getServletContext()).addFilter("filterBean2", filter2);
    verify(factory.getRegisteredFilter(0).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    verify(factory.getRegisteredFilter(1).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(javax.servlet.Filter) Ordered(org.springframework.core.Ordered) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 60 with Servlet

use of javax.servlet.Servlet in project spring-framework by spring-projects.

the class MockFilterChainTests method doFilterWithServletAndFilters.

@Test
public void doFilterWithServletAndFilters() throws Exception {
    Servlet servlet = mock(Servlet.class);
    MockFilter filter2 = new MockFilter(servlet);
    MockFilter filter1 = new MockFilter(null);
    MockFilterChain chain = new MockFilterChain(servlet, filter1, filter2);
    chain.doFilter(this.request, this.response);
    assertTrue(filter1.invoked);
    assertTrue(filter2.invoked);
    verify(servlet).service(this.request, this.response);
    try {
        chain.doFilter(this.request, this.response);
        fail("Expected Exception");
    } catch (IllegalStateException ex) {
        assertEquals("This FilterChain has already been called!", ex.getMessage());
    }
}
Also used : Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Aggregations

Servlet (javax.servlet.Servlet)86 Test (org.junit.Test)18 HttpServlet (javax.servlet.http.HttpServlet)16 ServletException (javax.servlet.ServletException)15 IOException (java.io.IOException)11 OptingServlet (org.apache.sling.api.servlets.OptingServlet)11 GenericServlet (javax.servlet.GenericServlet)10 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)9 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)9 ServletContext (javax.servlet.ServletContext)8 UnavailableException (javax.servlet.UnavailableException)8 Resource (org.apache.sling.api.resource.Resource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)7 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)7 ServletInfo (io.undertow.servlet.api.ServletInfo)6 ArrayList (java.util.ArrayList)5 Filter (javax.servlet.Filter)5 Context (org.apache.catalina.Context)5 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5