]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationServiceTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationServiceTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2010, 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)", "de", 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)", "de", 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                           "de", "de", "t",
371                           "de@collation=direct", "de", "t",
372                           "de@collation=traditional", "de", "t",
373                           "de@collation=gb2312han", "de", "t",
374                           "de@collation=stroke", "de", "t",
375                           "de@collation=pinyin", "de", "t",
376                           "de@collation=phonebook", "de@collation=phonebook", "t",
377                           "de@collation=big5han", "de", "t",
378                           "de_AT", "de", "t",
379                           "de_AT@collation=direct", "de", "t",
380                           "de_AT@collation=traditional", "de", "t",
381                           "de_AT@collation=gb2312han", "de", "t",
382                           "de_AT@collation=stroke", "de", "t",
383                           "de_AT@collation=pinyin", "de", "t",
384                           "de_AT@collation=phonebook", "de@collation=phonebook", "t",
385                           "de_AT@collation=big5han", "de", "t",
386                           "nl", "root", "t",
387                           "nl@collation=direct", "root", "t",
388                           "nl_BE", "root", "t",
389                           "nl_BE@collation=direct", "root", "t",
390                           "nl_BE@collation=traditional", "root", "t",
391                           "nl_BE@collation=gb2312han", "root", "t",
392                           "nl_BE@collation=stroke", "root", "t",
393                           "nl_BE@collation=pinyin", "root", "t",
394                           "nl_BE@collation=big5han", "root", "t",
395                           "nl_BE@collation=phonebook", "root", "t",
396                           "en_US_VALLEYGIRL","root","f"
397                         };
398         final int DATA_COUNT=(DATA.length/3);
399         
400         for(int i=0;i<DATA_COUNT;i++) {
401             boolean isAvailable[] = new boolean[1];
402             ULocale input = new ULocale(DATA[(i*3)+0]);
403             ULocale expect = new ULocale(DATA[(i*3)+1]);
404             boolean expectAvailable = DATA[(i*3)+2].equals("t");
405             ULocale actual = Collator.getFunctionalEquivalent(kw[0],input,isAvailable);
406             if(!actual.equals(expect) || (expectAvailable!=isAvailable[0])) {
407                 errln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + new Boolean(isAvailable[0]) + ", " +
408                         "expected " + expect + " avail " + new Boolean(expectAvailable));
409             } else {
410                 logln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + new Boolean(isAvailable[0]));
411             }
412         }
413     }
414
415 //    public void PrintFunctionalEquivalentList() {
416 //        ULocale[] locales = Collator.getAvailableULocales();
417 //        String[] keywords = Collator.getKeywords();
418 //        logln("Collation");
419 //        logln("Possible keyword=values pairs:");
420 //        for (int i = 0; i < Collator.getKeywords().length; ++i) {
421 //                String[] values = Collator.getKeywordValues(keywords[i]);
422 //                for (int j = 0; j < values.length; ++j) {
423 //                        System.out.println(keywords[i] + "=" + values[j]);
424 //                }
425 //        }
426 //        logln("Differing Collators:");
427 //        boolean[] isAvailable = {true};
428 //        for (int k = 0; k < locales.length; ++k) {
429 //                logln(locales[k].getDisplayName(ULocale.ENGLISH) + " [" +locales[k] + "]");
430 //                for (int i = 0; i < Collator.getKeywords().length; ++i) {
431 //                        ULocale base = Collator.getFunctionalEquivalent(keywords[i],locales[k]);
432 //                        String[] values = Collator.getKeywordValues(keywords[i]);
433 //                        for (int j = 0; j < Collator.getKeywordValues(keywords[i]).length;++j) {                          
434 //                                ULocale other = Collator.getFunctionalEquivalent(keywords[i], 
435 //                                        new ULocale(locales[k] + "@" + keywords[i] + "=" + values[j]),
436 //                                        isAvailable);
437 //                                if (isAvailable[0] && !other.equals(base)) {
438 //                                        logln("\t" + keywords[i] + "=" + values[j] + ";\t" + base + ";\t" + other);
439 //                                }
440 //                        }
441 //                }
442 //        }
443 //    }
444     
445     public void TestGetKeywordValues(){
446         final String[][] PREFERRED = {
447             {"und",             "standard", "ducet", "search"},
448             {"en_US",           "standard", "ducet", "search"},
449             {"en_029",          "standard", "ducet", "search"},
450             {"de_DE",           "standard", "phonebook", "search", "ducet"},
451             {"de_Latn_DE",      "standard", "phonebook", "search", "ducet"},
452             {"zh",              "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search"},
453             {"zh_Hans",         "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search"},
454             {"zh_CN",           "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search"},
455             {"zh_Hant",         "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search"},
456             {"zh_TW",           "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search"},
457             {"zh__PINYIN",      "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search"},
458             {"es_ES",           "standard", "search", "traditional", "ducet"},
459             {"es__TRADITIONAL", "traditional", "search", "standard", "ducet"},
460             {"und@collation=phonebook",     "standard", "ducet", "search"},
461             {"de_DE@collation=big5han",     "standard", "phonebook", "search", "ducet"},
462             {"zzz@collation=xxx",           "standard", "ducet", "search"},
463         };
464
465         for (int i = 0; i < PREFERRED.length; i++) {
466             ULocale loc = new ULocale(PREFERRED[i][0]);
467             String[] expected = new String[PREFERRED[i].length - 1];
468             System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);
469
470             String[] pref = Collator.getKeywordValuesForLocale("collation", loc, true);
471             boolean matchPref = false;
472             if (pref.length == expected.length) {
473                 matchPref = true;
474                 for (int j = 0; j < pref.length; j++) {
475                     if (!pref[j].equals(expected[j])) {
476                         matchPref = false;
477                     }
478                 }
479             }
480             if (!matchPref) {
481                 errln("FAIL: Preferred values for locale " + loc 
482                         + " got:" + Arrays.toString(pref) + " expected:" + Arrays.toString(expected));
483             }
484  
485             String[] all = Collator.getKeywordValuesForLocale("collation", loc, true);
486
487             // Collator.getKeywordValues return the same contents for both commonlyUsed
488             // true and false.
489             boolean matchAll = false;
490             if (pref.length == all.length) {
491                 matchAll = true;
492                 for (int j = 0; j < pref.length; j++) {
493                     boolean foundMatch = false;
494                     for (int k = 0; k < all.length; k++) {
495                         if (pref[j].equals(all[k])) {
496                             foundMatch = true;
497                             break;
498                         }
499                     }
500                     if (!foundMatch) {
501                         matchAll = false;
502                         break;
503                     }
504                 }
505             }
506             if (!matchAll) {
507                 errln("FAIL: All values for locale " + loc
508                         + " got:" + Arrays.toString(all) + " expected:" + Arrays.toString(pref));
509             }
510         }
511     }
512 }