Search in sources :

Example 6 with DispatcherServlet

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

the class DispatcherServletAutoConfigurationTests method registrationOverrideWithDispatcherServletWrongName.

// If a DispatcherServlet instance is registered with a name different
// from the default one, we're registering one anyway
@Test
public void registrationOverrideWithDispatcherServletWrongName() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(CustomDispatcherServletWrongName.class, DispatcherServletAutoConfiguration.class);
    this.context.setServletContext(new MockServletContext());
    this.context.refresh();
    ServletRegistrationBean<?> registration = this.context.getBean(ServletRegistrationBean.class);
    assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]");
    assertThat(registration.getServletName()).isEqualTo("dispatcherServlet");
    assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length).isEqualTo(2);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 7 with DispatcherServlet

use of org.springframework.web.servlet.DispatcherServlet 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)

Example 8 with DispatcherServlet

use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.

the class ContextLoaderTests method testFrameworkServletWithCustomLocation.

@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 9 with DispatcherServlet

use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.

the class ViewResolutionIntegrationTests method runTest.

private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
    String basePath = "org/springframework/web/servlet/config/annotation";
    MockServletContext servletContext = new MockServletContext(basePath);
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(configClass);
    context.setServletContext(servletContext);
    context.refresh();
    DispatcherServlet servlet = new DispatcherServlet(context);
    servlet.init(servletConfig);
    servlet.service(request, response);
    return response;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 10 with DispatcherServlet

use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.

the class CglibProxyControllerTests method initServlet.

@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
    servlet = new DispatcherServlet() {

        @Override
        protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
            DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
            autoProxyCreator.setProxyTargetClass(true);
            autoProxyCreator.setBeanFactory(wac.getBeanFactory());
            wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
            wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
            wac.refresh();
            return wac;
        }
    };
    servlet.init(new MockServletConfig());
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Aggregations

DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)33 Test (org.junit.Test)16 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)7 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 ServletRegistration (javax.servlet.ServletRegistration)4 Server (org.eclipse.jetty.server.Server)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)4 Bean (org.springframework.context.annotation.Bean)4 MockServletContext (org.springframework.mock.web.MockServletContext)4 InetSocketAddress (java.net.InetSocketAddress)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 Order (org.springframework.core.annotation.Order)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 Filter (javax.servlet.Filter)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2