Support parsing locales without country, with variant

Prior to this commit, the parsing algorithm in
`StringUtils#parseLocaleString` would skip empty tokens, turning
`en__VARIANT` into `en_VARIANT` when parsed.

This commit ensures that such cases are now supported and that more
invalid formats are rejected by the parser.

Fixes gh-29248
This commit is contained in:
Brian Clozel
2022-10-03 22:44:09 +02:00
parent 4d5b0c91a3
commit 3b91eea0b7
2 changed files with 35 additions and 55 deletions

View File

@@ -624,12 +624,6 @@ class StringUtilsTests {
assertThat(locale).isEqualTo(expectedLocale);
}
@Test
void parseLocaleStringWithMalformedLocaleString() {
Locale locale = StringUtils.parseLocaleString("_banjo_on_my_knee");
assertThat(locale).as("When given a malformed Locale string, must not return null.").isNotNull();
}
@Test
void parseLocaleStringWithEmptyLocaleStringYieldsNullLocale() {
Locale locale = StringUtils.parseLocaleString("");
@@ -668,22 +662,6 @@ class StringUtilsTests {
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() {
String variant = "proper northern";
String localeString = "en GB " + variant; // lots of whitespace
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-3671
void parseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() {
String variant = "proper_northern";
String localeString = "en_GB_____" + variant; // lots of underscores
Locale locale = StringUtils.parseLocaleString(localeString);
assertThat(locale.getVariant()).as("Multi-valued variant portion of the Locale not extracted correctly.").isEqualTo(variant);
}
@Test // SPR-7779
void parseLocaleWithInvalidCharacters() {
assertThatIllegalArgumentException().isThrownBy(() ->
@@ -751,6 +729,11 @@ class StringUtilsTests {
assertThat(StringUtils.parseLocale("")).isNull();
}
@Test
void parseLocaleStringWithEmptyCountryAndVariant() {
assertThat(StringUtils.parseLocale("be__TARASK").toString()).isEqualTo("be__TARASK");
}
@Test
void split() {
assertThat(StringUtils.split("Hello, world", ",")).containsExactly("Hello", " world");