|
|
|
@ -36,123 +36,109 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -36,123 +36,109 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unit tests for {@link CookieLocaleResolver}. |
|
|
|
|
* |
|
|
|
|
* @author Alef Arendsen |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
* @author Rick Evans |
|
|
|
|
* @author Sam Brannen |
|
|
|
|
*/ |
|
|
|
|
public class CookieLocaleResolverTests { |
|
|
|
|
class CookieLocaleResolverTests { |
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
|
|
private MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
private CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocale() { |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "nl"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
Locale loc = resolver.resolveLocale(request); |
|
|
|
|
assertThat(loc.getLanguage()).isEqualTo("nl"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContext() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContext() { |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "nl"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl"); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithTimeZone() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithTimeZone() { |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "nl GMT+1"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl"); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithInvalidLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithInvalidLocale() { |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
assertThatIllegalStateException().isThrownBy(() -> |
|
|
|
|
resolver.resolveLocaleContext(request)) |
|
|
|
|
assertThatIllegalStateException().isThrownBy(() -> resolver.resolveLocaleContext(request)) |
|
|
|
|
.withMessageContaining("LanguageKoekje") |
|
|
|
|
.withMessageContaining("++ GMT+1"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithInvalidLocaleOnErrorDispatch() { |
|
|
|
|
request.addPreferredLocale(Locale.GERMAN); |
|
|
|
|
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException()); |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2")); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale()).isEqualTo(Locale.GERMAN); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+2")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithInvalidTimeZone() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithInvalidTimeZone() { |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "nl X-MT"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
assertThatIllegalStateException().isThrownBy(() -> |
|
|
|
|
resolver.resolveLocaleContext(request)) |
|
|
|
|
assertThatIllegalStateException().isThrownBy(() -> resolver.resolveLocaleContext(request)) |
|
|
|
|
.withMessageContaining("LanguageKoekje") |
|
|
|
|
.withMessageContaining("nl X-MT"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithInvalidTimeZoneOnErrorDispatch() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithInvalidTimeZoneOnErrorDispatch() { |
|
|
|
|
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException()); |
|
|
|
|
Cookie cookie = new Cookie("LanguageKoekje", "nl X-MT"); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2")); |
|
|
|
|
resolver.setCookieName("LanguageKoekje"); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl"); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+2")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocale() { |
|
|
|
|
resolver.setLocale(request, response, new Locale("nl", "")); |
|
|
|
|
|
|
|
|
|
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
@ -171,11 +157,7 @@ public class CookieLocaleResolverTests {
@@ -171,11 +157,7 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocaleContext() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocaleContext() { |
|
|
|
|
resolver.setLocaleContext(request, response, new SimpleLocaleContext(new Locale("nl", ""))); |
|
|
|
|
|
|
|
|
|
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
@ -185,17 +167,12 @@ public class CookieLocaleResolverTests {
@@ -185,17 +167,12 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
resolver = new CookieLocaleResolver(); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl"); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocaleContextWithTimeZone() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocaleContextWithTimeZone() { |
|
|
|
|
resolver.setLocaleContext(request, response, |
|
|
|
|
new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1"))); |
|
|
|
|
|
|
|
|
@ -206,17 +183,12 @@ public class CookieLocaleResolverTests {
@@ -206,17 +183,12 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
resolver = new CookieLocaleResolver(); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl"); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocaleContextWithTimeZoneOnly() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocaleContextWithTimeZoneOnly() { |
|
|
|
|
resolver.setLocaleContext(request, response, |
|
|
|
|
new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+1"))); |
|
|
|
|
|
|
|
|
@ -228,17 +200,12 @@ public class CookieLocaleResolverTests {
@@ -228,17 +200,12 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
resolver = new CookieLocaleResolver(); |
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale()).isEqualTo(Locale.GERMANY); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocaleWithCountry() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocaleWithCountry() { |
|
|
|
|
resolver.setLocale(request, response, new Locale("de", "AT")); |
|
|
|
|
|
|
|
|
|
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
@ -259,11 +226,7 @@ public class CookieLocaleResolverTests {
@@ -259,11 +226,7 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetAndResolveLocaleWithCountryAsLegacyJava() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void setAndResolveLocaleWithCountryAsLegacyJava() { |
|
|
|
|
resolver.setLanguageTagCompliant(false); |
|
|
|
|
resolver.setLocale(request, response, new Locale("de", "AT")); |
|
|
|
|
|
|
|
|
@ -285,11 +248,7 @@ public class CookieLocaleResolverTests {
@@ -285,11 +248,7 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testCustomCookie() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
void customCookie() { |
|
|
|
|
resolver.setCookieName("LanguageKoek"); |
|
|
|
|
resolver.setCookieDomain(".springframework.org"); |
|
|
|
|
resolver.setCookiePath("/mypath"); |
|
|
|
@ -315,36 +274,27 @@ public class CookieLocaleResolverTests {
@@ -315,36 +274,27 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleWithoutCookie() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleWithoutCookie() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
|
|
|
|
|
Locale loc = resolver.resolveLocale(request); |
|
|
|
|
assertThat(loc).isEqualTo(request.getLocale()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithoutCookie() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithoutCookie() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
|
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale()).isEqualTo(request.getLocale()); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleWithoutCookieAndDefaultLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleWithoutCookieAndDefaultLocale() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultLocale(Locale.GERMAN); |
|
|
|
|
|
|
|
|
|
Locale loc = resolver.resolveLocale(request); |
|
|
|
@ -352,79 +302,63 @@ public class CookieLocaleResolverTests {
@@ -352,79 +302,63 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithoutCookieAndDefaultLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithoutCookieAndDefaultLocale() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultLocale(Locale.GERMAN); |
|
|
|
|
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
|
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale()).isEqualTo(Locale.GERMAN); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleWithCookieWithoutLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleWithCookieWithoutLocale() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, ""); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
|
|
|
|
|
Locale loc = resolver.resolveLocale(request); |
|
|
|
|
assertThat(loc).isEqualTo(request.getLocale()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testResolveLocaleContextWithCookieWithoutLocale() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void resolveLocaleContextWithCookieWithoutLocale() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, ""); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
|
|
|
|
|
LocaleContext loc = resolver.resolveLocaleContext(request); |
|
|
|
|
assertThat(loc.getLocale()).isEqualTo(request.getLocale()); |
|
|
|
|
boolean condition = loc instanceof TimeZoneAwareLocaleContext; |
|
|
|
|
assertThat(condition).isTrue(); |
|
|
|
|
assertThat(loc).isInstanceOf(TimeZoneAwareLocaleContext.class); |
|
|
|
|
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetLocaleToNull() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void setLocaleToNull() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString()); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setLocale(request, response, null); |
|
|
|
|
Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME); |
|
|
|
|
assertThat(locale).isEqualTo(Locale.TAIWAN); |
|
|
|
|
|
|
|
|
|
Cookie[] cookies = response.getCookies(); |
|
|
|
|
assertThat(cookies.length).isEqualTo(1); |
|
|
|
|
assertThat(cookies).hasSize(1); |
|
|
|
|
Cookie localeCookie = cookies[0]; |
|
|
|
|
assertThat(localeCookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
|
assertThat(localeCookie.getValue()).isEqualTo(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetLocaleContextToNull() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void setLocaleContextToNull() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString()); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setLocaleContext(request, response, null); |
|
|
|
|
Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME); |
|
|
|
|
assertThat(locale).isEqualTo(Locale.TAIWAN); |
|
|
|
@ -432,42 +366,36 @@ public class CookieLocaleResolverTests {
@@ -432,42 +366,36 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
assertThat(timeZone).isNull(); |
|
|
|
|
|
|
|
|
|
Cookie[] cookies = response.getCookies(); |
|
|
|
|
assertThat(cookies.length).isEqualTo(1); |
|
|
|
|
assertThat(cookies).hasSize(1); |
|
|
|
|
Cookie localeCookie = cookies[0]; |
|
|
|
|
assertThat(localeCookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
|
assertThat(localeCookie.getValue()).isEqualTo(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetLocaleToNullWithDefault() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void setLocaleToNullWithDefault() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString()); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultLocale(Locale.CANADA_FRENCH); |
|
|
|
|
resolver.setLocale(request, response, null); |
|
|
|
|
Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME); |
|
|
|
|
assertThat(locale).isEqualTo(Locale.CANADA_FRENCH); |
|
|
|
|
|
|
|
|
|
Cookie[] cookies = response.getCookies(); |
|
|
|
|
assertThat(cookies.length).isEqualTo(1); |
|
|
|
|
assertThat(cookies).hasSize(1); |
|
|
|
|
Cookie localeCookie = cookies[0]; |
|
|
|
|
assertThat(localeCookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
|
assertThat(localeCookie.getValue()).isEqualTo(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetLocaleContextToNullWithDefault() { |
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
void setLocaleContextToNullWithDefault() { |
|
|
|
|
request.addPreferredLocale(Locale.TAIWAN); |
|
|
|
|
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString()); |
|
|
|
|
request.setCookies(cookie); |
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse(); |
|
|
|
|
|
|
|
|
|
CookieLocaleResolver resolver = new CookieLocaleResolver(); |
|
|
|
|
resolver.setDefaultLocale(Locale.CANADA_FRENCH); |
|
|
|
|
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
resolver.setLocaleContext(request, response, null); |
|
|
|
@ -477,7 +405,7 @@ public class CookieLocaleResolverTests {
@@ -477,7 +405,7 @@ public class CookieLocaleResolverTests {
|
|
|
|
|
assertThat(timeZone).isEqualTo(TimeZone.getTimeZone("GMT+1")); |
|
|
|
|
|
|
|
|
|
Cookie[] cookies = response.getCookies(); |
|
|
|
|
assertThat(cookies.length).isEqualTo(1); |
|
|
|
|
assertThat(cookies).hasSize(1); |
|
|
|
|
Cookie localeCookie = cookies[0]; |
|
|
|
|
assertThat(localeCookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME); |
|
|
|
|
assertThat(localeCookie.getValue()).isEqualTo(""); |
|
|
|
|