Search in sources :

Example 11 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method checkMappings.

private void checkMappings(String beanName) throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setServletContext(sc);
    wac.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map2.xml" });
    wac.refresh();
    Object bean = wac.getBean("mainController");
    Object otherBean = wac.getBean("otherController");
    Object defaultBean = wac.getBean("starController");
    HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
    HandlerExecutionChain hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome.x");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/");
    req.setServletPath("/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/welcome.html");
    req.setContextPath("/app");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-welcome.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-show.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-bookseats.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/somePath");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Example 12 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-security by spring-projects.

the class AbstractWebServerIntegrationTests method createMockMvc.

protected final MockMvc createMockMvc(String... configLocations) throws Exception {
    if (this.context != null) {
        throw new IllegalStateException("context is already loaded");
    }
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocations(configLocations);
    context.setServletContext(new MockServletContext());
    context.refresh();
    this.context = context;
    return MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Aggregations

XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 Test (org.junit.Test)4 Before (org.junit.Before)3 MockServletContext (org.springframework.mock.web.MockServletContext)2 Filter (javax.servlet.Filter)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 NoSuchMessageException (org.springframework.context.NoSuchMessageException)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 LifecycleBean (org.springframework.tests.sample.beans.LifecycleBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1 AbstractRefreshableWebApplicationContext (org.springframework.web.context.support.AbstractRefreshableWebApplicationContext)1 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1