StringUtils.parseLocale consistently handles invalid locale values
Closes gh-22603
This commit is contained in:
@@ -773,7 +773,9 @@ public abstract class StringUtils {
|
||||
String[] tokens = tokenizeLocaleSource(localeValue);
|
||||
if (tokens.length == 1) {
|
||||
Locale resolved = Locale.forLanguageTag(localeValue);
|
||||
return (resolved.getLanguage().length() > 0 ? resolved : null);
|
||||
if (resolved.getLanguage().length() > 0) {
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
return parseLocaleTokens(localeValue, tokens);
|
||||
}
|
||||
@@ -820,7 +822,7 @@ public abstract class StringUtils {
|
||||
}
|
||||
}
|
||||
|
||||
if ("".equals(variant) && country.startsWith("#")) {
|
||||
if (variant.isEmpty() && country.startsWith("#")) {
|
||||
variant = country;
|
||||
country = "";
|
||||
}
|
||||
@@ -1193,7 +1195,7 @@ public abstract class StringUtils {
|
||||
}
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
if ("".equals(delimiter)) {
|
||||
if (delimiter.isEmpty()) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
result.add(deleteAny(str.substring(i, i + 1), charsToDelete));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -765,4 +765,20 @@ public class StringUtilsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidLocaleWithLocaleString() {
|
||||
assertEquals(new Locale("invalid"), StringUtils.parseLocaleString("invalid"));
|
||||
assertEquals(new Locale("invalidvalue"), StringUtils.parseLocaleString("invalidvalue"));
|
||||
assertEquals(new Locale("invalidvalue", "foo"), StringUtils.parseLocaleString("invalidvalue_foo"));
|
||||
assertNull(StringUtils.parseLocaleString(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidLocaleWithLanguageTag() {
|
||||
assertEquals(new Locale("invalid"), StringUtils.parseLocale("invalid"));
|
||||
assertEquals(new Locale("invalidvalue"), StringUtils.parseLocale("invalidvalue"));
|
||||
assertEquals(new Locale("invalidvalue", "foo"), StringUtils.parseLocale("invalidvalue_foo"));
|
||||
assertNull(StringUtils.parseLocale(""));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user