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