001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Date;
012: import java.util.Enumeration;
013: import java.util.HashMap;
014: import java.util.Map;
015: import java.util.Set;
016: import java.util.TreeMap;
017: import java.util.TreeSet;
018:
019: import com.ibm.icu.dev.test.TestFmwk;
020: import com.ibm.icu.impl.ICUResourceBundle; //import com.ibm.icu.text.SimpleDateFormat;
021: import com.ibm.icu.util.Currency;
022: import com.ibm.icu.util.TimeZone;
023: import com.ibm.icu.util.ULocale;
024: import com.ibm.icu.util.UResourceBundle;
025:
026: public class DisplayNameTest extends TestFmwk {
027: static final boolean SHOW_ALL = false;
028:
029: public static void main(String[] args) throws Exception {
030: new DisplayNameTest().run(args);
031: }
032:
033: interface DisplayNameGetter {
034: public String get(ULocale locale, String code, Object context);
035: }
036:
037: Map[] codeToName = new Map[10];
038: {
039: for (int k = 0; k < codeToName.length; ++k)
040: codeToName[k] = new HashMap();
041: }
042:
043: static final Object[] zoneFormats = { new Integer(0),
044: new Integer(1), new Integer(2), new Integer(3),
045: new Integer(4), new Integer(5), new Integer(6),
046: new Integer(7) };
047: static final Object[] currencyFormats = {
048: new Integer(Currency.SYMBOL_NAME),
049: new Integer(Currency.LONG_NAME) };
050: static final Object[] NO_CONTEXT = { null };
051:
052: static final Date JAN1 = new Date(2004 - 1900, 0, 1);
053: static final Date JULY1 = new Date(2004 - 1900, 6, 1);
054:
055: String[] countries = addUnknown(ULocale.getISOCountries(), 2);
056: String[] languages = addUnknown(ULocale.getISOLanguages(), 2);
057: String[] zones = addUnknown(getRealZoneIDs(), 5);
058: String[] scripts = addUnknown(getCodes(new ULocale("en", "US", ""),
059: "Scripts"), 4);
060: // TODO fix once there is a way to get a list of all script codes
061: String[] currencies = addUnknown(getCodes(
062: new ULocale("en", "", ""), "Currencies"), 3);
063:
064: // TODO fix once there is a way to get a list of all currency codes
065:
066: public void TestLocales() {
067: ULocale[] locales = ULocale.getAvailableLocales();
068: for (int i = 0; i < locales.length; ++i) {
069: checkLocale(locales[i]);
070: }
071: }
072:
073: /**
074: * @return
075: */
076: private String[] getRealZoneIDs() {
077: Set temp = new TreeSet(Arrays
078: .asList(TimeZone.getAvailableIDs()));
079: temp.removeAll(getAliasMap().keySet());
080: return (String[]) temp.toArray(new String[temp.size()]);
081: }
082:
083: public void TestEnglish() {
084: checkLocale(ULocale.ENGLISH);
085: }
086:
087: public void TestFrench() {
088: checkLocale(ULocale.FRENCH);
089: }
090:
091: private void checkLocale(ULocale locale) {
092: logln("Checking " + locale);
093: check("Language", locale, languages, null,
094: new DisplayNameGetter() {
095: public String get(ULocale locale, String code,
096: Object context) {
097: return ULocale.getDisplayLanguage(code, locale);
098: }
099: });
100: check("Script", locale, scripts, null, new DisplayNameGetter() {
101: public String get(ULocale locale, String code,
102: Object context) {
103: // TODO This is kinda a hack; ought to be direct way.
104: return ULocale.getDisplayScript("en_" + code, locale);
105: }
106: });
107: check("Country", locale, countries, null,
108: new DisplayNameGetter() {
109: public String get(ULocale locale, String code,
110: Object context) {
111: // TODO This is kinda a hack; ought to be direct way.
112: return ULocale.getDisplayCountry("en_" + code,
113: locale);
114: }
115: });
116: check("Currencies", locale, currencies, currencyFormats,
117: new DisplayNameGetter() {
118: public String get(ULocale locale, String code,
119: Object context) {
120: Currency s = Currency.getInstance(code);
121: return s.getName(locale, ((Integer) context)
122: .intValue(), new boolean[1]);
123: }
124: });
125: // comment this out, because the zone string information is lost
126: // we'd have to access the resources directly to test them
127:
128: check("Zones", locale, zones, zoneFormats,
129: new DisplayNameGetter() {
130: // TODO replace once we have real API
131: public String get(ULocale locale, String code,
132: Object context) {
133: return getZoneString(locale, code,
134: ((Integer) context).intValue());
135: }
136: });
137:
138: }
139:
140: Map zoneData = new HashMap();
141:
142: private String getZoneString(ULocale locale, String olsonID,
143: int item) {
144: Map data = (Map) zoneData.get(locale);
145: if (data == null) {
146: data = new HashMap();
147: if (SHOW_ALL)
148: System.out.println();
149: if (SHOW_ALL)
150: System.out.println("zones for " + locale);
151: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
152: .getBundleInstance(locale);
153: ICUResourceBundle table = bundle
154: .getWithFallback("zoneStrings");
155: for (int i = 0; i < table.getSize(); ++i) {
156: ICUResourceBundle stringSet = table.get(i);
157: //ICUResourceBundle stringSet = table.getWithFallback(String.valueOf(i));
158: String key = stringSet.getString(0);
159: if (SHOW_ALL)
160: System.out.println("key: " + key);
161: ArrayList list = new ArrayList();
162: for (int j = 1; j < stringSet.getSize(); ++j) {
163: String entry = stringSet.getString(j);
164: if (SHOW_ALL)
165: System.out.println(" entry: " + entry);
166: list.add(entry);
167: }
168: data.put(key, list.toArray(new String[list.size()]));
169: }
170: zoneData.put(locale, data);
171: }
172: String[] strings = (String[]) data.get(olsonID);
173: if (strings == null || item >= strings.length)
174: return olsonID;
175: return strings[item];
176: }
177:
178: static String[][] zonesAliases = {
179: { "America/Atka", "America/Atka" },
180: { "America/Ensenada", "America/Ensenada" },
181: { "America/Fort_Wayne", "America/Fort_Wayne" },
182: { "America/Indiana/Indianapolis",
183: "America/Indiana/Indianapolis" },
184: { "America/Kentucky/Louisville",
185: "America/Kentucky/Louisville" },
186: { "America/Knox_IN", "America/Knox_IN" },
187: { "America/Porto_Acre", "America/Porto_Acre" },
188: { "America/Rosario", "America/Rosario" },
189: { "America/Shiprock", "America/Shiprock" },
190: { "America/Virgin", "America/Virgin" },
191: { "Antarctica/South_Pole", "Antarctica/South_Pole" },
192: { "Arctic/Longyearbyen", "Arctic/Longyearbyen" },
193: { "Asia/Ashkhabad", "Asia/Ashkhabad" },
194: { "Asia/Chungking", "Asia/Chungking" },
195: { "Asia/Dacca", "Asia/Dacca" },
196: { "Asia/Istanbul", "Asia/Istanbul" },
197: { "Asia/Macao", "Asia/Macao" },
198: { "Asia/Tel_Aviv", "Asia/Tel_Aviv" },
199: { "Asia/Thimbu", "Asia/Thimbu" },
200: { "Asia/Ujung_Pandang", "Asia/Ujung_Pandang" },
201: { "Asia/Ulan_Bator", "Asia/Ulan_Bator" },
202: { "Australia/ACT", "Australia/ACT" },
203: { "Australia/Canberra", "Australia/Canberra" },
204: { "Australia/LHI", "Australia/LHI" },
205: { "Australia/NSW", "Australia/NSW" },
206: { "Australia/North", "Australia/North" },
207: { "Australia/Queensland", "Australia/Queensland" },
208: { "Australia/South", "Australia/South" },
209: { "Australia/Tasmania", "Australia/Tasmania" },
210: { "Australia/Victoria", "Australia/Victoria" },
211: { "Australia/West", "Australia/West" },
212: { "Australia/Yancowinna", "Australia/Yancowinna" },
213: { "Brazil/Acre", "Brazil/Acre" },
214: { "Brazil/DeNoronha", "Brazil/DeNoronha" },
215: { "Brazil/East", "Brazil/East" },
216: { "Brazil/West", "Brazil/West" }, { "CST6CDT", "CST6CDT" },
217: { "Canada/Atlantic", "Canada/Atlantic" },
218: { "Canada/Central", "Canada/Central" },
219: { "Canada/East-Saskatchewan", "Canada/East-Saskatchewan" },
220: { "Canada/Eastern", "Canada/Eastern" },
221: { "Canada/Mountain", "Canada/Mountain" },
222: { "Canada/Newfoundland", "Canada/Newfoundland" },
223: { "Canada/Pacific", "Canada/Pacific" },
224: { "Canada/Saskatchewan", "Canada/Saskatchewan" },
225: { "Canada/Yukon", "Canada/Yukon" },
226: { "Chile/Continental", "Chile/Continental" },
227: { "Chile/EasterIsland", "Chile/EasterIsland" },
228: { "Cuba", "Cuba" }, { "EST", "EST" },
229: { "EST5EDT", "EST5EDT" }, { "Egypt", "Egypt" },
230: { "Eire", "Eire" }, { "Etc/GMT+0", "Etc/GMT+0" },
231: { "Etc/GMT-0", "Etc/GMT-0" }, { "Etc/GMT0", "Etc/GMT0" },
232: { "Etc/Greenwich", "Etc/Greenwich" },
233: { "Etc/Universal", "Etc/Universal" },
234: { "Etc/Zulu", "Etc/Zulu" },
235: { "Europe/Nicosia", "Europe/Nicosia" },
236: { "Europe/Tiraspol", "Europe/Tiraspol" }, { "GB", "GB" },
237: { "GB-Eire", "GB-Eire" }, { "GMT", "GMT" },
238: { "GMT+0", "GMT+0" }, { "GMT-0", "GMT-0" },
239: { "GMT0", "GMT0" }, { "Greenwich", "Greenwich" },
240: { "HST", "HST" }, { "Hongkong", "Hongkong" },
241: { "Iceland", "Iceland" }, { "Iran", "Iran" },
242: { "Israel", "Israel" }, { "Jamaica", "Jamaica" },
243: { "Japan", "Japan" }, { "Kwajalein", "Kwajalein" },
244: { "Libya", "Libya" }, { "MST", "MST" },
245: { "MST7MDT", "MST7MDT" },
246: { "Mexico/BajaNorte", "Mexico/BajaNorte" },
247: { "Mexico/BajaSur", "Mexico/BajaSur" },
248: { "Mexico/General", "Mexico/General" },
249: { "Mideast/Riyadh87", "Mideast/Riyadh87" },
250: { "Mideast/Riyadh88", "Mideast/Riyadh88" },
251: { "Mideast/Riyadh89", "Mideast/Riyadh89" }, { "NZ", "NZ" },
252: { "NZ-CHAT", "NZ-CHAT" }, { "Navajo", "Navajo" },
253: { "PRC", "PRC" }, { "PST8PDT", "PST8PDT" },
254: { "Pacific/Samoa", "Pacific/Samoa" },
255: { "Poland", "Poland" }, { "Portugal", "Portugal" },
256: { "ROC", "ROC" }, { "ROK", "ROK" },
257: { "Singapore", "Singapore" },
258: { "SystemV/AST4", "SystemV/AST4" },
259: { "SystemV/AST4ADT", "SystemV/AST4ADT" },
260: { "SystemV/CST6", "SystemV/CST6" },
261: { "SystemV/CST6CDT", "SystemV/CST6CDT" },
262: { "SystemV/EST5", "SystemV/EST5" },
263: { "SystemV/EST5EDT", "SystemV/EST5EDT" },
264: { "SystemV/HST10", "SystemV/HST10" },
265: { "SystemV/MST7", "SystemV/MST7" },
266: { "SystemV/MST7MDT", "SystemV/MST7MDT" },
267: { "SystemV/PST8", "SystemV/PST8" },
268: { "SystemV/PST8PDT", "SystemV/PST8PDT" },
269: { "SystemV/YST9", "SystemV/YST9" },
270: { "SystemV/YST9YDT", "SystemV/YST9YDT" },
271: { "Turkey", "Turkey" }, { "UCT", "UCT" },
272: { "US/Alaska", "US/Alaska" },
273: { "US/Aleutian", "US/Aleutian" },
274: { "US/Arizona", "US/Arizona" },
275: { "US/Central", "US/Central" },
276: { "US/East-Indiana", "US/East-Indiana" },
277: { "US/Eastern", "US/Eastern" },
278: { "US/Hawaii", "US/Hawaii" },
279: { "US/Indiana-Starke", "US/Indiana-Starke" },
280: { "US/Michigan", "US/Michigan" },
281: { "US/Mountain", "US/Mountain" },
282: { "US/Pacific", "US/Pacific" },
283: { "US/Pacific-New", "US/Pacific-New" },
284: { "US/Samoa", "US/Samoa" }, { "UTC", "UTC" },
285: { "Universal", "Universal" }, { "W-SU", "W-SU" },
286: { "Zulu", "Zulu" }, { "ACT", "ACT" }, { "AET", "AET" },
287: { "AGT", "AGT" }, { "ART", "ART" }, { "AST", "AST" },
288: { "BET", "BET" }, { "BST", "BST" }, { "CAT", "CAT" },
289: { "CNT", "CNT" }, { "CST", "CST" }, { "CTT", "CTT" },
290: { "EAT", "EAT" }, { "ECT", "ECT" }, { "IET", "IET" },
291: { "IST", "IST" }, { "JST", "JST" }, { "MIT", "MIT" },
292: { "NET", "NET" }, { "NST", "NST" }, { "PLT", "PLT" },
293: { "PNT", "PNT" }, { "PRT", "PRT" }, { "PST", "PST" },
294: { "SST", "SST" }, { "VST", "VST" }, };
295:
296: /**
297: * Hack to get code list
298: * @return
299: */
300: private static String[] getCodes(ULocale locale, String tableName) {
301: // TODO remove Ugly Hack
302: // get stuff
303: ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle
304: .getBundleInstance(locale);
305: ICUResourceBundle table = bundle.getWithFallback(tableName);
306: // copy into array
307: ArrayList stuff = new ArrayList();
308: for (Enumeration keys = table.getKeys(); keys.hasMoreElements();) {
309: stuff.add(keys.nextElement());
310: }
311: String[] result = new String[stuff.size()];
312: return (String[]) stuff.toArray(result);
313: //return new String[] {"Latn", "Cyrl"};
314: }
315:
316: /**
317: * Add two unknown strings, just to make sure they get passed through without colliding
318: * @param strings
319: * @return
320: */
321: private String[] addUnknown(String[] strings, int len) {
322: String[] result = new String[strings.length + 2];
323: result[0] = "x1unknown".substring(0, len);
324: result[1] = "y1nknown".substring(0, len);
325: System.arraycopy(strings, 0, result, 2, strings.length);
326: return result;
327: }
328:
329: Map bogusZones = null;
330:
331: private Map getAliasMap() {
332: if (bogusZones == null) {
333: bogusZones = new TreeMap();
334: for (int i = 0; i < zonesAliases.length; ++i) {
335: bogusZones.put(zonesAliases[i][0], zonesAliases[i][1]);
336: }
337: }
338: return bogusZones;
339: }
340:
341: private void check(String type, ULocale locale, String[] codes,
342: Object[] contextList, DisplayNameGetter getter) {
343: if (contextList == null)
344: contextList = NO_CONTEXT;
345: for (int k = 0; k < contextList.length; ++k)
346: codeToName[k].clear();
347: for (int j = 0; j < codes.length; ++j) {
348: String code = codes[j];
349: for (int k = 0; k < contextList.length; ++k) {
350: Object context = contextList[k];
351: String name = getter.get(locale, code, context);
352: if (name == null || name.length() == 0) {
353: errln("Null or Zero-Length Display Name\t"
354: + type
355: + "\t("
356: + ((context != null) ? context : "")
357: + ")"
358: + ":\t"
359: + locale
360: + " ["
361: + locale.getDisplayName(ULocale.ENGLISH)
362: + "]"
363: + "\t"
364: + code
365: + " ["
366: + getter
367: .get(ULocale.ENGLISH, code, context)
368: + "]");
369: continue;
370: }
371: String otherCode = (String) codeToName[k].get(name);
372: if (otherCode != null) {
373: errln("Display Names collide for\t"
374: + type
375: + "\t("
376: + ((context != null) ? context : "")
377: + ")"
378: + ":\t"
379: + locale
380: + " ["
381: + locale.getDisplayName(ULocale.ENGLISH)
382: + "]"
383: + "\t"
384: + code
385: + " ["
386: + getter
387: .get(ULocale.ENGLISH, code, context)
388: + "]"
389: + "\t& "
390: + otherCode
391: + " ["
392: + getter.get(ULocale.ENGLISH, otherCode,
393: context) + "]" + "\t=> " + name);
394: } else {
395: codeToName[k].put(name, code);
396: if (SHOW_ALL)
397: logln(type
398: + " ("
399: + ((context != null) ? context : "")
400: + ")"
401: + "\t"
402: + locale
403: + " ["
404: + locale
405: .getDisplayName(ULocale.ENGLISH)
406: + "]"
407: + "\t"
408: + code
409: + "["
410: + getter.get(ULocale.ENGLISH, code,
411: context) + "]" + "\t=> " + name);
412: }
413: }
414: }
415: }
416: }
|