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