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("/");
}
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);
}
}
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());
}
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, "/*");
}
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());
}
}
Aggregations