2 *******************************************************************************
3 * Copyright (C) 2003-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.dev.test.collator;
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;
17 import java.util.MissingResourceException;
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;
25 public class CollationServiceTest extends TestFmwk {
26 public static void main(String[] args) {
27 new CollationServiceTest().run(args);
30 public void TestRegister() {
31 // register a singleton
32 Collator frcol = Collator.getInstance(ULocale.FRANCE);
33 Collator uscol = Collator.getInstance(ULocale.US);
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");
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));
48 if (!Collator.unregister(key)) {
49 errln("failed to unregister french collator");
51 ncol = Collator.getInstance(ULocale.US);
52 if (!uscol.equals(ncol)) {
53 errln("collator after unregister does not match original");
57 ULocale fu_FU = new ULocale("fu_FU_FOO");
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");
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)) {
76 errln("new locale fu_FU not reported as supported locale");
79 String name = Collator.getDisplayName(fu_FU);
80 if (!"fu (FU, FOO)".equals(name)) {
81 errln("found " + name + " for fu_FU");
83 }catch(MissingResourceException ex){
84 warnln("Could not load locale data.");
87 String name = Collator.getDisplayName(fu_FU, fu_FU);
88 if (!"fu (FU, FOO)".equals(name)) {
89 errln("found " + name + " for fu_FU");
91 }catch(MissingResourceException ex){
92 warnln("Could not load locale data.");
95 if (!Collator.unregister(key)) {
96 errln("failed to unregister french collator");
98 ncol = Collator.getInstance(fu_FU);
99 if (!fucol.equals(ncol)) {
100 errln("collator after unregister does not match original fu_FU");
105 // coverage after return to default
106 ULocale[] locales = Collator.getAvailableULocales();
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");
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));
122 public void TestRegisterFactory() {
127 Map displayNames; // locale -> string
129 CollatorInfo(ULocale locale, Collator collator, Map displayNames) {
130 this.locale = locale;
131 this.collator = collator;
132 this.displayNames = displayNames;
135 String getDisplayName(ULocale displayLocale) {
137 if (displayNames != null) {
138 name = (String)displayNames.get(displayLocale);
141 name = locale.getDisplayName(displayLocale);
147 class TestFactory extends CollatorFactory {
151 TestFactory(CollatorInfo[] info) {
153 for (int i = 0; i < info.length; ++i) {
154 CollatorInfo ci = info[i];
155 map.put(ci.locale, ci);
159 public Collator createCollator(ULocale loc) {
160 CollatorInfo ci = (CollatorInfo)map.get(loc);
167 public String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
168 CollatorInfo ci = (CollatorInfo)map.get(objectLocale);
170 return ci.getDisplayName(displayLocale);
175 public Set getSupportedLocaleIDs() {
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();
184 ids = Collections.unmodifiableSet(set);
190 class TestFactoryWrapper extends CollatorFactory {
191 CollatorFactory delegate;
193 TestFactoryWrapper(CollatorFactory delegate) {
194 this.delegate = delegate;
197 public Collator createCollator(ULocale loc) {
198 return delegate.createCollator(loc);
201 // use CollatorFactory getDisplayName(ULocale, ULocale) for coverage
203 public Set getSupportedLocaleIDs() {
204 return delegate.getSupportedLocaleIDs();
208 ULocale fu_FU = new ULocale("fu_FU");
209 ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");
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");
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);
222 CollatorInfo[] info = {
223 new CollatorInfo(ULocale.US, frcol, null),
224 new CollatorInfo(ULocale.FRANCE, gecol, null),
225 new CollatorInfo(fu_FU, jpcol, fuFUNames),
227 TestFactory factory = null;
229 factory = new TestFactory(info);
230 }catch(MissingResourceException ex){
231 warnln("Could not load locale data.");
235 TestFactoryWrapper wrapper = new TestFactoryWrapper(factory); // in java, gc lets us easily multiply reference!
236 Object key = Collator.registerFactory(wrapper);
239 name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
240 }catch(MissingResourceException ex){
241 warnln("Could not load locale data.");
243 logln("*** default name: " + name);
244 Collator.unregister(key);
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));
253 int n1 = checkAvailable("before registerFactory");
256 Object key = Collator.registerFactory(factory);
258 int n2 = checkAvailable("after registerFactory");
260 Collator ncol = Collator.getInstance(ULocale.US);
261 if (!frcol.equals(ncol)) {
262 errln("frcoll for en_US failed");
265 ncol = Collator.getInstance(fu_FU_FOO);
266 if (!jpcol.equals(ncol)) {
267 errln("jpcol for fu_FU_FOO failed, got: " + ncol);
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)) {
279 errln("new locale fu_FU not reported as supported locale");
282 String name = Collator.getDisplayName(fu_FU);
283 if (!"little bunny Foo Foo".equals(name)) {
284 errln("found " + name + " for fu_FU");
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");
292 if (!Collator.unregister(key)) {
293 errln("failed to unregister factory");
296 int n3 = checkAvailable("after unregister");
297 assertTrue("register increases count", n2>n1);
298 assertTrue("unregister restores count", n3==n1);
300 ncol = Collator.getInstance(fu_FU);
301 if (!fucol.equals(ncol)) {
302 errln("collator after unregister does not match original fu_FU");
308 * Check the integrity of the results of Collator.getAvailableULocales().
309 * Return the number of items returned.
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);
324 private static final String KW[] = {
328 private static final String KWVAL[] = {
333 public void TestSeparateTrees() {
334 String kw[] = Collator.getKeywords();
335 if (!assertTrue("getKeywords != null", kw!=null)) return;
336 checkArray("getKeywords", kw, KW);
338 String kwval[] = Collator.getKeywordValues(KW[0]);
339 if (!assertTrue("getKeywordValues != null", kwval!=null)) return;
340 checkArray("getKeywordValues", kwval, KWVAL);
342 boolean isAvailable[] = new boolean[1];
343 ULocale equiv = Collator.getFunctionalEquivalent(KW[0],
346 if (assertTrue("getFunctionalEquivalent(de)!=null", equiv!=null)) {
347 assertEquals("getFunctionalEquivalent(de)", "root", equiv.toString());
349 assertTrue("getFunctionalEquivalent(de).isAvailable==true",
350 isAvailable[0] == true);
352 equiv = Collator.getFunctionalEquivalent(KW[0],
353 new ULocale("de_DE"),
355 if (assertTrue("getFunctionalEquivalent(de_DE)!=null", equiv!=null)) {
356 assertEquals("getFunctionalEquivalent(de_DE)", "root", equiv.toString());
358 assertTrue("getFunctionalEquivalent(de_DE).isAvailable==true",
359 isAvailable[0] == true);
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());
367 public void TestGetFunctionalEquivalent() {
368 String kw[] = Collator.getKeywords();
369 final String DATA[] = {
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",
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",
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"
400 final int DATA_COUNT=(DATA.length/3);
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));
412 logln("#" + i + ": Collator.getFunctionalEquivalent(" + input + ")=" + actual + ", avail " + new Boolean(isAvailable[0]));
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]);
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]),
439 // if (isAvailable[0] && !other.equals(base)) {
440 // logln("\t" + keywords[i] + "=" + values[j] + ";\t" + base + ";\t" + other);
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"},
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);
472 String[] pref = Collator.getKeywordValuesForLocale("collation", loc, true);
473 boolean matchPref = false;
474 if (pref.length == expected.length) {
476 for (int j = 0; j < pref.length; j++) {
477 if (!pref[j].equals(expected[j])) {
483 errln("FAIL: Preferred values for locale " + loc
484 + " got:" + Arrays.toString(pref) + " expected:" + Arrays.toString(expected));
487 String[] all = Collator.getKeywordValuesForLocale("collation", loc, true);
489 // Collator.getKeywordValues return the same contents for both commonlyUsed
491 boolean matchAll = false;
492 if (pref.length == all.length) {
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])) {
509 errln("FAIL: All values for locale " + loc
510 + " got:" + Arrays.toString(all) + " expected:" + Arrays.toString(pref));