LANG-915: Wrong locale handling in LocaleUtils.toLocale(). Thanks to Sergio Fernández

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1557378 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2014-01-11 13:06:36 +00:00
parent e42dad3901
commit 8ea0c02848
3 changed files with 50 additions and 38 deletions

View File

@ -21,6 +21,10 @@
</properties>
<body>
<release version="3.2.2" date="TBA" description="Bug fix for 3.2.1">
<action issue="LANG-915" type="fix" dev="britter" due-to="Sergio Fernández">Wrong locale handling in LocaleUtils.toLocale()</action>
</release>
<release version="3.2.1" date="2014-01-05" description="Bug fix for 3.2">
<action issue="LANG-937" type="fix" dev="britter">Fix missing Hamcrest dependency in Ant Build</action>
<action issue="LANG-941" type="fix" dev="britter">Test failure in LocaleUtilsTest when building with JDK 8</action>

View File

@ -122,37 +122,37 @@ public class LocaleUtils {
}
return new Locale("", str.substring(1, 3), str.substring(4));
}
final char ch1 = str.charAt(1);
if (!Character.isLowerCase(ch0) || !Character.isLowerCase(ch1)) {
throw new IllegalArgumentException("Invalid locale format: " + str);
String[] split = str.split("_", -1);
int occurrences = split.length -1;
switch (occurrences) {
case 0:
if (StringUtils.isAllLowerCase(str) && (len == 2 || len == 3)) {
return new Locale(str);
} else {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
case 1:
if (StringUtils.isAllLowerCase(split[0]) &&
(split[0].length() == 2 || split[0].length() == 3) &&
split[1].length() == 2 && StringUtils.isAllUpperCase(split[1])) {
return new Locale(split[0], split[1]);
} else {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
case 2:
if (StringUtils.isAllLowerCase(split[0]) &&
(split[0].length() == 2 || split[0].length() == 3) &&
(split[1].length() == 0 || (split[1].length() == 2 && StringUtils.isAllUpperCase(split[1]))) &&
split[2].length() > 0) {
return new Locale(split[0], split[1], split[2]);
}
default:
throw new IllegalArgumentException("Invalid locale format: " + str);
}
if (len == 2) {
return new Locale(str);
}
if (len < 5) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
if (str.charAt(2) != '_') {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
final char ch3 = str.charAt(3);
if (ch3 == '_') {
return new Locale(str.substring(0, 2), "", str.substring(4));
}
final char ch4 = str.charAt(4);
if (!Character.isUpperCase(ch3) || !Character.isUpperCase(ch4)) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
if (len == 5) {
return new Locale(str.substring(0, 2), str.substring(3, 5));
}
if (len < 7) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
if (str.charAt(5) != '_') {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
return new Locale(str.substring(0, 2), str.substring(3, 5), str.substring(6));
}
//-----------------------------------------------------------------------

View File

@ -51,9 +51,7 @@ public class LocaleUtilsTest {
private static final Locale LOCALE_FR = new Locale("fr", "");
private static final Locale LOCALE_FR_CA = new Locale("fr", "CA");
private static final Locale LOCALE_QQ = new Locale("qq", "");
private static final Locale LOCALE_QQ_ZZ = new Locale("qq", "ZZ");
private static final Locale LOCALE_QQ_ZZ = new Locale("qq", "ZZ");
@Before
public void setUp() throws Exception {
@ -161,11 +159,6 @@ public class LocaleUtilsTest {
LocaleUtils.toLocale("u");
fail("Must be 2 chars if less than 5");
} catch (final IllegalArgumentException iae) {}
try {
LocaleUtils.toLocale("uuu");
fail("Must be 2 chars if less than 5");
} catch (final IllegalArgumentException iae) {}
try {
LocaleUtils.toLocale("uu_U");
@ -378,6 +371,21 @@ public class LocaleUtilsTest {
assertEquals(set.contains(LOCALE_QQ), LocaleUtils.isAvailableLocale(LOCALE_QQ));
assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isAvailableLocale(LOCALE_QQ_ZZ));
}
/**
* Test for 3-chars locale, further details at LANG-915
*
*/
@Test
public void testThreeCharsLocale() {
for (String str : Arrays.asList("udm", "tet")) {
Locale locale = LocaleUtils.toLocale(str);
assertNotNull(locale);
assertEquals(str, locale.getLanguage());
assertTrue(StringUtils.isBlank(locale.getCountry()));
assertEquals(new Locale(str), locale);
}
}
//-----------------------------------------------------------------------
/**