Search in sources :

Example 71 with WebApplicationContext

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

the class DefaultMockMvcBuilderTests method rootWacServletContainerAttributeNotPreviouslySet.

/**
	 * See SPR-12553 and SPR-13075.
	 */
@Test
public void rootWacServletContainerAttributeNotPreviouslySet() {
    StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
    DefaultMockMvcBuilder builder = webAppContextSetup(root);
    WebApplicationContext wac = builder.initWebAppContext();
    assertSame(root, wac);
    assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext));
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 72 with WebApplicationContext

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

the class DefaultMockMvcBuilderTests method rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy.

/**
	 * See SPR-12553 and SPR-13075.
	 */
@Test
public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy() {
    StaticApplicationContext ear = new StaticApplicationContext();
    StaticWebApplicationContext root = new StaticWebApplicationContext();
    root.setParent(ear);
    root.setServletContext(this.servletContext);
    StaticWebApplicationContext dispatcher = new StaticWebApplicationContext();
    dispatcher.setParent(root);
    dispatcher.setServletContext(this.servletContext);
    DefaultMockMvcBuilder builder = webAppContextSetup(dispatcher);
    WebApplicationContext wac = builder.initWebAppContext();
    assertSame(dispatcher, wac);
    assertSame(root, wac.getParent());
    assertSame(ear, wac.getParent().getParent());
    assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext));
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 73 with WebApplicationContext

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

the class AbstractMockMvcBuilder method build.

/**
	 * Build a {@link org.springframework.test.web.servlet.MockMvc} instance.
	 */
@Override
@SuppressWarnings("rawtypes")
public final MockMvc build() {
    WebApplicationContext wac = initWebAppContext();
    ServletContext servletContext = wac.getServletContext();
    MockServletConfig mockServletConfig = new MockServletConfig(servletContext);
    for (MockMvcConfigurer configurer : this.configurers) {
        RequestPostProcessor processor = configurer.beforeMockMvcCreated(this, wac);
        if (processor != null) {
            if (this.defaultRequestBuilder == null) {
                this.defaultRequestBuilder = MockMvcRequestBuilders.get("/");
            }
            if (this.defaultRequestBuilder instanceof ConfigurableSmartRequestBuilder) {
                ((ConfigurableSmartRequestBuilder) this.defaultRequestBuilder).with(processor);
            }
        }
    }
    Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]);
    return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers);
}
Also used : RequestPostProcessor(org.springframework.test.web.servlet.request.RequestPostProcessor) Filter(javax.servlet.Filter) ConfigurableSmartRequestBuilder(org.springframework.test.web.servlet.request.ConfigurableSmartRequestBuilder) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.MockServletConfig) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 74 with WebApplicationContext

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

the class ControllerIntegrationTests method verifyRootWacSupport.

@Test
public void verifyRootWacSupport() {
    assertEquals("foo", foo);
    assertEquals("bar", bar);
    ApplicationContext parent = wac.getParent();
    assertNotNull(parent);
    assertTrue(parent instanceof WebApplicationContext);
    WebApplicationContext root = (WebApplicationContext) parent;
    assertFalse(root.getBeansOfType(String.class).containsKey("bar"));
    ServletContext childServletContext = wac.getServletContext();
    assertNotNull(childServletContext);
    ServletContext rootServletContext = root.getServletContext();
    assertNotNull(rootServletContext);
    assertSame(childServletContext, rootServletContext);
    assertSame(root, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertSame(root, childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 75 with WebApplicationContext

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

the class DispatcherWacRootWacEarTests method verifyDispatcherWacConfig.

@Test
public void verifyDispatcherWacConfig() {
    ApplicationContext parent = wac.getParent();
    assertNotNull(parent);
    assertTrue(parent instanceof WebApplicationContext);
    ApplicationContext grandParent = parent.getParent();
    assertNotNull(grandParent);
    assertFalse(grandParent instanceof WebApplicationContext);
    ServletContext dispatcherServletContext = wac.getServletContext();
    assertNotNull(dispatcherServletContext);
    ServletContext rootServletContext = ((WebApplicationContext) parent).getServletContext();
    assertNotNull(rootServletContext);
    assertSame(dispatcherServletContext, rootServletContext);
    assertSame(parent, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertSame(parent, dispatcherServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertEquals("ear", ear);
    assertEquals("root", root);
    assertEquals("dispatcher", dispatcher);
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Aggregations

WebApplicationContext (org.springframework.web.context.WebApplicationContext)103 Test (org.junit.Test)32 ServletContext (javax.servlet.ServletContext)17 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)13 ApplicationContext (org.springframework.context.ApplicationContext)11 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)11 HashMap (java.util.HashMap)9 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)9 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)7 Filter (javax.servlet.Filter)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)6 Binding (groovy.lang.Binding)5 BeanBuilder (hudson.util.spring.BeanBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4