LocaleTest.java revision 743fc438ecba5ee39e44e4e8b36dfbe9381340bd
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.KnownFailure;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargets;
23import dalvik.annotation.TestLevel;
24import dalvik.annotation.TestTargetClass;
25import dalvik.annotation.AndroidOnly;
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    @KnownFailure("Some locales were removed last minute in cupcake")
262    public void test_getDisplayCountryLjava_util_Locale() {
263        // Test for method java.lang.String
264        // java.util.Locale.getDisplayCountry(java.util.Locale)
265        assertEquals("Returned incorrect country", "Italie", Locale.ITALY
266                .getDisplayCountry(l));
267    }
268
269    /**
270     * @tests java.util.Locale#getDisplayLanguage()
271     */
272    @TestTargetNew(
273        level = TestLevel.COMPLETE,
274        notes = "",
275        method = "getDisplayLanguage",
276        args = {}
277    )
278    public void test_getDisplayLanguage() {
279        // Test for method java.lang.String
280        // java.util.Locale.getDisplayLanguage()
281        assertTrue("Returned incorrect language: "
282                + testLocale.getDisplayLanguage(), testLocale
283                .getDisplayLanguage().equals("English"));
284
285        // Regression for Harmony-1146
286        Locale l_languageAE = new Locale("ae", ""); //$NON-NLS-1$ //$NON-NLS-2$
287        assertEquals("Avestan", l_languageAE.getDisplayLanguage()); //$NON-NLS-1$
288    }
289
290    /**
291     * @tests java.util.Locale#getDisplayLanguage(java.util.Locale)
292     */
293    @TestTargetNew(
294        level = TestLevel.COMPLETE,
295        notes = "",
296        method = "getDisplayLanguage",
297        args = {java.util.Locale.class}
298    )
299    @KnownFailure("Some locales were removed last minute in cupcake")
300    public void test_getDisplayLanguageLjava_util_Locale() {
301        // Test for method java.lang.String
302        // java.util.Locale.getDisplayLanguage(java.util.Locale)
303        assertTrue("Returned incorrect language: "
304                + testLocale.getDisplayLanguage(l), testLocale
305                .getDisplayLanguage(l).equals("anglais"));
306    }
307
308    /**
309     * @tests java.util.Locale#getDisplayName()
310     */
311    @TestTargetNew(
312        level = TestLevel.COMPLETE,
313        notes = "",
314        method = "getDisplayName",
315        args = {}
316    )
317    public void test_getDisplayName() {
318        // Test for method java.lang.String java.util.Locale.getDisplayName()
319        assertTrue("Returned incorrect name: " + testLocale.getDisplayName(),
320                testLocale.getDisplayName().equals("English (Canada,WIN32)"));
321    }
322
323    /**
324     * @tests java.util.Locale#getDisplayName(java.util.Locale)
325     */
326    @TestTargetNew(
327        level = TestLevel.COMPLETE,
328        notes = "",
329        method = "getDisplayName",
330        args = {java.util.Locale.class}
331    )
332    @KnownFailure("Some locales were removed last minute in cupcake")
333    public void test_getDisplayNameLjava_util_Locale() {
334        // Test for method java.lang.String
335        // java.util.Locale.getDisplayName(java.util.Locale)
336        assertTrue("Returned incorrect name: " + testLocale.getDisplayName(l),
337                testLocale.getDisplayName(l).equals("anglais (Canada,WIN32)"));
338    }
339
340    /**
341     * @tests java.util.Locale#getDisplayVariant()
342     */
343    @TestTargetNew(
344        level = TestLevel.COMPLETE,
345        notes = "",
346        method = "getDisplayVariant",
347        args = {}
348    )
349    public void test_getDisplayVariant() {
350        // Test for method java.lang.String java.util.Locale.getDisplayVariant()
351        assertTrue("Returned incorrect variant: "
352                + testLocale.getDisplayVariant(), testLocale
353                .getDisplayVariant().equals("WIN32"));
354    }
355
356    /**
357     * @tests java.util.Locale#getDisplayVariant(java.util.Locale)
358     */
359    @TestTargetNew(
360        level = TestLevel.COMPLETE,
361        notes = "",
362        method = "getDisplayVariant",
363        args = {java.util.Locale.class}
364    )
365    public void test_getDisplayVariantLjava_util_Locale() {
366        // Test for method java.lang.String
367        // java.util.Locale.getDisplayVariant(java.util.Locale)
368        assertTrue("Returned incorrect variant: "
369                + testLocale.getDisplayVariant(l), testLocale
370                .getDisplayVariant(l).equals("WIN32"));
371    }
372
373    /**
374     * @tests java.util.Locale#getISO3Country()
375     */
376    @TestTargetNew(
377        level = TestLevel.COMPLETE,
378        notes = "",
379        method = "getISO3Country",
380        args = {}
381    )
382    public void test_getISO3Country() {
383        // Test for method java.lang.String java.util.Locale.getISO3Country()
384        assertTrue("Returned incorrect ISO3 country: "
385                + testLocale.getISO3Country(), testLocale.getISO3Country()
386                .equals("CAN"));
387
388        Locale l = new Locale("", "CD");
389        assertEquals("COD", l.getISO3Country());
390
391        Locale x = new Locale("xx", "C");
392        try {
393            x.getISO3Country();
394        } catch (MissingResourceException e) {
395            //expected
396        }
397    }
398
399    /**
400     * @tests java.util.Locale#getISO3Language()
401     */
402    @TestTargetNew(
403        level = TestLevel.COMPLETE,
404        notes = "",
405        method = "getISO3Language",
406        args = {}
407    )
408    public void test_getISO3Language() {
409        // Test for method java.lang.String java.util.Locale.getISO3Language()
410        assertTrue("Returned incorrect ISO3 language: "
411                + testLocale.getISO3Language(), testLocale.getISO3Language()
412                .equals("eng"));
413
414        Locale l = new Locale("ae");
415        assertEquals("ave", l.getISO3Language());
416
417        // Regression for Harmony-1146
418        Locale l_CountryCS = new Locale("", "CS"); //$NON-NLS-1$ //$NON-NLS-2$
419        assertEquals("SCG", l_CountryCS.getISO3Country()); //$NON-NLS-1$
420
421        // Regression for Harmony-1129
422        l = new Locale("ak", ""); //$NON-NLS-1$ //$NON-NLS-2$
423        assertEquals("aka", l.getISO3Language()); //$NON-NLS-1$
424
425        Locale x = new Locale("xx", "C");
426        try {
427            x.getISO3Language();
428        } catch (MissingResourceException e) {
429            //expected
430        }
431    }
432
433    /**
434     * @tests java.util.Locale#getISOCountries()
435     */
436    @TestTargetNew(
437        level = TestLevel.COMPLETE,
438        notes = "",
439        method = "getISOCountries",
440        args = {}
441    )
442    public void test_getISOCountries() {
443        // Test for method java.lang.String []
444        // java.util.Locale.getISOCountries()
445        // Assumes all countries are 2 digits, and that there will always be
446        // 230 countries on the list...
447        String[] isoCountries = Locale.getISOCountries();
448        int length = isoCountries.length;
449        int familiarCount = 0;
450        for (int i = 0; i < length; i++) {
451            if (isoCountries[i].length() != 2) {
452                fail("Wrong format for ISOCountries.");
453            }
454            if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB")
455                    || isoCountries[i].equals("US")
456                    || isoCountries[i].equals("KR"))
457                familiarCount++;
458        }
459        assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230);
460    }
461
462    /**
463     * @tests java.util.Locale#getISOLanguages()
464     */
465    @TestTargetNew(
466        level = TestLevel.COMPLETE,
467        notes = "",
468        method = "getISOLanguages",
469        args = {}
470    )
471    public void test_getISOLanguages() {
472        // Test for method java.lang.String []
473        // java.util.Locale.getISOLanguages()
474        // Assumes always at least 131 ISOlanguages...
475        String[] isoLang = Locale.getISOLanguages();
476        int length = isoLang.length;
477        assertTrue("Random element in wrong format.", (isoLang[length / 2]
478                .length() == 2)
479                && isoLang[length / 2].toLowerCase()
480                        .equals(isoLang[length / 2]));
481        assertTrue("Wrong number of ISOLanguages.", length > 130);
482    }
483
484    /**
485     * @tests java.util.Locale#getLanguage()
486     */
487    @TestTargetNew(
488        level = TestLevel.COMPLETE,
489        notes = "",
490        method = "getLanguage",
491        args = {}
492    )
493    public void test_getLanguage() {
494        // Test for method java.lang.String java.util.Locale.getLanguage()
495        assertTrue("Returned incorrect language: " + testLocale.getLanguage(),
496                testLocale.getLanguage().equals("en"));
497    }
498
499    /**
500     * @tests java.util.Locale#getVariant()
501     */
502    @TestTargetNew(
503        level = TestLevel.COMPLETE,
504        notes = "",
505        method = "getVariant",
506        args = {}
507    )
508    public void test_getVariant() {
509        // Test for method java.lang.String java.util.Locale.getVariant()
510        assertTrue("Returned incorrect variant: " + testLocale.getVariant(),
511                testLocale.getVariant().equals("WIN32"));
512    }
513
514    SecurityManager sm = new SecurityManager() {
515        final String forbidenPermissionName = "user.language";
516
517        public void checkPermission(Permission perm) {
518            if (perm.getName().equals(forbidenPermissionName)) {
519                throw new SecurityException();
520            }
521        }
522    };
523    /**
524     * @tests java.util.Locale#setDefault(java.util.Locale)
525     */
526    @TestTargetNew(
527        level = TestLevel.COMPLETE,
528        notes = "",
529        method = "setDefault",
530        args = {java.util.Locale.class}
531    )
532    public void test_setDefaultLjava_util_Locale() {
533        // Test for method void java.util.Locale.setDefault(java.util.Locale)
534
535        Locale org = Locale.getDefault();
536        Locale.setDefault(l);
537        Locale x = Locale.getDefault();
538        Locale.setDefault(org);
539        assertEquals("Failed to set locale", "fr_CA_WIN32", x.toString());
540
541        Locale.setDefault(new Locale("tr", ""));
542        String res1 = "\u0069".toUpperCase();
543        String res2 = "\u0049".toLowerCase();
544        Locale.setDefault(org);
545        assertEquals("Wrong toUppercase conversion", "\u0130", res1);
546        assertEquals("Wrong toLowercase conversion", "\u0131", res2);
547
548        try {
549            Locale.setDefault(null);
550            fail("NullPointerException expected");
551        } catch (NullPointerException e) {
552            //expected
553        }
554
555        SecurityManager oldSm = System.getSecurityManager();
556        System.setSecurityManager(sm);
557        try {
558            Locale.setDefault(Locale.CANADA);
559            fail("Should throw SecurityException");
560        } catch (SecurityException e) {
561            // expected
562        } finally {
563            System.setSecurityManager(oldSm);
564        }
565    }
566
567    /**
568     * @tests java.util.Locale#toString()
569     */
570    @TestTargetNew(
571        level = TestLevel.COMPLETE,
572        notes = "",
573        method = "toString",
574        args = {}
575    )
576    public void test_toString() {
577        // Test for method java.lang.String java.util.Locale.toString()
578        assertEquals("Returned incorrect string representation", "en_CA_WIN32", testLocale
579                .toString());
580
581        Locale l = new Locale("en", "");
582        assertEquals("Wrong representation 1", "en", l.toString());
583        l = new Locale("", "CA");
584        assertEquals("Wrong representation 2", "_CA", l.toString());
585        l = new Locale("", "CA", "var");
586        assertEquals("Wrong representation 2.5", "_CA_var", l.toString());
587        l = new Locale("en", "", "WIN");
588        assertEquals("Wrong representation 4", "en__WIN", l.toString());
589        l = new Locale("en", "CA");
590        assertEquals("Wrong representation 6", "en_CA", l.toString());
591        l = new Locale("en", "CA", "VAR");
592        assertEquals("Wrong representation 7", "en_CA_VAR", l.toString());
593
594        l = new Locale("", "", "var");
595        assertEquals("Wrong representation 8", "", l.toString());
596
597    }
598
599    @TestTargetNew(
600        level = TestLevel.COMPLETE,
601        notes = "",
602        method = "hashCode",
603        args = {}
604    )
605    public void test_hashCode() {
606        Locale l1 = new Locale("en", "US");
607        Locale l2 = new Locale("fr", "CA");
608
609        assertTrue(l1.hashCode() != l2.hashCode());
610    }
611
612// BEGIN android-removed
613// These locales are not part of the android reference impl
614//    // Regression Test for HARMONY-2953
615//    public void test_getISO() {
616//        Locale locale = new Locale("an");
617//        assertEquals("arg", locale.getISO3Language());
618//
619//        locale = new Locale("PS");
620//        assertEquals("pus", locale.getISO3Language());
621//
622//        List<String> languages = Arrays.asList(Locale.getISOLanguages());
623//        assertTrue(languages.contains("ak"));
624//
625//        List<String> countries = Arrays.asList(Locale.getISOCountries());
626//        assertTrue(countries.contains("CS"));
627//    }
628// END android-removed
629
630    /**
631     * Sets up the fixture, for example, open a network connection. This method
632     * is called before a test is executed.
633     */
634    protected void setUp() {
635        defaultLocale = Locale.getDefault();
636        Locale.setDefault(Locale.US);
637        testLocale = new Locale("en", "CA", "WIN32");
638        l = new Locale("fr", "CA", "WIN32");
639    }
640
641    /**
642     * Tears down the fixture, for example, close a network connection. This
643     * method is called after a test is executed.
644     */
645    protected void tearDown() {
646        Locale.setDefault(defaultLocale);
647    }
648}
649
650
651