Home | History | Annotate | Download | only in icu
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.icu;
     18 
     19 import java.text.BreakIterator;
     20 import java.text.Collator;
     21 import java.util.Arrays;
     22 import java.util.Locale;
     23 
     24 public class ICUTest extends junit.framework.TestCase {
     25   public void test_getISOLanguages() throws Exception {
     26     // Check that corrupting our array doesn't affect other callers.
     27     assertNotNull(ICU.getISOLanguages()[0]);
     28     ICU.getISOLanguages()[0] = null;
     29     assertNotNull(ICU.getISOLanguages()[0]);
     30   }
     31 
     32   public void test_getISOCountries() throws Exception {
     33     // Check that corrupting our array doesn't affect other callers.
     34     assertNotNull(ICU.getISOCountries()[0]);
     35     ICU.getISOCountries()[0] = null;
     36     assertNotNull(ICU.getISOCountries()[0]);
     37   }
     38 
     39   public void test_getAvailableLocales() throws Exception {
     40     // Check that corrupting our array doesn't affect other callers.
     41     assertNotNull(ICU.getAvailableLocales()[0]);
     42     ICU.getAvailableLocales()[0] = null;
     43     assertNotNull(ICU.getAvailableLocales()[0]);
     44   }
     45 
     46   public void test_getBestDateTimePattern() throws Exception {
     47     assertEquals("d MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("ca", "ES")));
     48     assertEquals("d 'de' MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("es", "ES")));
     49     assertEquals("d. MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("de", "CH")));
     50     assertEquals("MMMM d", ICU.getBestDateTimePattern("MMMMd", new Locale("en", "US")));
     51     assertEquals("d LLLL", ICU.getBestDateTimePattern("MMMMd", new Locale("fa", "IR")));
     52     assertEquals("Md", ICU.getBestDateTimePattern("MMMMd", new Locale("ja", "JP")));
     53   }
     54 
     55   public void test_localeFromString() throws Exception {
     56     // localeFromString is pretty lenient. Some of these can't be round-tripped
     57     // through Locale.toString.
     58     assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en"));
     59     assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en_"));
     60     assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en__"));
     61     assertEquals(Locale.US, ICU.localeFromIcuLocaleId("en_US"));
     62     assertEquals(Locale.US, ICU.localeFromIcuLocaleId("en_US_"));
     63     assertEquals(new Locale("", "US", ""), ICU.localeFromIcuLocaleId("_US"));
     64     assertEquals(new Locale("", "US", ""), ICU.localeFromIcuLocaleId("_US_"));
     65     assertEquals(new Locale("", "", "POSIX"), ICU.localeFromIcuLocaleId("__POSIX"));
     66     assertEquals(new Locale("aa", "BB", "CC"), ICU.localeFromIcuLocaleId("aa_BB_CC"));
     67   }
     68 
     69   public void test_getScript_addLikelySubtags() throws Exception {
     70     assertEquals("Latn", ICU.getScript(ICU.addLikelySubtags("en_US")));
     71     assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("he")));
     72     assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("he_IL")));
     73     assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("iw")));
     74     assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("iw_IL")));
     75   }
     76 
     77   public void test_addLikelySubtags() throws Exception {
     78     assertEquals("Latn", ICU.addLikelySubtags(new Locale("en", "US")).getScript());
     79     assertEquals("Hebr", ICU.addLikelySubtags(new Locale("he")).getScript());
     80     assertEquals("Hebr", ICU.addLikelySubtags(new Locale("he", "IL")).getScript());
     81     assertEquals("Hebr", ICU.addLikelySubtags(new Locale("iw")).getScript());
     82     assertEquals("Hebr", ICU.addLikelySubtags(new Locale("iw", "IL")).getScript());
     83   }
     84 
     85   private String best(Locale l, String skeleton) {
     86     return ICU.getBestDateTimePattern(skeleton, l);
     87   }
     88 
     89   public void test_getDateFormatOrder() throws Exception {
     90     // lv and fa use differing orders depending on whether you're using numeric or textual months.
     91     Locale lv = new Locale("lv");
     92     assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "yyyy-M-dd"))));
     93     assertEquals("[y, d, M]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "yyyy-MMM-dd"))));
     94     assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "MMM-dd"))));
     95     Locale fa = new Locale("fa");
     96     assertEquals("[y, M, d]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "yyyy-M-dd"))));
     97     assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "yyyy-MMM-dd"))));
     98     assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "MMM-dd"))));
     99 
    100     // English differs on each side of the Atlantic.
    101     Locale en_US = Locale.US;
    102     assertEquals("[M, d, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "yyyy-M-dd"))));
    103     assertEquals("[M, d, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "yyyy-MMM-dd"))));
    104     assertEquals("[M, d, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "MMM-dd"))));
    105     Locale en_GB = Locale.UK;
    106     assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "yyyy-M-dd"))));
    107     assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "yyyy-MMM-dd"))));
    108     assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "MMM-dd"))));
    109 
    110     assertEquals("[y, M, d]", Arrays.toString(ICU.getDateFormatOrder("yyyy - 'why' '' 'ddd' MMM-dd")));
    111 
    112     try {
    113       ICU.getDateFormatOrder("the quick brown fox jumped over the lazy dog");
    114       fail();
    115     } catch (IllegalArgumentException expected) {
    116     }
    117 
    118     try {
    119       ICU.getDateFormatOrder("'");
    120       fail();
    121     } catch (IllegalArgumentException expected) {
    122     }
    123 
    124     try {
    125       ICU.getDateFormatOrder("yyyy'");
    126       fail();
    127     } catch (IllegalArgumentException expected) {
    128     }
    129 
    130     try {
    131       ICU.getDateFormatOrder("yyyy'MMM");
    132       fail();
    133     } catch (IllegalArgumentException expected) {
    134     }
    135   }
    136 
    137   public void testScriptsPassedToIcu() throws Exception {
    138     Locale sr_Cyrl_BA = Locale.forLanguageTag("sr-Cyrl-BA");
    139     Locale sr_Cyrl_ME = Locale.forLanguageTag("sr-Cyrl-ME");
    140     Locale sr_Latn_BA = Locale.forLanguageTag("sr-Latn-BA");
    141     Locale sr_Latn_ME = Locale.forLanguageTag("sr-Latn-ME");
    142 
    143     assertEquals("sr_BA_#Cyrl", sr_Cyrl_BA.toString());
    144     assertEquals("Cyrl",        sr_Cyrl_BA.getScript());
    145 
    146     assertEquals("sr_ME_#Cyrl", sr_Cyrl_ME.toString());
    147     assertEquals("Cyrl",        sr_Cyrl_ME.getScript());
    148 
    149     assertEquals("sr_BA_#Latn", sr_Latn_BA.toString());
    150     assertEquals("Latn",        sr_Latn_BA.getScript());
    151 
    152     assertEquals("sr_ME_#Latn", sr_Latn_ME.toString());
    153     assertEquals("Latn",        sr_Latn_ME.getScript());
    154 
    155     assertEquals("",              sr_Cyrl_BA.getDisplayLanguage(sr_Cyrl_BA));
    156     assertEquals("  ", sr_Cyrl_BA.getDisplayCountry(sr_Cyrl_BA));
    157     assertEquals("",            sr_Cyrl_BA.getDisplayScript(sr_Cyrl_BA));
    158     assertEquals("",                    sr_Cyrl_BA.getDisplayVariant(sr_Cyrl_BA));
    159 
    160     assertEquals("",    sr_Cyrl_ME.getDisplayLanguage(sr_Cyrl_ME));
    161     assertEquals(" ", sr_Cyrl_ME.getDisplayCountry(sr_Cyrl_ME));
    162     assertEquals("",  sr_Cyrl_ME.getDisplayScript(sr_Cyrl_ME));
    163     assertEquals("",          sr_Cyrl_ME.getDisplayVariant(sr_Cyrl_ME));
    164 
    165     assertEquals("Srpski",              sr_Latn_BA.getDisplayLanguage(sr_Latn_BA));
    166     assertEquals("Bosna i Hercegovina", sr_Latn_BA.getDisplayCountry(sr_Latn_BA));
    167     assertEquals("Latinica",            sr_Latn_BA.getDisplayScript(sr_Latn_BA));
    168     assertEquals("",                    sr_Latn_BA.getDisplayVariant(sr_Latn_BA));
    169 
    170     assertEquals("Srpski",    sr_Latn_ME.getDisplayLanguage(sr_Latn_ME));
    171     assertEquals("Crna Gora", sr_Latn_ME.getDisplayCountry(sr_Latn_ME));
    172     assertEquals("Latinica",  sr_Latn_ME.getDisplayScript(sr_Latn_ME));
    173     assertEquals("",          sr_Latn_ME.getDisplayVariant(sr_Latn_ME));
    174 
    175     assertEquals("BIH", sr_Cyrl_BA.getISO3Country());
    176     assertEquals("srp", sr_Cyrl_BA.getISO3Language());
    177     assertEquals("MNE", sr_Cyrl_ME.getISO3Country());
    178     assertEquals("srp", sr_Cyrl_ME.getISO3Language());
    179     assertEquals("BIH", sr_Latn_BA.getISO3Country());
    180     assertEquals("srp", sr_Latn_BA.getISO3Language());
    181     assertEquals("MNE", sr_Latn_ME.getISO3Country());
    182     assertEquals("srp", sr_Latn_ME.getISO3Language());
    183 
    184     BreakIterator.getCharacterInstance(sr_Cyrl_BA);
    185     BreakIterator.getCharacterInstance(sr_Cyrl_ME);
    186     BreakIterator.getCharacterInstance(sr_Latn_BA);
    187     BreakIterator.getCharacterInstance(sr_Latn_ME);
    188 
    189     BreakIterator.getLineInstance(sr_Cyrl_BA);
    190     BreakIterator.getLineInstance(sr_Cyrl_ME);
    191     BreakIterator.getLineInstance(sr_Latn_BA);
    192     BreakIterator.getLineInstance(sr_Latn_ME);
    193 
    194     BreakIterator.getSentenceInstance(sr_Cyrl_BA);
    195     BreakIterator.getSentenceInstance(sr_Cyrl_ME);
    196     BreakIterator.getSentenceInstance(sr_Latn_BA);
    197     BreakIterator.getSentenceInstance(sr_Latn_ME);
    198 
    199     BreakIterator.getWordInstance(sr_Cyrl_BA);
    200     BreakIterator.getWordInstance(sr_Cyrl_ME);
    201     BreakIterator.getWordInstance(sr_Latn_BA);
    202     BreakIterator.getWordInstance(sr_Latn_ME);
    203 
    204     Collator.getInstance(sr_Cyrl_BA);
    205     Collator.getInstance(sr_Cyrl_ME);
    206     Collator.getInstance(sr_Latn_BA);
    207     Collator.getInstance(sr_Latn_ME);
    208 
    209     Locale l = Locale.forLanguageTag("de-u-co-phonebk-kf-upper-kn");
    210     assertEquals("de__#u-co-phonebk-kf-upper-kn", l.toString());
    211     assertEquals("de-u-co-phonebk-kf-upper-kn", l.toLanguageTag());
    212 
    213     Collator c = Collator.getInstance(l);
    214     assertTrue(c.compare("2", "11") < 0);
    215     assertTrue(c.compare("11", "ae") < 0);
    216     assertTrue(c.compare("ae", "") < 0);
    217     assertTrue(c.compare("", "") < 0);
    218     assertTrue(c.compare("", "AF") < 0);
    219     assertTrue(c.compare("AF", "af") < 0);
    220   }
    221 
    222   // Test for the behavior of currency symbol lookup when an unrecognized locale has been set as the
    223   // default.
    224   public void testIcuDefaultAffectsCurrencySymbol() {
    225     // A locale that is not going to be recognized by ICU and should fallback to "root" for the
    226     // currency symbol.
    227     final Locale unrecognizedLocale = new Locale("xy", "KR");
    228 
    229     // A known locale with a relatively stable representation for its currency symbol.
    230     final Locale enUsLocale = new Locale("en", "US");
    231     final String usDollar = "USD";
    232 
    233     String initialDefaultLocale = ICU.getDefaultLocale();
    234     try {
    235       // Confirm the "$" symbol for USD in en-US.
    236       assertEquals("$", ICU.getCurrencySymbol(enUsLocale, usDollar));
    237 
    238       // Set the default so this will be used as fallback for the unrecognized locale symbol lookup.
    239       ICU.setDefaultLocale(enUsLocale.toLanguageTag());
    240 
    241       // Demonstrate the USD symbol is reported as "$" for the unrecognized locale (which is using
    242       // the default).
    243       assertEquals("$", ICU.getCurrencySymbol(unrecognizedLocale, usDollar));
    244 
    245       // Change the default.
    246       ICU.setDefaultLocale(unrecognizedLocale.toLanguageTag());
    247 
    248       String currencySymbolAfterDefaultChange = ICU.getCurrencySymbol(unrecognizedLocale, usDollar);
    249       // "$US" is the value from root. With an unrecognized locale argument, and an unrecognized
    250       // locale as the default, ICU has returns the value in root.
    251       assertEquals("US$", currencySymbolAfterDefaultChange);
    252     } finally {
    253       ICU.setDefaultLocale(initialDefaultLocale);
    254     }
    255   }
    256 }
    257