ICUResourceBundleCollationTest.java revision aefe4d1f8f1773ead1a52f7a5d2c9e0009353600
1/* GENERATED SOURCE. DO NOT MODIFY. */
2/**
3 *******************************************************************************
4 * Copyright (C) 2009-2014, International Business Machines Corporation and    *
5 * others. All Rights Reserved.                                                *
6 *******************************************************************************
7 */
8package android.icu.dev.test.util;
9
10import java.util.MissingResourceException;
11
12import android.icu.dev.test.TestFmwk;
13import android.icu.impl.ICUResourceBundle;
14import android.icu.text.Collator;
15import android.icu.util.ULocale;
16import android.icu.util.UResourceBundle;
17
18
19public final class ICUResourceBundleCollationTest extends TestFmwk {
20    private static final String COLLATION_RESNAME = "collations";
21    private static final String COLLATION_KEYWORD = "collation";
22    private static final String DEFAULT_NAME = "default";
23    private static final String STANDARD_NAME = "standard";
24
25    public static void main(String args[]) throws Exception {
26        new ICUResourceBundleCollationTest().run(args);
27    }
28
29    public void TestFunctionalEquivalent(){
30       // Android patch: Add exceptions for big5han and gb2312han in genrb.
31       String[] collCases = {
32       //  avail   locale                               equiv
33           "f",     "sv_US_CALIFORNIA",                 "sv",
34       //  "f",     "zh_TW@collation=stroke",           "zh@collation=stroke", /* alias of zh_Hant_TW */
35           "f",     "zh_Hant_TW@collation=stroke",      "zh@collation=stroke",
36           "f",     "sv_CN@collation=pinyin",           "sv",
37           "t",     "zh@collation=pinyin",              "zh",
38           "f",     "zh_CN@collation=pinyin",           "zh", /* alias of zh_Hans_CN */
39           "f",     "zh_Hans_CN@collation=pinyin",      "zh",
40           "f",     "zh_HK@collation=pinyin",           "zh", /* alias of zh_Hant_HK */
41           "f",     "zh_Hant_HK@collation=pinyin",      "zh",
42           "f",     "zh_HK@collation=stroke",           "zh@collation=stroke", /* alias of zh_Hant_HK */
43           "f",     "zh_Hant_HK@collation=stroke",      "zh@collation=stroke",
44           "f",     "zh_HK",                            "zh@collation=stroke", /* alias of zh_Hant_HK */
45           "f",     "zh_Hant_HK",                       "zh@collation=stroke",
46           "f",     "zh_MO",                            "zh@collation=stroke", /* alias of zh_Hant_MO */
47           "f",     "zh_Hant_MO",                       "zh@collation=stroke",
48           "f",     "zh_TW_STROKE",                     "zh@collation=stroke",
49       //  "f",     "zh_TW_STROKE@collation=big5han",   "zh@collation=big5han",
50           "f",     "sv_CN@calendar=japanese",          "sv",
51           "t",     "sv@calendar=japanese",             "sv",
52       //  "f",     "zh_TW@collation=big5han",          "zh@collation=big5han", /* alias of zh_Hant_TW */
53       //  "f",     "zh_Hant_TW@collation=big5han",     "zh@collation=big5han",
54       //  "f",     "zh_TW@collation=gb2312han",        "zh@collation=gb2312han", /* alias of zh_Hant_TW */
55       //  "f",     "zh_Hant_TW@collation=gb2312han",   "zh@collation=gb2312han",
56       //  "f",     "zh_CN@collation=big5han",          "zh@collation=big5han", /* alias of zh_Hans_CN */
57       //  "f",     "zh_Hans_CN@collation=big5han",     "zh@collation=big5han",
58       //  "f",     "zh_CN@collation=gb2312han",        "zh@collation=gb2312han", /* alias of zh_Hans_CN */
59       //  "f",     "zh_Hans_CN@collation=gb2312han",   "zh@collation=gb2312han",
60       //  "t",     "zh@collation=big5han",             "zh@collation=big5han",
61       //  "t",     "zh@collation=gb2312han",           "zh@collation=gb2312han",
62           "t",     "hi@collation=standard",            "hi",
63           "f",     "hi_AU@collation=standard;currency=CHF;calendar=buddhist",  "hi",
64           "f",     "sv_SE@collation=pinyin",           "sv", /* bug 4582 tests */
65           "f",     "sv_SE_BONN@collation=pinyin",      "sv",
66           "t",     "nl",                               "root",
67           "f",     "nl_NL",                            "root",
68           "f",     "nl_NL_EEXT",                       "root",
69           "t",     "nl@collation=stroke",              "root",
70           "f",     "nl_NL@collation=stroke",           "root",
71           "f",     "nl_NL_EEXT@collation=stroke",      "root",
72       };
73       // Android patch end.
74
75       logln("Testing functional equivalents for collation...");
76       getFunctionalEquivalentTestCases(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
77                                        Collator.class.getClassLoader(),
78               COLLATION_RESNAME, COLLATION_KEYWORD, true, collCases);
79    }
80
81    public void TestGetWithFallback(){
82        /*
83        UResourceBundle bundle =(UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata","te_IN");
84        String key = bundle.getStringWithFallback("Keys/collation");
85        if(!key.equals("COLLATION")){
86            errln("Did not get the expected result from getStringWithFallback method.");
87        }
88        String type = bundle.getStringWithFallback("Types/collation/direct");
89        if(!type.equals("DIRECT")){
90            errln("Did not get the expected result form getStringWithFallback method.");
91        }
92        */
93        ICUResourceBundle bundle = null;
94        String key = null;
95        try{
96            bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,ULocale.canonicalize("de__PHONEBOOK"));
97
98            if(!bundle.getULocale().getName().equals("de")){
99                errln("did not get the expected bundle");
100            }
101            key = bundle.getStringWithFallback("collations/collation/default");
102            if(!key.equals("phonebook")){
103                errln("Did not get the expected result from getStringWithFallback method.");
104            }
105
106        }catch(MissingResourceException ex){
107            logln("got the expected exception");
108        }
109
110
111        bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"fr_FR");
112        key = bundle.getStringWithFallback("collations/default");
113        if(!key.equals("standard")){
114            errln("Did not get the expected result from getStringWithFallback method.");
115        }
116    }
117
118    public void TestKeywordValues(){
119        String kwVals[];
120        boolean foundStandard = false;
121        int n;
122
123        logln("Testing getting collation values:");
124        kwVals = ICUResourceBundle.getKeywordValues(ICUResourceBundle.ICU_COLLATION_BASE_NAME,COLLATION_RESNAME);
125        for(n=0;n<kwVals.length;n++) {
126            logln(new Integer(n).toString() + ": " + kwVals[n]);
127            if(DEFAULT_NAME.equals(kwVals[n])) {
128                errln("getKeywordValues for collation returned 'default' in the list.");
129            } else if(STANDARD_NAME.equals(kwVals[n])) {
130                if(foundStandard == false) {
131                    foundStandard = true;
132                    logln("found 'standard'");
133                } else {
134                    errln("Error - 'standard' is in the keyword list twice!");
135                }
136            }
137        }
138
139        if(foundStandard == false) {
140            errln("Error - 'standard' was not in the collation tree as a keyword.");
141        } else {
142            logln("'standard' was found as a collation keyword.");
143        }
144    }
145
146    public void TestOpen(){
147        UResourceBundle bundle = (UResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME, "en_US_POSIX");
148        if(bundle==null){
149            errln("could not load the stream");
150        }
151    }
152
153    private void getFunctionalEquivalentTestCases(String path, ClassLoader cl, String resName, String keyword,
154            boolean truncate, String[] testCases) {
155        //String F_STR = "f";
156        String T_STR = "t";
157        boolean isAvail[] = new boolean[1];
158
159        logln("Testing functional equivalents...");
160        for(int i = 0; i < testCases.length ;i+=3) {
161            boolean expectAvail = T_STR.equals(testCases[i+0]);
162            ULocale inLocale = new ULocale(testCases[i+1]);
163            ULocale expectLocale = new ULocale(testCases[i+2]);
164
165            logln(new Integer(i/3).toString() + ": " + new Boolean(expectAvail).toString() + "\t\t" +
166                    inLocale.toString() + "\t\t" + expectLocale.toString());
167
168            ULocale equivLocale = ICUResourceBundle.getFunctionalEquivalent(path, cl, resName, keyword, inLocale, isAvail, truncate);
169            boolean gotAvail = isAvail[0];
170
171            if((gotAvail != expectAvail) || !equivLocale.equals(expectLocale)) {
172                errln(new Integer(i/3).toString() + ":  Error, expected  Equiv=" + new Boolean(expectAvail).toString() + "\t\t" +
173                        inLocale.toString() + "\t\t--> " + expectLocale.toString() + ",  but got " + new Boolean(gotAvail).toString() + " " +
174                        equivLocale.toString());
175            }
176        }
177    }
178}
179