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