Browse Source

Polish LocaleResolver tests

pull/28720/head
Sam Brannen 2 years ago
parent
commit
8547f8601d
  1. 20
      spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolverTests.java
  2. 174
      spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/CookieLocaleResolverTests.java
  3. 14
      spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/LocaleResolverTests.java
  4. 38
      spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/SessionLocaleResolverTests.java

20
spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/AcceptHeaderLocaleResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,49 +43,49 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -43,49 +43,49 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Rossen Stoyanchev
* @author Juergen Hoeller
*/
public class AcceptHeaderLocaleResolverTests {
class AcceptHeaderLocaleResolverTests {
private final AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
@Test
public void resolve() {
void resolve() {
assertThat(this.resolver.resolveLocale(request(CANADA))).isEqualTo(CANADA);
assertThat(this.resolver.resolveLocale(request(US, CANADA))).isEqualTo(US);
}
@Test
public void resolvePreferredSupported() {
void resolvePreferredSupported() {
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
assertThat(this.resolver.resolveLocale(request(US, CANADA))).isEqualTo(CANADA);
}
@Test
public void resolvePreferredNotSupported() {
void resolvePreferredNotSupported() {
this.resolver.setSupportedLocales(Collections.singletonList(CANADA));
assertThat(this.resolver.resolveLocale(request(US, UK))).isEqualTo(US);
}
@Test
public void resolvePreferredAgainstLanguageOnly() {
void resolvePreferredAgainstLanguageOnly() {
this.resolver.setSupportedLocales(Collections.singletonList(ENGLISH));
assertThat(this.resolver.resolveLocale(request(GERMANY, US, UK))).isEqualTo(ENGLISH);
}
@Test
public void resolvePreferredAgainstCountryIfPossible() {
void resolvePreferredAgainstCountryIfPossible() {
this.resolver.setSupportedLocales(Arrays.asList(ENGLISH, UK));
assertThat(this.resolver.resolveLocale(request(GERMANY, US, UK))).isEqualTo(UK);
}
@Test
public void resolvePreferredAgainstLanguageWithMultipleSupportedLocales() {
void resolvePreferredAgainstLanguageWithMultipleSupportedLocales() {
this.resolver.setSupportedLocales(Arrays.asList(GERMAN, US));
assertThat(this.resolver.resolveLocale(request(GERMANY, US, UK))).isEqualTo(GERMAN);
}
@Test
public void resolvePreferredNotSupportedWithDefault() {
void resolvePreferredNotSupportedWithDefault() {
this.resolver.setSupportedLocales(Arrays.asList(US, JAPAN));
this.resolver.setDefaultLocale(Locale.JAPAN);
@ -96,7 +96,7 @@ public class AcceptHeaderLocaleResolverTests { @@ -96,7 +96,7 @@ public class AcceptHeaderLocaleResolverTests {
}
@Test
public void defaultLocale() {
void defaultLocale() {
this.resolver.setDefaultLocale(JAPANESE);
MockHttpServletRequest request = new MockHttpServletRequest();
assertThat(this.resolver.resolveLocale(request)).isEqualTo(JAPANESE);

174
spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/CookieLocaleResolverTests.java

@ -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("");

14
spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/LocaleResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -35,28 +35,30 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -35,28 +35,30 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* Unit tests for various {@link LocaleResolver} implementations.
*
* @author Juergen Hoeller
* @since 20.03.2003
*/
public class LocaleResolverTests {
class LocaleResolverTests {
@Test
public void testAcceptHeaderLocaleResolver() {
void acceptHeaderLocaleResolver() {
doTest(new AcceptHeaderLocaleResolver(), false);
}
@Test
public void testFixedLocaleResolver() {
void fixedLocaleResolver() {
doTest(new FixedLocaleResolver(Locale.UK), false);
}
@Test
public void testCookieLocaleResolver() {
void cookieLocaleResolver() {
doTest(new CookieLocaleResolver(), true);
}
@Test
public void testSessionLocaleResolver() {
void sessionLocaleResolver() {
doTest(new SessionLocaleResolver(), true);
}

38
spring-webmvc/src/test/java/org/springframework/web/servlet/i18n/SessionLocaleResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,25 +28,29 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse; @@ -28,25 +28,29 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link SessionLocaleResolver}.
*
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class SessionLocaleResolverTests {
class SessionLocaleResolverTests {
private MockHttpServletRequest request = new MockHttpServletRequest();
private MockHttpServletResponse response = new MockHttpServletResponse();
private SessionLocaleResolver resolver = new SessionLocaleResolver();
@Test
public void testResolveLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
void resolveLocale() {
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
SessionLocaleResolver resolver = new SessionLocaleResolver();
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
}
@Test
public void testSetAndResolveLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
SessionLocaleResolver resolver = new SessionLocaleResolver();
void setAndResolveLocale() {
resolver.setLocale(request, response, Locale.GERMAN);
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
@ -59,34 +63,26 @@ public class SessionLocaleResolverTests { @@ -59,34 +63,26 @@ public class SessionLocaleResolverTests {
}
@Test
public void testResolveLocaleWithoutSession() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
void resolveLocaleWithoutSession() throws Exception {
request.addPreferredLocale(Locale.TAIWAN);
SessionLocaleResolver resolver = new SessionLocaleResolver();
assertThat(resolver.resolveLocale(request)).isEqualTo(request.getLocale());
}
@Test
public void testResolveLocaleWithoutSessionAndDefaultLocale() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
void resolveLocaleWithoutSessionAndDefaultLocale() throws Exception {
request.addPreferredLocale(Locale.TAIWAN);
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.GERMAN);
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
}
@Test
public void testSetLocaleToNullLocale() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
void setLocaleToNullLocale() throws Exception {
request.addPreferredLocale(Locale.TAIWAN);
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
MockHttpServletResponse response = new MockHttpServletResponse();
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setLocale(request, response, null);
Locale locale = (Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
assertThat(locale).isNull();

Loading…
Cancel
Save