• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 2003-2014, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 
11 package ohos.global.icu.dev.test.collator;
12 
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.MissingResourceException;
21 import java.util.Set;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import ohos.global.icu.dev.test.TestFmwk;
28 import ohos.global.icu.text.Collator;
29 import ohos.global.icu.text.Collator.CollatorFactory;
30 import ohos.global.icu.util.ULocale;
31 
32 
33 
34 @RunWith(JUnit4.class)
35 public class CollationServiceTest extends TestFmwk {
36     @Test
TestRegister()37     public void TestRegister() {
38         // register a singleton
39         Collator frcol = Collator.getInstance(ULocale.FRANCE);
40         Collator uscol = Collator.getInstance(ULocale.US);
41 
42         { // try override en_US collator
43             Object key = Collator.registerInstance(frcol, ULocale.US);
44             Collator ncol = Collator.getInstance(ULocale.US);
45             if (!frcol.equals(ncol)) {
46                 errln("register of french collator for en_US failed");
47             }
48 
49             // coverage
50             Collator test = Collator.getInstance(ULocale.GERMANY); // CollatorFactory.handleCreate
51             if (!test.getLocale(ULocale.VALID_LOCALE).equals(ULocale.GERMAN)) {
52                 errln("Collation from Germany is really " + test.getLocale(ULocale.VALID_LOCALE));
53             }
54 
55             if (!Collator.unregister(key)) {
56                 errln("failed to unregister french collator");
57             }
58             ncol = Collator.getInstance(ULocale.US);
59             if (!uscol.equals(ncol)) {
60                 errln("collator after unregister does not match original");
61             }
62         }
63 
64         ULocale fu_FU = new ULocale("fu_FU_FOO");
65 
66         { // try create collator for new locale
67             Collator fucol = Collator.getInstance(fu_FU);
68             Object key = Collator.registerInstance(frcol, fu_FU);
69             Collator ncol = Collator.getInstance(fu_FU);
70             if (!frcol.equals(ncol)) {
71                 errln("register of fr collator for fu_FU failed");
72             }
73 
74             ULocale[] locales = Collator.getAvailableULocales();
75             boolean found = false;
76             for (int i = 0; i < locales.length; ++i) {
77                 if (locales[i].equals(fu_FU)) {
78                     found = true;
79                     break;
80                 }
81             }
82             if (!found) {
83                 errln("new locale fu_FU not reported as supported locale");
84             }
85             try{
86                 String name = Collator.getDisplayName(fu_FU);
87                 if (!"fu (FU, FOO)".equals(name)
88                         && !"fu_FU_FOO".equals(name) /* no LocaleDisplayNamesImpl */) {
89                     errln("found " + name + " for fu_FU");
90                 }
91             }catch(MissingResourceException ex){
92                 warnln("Could not load locale data.");
93             }
94             try{
95                 String name = Collator.getDisplayName(fu_FU, fu_FU);
96                 if (!"fu (FU, FOO)".equals(name)
97                         && !"fu_FU_FOO".equals(name) /* no LocaleDisplayNamesImpl */) {
98                     errln("found " + name + " for fu_FU");
99                 }
100             }catch(MissingResourceException ex){
101                 warnln("Could not load locale data.");
102             }
103 
104             if (!Collator.unregister(key)) {
105                 errln("failed to unregister french collator");
106             }
107             ncol = Collator.getInstance(fu_FU);
108             if (!fucol.equals(ncol)) {
109                 errln("collator after unregister does not match original fu_FU");
110             }
111         }
112 
113         {
114             // coverage after return to default
115             ULocale[] locales = Collator.getAvailableULocales();
116 
117             for (int i = 0; i < locales.length; ++i) {
118                 if (locales[i].equals(fu_FU)) {
119                     errln("new locale fu_FU not reported as supported locale");
120                     break;
121                 }
122             }
123 
124             Collator ncol = Collator.getInstance(ULocale.US);
125             if (!ncol.getLocale(ULocale.VALID_LOCALE).equals(ULocale.US)) {
126                 errln("Collation from US is really " + ncol.getLocale(ULocale.VALID_LOCALE));
127             }
128         }
129     }
130 
131     @Test
TestRegisterFactory()132     public void TestRegisterFactory() {
133 
134         class CollatorInfo {
135             ULocale locale;
136             Collator collator;
137             Map displayNames; // locale -> string
138 
139             CollatorInfo(ULocale locale, Collator collator, Map displayNames) {
140                 this.locale = locale;
141                 this.collator = collator;
142                 this.displayNames = displayNames;
143             }
144 
145             String getDisplayName(ULocale displayLocale) {
146                 String name = null;
147                 if (displayNames != null) {
148                     name = (String)displayNames.get(displayLocale);
149                 }
150                 if (name == null) {
151                     name = locale.getDisplayName(displayLocale);
152                 }
153                 return name;
154             }
155         }
156 
157         class TestFactory extends CollatorFactory {
158             private Map map;
159             private Set ids;
160 
161             TestFactory(CollatorInfo[] info) {
162                 map = new HashMap();
163                 for (int i = 0; i < info.length; ++i) {
164                     CollatorInfo ci = info[i];
165                     map.put(ci.locale, ci);
166                 }
167             }
168 
169             @Override
170             public Collator createCollator(ULocale loc) {
171                 CollatorInfo ci = (CollatorInfo)map.get(loc);
172                 if (ci != null) {
173                     return ci.collator;
174                 }
175                 return null;
176             }
177 
178             @Override
179             public String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
180                 CollatorInfo ci = (CollatorInfo)map.get(objectLocale);
181                 if (ci != null) {
182                     return ci.getDisplayName(displayLocale);
183                 }
184                 return null;
185             }
186 
187             @Override
188             public Set getSupportedLocaleIDs() {
189                 if (ids == null) {
190                     HashSet set = new HashSet();
191                     Iterator iter = map.keySet().iterator();
192                     while (iter.hasNext()) {
193                         ULocale locale = (ULocale)iter.next();
194                         String id = locale.toString();
195                         set.add(id);
196                     }
197                     ids = Collections.unmodifiableSet(set);
198                 }
199                 return ids;
200             }
201         }
202 
203         class TestFactoryWrapper extends CollatorFactory {
204             CollatorFactory delegate;
205 
206             TestFactoryWrapper(CollatorFactory delegate) {
207                 this.delegate = delegate;
208             }
209 
210             @Override
211             public Collator createCollator(ULocale loc) {
212                 return delegate.createCollator(loc);
213             }
214 
215             // use CollatorFactory getDisplayName(ULocale, ULocale) for coverage
216 
217             @Override
218             public Set getSupportedLocaleIDs() {
219                 return delegate.getSupportedLocaleIDs();
220             }
221         }
222 
223         ULocale fu_FU = new ULocale("fu_FU");
224         ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");
225 
226         Map fuFUNames = new HashMap();
227         fuFUNames.put(fu_FU, "ze leetle bunny Fu-Fu");
228         fuFUNames.put(fu_FU_FOO, "zee leetel bunny Foo-Foo");
229         fuFUNames.put(ULocale.US, "little bunny Foo Foo");
230 
231         Collator frcol = Collator.getInstance(ULocale.FRANCE);
232        /* Collator uscol = */Collator.getInstance(ULocale.US);
233         Collator gecol = Collator.getInstance(ULocale.GERMANY);
234         Collator jpcol = Collator.getInstance(ULocale.JAPAN);
235         Collator fucol = Collator.getInstance(fu_FU);
236 
237         CollatorInfo[] info = {
238             new CollatorInfo(ULocale.US, frcol, null),
239             new CollatorInfo(ULocale.FRANCE, gecol, null),
240             new CollatorInfo(fu_FU, jpcol, fuFUNames),
241         };
242         TestFactory factory = null;
243         try{
244             factory = new TestFactory(info);
245         }catch(MissingResourceException ex){
246             warnln("Could not load locale data.");
247         }
248         // coverage
249         {
250             TestFactoryWrapper wrapper = new TestFactoryWrapper(factory); // in java, gc lets us easily multiply reference!
251             Object key = Collator.registerFactory(wrapper);
252             String name = null;
253             try{
254                 name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
255             }catch(MissingResourceException ex){
256                 warnln("Could not load locale data.");
257             }
258             logln("*** default name: " + name);
259             Collator.unregister(key);
260 
261             ULocale bar_BAR = new ULocale("bar_BAR");
262             Collator col = Collator.getInstance(bar_BAR);
263             ULocale valid = col.getLocale(ULocale.VALID_LOCALE);
264             String validName = valid.getName();
265             if(validName.length() != 0 && !validName.equals("root")) {
266                 errln("Collation from bar_BAR is really \"" + validName + "\" but should be root");
267             }
268         }
269 
270         int n1 = checkAvailable("before registerFactory");
271 
272         {
273             Object key = Collator.registerFactory(factory);
274 
275             int n2 = checkAvailable("after registerFactory");
276 
277             Collator ncol = Collator.getInstance(ULocale.US);
278             if (!frcol.equals(ncol)) {
279                 errln("frcoll for en_US failed");
280             }
281 
282             ncol = Collator.getInstance(fu_FU_FOO);
283             if (!jpcol.equals(ncol)) {
284                 errln("jpcol for fu_FU_FOO failed, got: " + ncol);
285             }
286 
287             ULocale[] locales = Collator.getAvailableULocales();
288             boolean found = false;
289             for (int i = 0; i < locales.length; ++i) {
290                 if (locales[i].equals(fu_FU)) {
291                     found = true;
292                     break;
293                 }
294             }
295             if (!found) {
296                 errln("new locale fu_FU not reported as supported locale");
297             }
298 
299             String name = Collator.getDisplayName(fu_FU);
300             if (!"little bunny Foo Foo".equals(name)) {
301                 errln("found " + name + " for fu_FU");
302             }
303 
304             name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
305             if (!"zee leetel bunny Foo-Foo".equals(name)) {
306                 errln("found " + name + " for fu_FU in fu_FU_FOO");
307             }
308 
309             if (!Collator.unregister(key)) {
310                 errln("failed to unregister factory");
311             }
312 
313             int n3 = checkAvailable("after unregister");
314             assertTrue("register increases count", n2>n1);
315             assertTrue("unregister restores count", n3==n1);
316 
317             ncol = Collator.getInstance(fu_FU);
318             if (!fucol.equals(ncol)) {
319                 errln("collator after unregister does not match original fu_FU");
320             }
321         }
322     }
323 
324     /**
325      * Check the integrity of the results of Collator.getAvailableULocales().
326      * Return the number of items returned.
327      */
checkAvailable(String msg)328     int checkAvailable(String msg) {
329         Locale locs[] = Collator.getAvailableLocales();
330         if (!assertTrue("getAvailableLocales != null", locs!=null)) return -1;
331         checkArray(msg, locs, null);
332         ULocale ulocs[] = Collator.getAvailableULocales();
333         if (!assertTrue("getAvailableULocales != null", ulocs!=null)) return -1;
334         checkArray(msg, ulocs, null);
335         // This is not true because since ULocale objects with script code cannot be
336         // converted to Locale objects
337         //assertTrue("getAvailableLocales().length == getAvailableULocales().length", locs.length == ulocs.length);
338         return locs.length;
339     }
340 
341     private static final String KW[] = {
342         "collation"
343     };
344 
345     private static final String KWVAL[] = {
346         "phonebook",
347         "stroke"
348     };
349 
350     @Test
TestSeparateTrees()351     public void TestSeparateTrees() {
352         String kw[] = Collator.getKeywords();
353         if (!assertTrue("getKeywords != null", kw!=null)) return;
354         checkArray("getKeywords", kw, KW);
355 
356         String kwval[] = Collator.getKeywordValues(KW[0]);
357         if (!assertTrue("getKeywordValues != null", kwval!=null)) return;
358         checkArray("getKeywordValues", kwval, KWVAL);
359 
360         boolean isAvailable[] = new boolean[1];
361         ULocale equiv = Collator.getFunctionalEquivalent(KW[0],
362                                                          new ULocale("de"),
363                                                          isAvailable);
364         if (assertTrue("getFunctionalEquivalent(de)!=null", equiv!=null)) {
365             assertEquals("getFunctionalEquivalent(de)", "", equiv.toString());
366         }
367         assertTrue("getFunctionalEquivalent(de).isAvailable==true",
368                    isAvailable[0] == true);
369 
370         equiv = Collator.getFunctionalEquivalent(KW[0],
371                                                  new ULocale("de_DE"),
372                                                  isAvailable);
373         if (assertTrue("getFunctionalEquivalent(de_DE)!=null", equiv!=null)) {
374             assertEquals("getFunctionalEquivalent(de_DE)", "", equiv.toString());
375         }
376         assertTrue("getFunctionalEquivalent(de_DE).isAvailable==false",
377                    isAvailable[0] == false);
378 
379         equiv = Collator.getFunctionalEquivalent(KW[0], new ULocale("zh_Hans"));
380         if (assertTrue("getFunctionalEquivalent(zh_Hans)!=null", equiv!=null)) {
381             assertEquals("getFunctionalEquivalent(zh_Hans)", "zh", equiv.toString());
382         }
383     }
384 
385     @Test
TestGetFunctionalEquivalent()386     public void TestGetFunctionalEquivalent() {
387         String kw[] = Collator.getKeywords();
388         final String DATA[] = {
389                           "sv", "sv", "t",
390                           "sv@collation=direct", "sv", "t",
391                           "sv@collation=traditional", "sv", "t",
392                           "sv@collation=gb2312han", "sv", "t",
393                           "sv@collation=stroke", "sv", "t",
394                           "sv@collation=pinyin", "sv", "t",
395                           "sv@collation=standard", "sv@collation=standard", "t",
396                           "sv@collation=reformed", "sv", "t",
397                           "sv@collation=big5han", "sv", "t",
398                           "sv_FI", "sv", "f",
399                           "sv_FI@collation=direct", "sv", "f",
400                           "sv_FI@collation=traditional", "sv", "f",
401                           "sv_FI@collation=gb2312han", "sv", "f",
402                           "sv_FI@collation=stroke", "sv", "f",
403                           "sv_FI@collation=pinyin", "sv", "f",
404                           "sv_FI@collation=standard", "sv@collation=standard", "f",
405                           "sv_FI@collation=reformed", "sv", "f",
406                           "sv_FI@collation=big5han", "sv", "f",
407                           "nl", "root", "t",
408                           "nl@collation=direct", "root", "t",
409                           "nl_BE", "root", "f",
410                           "nl_BE@collation=direct", "root", "f",
411                           "nl_BE@collation=traditional", "root", "f",
412                           "nl_BE@collation=gb2312han", "root", "f",
413                           "nl_BE@collation=stroke", "root", "f",
414                           "nl_BE@collation=pinyin", "root", "f",
415                           "nl_BE@collation=big5han", "root", "f",
416                           "nl_BE@collation=phonebook", "root", "f",
417                           "en_US_VALLEYGIRL","root","f"
418                         };
419         final int DATA_COUNT=(DATA.length/3);
420 
421         for(int i=0;i<DATA_COUNT;i++) {
422             boolean isAvailable[] = new boolean[1];
423             ULocale input = new ULocale(DATA[(i*3)+0]);
424             ULocale expect = new ULocale(DATA[(i*3)+1]);
425             boolean expectAvailable = DATA[(i*3)+2].equals("t");
426             ULocale actual = Collator.getFunctionalEquivalent(kw[0],input,isAvailable);
427             if(!actual.equals(expect) || (expectAvailable!=isAvailable[0])) {
428                 errln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + new Boolean(isAvailable[0]) + ", " +
429                         "expected " + expect + " avail " + new Boolean(expectAvailable));
430             } else {
431                 logln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + new Boolean(isAvailable[0]));
432             }
433         }
434     }
435 
436 //    public void PrintFunctionalEquivalentList() {
437 //        ULocale[] locales = Collator.getAvailableULocales();
438 //        String[] keywords = Collator.getKeywords();
439 //        logln("Collation");
440 //        logln("Possible keyword=values pairs:");
441 //        for (int i = 0; i < Collator.getKeywords().length; ++i) {
442 //                String[] values = Collator.getKeywordValues(keywords[i]);
443 //                for (int j = 0; j < values.length; ++j) {
444 //                        System.out.println(keywords[i] + "=" + values[j]);
445 //                }
446 //        }
447 //        logln("Differing Collators:");
448 //        boolean[] isAvailable = {true};
449 //        for (int k = 0; k < locales.length; ++k) {
450 //                logln(locales[k].getDisplayName(ULocale.ENGLISH) + " [" +locales[k] + "]");
451 //                for (int i = 0; i < Collator.getKeywords().length; ++i) {
452 //                        ULocale base = Collator.getFunctionalEquivalent(keywords[i],locales[k]);
453 //                        String[] values = Collator.getKeywordValues(keywords[i]);
454 //                        for (int j = 0; j < Collator.getKeywordValues(keywords[i]).length;++j) {
455 //                                ULocale other = Collator.getFunctionalEquivalent(keywords[i],
456 //                                        new ULocale(locales[k] + "@" + keywords[i] + "=" + values[j]),
457 //                                        isAvailable);
458 //                                if (isAvailable[0] && !other.equals(base)) {
459 //                                        logln("\t" + keywords[i] + "=" + values[j] + ";\t" + base + ";\t" + other);
460 //                                }
461 //                        }
462 //                }
463 //        }
464 //    }
465 
arrayContains(String[] array, String s)466     private static boolean arrayContains(String[] array, String s) {
467         for (int i = 0; i < array.length; ++i) {
468             if (s.equals(array[i])) {
469                 return true;
470             }
471         }
472         return false;
473     }
474 
475     @Test
TestGetKeywordValues()476     public void TestGetKeywordValues(){
477         final String[][] PREFERRED = {
478             {"und",             "standard", "eor", "search"},
479             {"en_US",           "standard", "eor", "search"},
480             {"en_029",          "standard", "eor", "search"},
481             {"de_DE",           "standard", "phonebook", "search", "eor"},
482             {"de_Latn_DE",      "standard", "phonebook", "search", "eor"},
483             {"zh",              "pinyin", "stroke", "eor", "search", "standard"},
484             {"zh_Hans",         "pinyin", "stroke", "eor", "search", "standard"},
485             {"zh_CN",           "pinyin", "stroke", "eor", "search", "standard"},
486             {"zh_Hant",         "stroke", "pinyin", "eor", "search", "standard"},
487             {"zh_TW",           "stroke", "pinyin", "eor", "search", "standard"},
488             {"zh__PINYIN",      "pinyin", "stroke", "eor", "search", "standard"},
489             {"es_ES",           "standard", "search", "traditional", "eor"},
490             {"es__TRADITIONAL", "traditional", "search", "standard", "eor"},
491             {"und@collation=phonebook",     "standard", "eor", "search"},
492             {"de_DE@collation=big5han",     "standard", "phonebook", "search", "eor"},
493             {"zzz@collation=xxx",           "standard", "eor", "search"},
494         };
495 
496         for (int i = 0; i < PREFERRED.length; i++) {
497             String locale = PREFERRED[i][0];
498             ULocale loc = new ULocale(locale);
499             String[] expected = PREFERRED[i];
500             String[] pref = Collator.getKeywordValuesForLocale("collation", loc, true);
501             for (int j = 1; j < expected.length; ++j) {
502                 if (!arrayContains(pref, expected[j])) {
503                     errln("Keyword value " + expected[j] + " missing for locale: " + locale);
504                 }
505             }
506 
507             // Collator.getKeywordValues return the same contents for both commonlyUsed
508             // true and false.
509             String[] all = Collator.getKeywordValuesForLocale("collation", loc, false);
510             boolean matchAll = false;
511             if (pref.length == all.length) {
512                 matchAll = true;
513                 for (int j = 0; j < pref.length; j++) {
514                     boolean foundMatch = false;
515                     for (int k = 0; k < all.length; k++) {
516                         if (pref[j].equals(all[k])) {
517                             foundMatch = true;
518                             break;
519                         }
520                     }
521                     if (!foundMatch) {
522                         matchAll = false;
523                         break;
524                     }
525                 }
526             }
527             if (!matchAll) {
528                 errln("FAIL: All values for locale " + loc
529                         + " got:" + Arrays.toString(all) + " expected:" + Arrays.toString(pref));
530             }
531         }
532     }
533 }
534