1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.util;
19
20import java.util.Arrays;
21import java.util.HashSet;
22import java.util.List;
23import java.util.Locale;
24import java.util.Set;
25
26public class LocaleTest extends junit.framework.TestCase {
27
28    Locale testLocale;
29
30    Locale l;
31
32    Locale defaultLocale;
33
34    /**
35     * java.util.Locale#Locale(java.lang.String, java.lang.String)
36     */
37    public void test_ConstructorLjava_lang_String() {
38        // Test for method java.util.Locale(java.lang.String)
39        Locale x = new Locale("xx");
40        assertTrue("Failed to create Locale", x.getVariant().equals(""));
41
42        try {
43            new Locale(null);
44            fail("NullPointerException expected");
45        } catch (NullPointerException e) {
46            //expected
47        }
48    }
49
50    /**
51     * java.util.Locale#Locale(java.lang.String, java.lang.String)
52     */
53    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
54        // Test for method java.util.Locale(java.lang.String, java.lang.String)
55        Locale x = new Locale("xx", "CV");
56        assertTrue("Failed to create Locale", x.getCountry().equals("CV")
57                && x.getVariant().equals(""));
58
59        try {
60            new Locale("xx", null);
61            fail("NullPointerException expected");
62        } catch (NullPointerException e) {
63            //expected
64        }
65
66        try {
67            new Locale(null, "CV");
68            fail("NullPointerException expected");
69        } catch (NullPointerException e) {
70            //expected
71        }
72    }
73
74    /**
75     * java.util.Locale#Locale(java.lang.String, java.lang.String,
76     *        java.lang.String)
77     */
78    public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() {
79        // Test for method java.util.Locale(java.lang.String, java.lang.String,
80        // java.lang.String)
81        Locale x = new Locale("xx", "CV", "ZZ");
82        assertTrue("Failed to create Locale", x.getLanguage().equals("xx")
83                && (x.getCountry().equals("CV") && x.getVariant().equals("ZZ")));
84        try {
85            new Locale(null, "CV", "ZZ");
86            fail("expected NullPointerException with 1st parameter == null");
87        } catch (NullPointerException e) {
88        }
89
90        try {
91            new Locale("xx", null, "ZZ");
92            fail("expected NullPointerException with 2nd parameter == null");
93        } catch (NullPointerException e) {
94        }
95
96        try {
97            new Locale("xx", "CV", null);
98            fail("expected NullPointerException with 3rd parameter == null");
99        } catch (NullPointerException e) {
100        }
101    }
102
103    /**
104     * java.util.Locale#clone()
105     */
106    public void test_clone() {
107        // Test for method java.lang.Object java.util.Locale.clone()
108        assertTrue("Clone failed", l.clone().equals(l));
109    }
110
111    /**
112     * java.util.Locale#equals(java.lang.Object)
113     */
114    public void test_equalsLjava_lang_Object() {
115        // Test for method boolean java.util.Locale.equals(java.lang.Object)
116        Locale l2 = new Locale("en", "CA", "WIN32");
117        assertTrue("Same object returned false", testLocale.equals(testLocale));
118        assertTrue("Same values returned false", testLocale.equals(l2));
119        assertTrue("Different locales returned true", !testLocale.equals(l));
120
121    }
122
123    /**
124     * java.util.Locale#getAvailableLocales()
125     */
126    public void test_getAvailableLocales() {
127// BEGIN Android-changed
128        // Test for method java.util.Locale []
129        // java.util.Locale.getAvailableLocales()
130        // Assumes there will generally be about 10+ available locales...
131        // even in minimal configurations for android
132        try {
133            Locale[] locales = Locale.getAvailableLocales();
134            assertTrue("Wrong number of locales: " + locales.length, locales.length > 10);
135            // regression test for HARMONY-1514
136            // HashSet can filter duplicate locales
137            Set<Locale> localesSet = new HashSet<Locale>(Arrays.asList(locales));
138            assertEquals(localesSet.size(), locales.length);
139        } catch (Exception e) {
140            fail("Exception during test : " + e.getMessage());
141        }
142// END Android-changed
143    }
144
145    /**
146     * java.util.Locale#getCountry()
147     */
148    public void test_getCountry() {
149        // Test for method java.lang.String java.util.Locale.getCountry()
150        assertTrue("Returned incorrect country: " + testLocale.getCountry(),
151                testLocale.getCountry().equals("CA"));
152    }
153
154    /**
155     * java.util.Locale#getDefault()
156     */
157    public void test_getDefault() {
158        // Test for method java.util.Locale java.util.Locale.getDefault()
159        assertTrue("returns copy", Locale.getDefault() == Locale.getDefault());
160        Locale org = Locale.getDefault();
161        Locale.setDefault(l);
162        Locale x = Locale.getDefault();
163        Locale.setDefault(org);
164        assertEquals("Failed to get locale", "fr_CA_WIN32", x.toString());
165    }
166
167    /**
168     * java.util.Locale#getDisplayCountry()
169     */
170    public void test_getDisplayCountry() {
171        // Test for method java.lang.String java.util.Locale.getDisplayCountry()
172        assertTrue("Returned incorrect country: "
173                + testLocale.getDisplayCountry(), testLocale
174                .getDisplayCountry().equals("Canada"));
175    }
176
177    public void test_getDisplayCountryLjava_util_Locale() {
178        assertEquals("Italie", Locale.ITALY.getDisplayCountry(new Locale("fr", "CA", "WIN32")));
179    }
180
181    /**
182     * java.util.Locale#getDisplayLanguage()
183     */
184    public void test_getDisplayLanguage() {
185        // Test for method java.lang.String
186        // java.util.Locale.getDisplayLanguage()
187        assertTrue("Returned incorrect language: "
188                + testLocale.getDisplayLanguage(), testLocale
189                .getDisplayLanguage().equals("English"));
190
191        // Regression for Harmony-1146
192        Locale l_languageAE = new Locale("ae", "");
193        assertEquals("Avestan", l_languageAE.getDisplayLanguage());
194
195        // Regression for HARMONY-4402
196        Locale defaultLocale = Locale.getDefault();
197        try {
198            Locale locale = new Locale("no", "NO");
199            Locale.setDefault(locale);
200            assertEquals("norsk", locale.getDisplayLanguage());
201        } finally {
202            Locale.setDefault(defaultLocale);
203        }
204    }
205
206    public void test_getDisplayLanguageLjava_util_Locale() {
207        assertEquals("anglais", new Locale("en", "CA", "WIN32").getDisplayLanguage(l));
208    }
209
210    public void test_getDisplayName() {
211        assertEquals("English (Canada,WIN32)", new Locale("en", "CA", "WIN32").getDisplayName());
212    }
213
214    public void test_getDisplayNameLjava_util_Locale() {
215        assertEquals("anglais (Canada,WIN32)", new Locale("en", "CA", "WIN32").getDisplayName(l));
216    }
217
218    /**
219     * java.util.Locale#getDisplayVariant()
220     */
221    public void test_getDisplayVariant() {
222        // Test for method java.lang.String java.util.Locale.getDisplayVariant()
223        assertTrue("Returned incorrect variant: "
224                + testLocale.getDisplayVariant(), testLocale
225                .getDisplayVariant().equals("WIN32"));
226    }
227
228    /**
229     * java.util.Locale#getDisplayVariant(java.util.Locale)
230     */
231    public void test_getDisplayVariantLjava_util_Locale() {
232        // Test for method java.lang.String
233        // java.util.Locale.getDisplayVariant(java.util.Locale)
234        assertTrue("Returned incorrect variant: "
235                + testLocale.getDisplayVariant(l), testLocale
236                .getDisplayVariant(l).equals("WIN32"));
237    }
238
239    /**
240     * java.util.Locale#getISO3Country()
241     */
242    public void test_getISO3Country() {
243        // Test for method java.lang.String java.util.Locale.getISO3Country()
244        assertTrue("Returned incorrect ISO3 country: "
245                + testLocale.getISO3Country(), testLocale.getISO3Country()
246                .equals("CAN"));
247
248        Locale l = new Locale("", "CD");
249        assertEquals("COD", l.getISO3Country());
250    }
251
252    /**
253     * java.util.Locale#getISO3Language()
254     */
255    public void test_getISO3Language() {
256        // Test for method java.lang.String java.util.Locale.getISO3Language()
257        assertTrue("Returned incorrect ISO3 language: "
258                + testLocale.getISO3Language(), testLocale.getISO3Language()
259                .equals("eng"));
260
261        Locale l = new Locale("ae");
262        assertEquals("ave", l.getISO3Language());
263
264        // Regression for Harmony-1146
265        Locale l_CountryCS = new Locale("", "CS");
266        assertEquals("SCG", l_CountryCS.getISO3Country());
267
268        // Regression for Harmony-1129
269        l = new Locale("ak", "");
270        assertEquals("aka", l.getISO3Language());
271    }
272
273    /**
274     * java.util.Locale#getISOCountries()
275     */
276    public void test_getISOCountries() {
277        // Test for method java.lang.String []
278        // java.util.Locale.getISOCountries()
279        // Assumes all countries are 2 digits, and that there will always be
280        // 230 countries on the list...
281        String[] isoCountries = Locale.getISOCountries();
282        int length = isoCountries.length;
283        int familiarCount = 0;
284        for (int i = 0; i < length; i++) {
285            if (isoCountries[i].length() != 2) {
286                fail("Wrong format for ISOCountries.");
287            }
288            if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB")
289                    || isoCountries[i].equals("US")
290                    || isoCountries[i].equals("KR"))
291                familiarCount++;
292        }
293        assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230);
294    }
295
296    /**
297     * java.util.Locale#getISOLanguages()
298     */
299    public void test_getISOLanguages() {
300        // Test for method java.lang.String []
301        // java.util.Locale.getISOLanguages()
302        // Assumes always at least 131 ISOlanguages...
303        String[] isoLang = Locale.getISOLanguages();
304        int length = isoLang.length;
305
306        // BEGIN Android-changed
307        // Language codes are 2- and 3-letter, with preference given
308        // to 2-letter codes where possible. 3-letter codes are used
309        // when lack a 2-letter equivalent.
310        assertTrue("Random element in wrong format.",
311                   (isoLang[length / 2].length() == 2 || isoLang[length / 2].length() == 3)
312                   && isoLang[length / 2].toLowerCase().equals(isoLang[length / 2]));
313        // END Android-changed
314
315        assertTrue("Wrong number of ISOLanguages.", length > 130);
316    }
317
318    /**
319     * java.util.Locale#getLanguage()
320     */
321    public void test_getLanguage() {
322        // Test for method java.lang.String java.util.Locale.getLanguage()
323        assertTrue("Returned incorrect language: " + testLocale.getLanguage(),
324                testLocale.getLanguage().equals("en"));
325    }
326
327    /**
328     * java.util.Locale#getVariant()
329     */
330    public void test_getVariant() {
331        // Test for method java.lang.String java.util.Locale.getVariant()
332        assertTrue("Returned incorrect variant: " + testLocale.getVariant(),
333                testLocale.getVariant().equals("WIN32"));
334    }
335
336    /**
337     * java.util.Locale#setDefault(java.util.Locale)
338     */
339    public void test_setDefaultLjava_util_Locale() {
340        // Test for method void java.util.Locale.setDefault(java.util.Locale)
341
342        Locale org = Locale.getDefault();
343        Locale.setDefault(l);
344        Locale x = Locale.getDefault();
345        Locale.setDefault(org);
346        assertEquals("Failed to set locale", "fr_CA_WIN32", x.toString());
347
348        Locale.setDefault(new Locale("tr", ""));
349        String res1 = "\u0069".toUpperCase();
350        String res2 = "\u0049".toLowerCase();
351        Locale.setDefault(org);
352        assertEquals("Wrong toUppercase conversion", "\u0130", res1);
353        assertEquals("Wrong toLowercase conversion", "\u0131", res2);
354
355        try {
356            Locale.setDefault(null);
357            fail("NullPointerException expected");
358        } catch (NullPointerException e) {
359            //expected
360        }
361    }
362
363    /**
364     * java.util.Locale#toString()
365     */
366    public void test_toString() {
367        // Test for method java.lang.String java.util.Locale.toString()
368        assertEquals("en_CA_WIN32", new Locale("en", "CA", "WIN32").toString());
369
370        Locale l = new Locale("en", "");
371        assertEquals("Wrong representation 1", "en", l.toString());
372        l = new Locale("", "CA");
373        assertEquals("Wrong representation 2", "_CA", l.toString());
374
375        // Non-bug difference for HARMONY-5442
376        l = new Locale("", "CA", "var");
377        assertEquals("Wrong representation 2.5", "_CA_var", l.toString());
378        l = new Locale("en", "", "WIN");
379        assertEquals("Wrong representation 4", "en__WIN", l.toString());
380        l = new Locale("en", "CA");
381        assertEquals("Wrong representation 6", "en_CA", l.toString());
382        l = new Locale("en", "CA", "VAR");
383        assertEquals("Wrong representation 7", "en_CA_VAR", l.toString());
384
385        l = new Locale("", "", "var");
386        assertEquals("Wrong representation 8", "", l.toString());
387
388    }
389
390    public void test_hashCode() {
391        Locale l1 = new Locale("en", "US");
392        Locale l2 = new Locale("fr", "CA");
393
394        assertTrue(l1.hashCode() != l2.hashCode());
395    }
396
397    /**
398     * {@value java.util.Locale#ROOT}
399     * @since 1.6
400     */
401    public void test_constantROOT() {
402        Locale root = Locale.ROOT;
403        assertEquals("", root.getLanguage());
404        assertEquals("", root.getCountry());
405        assertEquals("", root.getVariant());
406    }
407
408// BEGIN Android-removed
409// These locales are not part of the android reference impl
410//    // Regression Test for HARMONY-2953
411//    public void test_getISO() {
412//        Locale locale = new Locale("an");
413//        assertEquals("arg", locale.getISO3Language());
414//
415//        locale = new Locale("PS");
416//        assertEquals("pus", locale.getISO3Language());
417//
418//        List<String> languages = Arrays.asList(Locale.getISOLanguages());
419//        assertTrue(languages.contains("ak"));
420//
421//        List<String> countries = Arrays.asList(Locale.getISOCountries());
422//        assertTrue(countries.contains("CS"));
423//    }
424// END Android-removed
425
426    /**
427     * Sets up the fixture, for example, open a network connection. This method
428     * is called before a test is executed.
429     */
430    protected void setUp() {
431        defaultLocale = Locale.getDefault();
432        Locale.setDefault(Locale.US);
433        testLocale = new Locale("en", "CA", "WIN32");
434        l = new Locale("fr", "CA", "WIN32");
435    }
436
437    /**
438     * Tears down the fixture, for example, close a network connection. This
439     * method is called after a test is executed.
440     */
441    protected void tearDown() {
442        Locale.setDefault(defaultLocale);
443    }
444}
445