Search in sources :

Example 1 with AcceptHeaderLocaleResolver

use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver 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 2 with AcceptHeaderLocaleResolver

use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework by TAKETODAY.

the class FreeMarkerViewTests method validTemplateName.

@Test
public void validTemplateName() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    MockServletContext sc = new MockServletContext();
    Map<String, FreeMarkerConfig> configs = new HashMap<>();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    configs.put("configurer", configurer);
    given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
    given(wac.getServletContext()).willReturn(sc);
    fv.setUrl("templateName");
    fv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    given(wac.getBean(LocaleResolver.BEAN_NAME)).willReturn(new AcceptHeaderLocaleResolver());
    given(wac.getBean(LocaleResolver.BEAN_NAME, LocaleResolver.class)).willReturn(new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    model.put("myattr", "myvalue");
    ServletRequestContext context = new ServletRequestContext(wac, request, response);
    fv.render(model, context);
    assertThat(response.getContentType()).isEqualTo(AbstractView.DEFAULT_CONTENT_TYPE);
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) AcceptHeaderLocaleResolver(cn.taketoday.web.i18n.AcceptHeaderLocaleResolver) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with AcceptHeaderLocaleResolver

use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework by TAKETODAY.

the class ViewResolverTests method internalResourceViewResolverWithContextBeans.

@Test
public void internalResourceViewResolverWithContextBeans() throws Exception {
    this.wac.registerSingleton("myBean", TestBean.class);
    this.wac.registerSingleton("myBean2", TestBean.class);
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setExposeContextBeansAsAttributes(true);
    vr.setApplicationContext(this.wac);
    this.wac.registerSingleton(LocaleResolver.BEAN_NAME, new AcceptHeaderLocaleResolver());
    View view = vr.resolveViewName("example1", Locale.getDefault());
    view.render(new HashMap<String, Object>(), new ServletRequestContext(null, request, response));
}
Also used : HashMap(java.util.HashMap) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Properties(java.util.Properties) AcceptHeaderLocaleResolver(cn.taketoday.web.i18n.AcceptHeaderLocaleResolver) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 4 with AcceptHeaderLocaleResolver

use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework by TAKETODAY.

the class ViewResolverTests method internalResourceViewResolverWithAttributes.

@Test
public void internalResourceViewResolverWithAttributes() throws Exception {
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setApplicationContext(this.wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
    Map<String, Object> attributes = ((InternalResourceView) view).getStaticAttributes();
    assertThat(attributes.get("key1")).isEqualTo("value1");
    assertThat(attributes.get("key2")).isEqualTo(2);
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
    attributes = ((InternalResourceView) view).getStaticAttributes();
    assertThat(attributes.get("key1")).isEqualTo("value1");
    assertThat(attributes.get("key2")).isEqualTo(2);
    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();
    assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
    assertThat(this.request.getAttribute("key1")).isEqualTo("value1");
    assertThat(this.request.getAttribute("key2")).isEqualTo(2);
}
Also used : HashMap(java.util.HashMap) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Properties(java.util.Properties) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) AcceptHeaderLocaleResolver(cn.taketoday.web.i18n.AcceptHeaderLocaleResolver) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 5 with AcceptHeaderLocaleResolver

use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework by TAKETODAY.

the class WebMvcAutoConfiguration method localeResolver.

@Component
@ConditionalOnMissingBean(name = LocaleResolver.BEAN_NAME)
public LocaleResolver localeResolver() {
    if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
        return new FixedLocaleResolver(this.webProperties.getLocale());
    }
    AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
    localeResolver.setDefaultLocale(this.webProperties.getLocale());
    return localeResolver;
}
Also used : FixedLocaleResolver(cn.taketoday.web.i18n.FixedLocaleResolver) AcceptHeaderLocaleResolver(cn.taketoday.web.i18n.AcceptHeaderLocaleResolver) ConditionalOnMissingBean(cn.taketoday.context.condition.ConditionalOnMissingBean) Component(cn.taketoday.lang.Component)

Aggregations

AcceptHeaderLocaleResolver (cn.taketoday.web.i18n.AcceptHeaderLocaleResolver)12 HashMap (java.util.HashMap)10 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)8 Test (org.junit.jupiter.api.Test)8 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)6 JstlView (cn.taketoday.web.servlet.view.JstlView)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)4 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)4 InternalResourceViewResolver (cn.taketoday.web.servlet.view.InternalResourceViewResolver)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)4 Properties (java.util.Properties)4 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)2 Component (cn.taketoday.lang.Component)2 RequestContext (cn.taketoday.web.RequestContext)2 FixedLocaleResolver (cn.taketoday.web.i18n.FixedLocaleResolver)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)2 MockServletContext (cn.taketoday.web.mock.MockServletContext)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)2