commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r291219 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/LocaleUtils.java test/org/apache/commons/lang/LangTestSuite.java test/org/apache/commons/lang/LocaleUtilsTest.java
Date Fri, 23 Sep 2005 23:17:38 GMT
Author: scolebourne
Date: Fri Sep 23 16:17:31 2005
New Revision: 291219

URL: http://svn.apache.org/viewcvs?rev=291219&view=rev
Log:
Test LocaleUtils

Added:
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
  (with props)
Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java?rev=291219&r1=291218&r2=291219&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/LocaleUtils.java Fri
Sep 23 16:17:31 2005
@@ -16,9 +16,13 @@
 package org.apache.commons.lang;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -34,6 +38,19 @@
  */
 public class LocaleUtils {
 
+    /** Unmodifiable list of available locales. */
+    private static final List cAvailableLocaleList;
+    /** Unmodifiable set of available locales. */
+    private static Set cAvailableLocaleSet;
+    /** Unmodifiable map of language locales by country. */
+    private static final Map cLanguagesByCountry = Collections.synchronizedMap(new HashMap());
+    /** Unmodifiable map of country locales by language. */
+    private static final Map cCountriesByLanguage = Collections.synchronizedMap(new HashMap());
+    static {
+        List list = Arrays.asList(Locale.getAvailableLocales());
+        cAvailableLocaleList = Collections.unmodifiableList(list);
+    }
+
     /**
      * <p><code>LocaleUtils</code> instances should NOT be constructed
in standard programming.
      * Instead, the class should be used as <code>LocaleUtils.toLocale("en_GB");</code>.</p>
@@ -57,34 +74,48 @@
      *   LocaleUtils.toLocale("en_GB")  = new Locale("en", "GB")
      *   LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")
      * </pre>
+     * 
+     * <p>This method validates the input strictly.
+     * The language code must be lowercase.
+     * The country code must be uppercase.
+     * The separator must be an underscore.
+     * The length must be correct.
+     * </p>
      *
      * @param str  the locale String to convert, null returns null
-     * @return a Locale
+     * @return a Locale, null if null input
      * @throws IllegalArgumentException if the string is an invalid format
      */
     public static Locale toLocale(String str) {
         if (str == null) {
             return null;
         }
-        if (str.length() != 2 &&
-            str.length() != 5 &&
-            str.length() < 7) {
+        int len = str.length();
+        if (len != 2 && len != 5 && len < 7) {
             throw new IllegalArgumentException("Invalid locale format: " + str);
         }
-        if (Character.isLowerCase(str.charAt(0)) == false ||
-            Character.isLowerCase(str.charAt(1)) == false) {
+        char ch0 = str.charAt(0);
+        char ch1 = str.charAt(1);
+        if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z') {
             throw new IllegalArgumentException("Invalid locale format: " + str);
         }
-        if (str.length() == 2) {
+        if (len == 2) {
             return new Locale(str, "");
         } else {
-            if (Character.isUpperCase(str.charAt(3)) == false ||
-                Character.isUpperCase(str.charAt(4)) == false) {
+            if (str.charAt(2) != '_') {
+                throw new IllegalArgumentException("Invalid locale format: " + str);
+            }
+            char ch3 = str.charAt(3);
+            char ch4 = str.charAt(4);
+            if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z') {
                 throw new IllegalArgumentException("Invalid locale format: " + str);
             }
-            if (str.length() == 5) {
+            if (len == 5) {
                 return new Locale(str.substring(0, 2), str.substring(3, 5));
             } else {
+                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));
             }
         }
@@ -101,7 +132,7 @@
      * </pre>
      *
      * @param locale  the locale to start from
-     * @return the list of Locale objects, 0 being locale
+     * @return the unmodifiable list of Locale objects, 0 being locale, never null
      */
     public static List localeLookupList(Locale locale) {
         return localeLookupList(locale, locale);
@@ -117,12 +148,13 @@
      *   = [Locale("fr","CA","xxx"), Locale("fr","CA"), Locale("fr"), Locale("en"]
      * </pre>
      *
-     * <p>This method takes a country code and searches to find the
-     * languages available for that country. Variant locales are removed.</p>
+     * <p>The result list begins with the most specific locale, then the
+     * next more general and so on, finishing with the default locale.
+     * The list will never contain the same locale twice.</p>
      *
      * @param locale  the locale to start from, null returns empty list
      * @param defaultLocale  the default locale to use if no other is found
-     * @return the list of Locale objects, 0 being locale
+     * @return the unmodifiable list of Locale objects, 0 being locale, never null
      */
     public static List localeLookupList(Locale locale, Locale defaultLocale) {
         List list = new ArrayList(4);
@@ -138,55 +170,116 @@
                 list.add(defaultLocale);
             }
         }
-        return list;
+        return Collections.unmodifiableList(list);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Obtains an unmodifiable list of installed locales.</p>
+     * 
+     * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}.
+     * It is more efficient, as the JDK method must create a new array each
+     * time it is called.</p>
+     *
+     * @return the unmodifiable list of available locales
+     */
+    public static List availableLocaleList() {
+        return cAvailableLocaleList;
     }
 
     //-----------------------------------------------------------------------
     /**
-     * <p>Obtains the set of languages supported for a given country.</p>
-     
+     * <p>Obtains an unmodifiable set of installed locales.</p>
+     * 
+     * <p>This method is a wrapper around {@link Locale#getAvailableLocales()}.
+     * It is more efficient, as the JDK method must create a new array each
+     * time it is called.</p>
+     *
+     * @return the unmodifiable set of available locales
+     */
+    public static Set availableLocaleSet() {
+        Set set = cAvailableLocaleSet;
+        if (set == null) {
+            set = new HashSet(availableLocaleList());
+            set = Collections.unmodifiableSet(set);
+            cAvailableLocaleSet = set;
+        }
+        return set;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Checks if the locale specified is in the list of available locales.</p>
+     *
+     * @return true if the locale is a known locale
+     */
+    public static boolean isAvailableLocale(Locale locale) {
+        return cAvailableLocaleSet.contains(locale);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Obtains the list of languages supported for a given country.</p>
+     *
      * <p>This method takes a country code and searches to find the
      * languages available for that country. Variant locales are removed.</p>
      *
      * @param countryCode  the 2 letter country code, null returns empty
-     * @return a Set of Locale objects
+     * @return an unmodifiable List of Locale objects, never null
      */
-    public static Set languagesByCountry(String countryCode) {
-        Set set = new HashSet();
-        Locale[] array = Locale.getAvailableLocales();
-        if (countryCode != null) {
-            for (int i = 0; i < array.length; i++) {
-                if (countryCode.equals(array[i].getCountry()) &&
-                        array[i].getVariant().length() == 0) {
-                    set.add(array[i]);
+    public static List languagesByCountry(String countryCode) {
+        List langs = (List) cLanguagesByCountry.get(countryCode);
+        if (langs == null) {
+            if (countryCode != null) {
+                langs = new ArrayList();
+                List locales = availableLocaleList();
+                for (int i = 0; i < locales.size(); i++) {
+                    Locale locale = (Locale) locales.get(i);
+                    if (countryCode.equals(locale.getCountry()) &&
+                            locale.getVariant().length() == 0) {
+                        langs.add(locale);
+                    }
                 }
+                langs = Collections.unmodifiableList(langs);
+            } else {
+                langs = Collections.EMPTY_LIST;
             }
+            cLanguagesByCountry.put(countryCode, langs);
         }
-        return set;
+        return langs;
     }
 
     //-----------------------------------------------------------------------
     /**
-     * <p>Obtains the set of countries supported for a given language.</p>
+     * <p>Obtains the list of countries supported for a given language.</p>
      * 
      * <p>This method takes a language code and searches to find the
      * countries available for that language. Variant locales are removed.</p>
      *
      * @param languageCode  the 2 letter language code, null returns empty
-     * @return a Set of Locale objects
+     * @return an unmodifiable List of Locale objects, never null
      */
-    public static Set countriesByLanguage(String languageCode) {
-        Set set = new HashSet();
-        Locale[] array = Locale.getAvailableLocales();
-        if (languageCode != null) {
-            for (int i = 0; i < array.length; i++) {
-                if (languageCode.equals(array[i].getLanguage()) &&
-                        array[i].getVariant().length() == 0) {
-                    set.add(array[i]);
+    public static List countriesByLanguage(String languageCode) {
+        List countries = (List) cCountriesByLanguage.get(languageCode);
+        if (countries == null) {
+            if (languageCode != null) {
+                countries = new ArrayList();
+                List locales = availableLocaleList();
+                for (int i = 0; i < locales.size(); i++) {
+                    Locale locale = (Locale) locales.get(i);
+                    if (languageCode.equals(locale.getLanguage()) &&
+                            locale.getCountry().length() != 0 &&
+                            locale.getVariant().length() == 0) {
+                        countries.add(locale);
+                    }
                 }
+                countries = Collections.unmodifiableList(countries);
+            } else {
+                countries = Collections.EMPTY_LIST;
             }
+            cCountriesByLanguage.put(languageCode, countries);
         }
-        return set;
+        return countries;
     }
 
 }

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java?rev=291219&r1=291218&r2=291219&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LangTestSuite.java
Fri Sep 23 16:17:31 2005
@@ -65,6 +65,7 @@
         suite.addTest(IllegalClassExceptionTest.suite());
         suite.addTest(IncompleteArgumentExceptionTest.suite());
         suite.addTest(IntHashMapTest.suite());
+        suite.addTest(LocaleUtilsTest.suite());
         suite.addTest(NotImplementedExceptionTest.suite());
         suite.addTest(NullArgumentExceptionTest.suite());
         suite.addTest(NumberRangeTest.suite());

Added: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java?rev=291219&view=auto
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
(added)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
Fri Sep 23 16:17:31 2005
@@ -0,0 +1,475 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Unit tests for {@link LocaleUtils}.
+ *
+ * @author Chris Hyzer
+ * @author Stephen Colebourne
+ * @version $Id$
+ */
+public class LocaleUtilsTest extends TestCase {
+
+    private static final Locale LOCALE_EN = new Locale("en", "");
+    private static final Locale LOCALE_EN_US = new Locale("en", "US");
+    private static final Locale LOCALE_EN_US_ZZZZ = new Locale("en", "US", "ZZZZ");
+    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");
+
+    /**
+     * Constructor.
+     * 
+     * @param name
+     */
+    public LocaleUtilsTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Main.
+     * @param args
+     */
+    public static void main(String[] args) {
+        TestRunner.run(suite());
+    }
+
+    /**
+     * Run the test cases as a suite.
+     * @return the Test
+     */
+    public static Test suite() {
+        TestSuite suite = new TestSuite(LocaleUtilsTest.class);
+        suite.setName("LocaleUtilsTest Tests");
+        return suite;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Test that constructors are public, and work, etc.
+     */
+    public void testConstructor() {
+        assertNotNull(new LocaleUtils());
+        Constructor[] cons = LocaleUtils.class.getDeclaredConstructors();
+        assertEquals(1, cons.length);
+        assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
+        assertEquals(true, Modifier.isPublic(LocaleUtils.class.getModifiers()));
+        assertEquals(false, Modifier.isFinal(LocaleUtils.class.getModifiers()));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Pass in a valid language, test toLocale.
+     *
+     * @param language  the language string
+     */
+    private void assertValidToLocale(String language) {
+        Locale locale = LocaleUtils.toLocale(language);
+        assertNotNull("valid locale", locale);
+        assertEquals(language, locale.getLanguage());
+        //country and variant are empty
+        assertTrue(locale.getCountry() == null || locale.getCountry().length() == 0);
+        assertTrue(locale.getVariant() == null || locale.getVariant().length() == 0);
+    }
+
+    /**
+     * Pass in a valid language, test toLocale.
+     *
+     * @param localeString to pass to toLocale()
+     * @param language of the resulting Locale
+     * @param country of the resulting Locale
+     */
+    private void assertValidToLocale(String localeString, String language, String country)
{
+        Locale locale = LocaleUtils.toLocale(localeString);
+        assertNotNull("valid locale", locale);
+        assertEquals(language, locale.getLanguage());
+        assertEquals(country, locale.getCountry());
+        //variant is empty
+        assertTrue(locale.getVariant() == null || locale.getVariant().length() == 0);
+    }
+
+    /**
+     * Pass in a valid language, test toLocale.
+     *
+     * @param localeString to pass to toLocale()
+     * @param language of the resulting Locale
+     * @param country of the resulting Locale
+     * @param variant of the resulting Locale
+     */
+    private void assertValidToLocale(
+            String localeString, String language, 
+            String country, String variant) {
+        Locale locale = LocaleUtils.toLocale(localeString);
+        assertNotNull("valid locale", locale);
+        assertEquals(language, locale.getLanguage());
+        assertEquals(country, locale.getCountry());
+        assertEquals(variant, locale.getVariant());
+        
+    }
+
+    /**
+     * Test toLocale() method.
+     */
+    public void testToLocale_1Part() {
+        assertEquals(null, LocaleUtils.toLocale((String) null));
+        
+        assertValidToLocale("us");
+        assertValidToLocale("fr");
+        assertValidToLocale("de");
+        assertValidToLocale("zh");
+        // Valid format but lang doesnt exist, should make instance anyway
+        assertValidToLocale("qq");
+        
+        try {
+            LocaleUtils.toLocale("Us");
+            fail("Should fail if not lowercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("US");
+            fail("Should fail if not lowercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("uS");
+            fail("Should fail if not lowercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("u#");
+            fail("Should fail if not lowercase");
+        } catch (IllegalArgumentException iae) {}
+        
+        try {
+            LocaleUtils.toLocale("u");
+            fail("Must be 2 chars if less than 5");
+        } catch (IllegalArgumentException iae) {}
+       
+        try {
+            LocaleUtils.toLocale("uuu");
+            fail("Must be 2 chars if less than 5");
+        } catch (IllegalArgumentException iae) {}
+
+        try {
+            LocaleUtils.toLocale("uu_U");
+            fail("Must be 2 chars if less than 5");
+        } catch (IllegalArgumentException iae) {}
+    }        
+
+    /**
+     * Test toLocale() method.
+     */
+    public void testToLocale_2Part() {
+        assertValidToLocale("us_EN", "us", "EN");
+        //valid though doesnt exist
+        assertValidToLocale("us_ZH", "us", "ZH");
+        
+        try {
+            LocaleUtils.toLocale("us-EN");
+            fail("Should fail as not underscore");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("us_En");
+            fail("Should fail second part not uppercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("us_en");
+            fail("Should fail second part not uppercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("us_eN");
+            fail("Should fail second part not uppercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("uS_EN");
+            fail("Should fail first part not lowercase");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("us_E3");
+            fail("Should fail second part not uppercase");
+        } catch (IllegalArgumentException iae) {}
+    }        
+
+    /**
+     * Test toLocale() method.
+     */
+    public void testToLocale_3Part() {
+        assertValidToLocale("us_EN_a", "us", "EN", "a");
+        assertValidToLocale("us_EN_A", "us", "EN", "A");
+        assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFsafdFDsdfF");
+        
+        try {
+            LocaleUtils.toLocale("us_EN-a");
+            fail("Should fail as not underscore");
+        } catch (IllegalArgumentException iae) {}
+        try {
+            LocaleUtils.toLocale("uu_UU_");
+            fail("Must be 3, 5 or 7+ in length");
+        } catch (IllegalArgumentException iae) {}
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Helper method for local lookups.
+     *
+     * @param locale  the input locale
+     * @param defaultLocale  the input default locale
+     * @param expected  expected results
+     */
+    private void assertLocaleLookupList(Locale locale, Locale defaultLocale, Locale[] expected)
{
+        List localeList = defaultLocale == null ?
+                LocaleUtils.localeLookupList(locale) :
+                LocaleUtils.localeLookupList(locale, defaultLocale);
+        
+        assertEquals(expected.length, localeList.size());
+        assertEquals(Arrays.asList(expected), localeList);
+        try {
+            localeList.add("Unmodifiable");
+            fail();
+        } catch (UnsupportedOperationException ex) {}
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Test localeLookupList() method.
+     */
+    public void testLocaleLookupList_Locale() {
+        assertLocaleLookupList(null, null, new Locale[0]);
+        assertLocaleLookupList(LOCALE_QQ, null, new Locale[]{LOCALE_QQ});
+        assertLocaleLookupList(LOCALE_EN, null, new Locale[]{LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN, null, new Locale[]{LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN_US, null,
+            new Locale[] {
+                LOCALE_EN_US,
+                LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null,
+            new Locale[] {
+                LOCALE_EN_US_ZZZZ,
+                LOCALE_EN_US,
+                new Locale("en")});
+    }        
+
+    /**
+     * Test localeLookupList() method.
+     */
+    public void testLocaleLookupList_LocaleLocale() {
+        assertLocaleLookupList(LOCALE_QQ, LOCALE_QQ, 
+                new Locale[]{LOCALE_QQ});
+        assertLocaleLookupList(LOCALE_EN, LOCALE_EN, 
+                new Locale[]{LOCALE_EN});
+        
+        assertLocaleLookupList(LOCALE_EN_US, LOCALE_EN_US, 
+            new Locale[]{
+                LOCALE_EN_US,
+                LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ,
+            new Locale[] {
+                LOCALE_EN_US,
+                LOCALE_EN,
+                LOCALE_QQ});
+        assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ_ZZ,
+            new Locale[] {
+                LOCALE_EN_US,
+                LOCALE_EN,
+                LOCALE_QQ_ZZ});
+        
+        assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null,
+            new Locale[] {
+                LOCALE_EN_US_ZZZZ,
+                LOCALE_EN_US,
+                LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_EN_US_ZZZZ,
+            new Locale[] {
+                LOCALE_EN_US_ZZZZ,
+                LOCALE_EN_US,
+                LOCALE_EN});
+        assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ,
+            new Locale[] {
+                LOCALE_EN_US_ZZZZ,
+                LOCALE_EN_US,
+                LOCALE_EN,
+                LOCALE_QQ});
+        assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ_ZZ,
+            new Locale[] {
+                LOCALE_EN_US_ZZZZ,
+                LOCALE_EN_US,
+                LOCALE_EN,
+                LOCALE_QQ_ZZ});
+        assertLocaleLookupList(LOCALE_FR_CA, LOCALE_EN,
+            new Locale[] {
+                LOCALE_FR_CA,
+                LOCALE_FR,
+                LOCALE_EN});
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Test availableLocaleList() method.
+     */
+    public void testAvailableLocaleList() {
+        List list = LocaleUtils.availableLocaleList();
+        List list2 = LocaleUtils.availableLocaleList();
+        assertNotNull(list);
+        assertSame(list, list2);
+        Locale[] jdkLocaleArray = Locale.getAvailableLocales();
+        List jdkLocaleList = Arrays.asList(jdkLocaleArray);
+        assertEquals(jdkLocaleList, list);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Test availableLocaleSet() method.
+     */
+    public void testAvailableLocaleSet() {
+        Set set = LocaleUtils.availableLocaleSet();
+        Set set2 = LocaleUtils.availableLocaleSet();
+        assertNotNull(set);
+        assertSame(set, set2);
+        Locale[] jdkLocaleArray = Locale.getAvailableLocales();
+        List jdkLocaleList = Arrays.asList(jdkLocaleArray);
+        Set jdkLocaleSet = new HashSet(jdkLocaleList);
+        assertEquals(jdkLocaleSet, set);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Test availableLocaleSet() method.
+     */
+    public void testIsAvailableLocale() {
+        Set set = LocaleUtils.availableLocaleSet();
+        assertEquals(set.contains(LOCALE_EN), LocaleUtils.isAvailableLocale(LOCALE_EN));
+        assertEquals(set.contains(LOCALE_EN_US), LocaleUtils.isAvailableLocale(LOCALE_EN_US));
+        assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils.isAvailableLocale(LOCALE_EN_US_ZZZZ));
+        assertEquals(set.contains(LOCALE_FR), LocaleUtils.isAvailableLocale(LOCALE_FR));
+        assertEquals(set.contains(LOCALE_FR_CA), LocaleUtils.isAvailableLocale(LOCALE_FR_CA));
+        assertEquals(set.contains(LOCALE_QQ), LocaleUtils.isAvailableLocale(LOCALE_QQ));
+        assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isAvailableLocale(LOCALE_QQ_ZZ));
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Make sure the language by country is correct.
+     *
+     * @param country
+     * @param languages array of languages that should be returned
+     */
+    private void assertLanguageByCountry(String country, String[] languages) {
+        List list = LocaleUtils.languagesByCountry(country);
+        List list2 = LocaleUtils.languagesByCountry(country);
+        assertNotNull(list);
+        assertSame(list, list2);
+        assertEquals(languages.length, list.size());
+        //search through langauges
+        for (int i = 0; i < languages.length; i++) {
+            Iterator iterator = list.iterator();
+            boolean found = false;
+            // see if it was returned by the set
+            while (iterator.hasNext()) {
+                Locale locale = (Locale) iterator.next();
+                // should have an en empty variant
+                assertTrue(locale.getVariant() == null
+                        || locale.getVariant().length() == 0);
+                assertEquals(country, locale.getCountry());
+                if (languages[i].equals(locale.getLanguage())) {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) {
+                fail("Cound not find language: " + languages[i]
+                        + " for country: " + country);
+            }
+        }
+        try {
+            list.add("Unmodifiable");
+            fail();
+        } catch (UnsupportedOperationException ex) {}
+    }
+
+    /**
+     * Test languagesByCountry() method.
+     */
+    public void testLanguagesByCountry() {
+        assertLanguageByCountry(null, new String[0]);
+        assertLanguageByCountry("US", new String[]{"en"});
+        assertLanguageByCountry("ZZ", new String[0]);
+        assertLanguageByCountry("CH", new String[]{"fr", "de", "it"});
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Make sure the language by country is correct.
+     *
+     * @param language
+     * @param countries array of countries that should be returned
+     */
+    private void assertCountriesByLanguage(String language, String[] countries) {
+        List list = LocaleUtils.countriesByLanguage(language);
+        List list2 = LocaleUtils.countriesByLanguage(language);
+        assertNotNull(list);
+        assertSame(list, list2);
+        assertEquals(countries.length, list.size());
+        //search through langauges
+        for (int i = 0; i < countries.length; i++) {
+            Iterator iterator = list.iterator();
+            boolean found = false;
+            // see if it was returned by the set
+            while (iterator.hasNext()) {
+                Locale locale = (Locale) iterator.next();
+                // should have an en empty variant
+                assertTrue(locale.getVariant() == null
+                        || locale.getVariant().length() == 0);
+                assertEquals(language, locale.getLanguage());
+                if (countries[i].equals(locale.getCountry())) {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) {
+                fail("Cound not find language: " + countries[i]
+                        + " for country: " + language);
+            }
+        }
+        try {
+            list.add("Unmodifiable");
+            fail();
+        } catch (UnsupportedOperationException ex) {}
+    }
+
+    /**
+     * Test languagesByCountry() method.
+     */
+    public void testCountriesByLanguage() {
+        assertCountriesByLanguage(null, new String[0]);
+        assertCountriesByLanguage("de", new String[]{"DE", "CH", "AT", "LU"});
+        assertCountriesByLanguage("zz", new String[0]);
+        assertCountriesByLanguage("it", new String[]{"IT", "CH"});
+    }
+
+}

Propchange: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/LocaleUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = "author date id revision"



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message