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();
}
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();
}
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;
}
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);
}
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;
}
Aggregations