001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.dev.test.collator;
009:
010: import java.util.Collections;
011: import java.util.Locale;
012: import java.util.HashMap;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Set;
017: import java.util.MissingResourceException;
018: import com.ibm.icu.dev.test.TestFmwk;
019: import com.ibm.icu.text.Collator;
020: import com.ibm.icu.text.Collator.CollatorFactory;
021: import com.ibm.icu.util.ULocale;
022:
023: public class CollationServiceTest extends TestFmwk {
024: public static void main(String[] args) {
025: new CollationServiceTest().run(args);
026: }
027:
028: public void TestRegister() {
029: // register a singleton
030: Collator frcol = Collator.getInstance(ULocale.FRANCE);
031: Collator uscol = Collator.getInstance(ULocale.US);
032:
033: { // try override en_US collator
034: Object key = Collator.registerInstance(frcol, ULocale.US);
035: Collator ncol = Collator.getInstance(ULocale.US);
036: if (!frcol.equals(ncol)) {
037: errln("register of french collator for en_US failed");
038: }
039:
040: // coverage
041: Collator test = Collator.getInstance(ULocale.GERMANY); // CollatorFactory.handleCreate
042:
043: if (!Collator.unregister(key)) {
044: errln("failed to unregister french collator");
045: }
046: ncol = Collator.getInstance(ULocale.US);
047: if (!uscol.equals(ncol)) {
048: errln("collator after unregister does not match original");
049: }
050: }
051:
052: ULocale fu_FU = new ULocale("fu_FU_FOO");
053:
054: { // try create collator for new locale
055: Collator fucol = Collator.getInstance(fu_FU);
056: Object key = Collator.registerInstance(frcol, fu_FU);
057: Collator ncol = Collator.getInstance(fu_FU);
058: if (!frcol.equals(ncol)) {
059: errln("register of fr collator for fu_FU failed");
060: }
061:
062: ULocale[] locales = Collator.getAvailableULocales();
063: boolean found = false;
064: for (int i = 0; i < locales.length; ++i) {
065: if (locales[i].equals(fu_FU)) {
066: found = true;
067: break;
068: }
069: }
070: if (!found) {
071: errln("new locale fu_FU not reported as supported locale");
072: }
073: try {
074: String name = Collator.getDisplayName(fu_FU);
075: if (!"fu (FU, FOO)".equals(name)) {
076: errln("found " + name + " for fu_FU");
077: }
078: } catch (MissingResourceException ex) {
079: warnln("Could not load locale data.");
080: }
081: try {
082: String name = Collator.getDisplayName(fu_FU, fu_FU);
083: if (!"fu (FU, FOO)".equals(name)) {
084: errln("found " + name + " for fu_FU");
085: }
086: } catch (MissingResourceException ex) {
087: warnln("Could not load locale data.");
088: }
089:
090: if (!Collator.unregister(key)) {
091: errln("failed to unregister french collator");
092: }
093: ncol = Collator.getInstance(fu_FU);
094: if (!fucol.equals(ncol)) {
095: errln("collator after unregister does not match original fu_FU");
096: }
097: }
098:
099: {
100: // coverage after return to default
101: ULocale[] locales = Collator.getAvailableULocales();
102:
103: Collator ncol = Collator.getInstance(ULocale.US);
104: }
105: }
106:
107: public void TestRegisterFactory() {
108:
109: class CollatorInfo {
110: ULocale locale;
111: Collator collator;
112: Map displayNames; // locale -> string
113:
114: CollatorInfo(ULocale locale, Collator collator,
115: Map displayNames) {
116: this .locale = locale;
117: this .collator = collator;
118: this .displayNames = displayNames;
119: }
120:
121: String getDisplayName(ULocale displayLocale) {
122: String name = null;
123: if (displayNames != null) {
124: name = (String) displayNames.get(displayLocale);
125: }
126: if (name == null) {
127: name = locale.getDisplayName(displayLocale);
128: }
129: return name;
130: }
131: }
132:
133: class TestFactory extends CollatorFactory {
134: private Map map;
135: private Set ids;
136:
137: TestFactory(CollatorInfo[] info) {
138: map = new HashMap();
139: for (int i = 0; i < info.length; ++i) {
140: CollatorInfo ci = info[i];
141: map.put(ci.locale, ci);
142: }
143: }
144:
145: public Collator createCollator(ULocale loc) {
146: CollatorInfo ci = (CollatorInfo) map.get(loc);
147: if (ci != null) {
148: return ci.collator;
149: }
150: return null;
151: }
152:
153: public String getDisplayName(ULocale objectLocale,
154: ULocale displayLocale) {
155: CollatorInfo ci = (CollatorInfo) map.get(objectLocale);
156: if (ci != null) {
157: return ci.getDisplayName(displayLocale);
158: }
159: return null;
160: }
161:
162: public Set getSupportedLocaleIDs() {
163: if (ids == null) {
164: HashSet set = new HashSet();
165: Iterator iter = map.keySet().iterator();
166: while (iter.hasNext()) {
167: ULocale locale = (ULocale) iter.next();
168: String id = locale.toString();
169: set.add(id);
170: }
171: ids = Collections.unmodifiableSet(set);
172: }
173: return ids;
174: }
175: }
176:
177: class TestFactoryWrapper extends CollatorFactory {
178: CollatorFactory delegate;
179:
180: TestFactoryWrapper(CollatorFactory delegate) {
181: this .delegate = delegate;
182: }
183:
184: public Collator createCollator(ULocale loc) {
185: return delegate.createCollator(loc);
186: }
187:
188: // use CollatorFactory getDisplayName(ULocale, ULocale) for coverage
189:
190: public Set getSupportedLocaleIDs() {
191: return delegate.getSupportedLocaleIDs();
192: }
193: }
194:
195: ULocale fu_FU = new ULocale("fu_FU");
196: ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");
197:
198: Map fuFUNames = new HashMap();
199: fuFUNames.put(fu_FU, "ze leetle bunny Fu-Fu");
200: fuFUNames.put(fu_FU_FOO, "zee leetel bunny Foo-Foo");
201: fuFUNames.put(ULocale.US, "little bunny Foo Foo");
202:
203: Collator frcol = Collator.getInstance(ULocale.FRANCE);
204: /* Collator uscol = */Collator.getInstance(ULocale.US);
205: Collator gecol = Collator.getInstance(ULocale.GERMANY);
206: Collator jpcol = Collator.getInstance(ULocale.JAPAN);
207: Collator fucol = Collator.getInstance(fu_FU);
208:
209: CollatorInfo[] info = {
210: new CollatorInfo(ULocale.US, frcol, null),
211: new CollatorInfo(ULocale.FRANCE, gecol, null),
212: new CollatorInfo(fu_FU, jpcol, fuFUNames), };
213: TestFactory factory = null;
214: try {
215: factory = new TestFactory(info);
216: } catch (MissingResourceException ex) {
217: warnln("Could not load locale data.");
218: }
219: // coverage
220: {
221: TestFactoryWrapper wrapper = new TestFactoryWrapper(factory); // in java, gc lets us easily multiply reference!
222: Object key = Collator.registerFactory(wrapper);
223: String name = null;
224: try {
225: name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
226: } catch (MissingResourceException ex) {
227: warnln("Could not load locale data.");
228: }
229: logln("*** default name: " + name);
230: Collator.unregister(key);
231:
232: Collator col = Collator.getInstance(new ULocale("bar_BAR"));
233: }
234:
235: int n1 = checkAvailable("before registerFactory");
236:
237: {
238: Object key = Collator.registerFactory(factory);
239:
240: int n2 = checkAvailable("after registerFactory");
241:
242: Collator ncol = Collator.getInstance(ULocale.US);
243: if (!frcol.equals(ncol)) {
244: errln("frcoll for en_US failed");
245: }
246:
247: ncol = Collator.getInstance(fu_FU_FOO);
248: if (!jpcol.equals(ncol)) {
249: errln("jpcol for fu_FU_FOO failed, got: " + ncol);
250: }
251:
252: ULocale[] locales = Collator.getAvailableULocales();
253: boolean found = false;
254: for (int i = 0; i < locales.length; ++i) {
255: if (locales[i].equals(fu_FU)) {
256: found = true;
257: break;
258: }
259: }
260: if (!found) {
261: errln("new locale fu_FU not reported as supported locale");
262: }
263:
264: String name = Collator.getDisplayName(fu_FU);
265: if (!"little bunny Foo Foo".equals(name)) {
266: errln("found " + name + " for fu_FU");
267: }
268:
269: name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
270: if (!"zee leetel bunny Foo-Foo".equals(name)) {
271: errln("found " + name + " for fu_FU in fu_FU_FOO");
272: }
273:
274: if (!Collator.unregister(key)) {
275: errln("failed to unregister factory");
276: }
277:
278: int n3 = checkAvailable("after unregister");
279: assertTrue("register increases count", n2 > n1);
280: assertTrue("unregister restores count", n3 == n1);
281:
282: ncol = Collator.getInstance(fu_FU);
283: if (!fucol.equals(ncol)) {
284: errln("collator after unregister does not match original fu_FU");
285: }
286: }
287: }
288:
289: /**
290: * Check the integrity of the results of Collator.getAvailableULocales().
291: * Return the number of items returned.
292: */
293: int checkAvailable(String msg) {
294: Locale locs[] = Collator.getAvailableLocales();
295: if (!assertTrue("getAvailableLocales != null", locs != null))
296: return -1;
297: checkArray(msg, locs, null);
298: ULocale ulocs[] = Collator.getAvailableULocales();
299: if (!assertTrue("getAvailableULocales != null", ulocs != null))
300: return -1;
301: checkArray(msg, ulocs, null);
302: // This is not true because since ULocale objects with script code cannot be
303: // converted to Locale objects
304: //assertTrue("getAvailableLocales().length == getAvailableULocales().length", locs.length == ulocs.length);
305: return locs.length;
306: }
307:
308: private static final String KW[] = { "collation" };
309:
310: private static final String KWVAL[] = { "phonebook", "stroke" };
311:
312: public void TestSeparateTrees() {
313: String kw[] = Collator.getKeywords();
314: if (!assertTrue("getKeywords != null", kw != null))
315: return;
316: checkArray("getKeywords", kw, KW);
317:
318: String kwval[] = Collator.getKeywordValues(KW[0]);
319: if (!assertTrue("getKeywordValues != null", kwval != null))
320: return;
321: checkArray("getKeywordValues", kwval, KWVAL);
322:
323: boolean isAvailable[] = new boolean[1];
324: ULocale equiv = Collator.getFunctionalEquivalent(KW[0],
325: new ULocale("fr"), isAvailable);
326: if (assertTrue("getFunctionalEquivalent(fr)!=null",
327: equiv != null)) {
328: assertEquals("getFunctionalEquivalent(fr)", "fr", equiv
329: .toString());
330: }
331: assertTrue("getFunctionalEquivalent(fr).isAvailable==true",
332: isAvailable[0] == true);
333:
334: equiv = Collator.getFunctionalEquivalent(KW[0], new ULocale(
335: "fr_FR"), isAvailable);
336: if (assertTrue("getFunctionalEquivalent(fr_FR)!=null",
337: equiv != null)) {
338: assertEquals("getFunctionalEquivalent(fr_FR)", "fr", equiv
339: .toString());
340: }
341: assertTrue("getFunctionalEquivalent(fr_FR).isAvailable==true",
342: isAvailable[0] == true);
343:
344: equiv = Collator.getFunctionalEquivalent(KW[0], new ULocale(
345: "zh_Hans"));
346: if (assertTrue("getFunctionalEquivalent(zh_Hans)!=null",
347: equiv != null)) {
348: assertEquals("getFunctionalEquivalent(zh_Hans)", "zh",
349: equiv.toString());
350: }
351: }
352:
353: public void TestGetFunctionalEquivalent() {
354: String kw[] = Collator.getKeywords();
355: final String DATA[] = { "de", "de", "t", "de@collation=direct",
356: "de", "t", "de@collation=traditional", "de", "t",
357: "de@collation=gb2312han", "de", "t",
358: "de@collation=stroke", "de", "t",
359: "de@collation=pinyin", "de", "t",
360: "de@collation=phonebook", "de@collation=phonebook",
361: "t", "de@collation=big5han", "de", "t", "de_AT", "de",
362: "t", "de_AT@collation=direct", "de", "t",
363: "de_AT@collation=traditional", "de", "t",
364: "de_AT@collation=gb2312han", "de", "t",
365: "de_AT@collation=stroke", "de", "t",
366: "de_AT@collation=pinyin", "de", "t",
367: "de_AT@collation=phonebook", "de@collation=phonebook",
368: "t", "de_AT@collation=big5han", "de", "t", "nl",
369: "root", "t", "nl@collation=direct", "root", "t",
370: "nl_BE", "root", "t", "nl_BE@collation=direct", "root",
371: "t", "nl_BE@collation=traditional", "root", "t",
372: "nl_BE@collation=gb2312han", "root", "t",
373: "nl_BE@collation=stroke", "root", "t",
374: "nl_BE@collation=pinyin", "root", "t",
375: "nl_BE@collation=big5han", "root", "t",
376: "nl_BE@collation=phonebook", "root", "t",
377: "en_US_VALLEYGIRL", "en", "f" };
378: final int DATA_COUNT = (DATA.length / 3);
379:
380: for (int i = 0; i < DATA_COUNT; i++) {
381: boolean isAvailable[] = new boolean[1];
382: ULocale input = new ULocale(DATA[(i * 3) + 0]);
383: ULocale expect = new ULocale(DATA[(i * 3) + 1]);
384: boolean expectAvailable = DATA[(i * 3) + 2].equals("t");
385: ULocale actual = Collator.getFunctionalEquivalent(kw[0],
386: input, isAvailable);
387: if (!actual.equals(expect)
388: || (expectAvailable != isAvailable[0])) {
389: errln("#" + i + ": Collator.getFunctionalEquivalent("
390: + input + ")=" + actual + ", avail "
391: + new Boolean(isAvailable[0]) + ", "
392: + "expected " + expect + " avail "
393: + new Boolean(expectAvailable));
394: } else {
395: logln("#" + i + ": Collator.getFunctionalEquivalent("
396: + input + ")=" + actual + ", avail "
397: + new Boolean(isAvailable[0]));
398: }
399: }
400: }
401:
402: // public void PrintFunctionalEquivalentList() {
403: // ULocale[] locales = Collator.getAvailableULocales();
404: // String[] keywords = Collator.getKeywords();
405: // logln("Collation");
406: // logln("Possible keyword=values pairs:");
407: // for (int i = 0; i < Collator.getKeywords().length; ++i) {
408: // String[] values = Collator.getKeywordValues(keywords[i]);
409: // for (int j = 0; j < values.length; ++j) {
410: // System.out.println(keywords[i] + "=" + values[j]);
411: // }
412: // }
413: // logln("Differing Collators:");
414: // boolean[] isAvailable = {true};
415: // for (int k = 0; k < locales.length; ++k) {
416: // logln(locales[k].getDisplayName(ULocale.ENGLISH) + " [" +locales[k] + "]");
417: // for (int i = 0; i < Collator.getKeywords().length; ++i) {
418: // ULocale base = Collator.getFunctionalEquivalent(keywords[i],locales[k]);
419: // String[] values = Collator.getKeywordValues(keywords[i]);
420: // for (int j = 0; j < Collator.getKeywordValues(keywords[i]).length;++j) {
421: // ULocale other = Collator.getFunctionalEquivalent(keywords[i],
422: // new ULocale(locales[k] + "@" + keywords[i] + "=" + values[j]),
423: // isAvailable);
424: // if (isAvailable[0] && !other.equals(base)) {
425: // logln("\t" + keywords[i] + "=" + values[j] + ";\t" + base + ";\t" + other);
426: // }
427: // }
428: // }
429: // }
430: // }
431: }
|