Search in sources :

Example 21 with DispatcherServlet

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

the class DefaultMockMvcBuilderTests method dispatcherServletCustomizerProcessedInOrder.

@Test
public void dispatcherServletCustomizerProcessedInOrder() {
    StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
    DefaultMockMvcBuilder builder = webAppContextSetup(root);
    builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
    builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id"));
    builder.dispatchOptions(true);
    MockMvc mvc = builder.build();
    DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc).getPropertyValue("servlet");
    assertEquals("override-id", ds.getContextId());
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 22 with DispatcherServlet

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

the class SpringInitializer method onStartup.

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
    webApplicationContext.register(SpringConfiguration.class);
    servletContext.addServlet("dispatcherServlet", new DispatcherServlet(webApplicationContext)).addMapping("/*");
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext)

Example 23 with DispatcherServlet

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

the class ContextLoaderTests method testFrameworkServletWithDefaultLocation.

@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextClass(XmlWebApplicationContext.class);
    try {
        servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 24 with DispatcherServlet

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

the class AnnotationConfigDispatcherServletInitializerTests method rootContextOnly.

// SPR-11357
@Test
public void rootContextOnly() throws ServletException {
    initializer = new MyAnnotationConfigDispatcherServletInitializer() {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { MyConfiguration.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }
    };
    initializer.onStartup(servletContext);
    DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
    servlet.init(new MockServletConfig(this.servletContext));
    WebApplicationContext wac = servlet.getWebApplicationContext();
    ((AnnotationConfigWebApplicationContext) wac).refresh();
    assertTrue(wac.containsBean("bean"));
    assertTrue(wac.getBean("bean") instanceof MyBean);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 25 with DispatcherServlet

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

the class DispatcherServletAutoConfigurationTests method dispatcherServletDefaultConfig.

@Test
public void dispatcherServletDefaultConfig() {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(DispatcherServletAutoConfiguration.class);
    this.context.refresh();
    DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
    assertThat(bean).extracting("throwExceptionIfNoHandlerFound").containsExactly(false);
    assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true);
    assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false);
    assertThat(new DirectFieldAccessor(this.context.getBean("dispatcherServletRegistration")).getPropertyValue("loadOnStartup")).isEqualTo(-1);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)34 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 ServletRegistration (javax.servlet.ServletRegistration)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 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