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 tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24import dalvik.annotation.AndroidOnly;
25import tests.support.Support_Locale;
26
27import java.security.Permission;
28import java.util.Arrays;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Locale;
32import java.util.MissingResourceException;
33import java.util.Set;
34
35@TestTargetClass(Locale.class)
36public class LocaleTest extends junit.framework.TestCase {
37
38    Locale testLocale;
39
40    Locale l;
41
42    Locale defaultLocale;
43
44    /**
45     * @tests java.util.Locale#Locale(java.lang.String, java.lang.String)
46     */
47    @TestTargetNew(
48        level = TestLevel.COMPLETE,
49        notes = "",
50        method = "Locale",
51        args = {java.lang.String.class}
52    )
53    public void test_ConstructorLjava_lang_String() {
54        // Test for method java.util.Locale(java.lang.String)
55        Locale x = new Locale("xx");
56        assertTrue("Failed to create Locale", x.getVariant().equals(""));
57
58        try {
59            new Locale(null);
60            fail("NullPointerException expected");
61        } catch (NullPointerException e) {
62            //expected
63        }
64    }
65
66    /**
67     * @tests java.util.Locale#Locale(java.lang.String, java.lang.String)
68     */
69    @TestTargetNew(
70        level = TestLevel.COMPLETE,
71        notes = "",
72        method = "Locale",
73        args = {java.lang.String.class, java.lang.String.class}
74    )
75    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
76        // Test for method java.util.Locale(java.lang.String, java.lang.String)
77        Locale x = new Locale("xx", "CV");
78        assertTrue("Failed to create Locale", x.getCountry().equals("CV")
79                && x.getVariant().equals(""));
80
81        try {
82            new Locale("xx", null);
83            fail("NullPointerException expected");
84        } catch (NullPointerException e) {
85            //expected
86        }
87
88        try {
89            new Locale(null, "CV");
90            fail("NullPointerException expected");
91        } catch (NullPointerException e) {
92            //expected
93        }
94    }
95
96    /**
97     * @tests java.util.Locale#Locale(java.lang.String, java.lang.String,
98     *        java.lang.String)
99     */
100    @TestTargetNew(
101        level = TestLevel.COMPLETE,
102        notes = "",
103        method = "Locale",
104        args = {java.lang.String.class, java.lang.String.class, java.lang.String.class}
105    )
106    public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() {
107        // Test for method java.util.Locale(java.lang.String, java.lang.String,
108        // java.lang.String)
109        Locale x = new Locale("xx", "CV", "ZZ");
110        assertTrue("Failed to create Locale", x.getLanguage().equals("xx")
111                && (x.getCountry().equals("CV") && x.getVariant().equals("ZZ")));
112        try {
113           new Locale(null, "CV", "ZZ");
114           fail("expected NullPointerException with 1st parameter == null");
115        } catch(NullPointerException e) {
116        }
117
118        try {
119           new Locale("xx", null, "ZZ");
120           fail("expected NullPointerException with 2nd parameter == null");
121        } catch(NullPointerException e) {
122        }
123
124        try {
125           new Locale("xx", "CV", null);
126           fail("expected NullPointerException with 3rd parameter == null");
127        } catch(NullPointerException e) {
128        }
129    }
130
131    /**
132     * @tests java.util.Locale#clone()
133     */
134    @TestTargetNew(
135        level = TestLevel.COMPLETE,
136        notes = "",
137        method = "clone",
138        args = {}
139    )
140    public void test_clone() {
141        // Test for method java.lang.Object java.util.Locale.clone()
142        assertTrue("Clone failed", l.clone().equals(l));
143    }
144
145    /**
146     * @tests java.util.Locale#equals(java.lang.Object)
147     */
148    @TestTargetNew(
149        level = TestLevel.COMPLETE,
150        notes = "",
151        method = "equals",
152        args = {java.lang.Object.class}
153    )
154    public void test_equalsLjava_lang_Object() {
155        // Test for method boolean java.util.Locale.equals(java.lang.Object)
156        Locale l2 = new Locale("en", "CA", "WIN32");
157        assertTrue("Same object returned false", testLocale.equals(testLocale));
158        assertTrue("Same values returned false", testLocale.equals(l2));
159        assertTrue("Different locales returned true", !testLocale.equals(l));
160
161    }
162
163    /**
164     * @tests java.util.Locale#getAvailableLocales()
165     */
166    @TestTargetNew(
167        level = TestLevel.COMPLETE,
168        notes = "",
169        method = "getAvailableLocales",
170        args = {}
171    )
172    public void test_getAvailableLocales() {
173// BEGIN android-changed
174        // Test for method java.util.Locale []
175        // java.util.Locale.getAvailableLocales()
176        // Assumes there will generally be about 10+ available locales...
177        // even in minimal configurations for android
178        try {
179            Locale[] locales = Locale.getAvailableLocales();
180            assertTrue("Wrong number of locales: " + locales.length, locales.length > 10);
181            // regression test for HARMONY-1514
182            // HashSet can filter duplicate locales
183            Set<Locale> localesSet = new HashSet<Locale>(Arrays.asList(locales));
184            assertEquals(localesSet.size(), locales.length);
185        } catch (Exception e) {
186            fail("Exception during test : " + e.getMessage());
187        }
188// END android-changed
189    }
190
191    /**
192     * @tests java.util.Locale#getCountry()
193     */
194    @TestTargetNew(
195        level = TestLevel.COMPLETE,
196        notes = "",
197        method = "getCountry",
198        args = {}
199    )
200    public void test_getCountry() {
201        // Test for method java.lang.String java.util.Locale.getCountry()
202        assertTrue("Returned incorrect country: " + testLocale.getCountry(),
203                testLocale.getCountry().equals("CA"));
204    }
205
206    /**
207     * @tests java.util.Locale#getDefault()
208     */
209    @TestTargetNew(
210        level = TestLevel.COMPLETE,
211        notes = "",
212        method = "getDefault",
213        args = {}
214    )
215    public void test_getDefault() {
216        // Test for method java.util.Locale java.util.Locale.getDefault()
217        assertTrue("returns copy", Locale.getDefault() == Locale.getDefault());
218        Locale org = Locale.getDefault();
219        Locale.setDefault(l);
220        Locale x = Locale.getDefault();
221        Locale.setDefault(org);
222        assertEquals("Failed to get locale", "fr_CA_WIN32", x.toString());
223    }
224
225    /**
226     * @tests java.util.Locale#getDisplayCountry()
227     */
228    @TestTargetNew(
229        level = TestLevel.COMPLETE,
230        notes = "",
231        method = "getDisplayCountry",
232        args = {}
233    )
234    @AndroidOnly("ICU has different display name for countries")
235    public void test_getDisplayCountry() {
236        // Test for method java.lang.String java.util.Locale.getDisplayCountry()
237        assertTrue("Returned incorrect country: "
238                + testLocale.getDisplayCountry(), testLocale
239                .getDisplayCountry().equals("Canada"));
240
241        // Regression for Harmony-1146
242        Locale l_countryCD = new Locale("", "CD"); //$NON-NLS-1$ //$NON-NLS-2$
243// BEGIN android-changed
244// ICU has different display name for countries
245//                assertEquals("The Democratic Republic Of Congo", //$NON-NLS-1$
246//                        l_countryCD.getDisplayCountry());
247        assertEquals("Congo - Kinshasa", //$NON-NLS-1$
248              l_countryCD.getDisplayCountry());
249// END android-changed
250    }
251
252    /**
253     * @tests java.util.Locale#getDisplayCountry(java.util.Locale)
254     */
255    @TestTargetNew(
256        level = TestLevel.COMPLETE,
257        notes = "",
258        method = "getDisplayCountry",
259        args = {java.util.Locale.class}
260    )
261    public void test_getDisplayCountryLjava_util_Locale() {
262        Locale[] requiredLocales = {Locale.ITALY};
263        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
264            // locale dependent test, bug 1943269
265            return;
266        }
267        // Test for method java.lang.String
268        // java.util.Locale.getDisplayCountry(java.util.Locale)
269        assertEquals("Returned incorrect country", "Italie", Locale.ITALY
270                .getDisplayCountry(l));
271    }
272
273    /**
274     * @tests java.util.Locale#getDisplayLanguage()
275     */
276    @TestTargetNew(
277        level = TestLevel.COMPLETE,
278        notes = "",
279        method = "getDisplayLanguage",
280        args = {}
281    )
282    public void test_getDisplayLanguage() {
283        // Test for method java.lang.String
284        // java.util.Locale.getDisplayLanguage()
285        assertTrue("Returned incorrect language: "
286                + testLocale.getDisplayLanguage(), testLocale
287                .getDisplayLanguage().equals("English"));
288
289        // Regression for Harmony-1146
290        Locale l_languageAE = new Locale("ae", ""); //$NON-NLS-1$ //$NON-NLS-2$
291        assertEquals("Avestan", l_languageAE.getDisplayLanguage()); //$NON-NLS-1$
292    }
293
294    /**
295     * @tests java.util.Locale#getDisplayLanguage(java.util.Locale)
296     */
297    @TestTargetNew(
298        level = TestLevel.COMPLETE,
299        notes = "",
300        method = "getDisplayLanguage",
301        args = {java.util.Locale.class}
302    )
303    public void test_getDisplayLanguageLjava_util_Locale() {
304        Locale[] requiredLocales = {testLocale};
305        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
306            // locale dependent test, bug 1943269
307            return;
308        }
309        // Test for method java.lang.String
310        // java.util.Locale.getDisplayLanguage(java.util.Locale)
311        assertTrue("Returned incorrect language: "
312                + testLocale.getDisplayLanguage(l), testLocale
313                .getDisplayLanguage(l).equals("anglais"));
314    }
315
316    /**
317     * @tests java.util.Locale#getDisplayName()
318     */
319    @TestTargetNew(
320        level = TestLevel.COMPLETE,
321        notes = "",
322        method = "getDisplayName",
323        args = {}
324    )
325    public void test_getDisplayName() {
326        // Test for method java.lang.String java.util.Locale.getDisplayName()
327        assertTrue("Returned incorrect name: " + testLocale.getDisplayName(),
328                testLocale.getDisplayName().equals("English (Canada,WIN32)"));
329    }
330
331    /**
332     * @tests java.util.Locale#getDisplayName(java.util.Locale)
333     */
334    @TestTargetNew(
335        level = TestLevel.COMPLETE,
336        notes = "",
337        method = "getDisplayName",
338        args = {java.util.Locale.class}
339    )
340    public void test_getDisplayNameLjava_util_Locale() {
341        Locale[] requiredLocales = {testLocale};
342        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
343            // locale dependent test, bug 1943269
344            return;
345        }
346        // Test for method java.lang.String
347        // java.util.Locale.getDisplayName(java.util.Locale)
348        assertTrue("Returned incorrect name: " + testLocale.getDisplayName(l),
349                testLocale.getDisplayName(l).equals("anglais (Canada,WIN32)"));
350    }
351
352    /**
353     * @tests java.util.Locale#getDisplayVariant()
354     */
355    @TestTargetNew(
356        level = TestLevel.COMPLETE,
357        notes = "",
358        method = "getDisplayVariant",
359        args = {}
360    )
361    public void test_getDisplayVariant() {
362        // Test for method java.lang.String java.util.Locale.getDisplayVariant()
363        assertTrue("Returned incorrect variant: "
364                + testLocale.getDisplayVariant(), testLocale
365                .getDisplayVariant().equals("WIN32"));
366    }
367
368    /**
369     * @tests java.util.Locale#getDisplayVariant(java.util.Locale)
370     */
371    @TestTargetNew(
372        level = TestLevel.COMPLETE,
373        notes = "",
374        method = "getDisplayVariant",
375        args = {java.util.Locale.class}
376    )
377    public void test_getDisplayVariantLjava_util_Locale() {
378        // Test for method java.lang.String
379        // java.util.Locale.getDisplayVariant(java.util.Locale)
380        assertTrue("Returned incorrect variant: "
381                + testLocale.getDisplayVariant(l), testLocale
382                .getDisplayVariant(l).equals("WIN32"));
383    }
384
385    /**
386     * @tests java.util.Locale#getISO3Country()
387     */
388    @TestTargetNew(
389        level = TestLevel.COMPLETE,
390        notes = "",
391        method = "getISO3Country",
392        args = {}
393    )
394    public void test_getISO3Country() {
395        // Test for method java.lang.String java.util.Locale.getISO3Country()
396        assertTrue("Returned incorrect ISO3 country: "
397                + testLocale.getISO3Country(), testLocale.getISO3Country()
398                .equals("CAN"));
399
400        Locale l = new Locale("", "CD");
401        assertEquals("COD", l.getISO3Country());
402
403        Locale x = new Locale("xx", "C");
404        try {
405            x.getISO3Country();
406        } catch (MissingResourceException e) {
407            //expected
408        }
409    }
410
411    /**
412     * @tests java.util.Locale#getISO3Language()
413     */
414    @TestTargetNew(
415        level = TestLevel.COMPLETE,
416        notes = "",
417        method = "getISO3Language",
418        args = {}
419    )
420    public void test_getISO3Language() {
421        // Test for method java.lang.String java.util.Locale.getISO3Language()
422        assertTrue("Returned incorrect ISO3 language: "
423                + testLocale.getISO3Language(), testLocale.getISO3Language()
424                .equals("eng"));
425
426        Locale l = new Locale("ae");
427        assertEquals("ave", l.getISO3Language());
428
429        // Regression for Harmony-1146
430        Locale l_CountryCS = new Locale("", "CS"); //$NON-NLS-1$ //$NON-NLS-2$
431        assertEquals("SCG", l_CountryCS.getISO3Country()); //$NON-NLS-1$
432
433        // Regression for Harmony-1129
434        l = new Locale("ak", ""); //$NON-NLS-1$ //$NON-NLS-2$
435        assertEquals("aka", l.getISO3Language()); //$NON-NLS-1$
436
437        Locale x = new Locale("xx", "C");
438        try {
439            x.getISO3Language();
440        } catch (MissingResourceException e) {
441            //expected
442        }
443    }
444
445    /**
446     * @tests java.util.Locale#getISOCountries()
447     */
448    @TestTargetNew(
449        level = TestLevel.COMPLETE,
450        notes = "",
451        method = "getISOCountries",
452        args = {}
453    )
454    public void test_getISOCountries() {
455        // Test for method java.lang.String []
456        // java.util.Locale.getISOCountries()
457        // Assumes all countries are 2 digits, and that there will always be
458        // 230 countries on the list...
459        String[] isoCountries = Locale.getISOCountries();
460        int length = isoCountries.length;
461        int familiarCount = 0;
462        for (int i = 0; i < length; i++) {
463            if (isoCountries[i].length() != 2) {
464                fail("Wrong format for ISOCountries.");
465            }
466            if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB")
467                    || isoCountries[i].equals("US")
468                    || isoCountries[i].equals("KR"))
469                familiarCount++;
470        }
471        assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230);
472    }
473
474    /**
475     * @tests java.util.Locale#getISOLanguages()
476     */
477    @TestTargetNew(
478        level = TestLevel.COMPLETE,
479        notes = "",
480        method = "getISOLanguages",
481        args = {}
482    )
483    public void test_getISOLanguages() {
484        // Test for method java.lang.String []
485        // java.util.Locale.getISOLanguages()
486        // Assumes always at least 131 ISOlanguages...
487        String[] isoLang = Locale.getISOLanguages();
488        int length = isoLang.length;
489
490        // BEGIN android-changed
491        // Language codes are 2- and 3-letter, with preference given
492        // to 2-letter codes where possible. 3-letter codes are used
493        // when lack a 2-letter equivalent.
494        assertTrue("Random element in wrong format.",
495                   (isoLang[length / 2].length() == 2 || isoLang[length / 2].length() == 3)
496                   && isoLang[length / 2].toLowerCase().equals(isoLang[length / 2]));
497        // END android-changed
498
499        assertTrue("Wrong number of ISOLanguages.", length > 130);
500    }
501
502    /**
503     * @tests java.util.Locale#getLanguage()
504     */
505    @TestTargetNew(
506        level = TestLevel.COMPLETE,
507        notes = "",
508        method = "getLanguage",
509        args = {}
510    )
511    public void test_getLanguage() {
512        // Test for method java.lang.String java.util.Locale.getLanguage()
513        assertTrue("Returned incorrect language: " + testLocale.getLanguage(),
514                testLocale.getLanguage().equals("en"));
515    }
516
517    /**
518     * @tests java.util.Locale#getVariant()
519     */
520    @TestTargetNew(
521        level = TestLevel.COMPLETE,
522        notes = "",
523        method = "getVariant",
524        args = {}
525    )
526    public void test_getVariant() {
527        // Test for method java.lang.String java.util.Locale.getVariant()
528        assertTrue("Returned incorrect variant: " + testLocale.getVariant(),
529                testLocale.getVariant().equals("WIN32"));
530    }
531
532    SecurityManager sm = new SecurityManager() {
533        final String forbidenPermissionName = "user.language";
534
535        public void checkPermission(Permission perm) {
536            if (perm.getName().equals(forbidenPermissionName)) {
537                throw new SecurityException();
538            }
539        }
540    };
541    /**
542     * @tests java.util.Locale#setDefault(java.util.Locale)
543     */
544    @TestTargetNew(
545        level = TestLevel.COMPLETE,
546        notes = "",
547        method = "setDefault",
548        args = {java.util.Locale.class}
549    )
550    public void test_setDefaultLjava_util_Locale() {
551        // Test for method void java.util.Locale.setDefault(java.util.Locale)
552
553        Locale org = Locale.getDefault();
554        Locale.setDefault(l);
555        Locale x = Locale.getDefault();
556        Locale.setDefault(org);
557        assertEquals("Failed to set locale", "fr_CA_WIN32", x.toString());
558
559        Locale.setDefault(new Locale("tr", ""));
560        String res1 = "\u0069".toUpperCase();
561        String res2 = "\u0049".toLowerCase();
562        Locale.setDefault(org);
563        assertEquals("Wrong toUppercase conversion", "\u0130", res1);
564        assertEquals("Wrong toLowercase conversion", "\u0131", res2);
565
566        try {
567            Locale.setDefault(null);
568            fail("NullPointerException expected");
569        } catch (NullPointerException e) {
570            //expected
571        }
572
573        SecurityManager oldSm = System.getSecurityManager();
574        System.setSecurityManager(sm);
575        try {
576            Locale.setDefault(Locale.CANADA);
577            fail("Should throw SecurityException");
578        } catch (SecurityException e) {
579            // expected
580        } finally {
581            System.setSecurityManager(oldSm);
582        }
583    }
584
585    /**
586     * @tests java.util.Locale#toString()
587     */
588    @TestTargetNew(
589        level = TestLevel.COMPLETE,
590        notes = "",
591        method = "toString",
592        args = {}
593    )
594    public void test_toString() {
595        // Test for method java.lang.String java.util.Locale.toString()
596        assertEquals("Returned incorrect string representation", "en_CA_WIN32", testLocale
597                .toString());
598
599        Locale l = new Locale("en", "");
600        assertEquals("Wrong representation 1", "en", l.toString());
601        l = new Locale("", "CA");
602        assertEquals("Wrong representation 2", "_CA", l.toString());
603        l = new Locale("", "CA", "var");
604        assertEquals("Wrong representation 2.5", "_CA_var", l.toString());
605        l = new Locale("en", "", "WIN");
606        assertEquals("Wrong representation 4", "en__WIN", l.toString());
607        l = new Locale("en", "CA");
608        assertEquals("Wrong representation 6", "en_CA", l.toString());
609        l = new Locale("en", "CA", "VAR");
610        assertEquals("Wrong representation 7", "en_CA_VAR", l.toString());
611
612        l = new Locale("", "", "var");
613        assertEquals("Wrong representation 8", "", l.toString());
614
615    }
616
617    @TestTargetNew(
618        level = TestLevel.COMPLETE,
619        notes = "",
620        method = "hashCode",
621        args = {}
622    )
623    public void test_hashCode() {
624        Locale l1 = new Locale("en", "US");
625        Locale l2 = new Locale("fr", "CA");
626
627        assertTrue(l1.hashCode() != l2.hashCode());
628    }
629
630// BEGIN android-removed
631// These locales are not part of the android reference impl
632//    // Regression Test for HARMONY-2953
633//    public void test_getISO() {
634//        Locale locale = new Locale("an");
635//        assertEquals("arg", locale.getISO3Language());
636//
637//        locale = new Locale("PS");
638//        assertEquals("pus", locale.getISO3Language());
639//
640//        List<String> languages = Arrays.asList(Locale.getISOLanguages());
641//        assertTrue(languages.contains("ak"));
642//
643//        List<String> countries = Arrays.asList(Locale.getISOCountries());
644//        assertTrue(countries.contains("CS"));
645//    }
646// END android-removed
647
648    /**
649     * Sets up the fixture, for example, open a network connection. This method
650     * is called before a test is executed.
651     */
652    protected void setUp() {
653        defaultLocale = Locale.getDefault();
654        Locale.setDefault(Locale.US);
655        testLocale = new Locale("en", "CA", "WIN32");
656        l = new Locale("fr", "CA", "WIN32");
657    }
658
659    /**
660     * Tears down the fixture, for example, close a network connection. This
661     * method is called after a test is executed.
662     */
663    protected void tearDown() {
664        Locale.setDefault(defaultLocale);
665    }
666}
667