Search in sources :

Example 1 with FixedLocaleResolver

use of org.springframework.web.servlet.i18n.FixedLocaleResolver in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method zoneIdFromResolver.

@Test
public void zoneIdFromResolver() throws Exception {
    TimeZone timeZone = TimeZone.getTimeZone("America/New_York");
    servletRequest.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(Locale.US, timeZone));
    MethodParameter zoneIdParameter = new MethodParameter(method, 9);
    assertTrue("ZoneId not supported", resolver.supportsParameter(zoneIdParameter));
    Object result = resolver.resolveArgument(zoneIdParameter, null, webRequest, null);
    assertEquals("Invalid result", timeZone.toZoneId(), result);
}
Also used : TimeZone(java.util.TimeZone) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 2 with FixedLocaleResolver

use of org.springframework.web.servlet.i18n.FixedLocaleResolver in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithJstlAndContextParam.

@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("message1", lc.getResourceBundle().getString("code1"));
    assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
Also used : Locale(java.util.Locale) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) TestBean(org.springframework.tests.sample.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with FixedLocaleResolver

use of org.springframework.web.servlet.i18n.FixedLocaleResolver in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method timeZoneFromResolver.

@Test
public void timeZoneFromResolver() throws Exception {
    TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles");
    servletRequest.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(Locale.US, timeZone));
    MethodParameter timeZoneParameter = new MethodParameter(method, 8);
    assertTrue("TimeZone not supported", resolver.supportsParameter(timeZoneParameter));
    Object result = resolver.resolveArgument(timeZoneParameter, null, webRequest, null);
    assertEquals("Invalid result", timeZone, result);
}
Also used : TimeZone(java.util.TimeZone) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 4 with FixedLocaleResolver

use of org.springframework.web.servlet.i18n.FixedLocaleResolver in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method localeFromResolver.

@Test
public void localeFromResolver() throws Exception {
    Locale locale = Locale.ENGLISH;
    servletRequest.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    MethodParameter localeParameter = new MethodParameter(method, 4);
    assertTrue("Locale not supported", resolver.supportsParameter(localeParameter));
    Object result = resolver.resolveArgument(localeParameter, null, webRequest, null);
    assertSame("Invalid result", locale, result);
}
Also used : Locale(java.util.Locale) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 5 with FixedLocaleResolver

use of org.springframework.web.servlet.i18n.FixedLocaleResolver in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithJstl.

@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.addMessage("code1", locale, "messageX");
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    HttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map model = new HashMap();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, request, response);
    assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
    assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
Also used : Locale(java.util.Locale) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) TestBean(org.springframework.tests.sample.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 FixedLocaleResolver (org.springframework.web.servlet.i18n.FixedLocaleResolver)5 Locale (java.util.Locale)3 MethodParameter (org.springframework.core.MethodParameter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 LocalizationContext (javax.servlet.jsp.jstl.fmt.LocalizationContext)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 TestBean (org.springframework.tests.sample.beans.TestBean)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 View (org.springframework.web.servlet.View)2