Home | History | Annotate | Download | only in util
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2005-2016, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 
     11 /*
     12  * New added, 2005-5-10 [Terry/SGL]
     13  * Major modification by Ram
     14  */
     15 
     16 package android.icu.dev.test.util;
     17 
     18 import java.util.HashMap;
     19 
     20 import org.junit.Before;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 
     25 import android.icu.dev.test.TestFmwk;
     26 import android.icu.impl.ICUData;
     27 import android.icu.text.DateFormat;
     28 import android.icu.util.Calendar;
     29 import android.icu.util.ULocale;
     30 import android.icu.util.UResourceBundle;
     31 import android.icu.testsharding.MainTestShard;
     32 
     33 @MainTestShard
     34 @RunWith(JUnit4.class)
     35 public class LocaleAliasTest extends TestFmwk {
     36     private static final ULocale[][] _LOCALES = {
     37 
     38             {new ULocale("en", "RH"), new ULocale("en", "ZW")},
     39             {new ULocale("in"), new ULocale("id")},
     40             {new ULocale("in", "ID"), new ULocale("id", "ID")},
     41             {new ULocale("iw"), new ULocale("he")},
     42             {new ULocale("iw", "IL"), new ULocale("he", "IL")},
     43             {new ULocale("ji"), new ULocale("yi")},
     44 
     45             {new ULocale("en", "BU"), new ULocale("en", "MM")},
     46             {new ULocale("en", "DY"), new ULocale("en", "BJ")},
     47             {new ULocale("en", "HV"), new ULocale("en", "BF")},
     48             {new ULocale("en", "NH"), new ULocale("en", "VU")},
     49             {new ULocale("en", "TP"), new ULocale("en", "TL")},
     50             {new ULocale("en", "ZR"), new ULocale("en", "CD")}
     51     };
     52 
     53     private static final int _LOCALE_NUMBER = _LOCALES.length;
     54     private static ULocale[] available = null;
     55     private HashMap availableMap = new HashMap();
     56     private static final ULocale _DEFAULT_LOCALE = ULocale.US;
     57 
     58     public LocaleAliasTest() {
     59     }
     60 
     61     @Before
     62     public void init() {
     63         available = ULocale.getAvailableLocales();
     64         for(int i=0; i<available.length;i++){
     65             availableMap.put(available[i].toString(),"");
     66         }
     67     }
     68 
     69     @Test
     70     public void TestCalendar() {
     71         ULocale defLoc = ULocale.getDefault();
     72         ULocale.setDefault(_DEFAULT_LOCALE);
     73         for (int i=0; i<_LOCALE_NUMBER; i++) {
     74             ULocale oldLoc = _LOCALES[i][0];
     75             ULocale newLoc = _LOCALES[i][1];
     76             if(availableMap.get(_LOCALES[i][1])==null){
     77                 logln(_LOCALES[i][1]+" is not available. Skipping!");
     78                 continue;
     79             }
     80             Calendar c1 = Calendar.getInstance(oldLoc);
     81             Calendar c2 = Calendar.getInstance(newLoc);
     82             c1.setTime(c2.getTime());
     83             //Test function "getFirstDayOfWeek"
     84     //        int firstDayOfWeek1 = c1.getFirstDayOfWeek();
     85     //        int firstDayOfWeek2 = c2.getFirstDayOfWeek();
     86     //        if (firstDayOfWeek1 != firstDayOfWeek2) {
     87     //            this.logln("Calendar(getFirstDayOfWeek) old:"
     88     //                    +firstDayOfWeek1+"   new:"+firstDayOfWeek2);
     89     //            pass = false;
     90     //        }
     91 
     92             //Test function "getLocale(ULocale.VALID_LOCALE)"
     93             ULocale l1 = c1.getLocale(ULocale.VALID_LOCALE);
     94             ULocale l2 = c2.getLocale(ULocale.VALID_LOCALE);
     95             if (!newLoc.equals(l1)) {
     96                 errln("CalendarTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
     97             }
     98             if (!l1.equals(l2)) {
     99                 errln("CalendarTest: l1!=l2: l1= "+l1 +" l2= "+l2);
    100             }
    101             if(!c1.equals(c2)){
    102                 errln("CalendarTest: c1!=c2.  newLoc= "+newLoc +" oldLoc= "+oldLoc);
    103             }
    104             logln("Calendar(getLocale) old:"+l1+"   new:"+l2);
    105         }
    106         ULocale.setDefault(defLoc);
    107     }
    108 
    109     @Test
    110     public void  TestDateFormat() {
    111         ULocale defLoc = ULocale.getDefault();
    112         ULocale.setDefault(_DEFAULT_LOCALE);
    113         for (int i=0; i<_LOCALE_NUMBER; i++) {
    114             ULocale oldLoc = _LOCALES[i][0];
    115             ULocale newLoc = _LOCALES[i][1];
    116             if(availableMap.get(_LOCALES[i][1])==null){
    117                 logln(_LOCALES[i][1]+" is not available. Skipping!");
    118                 continue;
    119             }
    120             DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL, oldLoc);
    121             DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, newLoc);
    122 
    123             //Test function "getLocale"
    124             ULocale l1 = df1.getLocale(ULocale.VALID_LOCALE);
    125             ULocale l2 = df2.getLocale(ULocale.VALID_LOCALE);
    126             if (!newLoc.equals(l1)) {
    127                 errln("DateFormatTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
    128             }
    129             if (!l1.equals(l2)) {
    130                 errln("DateFormatTest: l1!=l2: l1= "+l1 +" l2= "+l2);
    131             }
    132             if (!df1.equals(df2)) {
    133                 errln("DateFormatTest: df1!=df2: newLoc= "+newLoc +" oldLoc= "+oldLoc);
    134             }
    135             TestFmwk.logln("DateFormat(getLocale) old:"+l1+"   new:"+l2);
    136 
    137             //Test function "format"
    138     //        Date d = new Date();
    139     //        String d1 = df1.format(d);
    140     //        String d2 = df2.format(d);
    141     //        if (!d1.equals(d2)) {
    142     //            pass = false;
    143     //        }
    144     //        this.logln("DateFormat(format) old:"+d1+"   new:"+d2);
    145         }
    146         ULocale.setDefault(defLoc);
    147     }
    148 
    149     @Test
    150     public void TestULocale() {
    151         ULocale defLoc = ULocale.getDefault();
    152         ULocale.setDefault(_DEFAULT_LOCALE);
    153         for (int i=0; i<_LOCALE_NUMBER; i++) {
    154             ULocale oldLoc = _LOCALES[i][0];
    155             ULocale newLoc = _LOCALES[i][1];
    156             if(availableMap.get(_LOCALES[i][1])==null){
    157                 logln(_LOCALES[i][1]+" is not available. Skipping!");
    158                 continue;
    159             }
    160             ULocale ul1 = new ULocale(oldLoc.toString());
    161             ULocale ul2 = new ULocale(newLoc.toString());
    162 
    163             String name1 = ul1.getDisplayName();
    164             String name2 = ul2.getDisplayName();
    165             if (!name1.equals(name2)) {
    166                 errln("name1!=name2. name1 = " + name1 +" name2 = " +name2);
    167             }
    168             logln("ULocale(getDisplayName) old:"+name1+"   new:"+name2);
    169         }
    170         ULocale.setDefault(defLoc);
    171     }
    172 
    173     @Test
    174     public void TestDisplayName() {
    175         ULocale defLoc = ULocale.getDefault();
    176         ULocale.setDefault(_DEFAULT_LOCALE);
    177         for (int i=0; i<_LOCALE_NUMBER; i++) {
    178             ULocale oldLoc = _LOCALES[i][0];
    179             ULocale newLoc = _LOCALES[i][1];
    180 
    181             for(int j=0; j<available.length; j++){
    182                String oldCountry = oldLoc.getDisplayCountry(available[j]);
    183                String newCountry = newLoc.getDisplayCountry(available[j]);
    184                String oldLang = oldLoc.getDisplayLanguage(available[j]);
    185                String newLang = newLoc.getDisplayLanguage(available[j]);
    186 
    187                // is  there  display name for the current country ID
    188                if(!newCountry.equals(newLoc.getCountry())){
    189                    if(!oldCountry.equals(newCountry)){
    190                        errln("getCountry() failed for "+ oldLoc +" oldCountry= "+ prettify(oldCountry) +" newCountry = "+prettify(newCountry)+ " in display locale "+ available[j].toString());
    191                    }
    192                }
    193                //there is a display name for the current lang ID
    194                if(!newLang.equals(newLoc.getLanguage())){
    195                    if(!oldLang.equals(newLang)){
    196                        errln("getLanguage() failed for " + oldLoc + " oldLang = "+ prettify(oldLang) +" newLang = "+prettify(newLang)+ " in display locale "+ available[j].toString());
    197                    }
    198                }
    199             }
    200         }
    201         ULocale.setDefault(defLoc);
    202     }
    203 
    204     @Test
    205     public void TestUResourceBundle() {
    206         ULocale defLoc = ULocale.getDefault();
    207         ULocale.setDefault(_DEFAULT_LOCALE);
    208         for (int i=0; i<_LOCALE_NUMBER; i++) {
    209             if(availableMap.get(_LOCALES[i][1])==null){
    210                 logln(_LOCALES[i][1]+" is not available. Skipping!");
    211                 continue;
    212             }
    213             ULocale oldLoc = _LOCALES[i][0];
    214             ULocale newLoc = _LOCALES[i][1];
    215             UResourceBundle urb1 = null;
    216             UResourceBundle urb2 = null;
    217 
    218             urb1 = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, oldLoc);
    219             urb2 = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, newLoc);
    220             ULocale l1 = urb1.getULocale();
    221             ULocale l2 = urb2.getULocale();
    222             if (!newLoc.equals(l1)) {
    223                 errln("ResourceBundleTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
    224             }
    225             if (!l1.equals(l2)) {
    226                 errln("ResourceBundleTest: l1!=l2: l1= "+l1 +" l2= "+l2);
    227             }
    228             TestFmwk.logln("UResourceBundle old:"+l1+"   new:"+l2);
    229         }
    230         ULocale.setDefault(defLoc);
    231     }
    232 }
    233