Search in sources :

Example 6 with TimeZoneAwareLocaleContext

use of org.springframework.context.i18n.TimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testResolveLocaleContextWithCookieWithoutLocale.

@Test
public void testResolveLocaleContextWithCookieWithoutLocale() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
    request.setCookies(cookie);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals(request.getLocale(), loc.getLocale());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : Cookie(javax.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 7 with TimeZoneAwareLocaleContext

use of org.springframework.context.i18n.TimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testResolveLocaleContextWithoutCookie.

@Test
public void testResolveLocaleContextWithoutCookie() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals(request.getLocale(), loc.getLocale());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 8 with TimeZoneAwareLocaleContext

use of org.springframework.context.i18n.TimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZone.

@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1")));
    Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    request = new MockHttpServletRequest();
    request.setCookies(cookie);
    resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals("nl", loc.getLocale().getLanguage());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : Locale(java.util.Locale) Cookie(javax.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 9 with TimeZoneAwareLocaleContext

use of org.springframework.context.i18n.TimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class SessionLocaleResolver method setLocaleContext.

@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
    Locale locale = null;
    TimeZone timeZone = null;
    if (localeContext != null) {
        locale = localeContext.getLocale();
        if (localeContext instanceof TimeZoneAwareLocaleContext) {
            timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
        }
    }
    WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
    WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext)

Example 10 with TimeZoneAwareLocaleContext

use of org.springframework.context.i18n.TimeZoneAwareLocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testResolveLocaleContextWithInvalidLocaleOnErrorDispatch.

@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.GERMAN);
    request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
    Cookie cookie = new Cookie("LanguageKoekje", "n-x GMT+1");
    request.setCookies(cookie);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
    resolver.setCookieName("LanguageKoekje");
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals(Locale.GERMAN, loc.getLocale());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : ServletException(javax.servlet.ServletException) Cookie(javax.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)14 LocaleContext (org.springframework.context.i18n.LocaleContext)12 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)12 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)11 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)11 Test (org.junit.Test)10 Cookie (javax.servlet.http.Cookie)8 Locale (java.util.Locale)5 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 TimeZone (java.util.TimeZone)2 ServletException (javax.servlet.ServletException)2 LocaleContextResolver (org.springframework.web.servlet.LocaleContextResolver)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 LocaleResolver (org.springframework.web.servlet.LocaleResolver)1 UrlPathHelper (org.springframework.web.util.UrlPathHelper)1