Search in sources :

Example 1 with WebServletApplicationContext

use of cn.taketoday.web.servlet.WebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class BaseViewTests method renderWithStaticAttributesNoCollision.

/**
 * Test attribute passing, NOT CSV parsing.
 */
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    tv.render(model, requestContext);
    checkContainsAll(model, tv.model);
    checkContainsAll(p, tv.model);
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.mock.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with WebServletApplicationContext

use of cn.taketoday.web.servlet.WebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class BaseViewTests method dynamicModelOverridesStaticAttributesIfCollision.

@Test
public void dynamicModelOverridesStaticAttributesIfCollision() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("one", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    tv.render(model, requestContext);
    // Check it contains all
    checkContainsAll(model, tv.model);
    assertThat(tv.model.size()).isEqualTo(3);
    assertThat(tv.model.get("something")).isEqualTo("else");
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.mock.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with WebServletApplicationContext

use of cn.taketoday.web.servlet.WebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class AbstractGenericWebContextLoader method loadContext.

// SmartContextLoader
/**
 * Load a Spring {@link WebServletApplicationContext} 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 GenericWebServletApplicationContext} 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 GenericWebServletApplicationContext#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 WebServletApplicationContext}.</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 StandardBeanFactory}.</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 SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebServletApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration, () -> String.format("Cannot load WebServletApplicationContext 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 WebServletApplicationContext for merged context configuration %s.", webMergedConfig));
    }
    validateMergedContextConfiguration(webMergedConfig);
    GenericWebServletApplicationContext context = new GenericWebServletApplicationContext();
    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    configureWebResources(context, webMergedConfig);
    prepareContext(context, webMergedConfig);
    customizeBeanFactory(context.getBeanFactory(), webMergedConfig);
    loadBeanDefinitions(context, webMergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context, webMergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}
Also used : GenericWebServletApplicationContext(cn.taketoday.web.context.support.GenericWebServletApplicationContext) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) GenericWebServletApplicationContext(cn.taketoday.web.context.support.GenericWebServletApplicationContext)

Example 4 with WebServletApplicationContext

use of cn.taketoday.web.servlet.WebServletApplicationContext in project today-infrastructure by TAKETODAY.

the class ControllerIntegrationTests method verifyRootWacSupport.

@Test
void verifyRootWacSupport() {
    assertThat(foo).isEqualTo("foo");
    assertThat(bar).isEqualTo("bar");
    ApplicationContext parent = wac.getParent();
    assertThat(parent).isNotNull();
    boolean condition = parent instanceof WebServletApplicationContext;
    assertThat(condition).isTrue();
    WebServletApplicationContext root = (WebServletApplicationContext) parent;
    assertThat(root.getBeansOfType(String.class).containsKey("bar")).isFalse();
    ServletContext childServletContext = wac.getServletContext();
    assertThat(childServletContext).isNotNull();
    ServletContext rootServletContext = root.getServletContext();
    assertThat(rootServletContext).isNotNull();
    assertThat(rootServletContext).isSameAs(childServletContext);
    assertThat(rootServletContext.getAttribute(WebServletApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(root);
    assertThat(childServletContext.getAttribute(WebServletApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(root);
}
Also used : WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) ServletContext(jakarta.servlet.ServletContext) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with WebServletApplicationContext

use of cn.taketoday.web.servlet.WebServletApplicationContext in project today-framework by TAKETODAY.

the class DispatcherServletInitializer method getServlet.

@Override
public DispatcherServlet getServlet() {
    DispatcherServlet dispatcherServlet = super.getServlet();
    if (dispatcherServlet == null && isAutoCreateDispatcher()) {
        WebServletApplicationContext context = getApplicationContext();
        BeanDefinitionRegistry registry = context.unwrapFactory(BeanDefinitionRegistry.class);
        if (!registry.containsBeanDefinition(DispatcherServlet.class)) {
            AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context, registry);
            reader.setEnableConditionEvaluation(false);
            reader.registerBean(DISPATCHER_SERVLET, DispatcherServlet.class);
        }
        dispatcherServlet = context.getBean(DispatcherServlet.class);
        setServlet(dispatcherServlet);
    }
    return dispatcherServlet;
}
Also used : DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext)

Aggregations

WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)27 Test (org.junit.jupiter.api.Test)20 HashMap (java.util.HashMap)14 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)12 RequestContext (cn.taketoday.web.RequestContext)10 ApplicationContext (cn.taketoday.context.ApplicationContext)8 MockServletContext (cn.taketoday.web.mock.MockServletContext)8 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)8 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)8 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)7 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)7 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)7 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)7 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)6 ServletContext (jakarta.servlet.ServletContext)6 Properties (java.util.Properties)6 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 HandlerMatchingMetadata (cn.taketoday.web.HandlerMatchingMetadata)4 GenericWebServletApplicationContext (cn.taketoday.web.context.support.GenericWebServletApplicationContext)4 AcceptHeaderLocaleResolver (cn.taketoday.web.i18n.AcceptHeaderLocaleResolver)4