Search in sources :

Example 6 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.

the class AbstractTagTests method createPageContext.

protected MockPageContext createPageContext() {
    MockServletContext sc = new MockServletContext();
    SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
    wac.setServletContext(sc);
    wac.setNamespace("test");
    wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (inDispatcherServlet()) {
        request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
        LocaleResolver lr = new AcceptHeaderLocaleResolver();
        request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
        ThemeResolver tr = new FixedThemeResolver();
        request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
        request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
    } else {
        sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    }
    return new MockPageContext(sc, request, response);
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) ThemeResolver(org.springframework.web.servlet.ThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 7 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.

the class ViewResolverTests method xmlViewResolver.

@Test
@SuppressWarnings("deprecation")
public void xmlViewResolver() throws Exception {
    this.wac.registerSingleton("testBean", TestBean.class);
    this.wac.refresh();
    TestBean testBean = (TestBean) this.wac.getBean("testBean");
    org.springframework.web.servlet.view.XmlViewResolver vr = new org.springframework.web.servlet.view.XmlViewResolver();
    vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
    vr.setApplicationContext(this.wac);
    View view1 = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(TestView.class.equals(view1.getClass())).as("Correct view class").isTrue();
    assertThat("/example1.jsp".equals(((InternalResourceView) view1).getUrl())).as("Correct URL").isTrue();
    View view2 = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(JstlView.class.equals(view2.getClass())).as("Correct view class").isTrue();
    assertThat("/example2new.jsp".equals(((InternalResourceView) view2).getUrl())).as("Correct URL").isTrue();
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view1.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
    assertThat(testBean.equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
    this.request.clearAttributes();
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    view2.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat("testvalue1".equals(this.request.getAttribute("test1"))).as("Correct test1 attribute").isTrue();
    assertThat("testvalue2".equals(this.request.getAttribute("test2"))).as("Correct test2 attribute").isTrue();
}
Also used : HashMap(java.util.HashMap) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) ClassPathResource(org.springframework.core.io.ClassPathResource) TestBean(org.springframework.beans.testfixture.beans.TestBean) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) Test(org.junit.jupiter.api.Test)

Example 8 with FixedThemeResolver

use of org.springframework.web.servlet.theme.FixedThemeResolver in project spring-framework by spring-projects.

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(((InternalResourceView) 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(((InternalResourceView) view).getContentType()).as("Correct textContentType").isEqualTo("myContentType");
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    this.request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.request, this.response);
    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 : FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) RequestContext(org.springframework.web.servlet.support.RequestContext) View(org.springframework.web.servlet.View) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)

Aggregations

FixedThemeResolver (org.springframework.web.servlet.theme.FixedThemeResolver)8 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 HashMap (java.util.HashMap)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 View (org.springframework.web.servlet.View)3 ServletContext (jakarta.servlet.ServletContext)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)2 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)1 TicketGrantingCookieProperties (org.apereo.cas.configuration.model.support.cookie.TicketGrantingCookieProperties)1