Search in sources :

Example 11 with WebApplicationContext

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

the class AbstractGenericWebContextLoader method loadContext.

// --- SmartContextLoader -----------------------------------------------
/**
	 * Load a Spring {@link WebApplicationContext} from the supplied
	 * {@link MergedContextConfiguration}.
	 *
	 * <p>Implementation details:
	 *
	 * <ul>
	 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
	 * to allow subclasses to validate the supplied configuration before proceeding.</li>
	 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
	 * <li>If the supplied {@code MergedContextConfiguration} references a
	 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
	 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
	 * ApplicationContext} will be retrieved and
	 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
	 * for the context created by this method.</li>
	 * <li>Delegates to {@link #configureWebResources} to create the
	 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
	 * <li>Calls {@link #prepareContext} to allow for customizing the context
	 * before bean definitions are loaded.</li>
	 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
	 * context's {@code DefaultListableBeanFactory}.</li>
	 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
	 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
	 * <li>Delegates to {@link AnnotationConfigUtils} for
	 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
	 * annotation configuration processors.</li>
	 * <li>Calls {@link #customizeContext} to allow for customizing the context
	 * before it is refreshed.</li>
	 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
	 * context and registers a JVM shutdown hook for it.</li>
	 * </ul>
	 *
	 * @return a new web application context
	 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
	 * @see GenericWebApplicationContext
	 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration, () -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig));
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.", webMergedConfig));
    }
    validateMergedContextConfiguration(webMergedConfig);
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    configureWebResources(context, webMergedConfig);
    prepareContext(context, webMergedConfig);
    customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
    loadBeanDefinitions(context, webMergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context, webMergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 12 with WebApplicationContext

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

the class MockServerContainerContextCustomizer method customizeContext.

@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
    if (context instanceof WebApplicationContext) {
        WebApplicationContext wac = (WebApplicationContext) context;
        wac.getServletContext().setAttribute("javax.websocket.server.ServerContainer", new MockServerContainer());
    }
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 13 with WebApplicationContext

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

the class RootWacEarTests method verifyRootWacConfig.

@Test
public void verifyRootWacConfig() {
    ApplicationContext parent = wac.getParent();
    assertNotNull(parent);
    assertFalse(parent instanceof WebApplicationContext);
    assertEquals("ear", ear);
    assertEquals("root", root);
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 14 with WebApplicationContext

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

the class DefaultMockMvcBuilder method initWebAppContext.

@Override
protected WebApplicationContext initWebAppContext() {
    ServletContext servletContext = this.webAppContext.getServletContext();
    ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (rootWac == null) {
        rootWac = this.webAppContext;
        ApplicationContext parent = this.webAppContext.getParent();
        while (parent != null) {
            if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
                rootWac = parent;
                break;
            }
            parent = parent.getParent();
        }
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
    }
    return this.webAppContext;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 15 with WebApplicationContext

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

the class JavaConfigTests method verifyRootWacSupport.

/**
	 * Verify that the breaking change introduced in <a
	 * href="https://jira.spring.io/browse/SPR-12553">SPR-12553</a> has been reverted.
	 *
	 * <p>This code has been copied from
	 * {@link org.springframework.test.context.hierarchies.web.ControllerIntegrationTests}.
	 *
	 * @see org.springframework.test.context.hierarchies.web.ControllerIntegrationTests#verifyRootWacSupport()
	 */
private void verifyRootWacSupport() {
    assertNotNull(personDao);
    assertNotNull(personController);
    ApplicationContext parent = wac.getParent();
    assertNotNull(parent);
    assertTrue(parent instanceof WebApplicationContext);
    WebApplicationContext root = (WebApplicationContext) parent;
    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)

Aggregations

WebApplicationContext (org.springframework.web.context.WebApplicationContext)100 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