Search in sources :

Example 16 with Servlet

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

the class ServletWebServerApplicationContextTests method servletContextInitializerBeansSkipsRegisteredServletsAndFilters.

@Test
public void servletContextInitializerBeansSkipsRegisteredServletsAndFilters() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter = mock(Filter.class);
    ServletRegistrationBean<Servlet> initializer = new ServletRegistrationBean<>(servlet, "/foo");
    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(servletContext, atMost(1)).addServlet(anyString(), (Servlet) any());
    verify(servletContext, atMost(1)).addFilter(anyString(), (Filter) any());
}
Also used : Filter(javax.servlet.Filter) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 17 with Servlet

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

the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.

@Test
public void multipleServletBeansWithMainDispatcher() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    ordered.verify(servletContext).addServlet("dispatcherServlet", servlet1);
    ordered.verify(servletContext).addServlet("servletBean2", servlet2);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
    verify(factory.getRegisteredServlet(1).getRegistration()).addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 18 with Servlet

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

the class ServletWebServerApplicationContextTests method multipleServletBeans.

@Test
public void multipleServletBeans() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("servletBean1", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    ordered.verify(servletContext).addServlet("servletBean1", servlet1);
    ordered.verify(servletContext).addServlet("servletBean2", servlet2);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/servletBean1/");
    verify(factory.getRegisteredServlet(1).getRegistration()).addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 19 with Servlet

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

the class MockFilterChainTests method doFilterWithServlet.

@Test
public void doFilterWithServlet() throws Exception {
    Servlet servlet = mock(Servlet.class);
    MockFilterChain chain = new MockFilterChain(servlet);
    chain.doFilter(this.request, this.response);
    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)

Example 20 with Servlet

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

the class ServletWebServerFactoryAutoConfigurationTests method verifyContext.

private void verifyContext() {
    MockServletWebServerFactory factory = getWebServerFactory();
    Servlet servlet = this.context.getBean(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME, Servlet.class);
    verify(factory.getServletContext()).addServlet("dispatcherServlet", servlet);
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Servlet(javax.servlet.Servlet) FrameworkServlet(org.springframework.web.servlet.FrameworkServlet) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet)

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