Search in sources :

Example 1 with InternalResourceView

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

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 2 with InternalResourceView

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

the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.

private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
    this.wac.refresh();
    vr.setApplicationContext(this.wac);
    vr.setContentType("myContentType");
    vr.setRequestContextAttribute("rc");
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
    assertThat(view.getContentType()).as("Correct textContentType").isEqualTo("myContentType");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
    assertThat(view.getContentType()).as("Correct textContentType").isEqualTo("myContentType");
    this.wac.registerSingleton(LocaleResolver.BEAN_NAME, new AcceptHeaderLocaleResolver());
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.requestContext);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    boolean condition = this.request.getAttribute("rc") instanceof RequestContext;
    assertThat(condition).as("Correct rc attribute").isTrue();
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    assertThat(((RedirectView) view).getApplicationContext()).as("View not initialized as bean").isSameAs(this.wac);
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : HashMap(java.util.HashMap) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) AcceptHeaderLocaleResolver(cn.taketoday.web.i18n.AcceptHeaderLocaleResolver)

Example 3 with InternalResourceView

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

the class FreeMarkerViewTests method freeMarkerViewResolver.

@Test
public void freeMarkerViewResolver() throws Exception {
    MockServletContext sc = new MockServletContext();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    wac.getBeanFactory().registerSingleton("configurer", configurer);
    wac.refresh();
    FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("test", Locale.CANADA);
    assertThat(view.getClass()).as("Correct view class").isEqualTo(FreeMarkerView.class);
    assertThat(((FreeMarkerView) view).getUrl()).as("Correct URL").isEqualTo("prefix_test_suffix");
    view = vr.resolveViewName("non-existing", Locale.CANADA);
    assertThat(view).isNull();
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) AbstractView(cn.taketoday.web.view.AbstractView) View(cn.taketoday.web.view.View) RedirectView(cn.taketoday.web.view.RedirectView) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 4 with InternalResourceView

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

the class ViewResolverTests method doTestUrlBasedViewResolverWithPrefixes.

private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception {
    this.wac.refresh();
    vr.setPrefix("/WEB-INF/");
    vr.setSuffix(".jsp");
    vr.setApplicationContext(this.wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/WEB-INF/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/WEB-INF/example2.jsp");
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView)

Example 5 with InternalResourceView

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

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Aggregations

InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)12 JstlView (cn.taketoday.web.servlet.view.JstlView)8 Test (org.junit.jupiter.api.Test)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 AcceptHeaderLocaleResolver (cn.taketoday.web.i18n.AcceptHeaderLocaleResolver)4 HashMap (java.util.HashMap)4 PropertyValue (cn.taketoday.beans.PropertyValue)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 RequestContext (cn.taketoday.web.RequestContext)2 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)2 AbstractView (cn.taketoday.web.view.AbstractView)2 RedirectView (cn.taketoday.web.view.RedirectView)2 View (cn.taketoday.web.view.View)2 Properties (java.util.Properties)2 MockServletContext (cn.taketoday.web.mock.MockServletContext)1 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)1