You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2014/01/11 14:06:37 UTC
svn commit: r1557378 - in /commons/proper/lang/trunk/src:
changes/changes.xml main/java/org/apache/commons/lang3/LocaleUtils.java
test/java/org/apache/commons/lang3/LocaleUtilsTest.java
Author: britter
Date: Sat Jan 11 13:06:36 2014
New Revision: 1557378
URL: http://svn.apache.org/r1557378
Log:
LANG-915: Wrong locale handling in LocaleUtils.toLocale(). Thanks to Sergio Fernández
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/LocaleUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1557378&r1=1557377&r2=1557378&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sat Jan 11 13:06:36 2014
@@ -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>
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/LocaleUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/LocaleUtils.java?rev=1557378&r1=1557377&r2=1557378&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/LocaleUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/LocaleUtils.java Sat Jan 11 13:06:36 2014
@@ -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);
- }
- 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);
+
+ 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);
}
- return new Locale(str.substring(0, 2), str.substring(3, 5), str.substring(6));
}
//-----------------------------------------------------------------------
Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java?rev=1557378&r1=1557377&r2=1557378&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java Sat Jan 11 13:06:36 2014
@@ -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);
+ }
+ }
//-----------------------------------------------------------------------
/**