0001: /*
0002: **********************************************************************
0003: * Copyright (c) 2004-2006, International Business Machines
0004: * Corporation and others. All Rights Reserved.
0005: **********************************************************************
0006: * Author: Alan Liu
0007: * Created: January 14 2004
0008: * Since: ICU 2.8
0009: **********************************************************************
0010: */
0011: package com.ibm.icu.dev.test.util;
0012:
0013: import com.ibm.icu.dev.test.TestFmwk;
0014: import com.ibm.icu.text.BreakIterator;
0015: import com.ibm.icu.text.Collator;
0016: import com.ibm.icu.text.DateFormat;
0017: import com.ibm.icu.text.DecimalFormat;
0018: import com.ibm.icu.text.NumberFormat;
0019: import com.ibm.icu.text.NumberFormat.*;
0020: import com.ibm.icu.text.SimpleDateFormat;
0021: import com.ibm.icu.util.Calendar;
0022: import com.ibm.icu.util.ULocale;
0023: import java.lang.reflect.*;
0024: import java.util.Locale;
0025: import java.util.Iterator;
0026: import java.util.Hashtable;
0027: import java.util.TreeMap;
0028:
0029: public class ULocaleTest extends TestFmwk {
0030:
0031: public static void main(String[] args) throws Exception {
0032: new ULocaleTest().run(args);
0033: }
0034:
0035: public void TestCalendar() {
0036: // TODO The CalendarFactory mechanism is not public,
0037: // so we can't test it yet. If it becomes public,
0038: // enable this code.
0039:
0040: // class CFactory implements CalendarFactory {
0041: // Locale loc;
0042: // Calendar proto;
0043: // public CFactory(Locale locale, Calendar prototype) {
0044: // loc = locale;
0045: // proto = prototype;
0046: // }
0047: // public Calendar create(TimeZone tz, Locale locale) {
0048: // // ignore tz -- not relevant to this test
0049: // return locale.equals(loc) ?
0050: // (Calendar) proto.clone() : null;
0051: // }
0052: // public String factoryName() {
0053: // return "CFactory";
0054: // }
0055: // };
0056:
0057: checkService("en_US_BROOKLYN", new ServiceFacade() {
0058: public Object create(ULocale req) {
0059: return Calendar.getInstance(req);
0060: }
0061: // }, null, new Registrar() {
0062: // public Object register(ULocale loc, Object prototype) {
0063: // CFactory f = new CFactory(loc, (Calendar) prototype);
0064: // return Calendar.register(f, loc);
0065: // }
0066: // public boolean unregister(Object key) {
0067: // return Calendar.unregister(key);
0068: // }
0069: });
0070: }
0071:
0072: // Currency getLocale API is obsolete in 3.2. Since it now returns ULocale.ROOT,
0073: // and this is not equal to the requested locale zh_TW_TAIPEI, the
0074: // checkService call would always fail. So we now omit the test.
0075: /*
0076: public void TestCurrency() {
0077: checkService("zh_TW_TAIPEI", new ServiceFacade() {
0078: public Object create(ULocale req) {
0079: return Currency.getInstance(req);
0080: }
0081: }, null, new Registrar() {
0082: public Object register(ULocale loc, Object prototype) {
0083: return Currency.registerInstance((Currency) prototype, loc);
0084: }
0085: public boolean unregister(Object key) {
0086: return Currency.unregister(key);
0087: }
0088: });
0089: }
0090: */
0091:
0092: public void TestBreakIterator() {
0093: checkService("ja_JP_OSAKA", new ServiceFacade() {
0094: public Object create(ULocale req) {
0095: return BreakIterator.getWordInstance(req);
0096: }
0097: }, null, new Registrar() {
0098: public Object register(ULocale loc, Object prototype) {
0099: return BreakIterator.registerInstance(
0100: (BreakIterator) prototype, loc,
0101: BreakIterator.KIND_WORD);
0102: }
0103:
0104: public boolean unregister(Object key) {
0105: return BreakIterator.unregister(key);
0106: }
0107: });
0108: }
0109:
0110: public void TestCollator() {
0111: checkService("ja_JP_YOKOHAMA", new ServiceFacade() {
0112: public Object create(ULocale req) {
0113: return Collator.getInstance(req);
0114: }
0115: }, null, new Registrar() {
0116: public Object register(ULocale loc, Object prototype) {
0117: return Collator.registerInstance((Collator) prototype,
0118: loc);
0119: }
0120:
0121: public boolean unregister(Object key) {
0122: return Collator.unregister(key);
0123: }
0124: });
0125: }
0126:
0127: public void TestDateFormat() {
0128: checkService("de_CH_ZURICH", new ServiceFacade() {
0129: public Object create(ULocale req) {
0130: return DateFormat.getDateInstance(DateFormat.DEFAULT,
0131: req);
0132: }
0133: }, new Subobject() {
0134: public Object get(Object parent) {
0135: return ((SimpleDateFormat) parent)
0136: .getDateFormatSymbols();
0137: }
0138: }, null);
0139: }
0140:
0141: public void TestNumberFormat() {
0142: class NFactory extends SimpleNumberFormatFactory {
0143: NumberFormat proto;
0144: ULocale locale;
0145:
0146: public NFactory(ULocale loc, NumberFormat fmt) {
0147: super (loc);
0148: this .locale = loc;
0149: this .proto = fmt;
0150: }
0151:
0152: public NumberFormat createFormat(ULocale loc, int formatType) {
0153: return (NumberFormat) (locale.equals(loc) ? proto
0154: .clone() : null);
0155: }
0156: }
0157:
0158: checkService("fr_FR_NICE", new ServiceFacade() {
0159: public Object create(ULocale req) {
0160: return NumberFormat.getInstance(req);
0161: }
0162: }, new Subobject() {
0163: public Object get(Object parent) {
0164: return ((DecimalFormat) parent)
0165: .getDecimalFormatSymbols();
0166: }
0167: }, new Registrar() {
0168: public Object register(ULocale loc, Object prototype) {
0169: NFactory f = new NFactory(loc, (NumberFormat) prototype);
0170: return NumberFormat.registerFactory(f);
0171: }
0172:
0173: public boolean unregister(Object key) {
0174: return NumberFormat.unregister(key);
0175: }
0176: });
0177: }
0178:
0179: public void TestSetULocaleKeywords() {
0180: ULocale uloc = new ULocale("en_Latn_US");
0181: uloc = uloc.setKeywordValue("Foo", "FooValue");
0182: if (!"en_Latn_US@foo=FooValue".equals(uloc.getName())) {
0183: errln("failed to add foo keyword, got: " + uloc.getName());
0184: }
0185: uloc = uloc.setKeywordValue("Bar", "BarValue");
0186: if (!"en_Latn_US@bar=BarValue;foo=FooValue".equals(uloc
0187: .getName())) {
0188: errln("failed to add bar keyword, got: " + uloc.getName());
0189: }
0190: uloc = uloc.setKeywordValue("BAR", "NewBarValue");
0191: if (!"en_Latn_US@bar=NewBarValue;foo=FooValue".equals(uloc
0192: .getName())) {
0193: errln("failed to change bar keyword, got: "
0194: + uloc.getName());
0195: }
0196: uloc = uloc.setKeywordValue("BaR", null);
0197: if (!"en_Latn_US@foo=FooValue".equals(uloc.getName())) {
0198: errln("failed to delete bar keyword, got: "
0199: + uloc.getName());
0200: }
0201: uloc = uloc.setKeywordValue(null, null);
0202: if (!"en_Latn_US".equals(uloc.getName())) {
0203: errln("failed to delete all keywords, got: "
0204: + uloc.getName());
0205: }
0206: }
0207:
0208: // ================= Infrastructure =================
0209:
0210: /**
0211: * Compare two locale IDs. If they are equal, return 0. If `string'
0212: * starts with `prefix' plus an additional element, that is, string ==
0213: * prefix + '_' + x, then return 1. Otherwise return a value < 0.
0214: */
0215: static int loccmp(String string, String prefix) {
0216: int slen = string.length(), plen = prefix.length();
0217: /* 'root' is "less than" everything */
0218: if (prefix.equals("root")) {
0219: return string.equals("root") ? 0 : 1;
0220: }
0221: // ON JAVA (only -- not on C -- someone correct me if I'm wrong)
0222: // consider "" to be an alternate name for "root".
0223: if (plen == 0) {
0224: return slen == 0 ? 0 : 1;
0225: }
0226: if (!string.startsWith(prefix))
0227: return -1; /* mismatch */
0228: if (slen == plen)
0229: return 0;
0230: if (string.charAt(plen) == '_')
0231: return 1;
0232: return -2; /* false match, e.g. "en_USX" cmp "en_US" */
0233: }
0234:
0235: /**
0236: * Check the relationship between requested locales, and report problems.
0237: * The caller specifies the expected relationships between requested
0238: * and valid (expReqValid) and between valid and actual (expValidActual).
0239: * Possible values are:
0240: * "gt" strictly greater than, e.g., en_US > en
0241: * "ge" greater or equal, e.g., en >= en
0242: * "eq" equal, e.g., en == en
0243: */
0244: void checklocs(String label, String req, Locale validLoc,
0245: Locale actualLoc, String expReqValid, String expValidActual) {
0246: String valid = validLoc.toString();
0247: String actual = actualLoc.toString();
0248: int reqValid = loccmp(req, valid);
0249: int validActual = loccmp(valid, actual);
0250: boolean reqOK = (expReqValid.equals("gt") && reqValid > 0)
0251: || (expReqValid.equals("ge") && reqValid >= 0)
0252: || (expReqValid.equals("eq") && reqValid == 0);
0253: boolean valOK = (expValidActual.equals("gt") && validActual > 0)
0254: || (expValidActual.equals("ge") && validActual >= 0)
0255: || (expValidActual.equals("eq") && validActual == 0);
0256: if (reqOK && valOK) {
0257: logln("Ok: " + label + "; req=" + req + ", valid=" + valid
0258: + ", actual=" + actual);
0259: } else {
0260: errln("FAIL: "
0261: + label
0262: + "; req="
0263: + req
0264: + ", valid="
0265: + valid
0266: + ", actual="
0267: + actual
0268: + (reqOK ? "" : "\n req !" + expReqValid
0269: + " valid")
0270: + (valOK ? "" : "\n val !" + expValidActual
0271: + " actual"));
0272: }
0273: }
0274:
0275: /**
0276: * Interface used by checkService defining a protocol to create an
0277: * object, given a requested locale.
0278: */
0279: interface ServiceFacade {
0280: Object create(ULocale requestedLocale);
0281: }
0282:
0283: /**
0284: * Interface used by checkService defining a protocol to get a
0285: * contained subobject, given its parent object.
0286: */
0287: interface Subobject {
0288: Object get(Object parent);
0289: }
0290:
0291: /**
0292: * Interface used by checkService defining a protocol to register
0293: * and unregister a service object prototype.
0294: */
0295: interface Registrar {
0296: Object register(ULocale loc, Object prototype);
0297:
0298: boolean unregister(Object key);
0299: }
0300:
0301: /**
0302: * Use reflection to call getLocale() on the given object to
0303: * determine both the valid and the actual locale. Verify these
0304: * for correctness.
0305: */
0306: void checkObject(String requestedLocale, Object obj,
0307: String expReqValid, String expValidActual) {
0308: Class[] params = new Class[] { ULocale.Type.class };
0309: try {
0310: Class cls = obj.getClass();
0311: Method getLocale = cls.getMethod("getLocale", params);
0312: ULocale valid = (ULocale) getLocale.invoke(obj,
0313: new Object[] { ULocale.VALID_LOCALE });
0314: ULocale actual = (ULocale) getLocale.invoke(obj,
0315: new Object[] { ULocale.ACTUAL_LOCALE });
0316: checklocs(cls.getName(), requestedLocale, valid.toLocale(),
0317: actual.toLocale(), expReqValid, expValidActual);
0318: }
0319:
0320: // Make the following exceptions _specific_ -- do not
0321: // catch(Exception), since that will catch the exception
0322: // that errln throws.
0323: catch (NoSuchMethodException e1) {
0324: // no longer an error, Currency has no getLocale
0325: // errln("FAIL: reflection failed: " + e1);
0326: } catch (SecurityException e2) {
0327: errln("FAIL: reflection failed: " + e2);
0328: } catch (IllegalAccessException e3) {
0329: errln("FAIL: reflection failed: " + e3);
0330: } catch (IllegalArgumentException e4) {
0331: errln("FAIL: reflection failed: " + e4);
0332: } catch (InvocationTargetException e5) {
0333: // no longer an error, Currency has no getLocale
0334: // errln("FAIL: reflection failed: " + e5);
0335: }
0336: }
0337:
0338: /**
0339: * Verify the correct getLocale() behavior for the given service.
0340: * @param requestedLocale the locale to request. This MUST BE
0341: * FAKE. In other words, it should be something like
0342: * en_US_FAKEVARIANT so this method can verify correct fallback
0343: * behavior.
0344: * @param svc a factory object that can create the object to be
0345: * tested. This isn't necessary here (one could just pass in the
0346: * object) but is required for the overload of this method that
0347: * takes a Registrar.
0348: */
0349: void checkService(String requestedLocale, ServiceFacade svc) {
0350: checkService(requestedLocale, svc, null, null);
0351: }
0352:
0353: /**
0354: * Verify the correct getLocale() behavior for the given service.
0355: * @param requestedLocale the locale to request. This MUST BE
0356: * FAKE. In other words, it should be something like
0357: * en_US_FAKEVARIANT so this method can verify correct fallback
0358: * behavior.
0359: * @param svc a factory object that can create the object to be
0360: * tested.
0361: * @param sub an object that can be used to retrieve a subobject
0362: * which should also be tested. May be null.
0363: * @param reg an object that supplies the registration and
0364: * unregistration functionality to be tested. May be null.
0365: */
0366: void checkService(String requestedLocale, ServiceFacade svc,
0367: Subobject sub, Registrar reg) {
0368: ULocale req = new ULocale(requestedLocale);
0369: Object obj = svc.create(req);
0370: checkObject(requestedLocale, obj, "gt", "ge");
0371: if (sub != null) {
0372: Object subobj = sub.get(obj);
0373: checkObject(requestedLocale, subobj, "gt", "ge");
0374: }
0375: if (reg != null) {
0376: logln("Info: Registering service");
0377: Object key = reg.register(req, obj);
0378: Object objReg = svc.create(req);
0379: checkObject(requestedLocale, objReg, "eq", "eq");
0380: if (sub != null) {
0381: Object subobj = sub.get(obj);
0382: // Assume subobjects don't come from services, so
0383: // their metadata should be structured normally.
0384: checkObject(requestedLocale, subobj, "gt", "ge");
0385: }
0386: logln("Info: Unregistering service");
0387: if (!reg.unregister(key)) {
0388: errln("FAIL: unregister failed");
0389: }
0390: Object objUnreg = svc.create(req);
0391: checkObject(requestedLocale, objUnreg, "gt", "ge");
0392: }
0393: }
0394:
0395: private static final int LOCALE_SIZE = 9;
0396: private static final String[][] rawData2 = new String[][] {
0397: /* language code */
0398: { "en", "fr", "ca", "el", "no", "zh", "de", "es", "ja" },
0399: /* script code */
0400: { "", "", "", "", "", "Hans", "", "", "" },
0401: /* country code */
0402: { "US", "FR", "ES", "GR", "NO", "CN", "DE", "", "JP" },
0403: /* variant code */
0404: { "", "", "", "", "NY", "", "", "", "" },
0405: /* full name */
0406: { "en_US", "fr_FR", "ca_ES", "el_GR", "no_NO_NY",
0407: "zh_Hans_CN", "de_DE@collation=phonebook",
0408: "es@collation=traditional",
0409: "ja_JP@calendar=japanese" },
0410: /* ISO-3 language */
0411: { "eng", "fra", "cat", "ell", "nor", "zho", "deu", "spa",
0412: "jpn" },
0413: /* ISO-3 country */
0414: { "USA", "FRA", "ESP", "GRC", "NOR", "CHN", "DEU", "",
0415: "JPN" },
0416: /* LCID */
0417: { "409", "40c", "403", "408", "814", "804", "407", "a",
0418: "411" },
0419:
0420: /* display language (English) */
0421: { "English", "French", "Catalan", "Greek", "Norwegian",
0422: "Chinese", "German", "Spanish", "Japanese" },
0423: /* display script code (English) */
0424: { "", "", "", "", "", "Simplified Han", "", "", "" },
0425: /* display country (English) */
0426: { "United States", "France", "Spain", "Greece", "Norway",
0427: "China", "Germany", "", "Japan" },
0428: /* display variant (English) */
0429: { "", "", "", "", "NY", "", "", "", "" },
0430: /* display name (English) */
0431: { "English (United States)", "French (France)",
0432: "Catalan (Spain)", "Greek (Greece)",
0433: "Norwegian (Norway, NY)",
0434: "Chinese (Simplified Han, China)",
0435: "German (Germany, Collation=Phonebook Order)",
0436: "Spanish (Collation=Traditional)",
0437: "Japanese (Japan, Calendar=Japanese Calendar)" },
0438:
0439: /* display language (French) */
0440: { "anglais", "fran\\u00E7ais", "catalan", "grec",
0441: "norv\\u00E9gien", "chinois", "allemand",
0442: "espagnol", "japonais" },
0443: /* display script code (French) */
0444: { "", "", "", "", "", "Hans", "", "", "" },
0445: /* display country (French) */
0446: { "\\u00C9tats-Unis", "France", "Espagne", "Gr\\u00E8ce",
0447: "Norv\\u00E8ge", "Chine", "Allemagne", "", "Japon" },
0448: /* display variant (French) */
0449: { "", "", "", "", "NY", "", "", "", "" },
0450: /* display name (French) */
0451: {
0452: "anglais (\\u00C9tats-Unis)",
0453: "fran\\u00E7ais (France)",
0454: "catalan (Espagne)",
0455: "grec (Gr\\u00E8ce)",
0456: "norv\\u00E9gien (Norv\\u00E8ge, NY)",
0457: "chinois (Hans, Chine)",
0458: "allemand (Allemagne, Ordonnancement=Ordre de l'annuaire)",
0459: "espagnol (Ordonnancement=Ordre traditionnel)",
0460: "japonais (Japon, Calendrier=Calendrier japonais)" },
0461:
0462: /* display language (Catalan) */
0463: { "angl\\u00E8s", "franc\\u00E8s", "catal\\u00E0", "grec",
0464: "noruec", "xin\\u00E9s", "alemany", "espanyol",
0465: "japon\\u00E8s" },
0466: /* display script code (Catalan) */
0467: { "", "", "", "", "", "Hans", "", "", "" },
0468: /* display country (Catalan) */
0469: { "Estats Units", "Fran\\u00E7a", "Espanya",
0470: "Gr\\u00E8cia", "Noruega", "Xina", "Alemanya", "",
0471: "Jap\\u00F3" },
0472: /* display variant (Catalan) */
0473: { "", "", "", "", "NY", "", "", "", "" },
0474: /* display name (Catalan) */
0475: { "angl\\u00E8s (Estats Units)",
0476: "franc\\u00E8s (Fran\\u00E7a)",
0477: "catal\\u00E0 (Espanya)", "grec (Gr\\u00E8cia)",
0478: "noruec (Noruega, NY)", "xin\\u00E9s (Hans, Xina)",
0479: "alemany (Alemanya, COLLATION=PHONEBOOK)",
0480: "espanyol (COLLATION=TRADITIONAL)",
0481: "japon\\u00E8s (Jap\\u00F3, CALENDAR=JAPANESE)" },
0482:
0483: /* display language (Greek) */
0484: {
0485: "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac",
0486: "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac",
0487: "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac",
0488: "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac",
0489: "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac",
0490: "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC",
0491: "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC",
0492: "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC",
0493: "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC" },
0494: /* display script code (Greek) */
0495: { "", "", "", "", "", "Hans", "", "", "" },
0496: /* display country (Greek) */
0497: {
0498: "\\u0397\\u03bd\\u03c9\\u03bc\\u03ad\\u03bd\\u03b5\\u03c2 \\u03a0\\u03bf\\u03bb\\u03b9\\u03c4\\u03b5\\u03af\\u03b5\\u03c2",
0499: "\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1",
0500: "\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1",
0501: "\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1",
0502: "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1",
0503: "\\u039A\\u03AF\\u03BD\\u03B1",
0504: "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1",
0505: "",
0506: "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1" },
0507: /* display variant (Greek) */
0508: { "", "", "", "", "NY", "", "", "", "" }, /* TODO: currently there is no translation for NY in Greek fix this test when we have it */
0509: /* display name (Greek) */
0510: {
0511: "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac (\\u0397\\u03bd\\u03c9\\u03bc\\u03ad\\u03bd\\u03b5\\u03c2 \\u03a0\\u03bf\\u03bb\\u03b9\\u03c4\\u03b5\\u03af\\u03b5\\u03c2)",
0512: "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac (\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1)",
0513: "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac (\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1)",
0514: "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac (\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1)",
0515: "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac (\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1, NY)",
0516: "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC (Hans, \\u039A\\u03AF\\u03BD\\u03B1)",
0517: "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1, COLLATION=PHONEBOOK)",
0518: "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (COLLATION=TRADITIONAL)",
0519: "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC (\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1, CALENDAR=JAPANESE)" } };
0520: private static final int ENGLISH = 0;
0521: private static final int FRENCH = 1;
0522: private static final int CATALAN = 2;
0523: private static final int GREEK = 3;
0524: private static final int NORWEGIAN = 4;
0525: private static final int LANG = 0;
0526: private static final int SCRIPT = 1;
0527: private static final int CTRY = 2;
0528: private static final int VAR = 3;
0529: private static final int NAME = 4;
0530: private static final int LANG3 = 5;
0531: private static final int CTRY3 = 6;
0532: private static final int LCID = 7;
0533: private static final int DLANG_EN = 8;
0534: private static final int DSCRIPT_EN = 9;
0535: private static final int DCTRY_EN = 10;
0536: private static final int DVAR_EN = 11;
0537: private static final int DNAME_EN = 12;
0538: private static final int DLANG_FR = 13;
0539: private static final int DSCRIPT_FR = 14;
0540: private static final int DCTRY_FR = 15;
0541: private static final int DVAR_FR = 16;
0542: private static final int DNAME_FR = 17;
0543: private static final int DLANG_CA = 18;
0544: private static final int DSCRIPT_CA = 19;
0545: private static final int DCTRY_CA = 20;
0546: private static final int DVAR_CA = 21;
0547: private static final int DNAME_CA = 22;
0548: private static final int DLANG_EL = 23;
0549: private static final int DSCRIPT_EL = 24;
0550: private static final int DCTRY_EL = 25;
0551: private static final int DVAR_EL = 26;
0552: private static final int DNAME_EL = 27;
0553:
0554: public void TestBasicGetters() {
0555: int i;
0556: logln("Testing Basic Getters\n");
0557: for (i = 0; i < LOCALE_SIZE; i++) {
0558: String testLocale = (rawData2[NAME][i]);
0559: logln("Testing " + testLocale + ".....\n");
0560:
0561: String lang = ULocale.getLanguage(testLocale);
0562: if (0 != lang.compareTo(rawData2[LANG][i])) {
0563: errln(" Language code mismatch: " + lang + " versus "
0564: + rawData2[LANG][i]);
0565: }
0566:
0567: String ctry = ULocale.getCountry(testLocale);
0568: if (0 != ctry.compareTo(rawData2[CTRY][i])) {
0569: errln(" Country code mismatch: " + ctry + " versus "
0570: + rawData2[CTRY][i]);
0571: }
0572:
0573: String var = ULocale.getVariant(testLocale);
0574: if (0 != var.compareTo(rawData2[VAR][i])) {
0575: errln(" Variant code mismatch: " + var + " versus "
0576: + rawData2[VAR][i]);
0577: }
0578:
0579: String name = ULocale.getName(testLocale);
0580: if (0 != name.compareTo(rawData2[NAME][i])) {
0581: errln(" Name mismatch: " + name + " versus "
0582: + rawData2[NAME][i]);
0583: }
0584:
0585: }
0586: }
0587:
0588: public void TestPrefixes() {
0589: // POSIX ids are no longer handled by getName, so POSIX failures are ignored
0590: final String[][] testData = new String[][] {
0591: /* null canonicalize() column means "expect same as getName()" */
0592: { "sv", "", "FI", "AL", "sv-fi-al", "sv_FI_AL", null },
0593: { "en", "", "GB", "", "en-gb", "en_GB", null },
0594: { "i-hakka", "", "MT", "XEMXIJA", "i-hakka_MT_XEMXIJA",
0595: "i-hakka_MT_XEMXIJA", null },
0596: { "i-hakka", "", "CN", "", "i-hakka_CN", "i-hakka_CN",
0597: null },
0598: { "i-hakka", "", "MX", "", "I-hakka_MX", "i-hakka_MX",
0599: null },
0600: { "x-klingon", "", "US", "SANJOSE",
0601: "X-KLINGON_us_SANJOSE", "x-klingon_US_SANJOSE",
0602: null },
0603:
0604: { "mr", "", "", "", "mr.utf8", "mr.utf8", "mr" },
0605: { "de", "", "TV", "", "de-tv.koi8r", "de_TV.koi8r",
0606: "de_TV" },
0607: { "x-piglatin", "", "ML", "", "x-piglatin_ML.MBE",
0608: "x-piglatin_ML.MBE", "x-piglatin_ML" }, /* Multibyte English */
0609: { "i-cherokee", "", "US", "", "i-Cherokee_US.utf7",
0610: "i-cherokee_US.utf7", "i-cherokee_US" },
0611: { "x-filfli", "", "MT", "FILFLA",
0612: "x-filfli_MT_FILFLA.gb-18030",
0613: "x-filfli_MT_FILFLA.gb-18030",
0614: "x-filfli_MT_FILFLA" },
0615: { "no", "", "NO", "NY_B", "no-no-ny.utf32@B",
0616: "no_NO_NY.utf32@B", "no_NO_NY_B" },
0617: { "no", "", "NO", "B", "no-no.utf32@B",
0618: "no_NO.utf32@B", "no_NO_B" },
0619: { "no", "", "", "NY", "no__ny", "no__NY", null },
0620: { "no", "", "", "NY", "no@ny", "no@ny", "no__NY" },
0621: { "el", "Latn", "", "", "el-latn", "el_Latn", null },
0622: { "en", "Cyrl", "RU", "", "en-cyrl-ru", "en_Cyrl_RU",
0623: null },
0624: { "zh", "Hant", "TW", "STROKE", "zh-hant_TW_STROKE",
0625: "zh_Hant_TW_STROKE", null },
0626: { "qq", "Qqqq", "QQ", "QQ", "qq_Qqqq_QQ_QQ",
0627: "qq_Qqqq_QQ_QQ", null },
0628: { "qq", "Qqqq", "", "QQ", "qq_Qqqq__QQ", "qq_Qqqq__QQ",
0629: null },
0630: { "12", "3456", "78", "90", "12_3456_78_90",
0631: "12_3456_78_90", null }, /* total garbage */
0632:
0633: // odd cases
0634: { "", "", "", "", "@FOO=bar", "@foo=bar", null },
0635: { "", "", "", "", "_@FOO=bar", "@foo=bar", null },
0636: { "", "", "", "", "__@FOO=bar", "@foo=bar", null },
0637: { "", "", "", "FOO", "__foo@FOO=bar", "__FOO@foo=bar",
0638: null }, // we have some of these prefixes
0639: };
0640:
0641: String loc, buf, buf1;
0642: final String[] testTitles = { "ULocale.getLanguage()",
0643: "ULocale.getScript()", "ULocale.getCountry()",
0644: "ULocale.getVariant()", "name", "ULocale.getName()",
0645: "canonicalize()", };
0646: ULocale uloc;
0647:
0648: for (int row = 0; row < testData.length; row++) {
0649: loc = testData[row][NAME];
0650: logln("Test #" + row + ": " + loc);
0651:
0652: uloc = new ULocale(loc);
0653:
0654: for (int n = 0; n <= (NAME + 2); n++) {
0655: if (n == NAME)
0656: continue;
0657:
0658: switch (n) {
0659: case LANG:
0660: buf = ULocale.getLanguage(loc);
0661: buf1 = uloc.getLanguage();
0662: break;
0663:
0664: case SCRIPT:
0665: buf = ULocale.getScript(loc);
0666: buf1 = uloc.getScript();
0667: break;
0668:
0669: case CTRY:
0670: buf = ULocale.getCountry(loc);
0671: buf1 = uloc.getCountry();
0672: break;
0673:
0674: case VAR:
0675: buf = ULocale.getVariant(loc);
0676: buf1 = buf;
0677: break;
0678:
0679: case NAME + 1:
0680: buf = ULocale.getName(loc);
0681: buf1 = uloc.getName();
0682: break;
0683:
0684: case NAME + 2:
0685: buf = ULocale.canonicalize(loc);
0686: buf1 = ULocale.createCanonical(loc).getName();
0687: break;
0688:
0689: default:
0690: buf = "**??";
0691: buf1 = buf;
0692: }
0693:
0694: logln("#" + row + ": " + testTitles[n] + " on " + loc
0695: + ": -> [" + buf + "]");
0696:
0697: String expected = testData[row][n];
0698: if (expected == null && n == (NAME + 2)) {
0699: expected = testData[row][NAME + 1];
0700: }
0701:
0702: // ignore POSIX failures in getName, we don't spec behavior in this case
0703: if (n == NAME + 1
0704: && (expected.indexOf('.') != -1 || expected
0705: .indexOf('@') != -1)) {
0706: continue;
0707: }
0708:
0709: if (buf.compareTo(expected) != 0) {
0710: errln("#" + row + ": " + testTitles[n] + " on "
0711: + loc + ": -> [" + buf + "] (expected '"
0712: + expected + "'!)");
0713: }
0714: if (buf1.compareTo(expected) != 0) {
0715: errln("#" + row + ": " + testTitles[n]
0716: + " on ULocale object " + loc + ": -> ["
0717: + buf1 + "] (expected '" + expected + "'!)");
0718: }
0719: }
0720: }
0721: }
0722:
0723: public void TestObsoleteNames() {
0724: final String[][] tests = new String[][] {
0725: /* locale, language3, language2, Country3, country2 */
0726: { "eng_USA", "eng", "en", "USA", "US" },
0727: { "kok", "kok", "kok", "", "" },
0728: { "in", "ind", "in", "", "" },
0729: { "id", "ind", "id", "", "" }, /* NO aliasing */
0730: { "sh", "srp", "sh", "", "" },
0731: { "zz_FX", "", "zz", "FXX", "FX" },
0732: { "zz_RO", "", "zz", "ROU", "RO" },
0733: { "zz_TP", "", "zz", "TMP", "TP" },
0734: { "zz_TL", "", "zz", "TLS", "TL" },
0735: { "zz_ZR", "", "zz", "ZAR", "ZR" },
0736: { "zz_FXX", "", "zz", "FXX", "FX" }, /* no aliasing. Doesn't go to PS(PSE). */
0737: { "zz_ROM", "", "zz", "ROU", "RO" },
0738: { "zz_ROU", "", "zz", "ROU", "RO" },
0739: { "zz_ZAR", "", "zz", "ZAR", "ZR" },
0740: { "zz_TMP", "", "zz", "TMP", "TP" },
0741: { "zz_TLS", "", "zz", "TLS", "TL" },
0742: { "mlt_PSE", "mlt", "mt", "PSE", "PS" },
0743: { "iw", "heb", "iw", "", "" },
0744: { "ji", "yid", "ji", "", "" },
0745: { "jw", "jaw", "jw", "", "" },
0746: { "sh", "srp", "sh", "", "" }, { "", "", "", "", "" } };
0747:
0748: for (int i = 0; i < tests.length; i++) {
0749: String locale = tests[i][0];
0750: logln("** Testing : " + locale);
0751: String buff, buff1;
0752: ULocale uloc = new ULocale(locale);
0753:
0754: buff = ULocale.getISO3Language(locale);
0755: if (buff.compareTo(tests[i][1]) != 0) {
0756: errln("FAIL: ULocale.getISO3Language(" + locale + ")=="
0757: + buff + ",\t expected " + tests[i][1]);
0758: } else {
0759: logln(" ULocale.getISO3Language(" + locale + ")=="
0760: + buff);
0761: }
0762:
0763: buff1 = uloc.getISO3Language();
0764: if (buff1.compareTo(tests[i][1]) != 0) {
0765: errln("FAIL: ULocale.getISO3Language(" + locale + ")=="
0766: + buff + ",\t expected " + tests[i][1]);
0767: } else {
0768: logln(" ULocale.getISO3Language(" + locale + ")=="
0769: + buff);
0770: }
0771:
0772: buff = ULocale.getLanguage(locale);
0773: if (buff.compareTo(tests[i][2]) != 0) {
0774: errln("FAIL: ULocale.getLanguage(" + locale + ")=="
0775: + buff + ",\t expected " + tests[i][2]);
0776: } else {
0777: logln(" ULocale.getLanguage(" + locale + ")==" + buff);
0778: }
0779:
0780: buff = ULocale.getISO3Country(locale);
0781: if (buff.compareTo(tests[i][3]) != 0) {
0782: errln("FAIL: ULocale.getISO3Country(" + locale + ")=="
0783: + buff + ",\t expected " + tests[i][3]);
0784: } else {
0785: logln(" ULocale.getISO3Country(" + locale + ")=="
0786: + buff);
0787: }
0788:
0789: buff1 = uloc.getISO3Country();
0790: if (buff1.compareTo(tests[i][3]) != 0) {
0791: errln("FAIL: ULocale.getISO3Country(" + locale + ")=="
0792: + buff + ",\t expected " + tests[i][3]);
0793: } else {
0794: logln(" ULocale.getISO3Country(" + locale + ")=="
0795: + buff);
0796: }
0797:
0798: buff = ULocale.getCountry(locale);
0799: if (buff.compareTo(tests[i][4]) != 0) {
0800: errln("FAIL: ULocale.getCountry(" + locale + ")=="
0801: + buff + ",\t expected " + tests[i][4]);
0802: } else {
0803: logln(" ULocale.getCountry(" + locale + ")==" + buff);
0804: }
0805: }
0806:
0807: if (ULocale.getLanguage("iw_IL").compareTo(
0808: ULocale.getLanguage("he_IL")) == 0) {
0809: errln("he,iw ULocale.getLanguage mismatch");
0810: }
0811:
0812: String buff = ULocale.getLanguage("kok_IN");
0813: if (buff.compareTo("kok") != 0) {
0814: errln("ULocale.getLanguage(\"kok\") failed. Expected: kok Got: "
0815: + buff);
0816: }
0817: }
0818:
0819: public void TestCanonicalization() {
0820: final String[][] testCases = new String[][] {
0821: { "ca_ES_PREEURO", "ca_ES_PREEURO",
0822: "ca_ES@currency=ESP" },
0823: { "de_AT_PREEURO", "de_AT_PREEURO",
0824: "de_AT@currency=ATS" },
0825: { "de_DE_PREEURO", "de_DE_PREEURO",
0826: "de_DE@currency=DEM" },
0827: { "de_LU_PREEURO", "de_LU_PREEURO",
0828: "de_LU@currency=EUR" },
0829: { "el_GR_PREEURO", "el_GR_PREEURO",
0830: "el_GR@currency=GRD" },
0831: { "en_BE_PREEURO", "en_BE_PREEURO",
0832: "en_BE@currency=BEF" },
0833: { "en_IE_PREEURO", "en_IE_PREEURO",
0834: "en_IE@currency=IEP" },
0835: { "es_ES_PREEURO", "es_ES_PREEURO",
0836: "es_ES@currency=ESP" },
0837: { "eu_ES_PREEURO", "eu_ES_PREEURO",
0838: "eu_ES@currency=ESP" },
0839: { "fi_FI_PREEURO", "fi_FI_PREEURO",
0840: "fi_FI@currency=FIM" },
0841: { "fr_BE_PREEURO", "fr_BE_PREEURO",
0842: "fr_BE@currency=BEF" },
0843: { "fr_FR_PREEURO", "fr_FR_PREEURO",
0844: "fr_FR@currency=FRF" },
0845: { "fr_LU_PREEURO", "fr_LU_PREEURO",
0846: "fr_LU@currency=LUF" },
0847: { "ga_IE_PREEURO", "ga_IE_PREEURO",
0848: "ga_IE@currency=IEP" },
0849: { "gl_ES_PREEURO", "gl_ES_PREEURO",
0850: "gl_ES@currency=ESP" },
0851: { "it_IT_PREEURO", "it_IT_PREEURO",
0852: "it_IT@currency=ITL" },
0853: { "nl_BE_PREEURO", "nl_BE_PREEURO",
0854: "nl_BE@currency=BEF" },
0855: { "nl_NL_PREEURO", "nl_NL_PREEURO",
0856: "nl_NL@currency=NLG" },
0857: { "pt_PT_PREEURO", "pt_PT_PREEURO",
0858: "pt_PT@currency=PTE" },
0859: { "de__PHONEBOOK", "de__PHONEBOOK",
0860: "de@collation=phonebook" },
0861: { "en_GB_EURO", "en_GB_EURO", "en_GB@currency=EUR" },
0862: { "en_GB@EURO", null, "en_GB@currency=EUR" }, /* POSIX ID */
0863: { "es__TRADITIONAL", "es__TRADITIONAL",
0864: "es@collation=traditional" },
0865: { "hi__DIRECT", "hi__DIRECT", "hi@collation=direct" },
0866: { "ja_JP_TRADITIONAL", "ja_JP_TRADITIONAL",
0867: "ja_JP@calendar=japanese" },
0868: { "th_TH_TRADITIONAL", "th_TH_TRADITIONAL",
0869: "th_TH@calendar=buddhist" },
0870: { "zh_TW_STROKE", "zh_TW_STROKE",
0871: "zh_TW@collation=stroke" },
0872: { "zh__PINYIN", "zh__PINYIN", "zh@collation=pinyin" },
0873: { "zh@collation=pinyin", "zh@collation=pinyin",
0874: "zh@collation=pinyin" },
0875: { "zh_CN@collation=pinyin", "zh_CN@collation=pinyin",
0876: "zh_CN@collation=pinyin" },
0877: { "zh_CN_CA@collation=pinyin",
0878: "zh_CN_CA@collation=pinyin",
0879: "zh_CN_CA@collation=pinyin" },
0880: { "en_US_POSIX", "en_US_POSIX", "en_US_POSIX" },
0881: { "hy_AM_REVISED", "hy_AM_REVISED", "hy_AM_REVISED" },
0882: { "no_NO_NY", "no_NO_NY", "no_NO_NY" /* not: "nn_NO" [alan ICU3.0] */},
0883: { "no@ny", null, "no__NY" /* not: "nn" [alan ICU3.0] */}, /* POSIX ID */
0884: { "no-no.utf32@B", null, "no_NO_B" /* not: "nb_NO_B" [alan ICU3.0] */}, /* POSIX ID */
0885: { "qz-qz@Euro", null, "qz_QZ@currency=EUR" }, /* qz-qz uses private use iso codes */
0886: { "en-BOONT", "en_BOONT", "en__BOONT" }, /* registered name */
0887: { "de-1901", "de_1901", "de__1901" }, /* registered name */
0888: { "de-1906", "de_1906", "de__1906" }, /* registered name */
0889: { "sr-SP-Cyrl", "sr_SP_CYRL", "sr_Cyrl_CS" }, /* .NET name */
0890: { "sr-SP-Latn", "sr_SP_LATN", "sr_Latn_CS" }, /* .NET name */
0891: { "sr_YU_CYRILLIC", "sr_YU_CYRILLIC", "sr_Cyrl_CS" }, /* Linux name */
0892: { "uz-UZ-Cyrl", "uz_UZ_CYRL", "uz_Cyrl_UZ" }, /* .NET name */
0893: { "uz-UZ-Latn", "uz_UZ_LATN", "uz_Latn_UZ" }, /* .NET name */
0894: { "zh-CHS", "zh_CHS", "zh_Hans" }, /* .NET name */
0895: { "zh-CHT", "zh_CHT", "zh_Hant" }, /* .NET name This may change back to zh_Hant */
0896:
0897: /* posix behavior that used to be performed by getName */
0898: { "mr.utf8", null, "mr" },
0899: { "de-tv.koi8r", null, "de_TV" },
0900: { "x-piglatin_ML.MBE", null, "x-piglatin_ML" },
0901: { "i-cherokee_US.utf7", null, "i-cherokee_US" },
0902: { "x-filfli_MT_FILFLA.gb-18030", null,
0903: "x-filfli_MT_FILFLA" },
0904: { "no-no-ny.utf8@B", null, "no_NO_NY_B" /* not: "nn_NO" [alan ICU3.0] */}, /* @ ignored unless variant is empty */
0905:
0906: /* fleshing out canonicalization */
0907: /* sort keywords, ';' is separator so not present at end in canonical form */
0908: {
0909: "en_Hant_IL_VALLEY_GIRL@currency=EUR;calendar=Japanese;",
0910: "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR",
0911: "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" },
0912: /* already-canonical ids are not changed */
0913: {
0914: "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR",
0915: "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR",
0916: "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" },
0917: /* PRE_EURO and EURO conversions don't affect other keywords */
0918: /* not in spec
0919: { "es_ES_PREEURO@CALendar=Japanese", "es_ES_PREEURO@calendar=Japanese", "es_ES@calendar=Japanese;currency=ESP" },
0920: { "es_ES_EURO@SHOUT=zipeedeedoodah", "es_ES_EURO@shout=zipeedeedoodah", "es_ES@currency=EUR;shout=zipeedeedoodah" },
0921: */
0922: /* currency keyword overrides PRE_EURO and EURO currency */
0923: /* not in spec
0924: { "es_ES_PREEURO@currency=EUR", "es_ES_PREEURO@currency=EUR", "es_ES@currency=EUR" },
0925: { "es_ES_EURO@currency=ESP", "es_ES_EURO@currency=ESP", "es_ES@currency=ESP" },
0926: */
0927: /* norwegian is just too weird, if we handle things in their full generality */
0928: /* this is a negative test to show that we DO NOT handle 'lang=no,var=NY' specially. */
0929: { "no-Hant-GB_NY@currency=$$$",
0930: "no_Hant_GB_NY@currency=$$$",
0931: "no_Hant_GB_NY@currency=$$$" /* not: "nn_Hant_GB@currency=$$$" [alan ICU3.0] */},
0932:
0933: /* test cases reflecting internal resource bundle usage */
0934: /* root is just a language */
0935: { "root@kw=foo", "root@kw=foo", "root@kw=foo" },
0936: /* level 2 canonicalization should not touch basename when there are keywords and it is null */
0937: { "@calendar=gregorian", "@calendar=gregorian",
0938: "@calendar=gregorian" }, };
0939:
0940: for (int i = 0; i < testCases.length; i++) {
0941: String[] testCase = testCases[i];
0942: String source = testCase[0];
0943: String level1Expected = testCase[1];
0944: String level2Expected = testCase[2];
0945:
0946: if (level1Expected != null) { // null means we have no expectations for how this case is handled
0947: String level1 = ULocale.getName(source);
0948: if (!level1.equals(level1Expected)) {
0949: errln("ULocale.getName error for: '" + source
0950: + "' expected: '" + level1Expected
0951: + "' but got: '" + level1 + "'");
0952: } else {
0953: logln("Ulocale.getName for: '" + source
0954: + "' returned: '" + level1 + "'");
0955: }
0956: } else {
0957: logln("ULocale.getName skipped: '" + source + "'");
0958: }
0959:
0960: if (level2Expected != null) {
0961: String level2 = ULocale.canonicalize(source);
0962: if (!level2.equals(level2Expected)) {
0963: errln("ULocale.getName error for: '" + source
0964: + "' expected: '" + level2Expected
0965: + "' but got: '" + level2 + "'");
0966: } else {
0967: logln("Ulocale.canonicalize for: '" + source
0968: + "' returned: '" + level2 + "'");
0969: }
0970: } else {
0971: logln("ULocale.canonicalize skipped: '" + source + "'");
0972: }
0973: }
0974: }
0975:
0976: public void TestGetAvailable() {
0977: ULocale[] locales = ULocale.getAvailableLocales();
0978: if (locales.length < 10) {
0979: errln("Did not get the correct result from getAvailableLocales");
0980: }
0981: if (!locales[locales.length - 1].equals("zh_Hant_TW")) {
0982: errln("Did not get the expected result");
0983: }
0984: }
0985:
0986: public void TestDisplayNames() {
0987: // consistency check, also check that all data is available
0988: {
0989: ULocale[] locales = ULocale.getAvailableLocales();
0990: for (int i = 0; i < locales.length; ++i) {
0991: ULocale l = locales[i];
0992: String name = l.getDisplayName();
0993:
0994: logln(l + " --> " + name + ", "
0995: + l.getDisplayName(ULocale.GERMAN) + ", "
0996: + l.getDisplayName(ULocale.FRANCE));
0997:
0998: String language = l.getDisplayLanguage();
0999: String script = l.getDisplayScript();
1000: String country = l.getDisplayCountry();
1001: String variant = l.getDisplayVariant();
1002:
1003: checkName(name, language, script, country, variant);
1004:
1005: for (int j = 0; j < locales.length; ++j) {
1006: ULocale dl = locales[j];
1007:
1008: name = l.getDisplayName(dl);
1009: language = l.getDisplayLanguage(dl);
1010: script = l.getDisplayScript(dl);
1011: country = l.getDisplayCountry(dl);
1012: variant = l.getDisplayVariant(dl);
1013:
1014: if (!checkName(name, language, script, country,
1015: variant)) {
1016: break;
1017: }
1018: }
1019: }
1020: }
1021: // spot check
1022: {
1023: ULocale[] locales = { ULocale.US, ULocale.GERMANY,
1024: ULocale.FRANCE };
1025: String[] names = { "Chinese (China)", "Chinesisch (China)",
1026: "chinois (Chine)" };
1027: ULocale locale = new ULocale("zh_CN");
1028: for (int i = 0; i < locales.length; ++i) {
1029: String name = locale.getDisplayName(locales[i]);
1030: if (!names[i].equals(name)) {
1031: errln("expected '" + names[i] + "' but got '"
1032: + name + "'");
1033: }
1034: }
1035: }
1036: }
1037:
1038: private boolean checkName(String name, String language,
1039: String script, String country, String variant) {
1040: if (language.length() > 0 && name.indexOf(language) == -1) {
1041: errln("name '" + name + "' does not contain language '"
1042: + language + "'");
1043: return false;
1044: }
1045: if (script.length() > 0 && name.indexOf(script) == -1) {
1046: errln("name '" + name + "' does not contain script '"
1047: + script + "'");
1048: return false;
1049: }
1050: if (country.length() > 0 && name.indexOf(country) == -1) {
1051: errln("name '" + name + "' does not contain country '"
1052: + country + "'");
1053: return false;
1054: }
1055: if (variant.length() > 0 && name.indexOf(variant) == -1) {
1056: errln("name '" + name + "' does not contain variant '"
1057: + variant + "'");
1058: return false;
1059: }
1060: return true;
1061: }
1062:
1063: public void TestCoverage() {
1064: {
1065: //Cover displayXXX
1066: int i, j;
1067: String localeID = "zh_CN";
1068: String name, language, script, country, variant;
1069: logln("Covering APIs with signature displayXXX(String, String)");
1070: for (i = 0; i < LOCALE_SIZE; i++) {
1071: //localeID String
1072: String testLocale = (rawData2[NAME][i]);
1073:
1074: logln("Testing " + testLocale + ".....");
1075: name = ULocale.getDisplayName(localeID, testLocale);
1076: language = ULocale.getDisplayLanguage(localeID,
1077: testLocale);
1078: script = ULocale.getDisplayScript(localeID, testLocale);
1079: country = ULocale.getDisplayCountry(localeID,
1080: testLocale);
1081: variant = ULocale.getDisplayVariant(localeID,
1082: testLocale);
1083:
1084: if (!checkName(name, language, script, country, variant)) {
1085: break;
1086: }
1087: }
1088:
1089: logln("Covering APIs with signature displayXXX(String, ULocale)\n");
1090: for (j = 0; j < LOCALE_SIZE; j++) {
1091: String testLocale = (rawData2[NAME][j]);
1092: ULocale loc = new ULocale(testLocale);
1093:
1094: logln("Testing " + testLocale + ".....");
1095: name = ULocale.getDisplayName(localeID, loc);
1096: language = ULocale.getDisplayLanguage(localeID, loc);
1097: script = ULocale.getDisplayScript(localeID, loc);
1098: country = ULocale.getDisplayCountry(localeID, loc);
1099: variant = ULocale.getDisplayVariant(localeID, loc);
1100:
1101: if (!checkName(name, language, script, country, variant)) {
1102: break;
1103: }
1104: }
1105: }
1106: ULocale loc1 = new ULocale("en_US_BROOKLYN");
1107: ULocale loc2 = new ULocale("en", "US", "BROOKLYN");
1108: if (!loc2.equals(loc1)) {
1109: errln("ULocale.ULocale(String a, String b, String c)");
1110: }
1111:
1112: ULocale loc3 = new ULocale("en_US");
1113: ULocale loc4 = new ULocale("en", "US");
1114: if (!loc4.equals(loc3)) {
1115: errln("ULocale.ULocale(String a, String b)");
1116: }
1117:
1118: ULocale loc5 = (ULocale) loc4.clone();
1119: if (!loc5.equals(loc4)) {
1120: errln("ULocale.clone should get the same ULocale");
1121: }
1122: ULocale.getISOCountries(); // To check the result ?!
1123: }
1124:
1125: public void TestBamBm() {
1126: // "bam" shouldn't be there since the official code is 'bm'
1127: String[] isoLanguages = ULocale.getISOLanguages();
1128: for (int i = 0; i < isoLanguages.length; ++i) {
1129: if ("bam".equals(isoLanguages[i])) {
1130: errln("found bam");
1131: }
1132: if (i > 0
1133: && isoLanguages[i].compareTo(isoLanguages[i - 1]) <= 0) {
1134: errln("language list out of order: '" + isoLanguages[i]
1135: + " <= " + isoLanguages[i - 1]);
1136: }
1137: }
1138: }
1139:
1140: public void TestDisplayKeyword() {
1141: //prepare testing data
1142: initHashtable();
1143: String[] data = {
1144: "en_US@collation=direct;calendar=islamic-civil",
1145: "zh_Hans@collation=pinyin;calendar=chinese",
1146: "foo_Bar_BAZ@collation=traditional;calendar=buddhist" };
1147:
1148: for (int i = 0; i < data.length; i++) {
1149: String localeID = data[i];
1150: logln("");
1151: logln("Testing locale " + localeID + " ...");
1152: ULocale loc = new ULocale(localeID);
1153:
1154: Iterator it = loc.getKeywords();
1155: Iterator it2 = ULocale.getKeywords(localeID);
1156: //it and it2 are not equal here. No way to verify their equivalence yet.
1157: while (it.hasNext()) {
1158: String key = (String) it.next();
1159:
1160: //To verify display of Keyword
1161: // display the above key in English
1162: String s0 = ULocale.getDisplayKeyword(key); //display in default locale
1163: String s1 = ULocale.getDisplayKeyword(key, ULocale.US);
1164: String s2 = ULocale.getDisplayKeyword(key, "en_US");
1165: if (!s1.equals(s2)) {
1166: errln("FAIL: one of the getDisplayKeyword methods failed.");
1167: }
1168: if (!s1.equals(h[0].get(key))) {
1169: warnln("Locale " + localeID
1170: + " getDisplayKeyword for key: " + key
1171: + " in English expected \"" + h[0].get(key)
1172: + "\" saw \"" + s1 + "\" instead");
1173: } else {
1174: logln("OK: getDisplayKeyword for key: " + key
1175: + " in English got " + s1);
1176: }
1177:
1178: // display the key in S-Chinese
1179: s1 = ULocale.getDisplayKeyword(key, ULocale.CHINA);
1180: s2 = ULocale.getDisplayKeyword(key, "zh_Hans");
1181: if (!s1.equals(s2)) {
1182: errln("one of the getDisplayKeyword methods failed.");
1183: }
1184: if (!s1.equals(h[1].get(key))) {
1185: warnln("Locale " + localeID
1186: + " getDisplayKeyword for key: " + key
1187: + " in Chinese expected \"" + h[1].get(key)
1188: + "\" saw \"" + s1 + "\" instead");
1189: } else {
1190: logln("OK: getDisplayKeyword for key: " + key
1191: + " in Chinese got " + s1);
1192: }
1193:
1194: //To verify display of Keyword values
1195: String type = loc.getKeywordValue(key);
1196: // display type in English
1197: String ss0 = loc.getDisplayKeywordValue(key);
1198: String ss1 = loc
1199: .getDisplayKeywordValue(key, ULocale.US);
1200: String ss2 = ULocale.getDisplayKeywordValue(localeID,
1201: key, "en_US");
1202: String ss3 = ULocale.getDisplayKeywordValue(localeID,
1203: key, ULocale.US);
1204: if (!ss1.equals(ss2) || !ss1.equals(ss3)) {
1205: errln("FAIL: one of the getDisplayKeywordValue methods failed.");
1206: }
1207: if (!ss1.equals(h[0].get(type))) {
1208: warnln(" Locale " + localeID
1209: + " getDisplayKeywordValue for key: " + key
1210: + " in English expected \""
1211: + h[0].get(type) + "\" saw \"" + ss1
1212: + "\" instead");
1213: } else {
1214: logln("OK: getDisplayKeywordValue for key: " + key
1215: + " in English got " + ss1);
1216: }
1217:
1218: // display type in Chinese
1219: ss0 = loc.getDisplayKeywordValue(key);
1220: ss1 = loc.getDisplayKeywordValue(key, ULocale.CHINA);
1221: ss2 = ULocale.getDisplayKeywordValue(localeID, key,
1222: "zh_Hans");
1223: ss3 = ULocale.getDisplayKeywordValue(localeID, key,
1224: ULocale.CHINA);
1225: if (!ss1.equals(ss2) || !ss1.equals(ss3)) {
1226: warnln("one of the getDisplayKeywordValue methods failed.");
1227: }
1228: if (!ss1.equals(h[1].get(type))) {
1229: warnln("Locale " + localeID
1230: + " getDisplayKeywordValue for key: " + key
1231: + " in Chinese expected \""
1232: + h[1].get(type) + "\" saw \"" + ss1
1233: + "\" instead");
1234: } else {
1235: logln("OK: getDisplayKeywordValue for key: " + key
1236: + " in Chinese got " + ss1);
1237: }
1238: }
1239: }
1240: }
1241:
1242: private void initHashtable() {
1243: h[0] = new Hashtable();
1244: h[1] = new Hashtable();
1245:
1246: //display in English
1247: h[0].put("collation", "Collation");
1248: h[0].put("calendar", "Calendar");
1249: h[0].put("currency", "Currency");
1250: h[0].put("phonebook", "Phonebook Order");
1251: h[0].put("pinyin", "Pinyin Order");
1252: h[0].put("traditional", "Traditional");
1253: h[0].put("stroke", "Stroke Order");
1254: h[0].put("direct", "Direct Order");
1255: h[0].put("japanese", "Japanese Calendar");
1256: h[0].put("buddhist", "Buddhist Calendar");
1257: h[0].put("islamic", "Islamic Calendar");
1258: h[0].put("islamic-civil", "Islamic-Civil Calendar");
1259: h[0].put("hebrew", "Hebrew Calendar");
1260: h[0].put("chinese", "Chinese Calendar");
1261: h[0].put("gregorian", "Gregorian Calendar");
1262:
1263: //display in S-Chinese
1264: h[1].put("collation", "\u5BF9\u7167");
1265: h[1].put("calendar", "\u65E5\u5386");
1266: h[1].put("currency", "\u8D27\u5E01");
1267: h[1].put("direct", "\u987A\u5E8F");
1268: h[1].put("phonebook", "\u7535\u8BDD\u7C3F\u987A\u5E8F");
1269: h[1].put("pinyin", "\u62FC\u97F3\u987a\u5e8f");
1270: h[1].put("stroke", "\u7B14\u5212\u987A\u5E8F");
1271: h[1].put("traditional", "\u4F20\u7EDF\u5386\u6CD5");
1272: h[1].put("japanese", "\u65E5\u672C\u65E5\u5386");
1273: h[1].put("buddhist", "\u4F5B\u6559\u65E5\u5386");
1274: h[1].put("islamic", "\u4F0A\u65AF\u5170\u65E5\u5386");
1275: h[1].put("islamic-civil",
1276: "\u4F0A\u65AF\u5170\u5E0C\u5409\u6765\u5386");
1277: h[1].put("hebrew", "\u5E0C\u4F2F\u6765\u65E5\u5386");
1278: h[1].put("chinese", "\u519C\u5386");
1279: h[1].put("gregorian", "\u516C\u5386");
1280: }
1281:
1282: //Hashtables for storing expected display of keys/types of locale in English and Chinese
1283: private static Hashtable[] h = new Hashtable[2];
1284:
1285: private static final String ACCEPT_LANGUAGE_TESTS[][] = {
1286: /*# result fallback? */
1287: /*0*/{ "mt_MT", "false" },
1288: /*1*/{ "en", "false" },
1289: /*2*/{ "en", "true" }, // fell back from en-zzz to en
1290: /*3*/{ null, "true" },
1291: /*4*/{ "es", "false" },
1292: /*5*/{ "de", "false" } };
1293:
1294: private static final String ACCEPT_LANGUAGE_HTTP[] = {
1295: /*0*/"mt-mt, ja;q=0.76, en-us;q=0.95, en;q=0.92, en-gb;q=0.89, fr;q=0.87, iu-ca;q=0.84, iu;q=0.82, ja-jp;q=0.79, mt;q=0.97, de-de;q=0.74, de;q=0.71, es;q=0.68, it-it;q=0.66, it;q=0.63, vi-vn;q=0.61, vi;q=0.58, nl-nl;q=0.55, nl;q=0.53, th-th-traditional;q=.01",
1296: /*1*/"ja;q=0.5, en;q=0.8, tlh",
1297: /*2*/"en-zzz, de-lx;q=0.8",
1298: /*3*/"mga-ie;q=0.9, tlh",
1299: /*4*/"xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1300: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1301: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1302: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1303: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1304: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1305: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1306: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1307: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1308: + "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "
1309: + "es",
1310: /*5*/"de;q=.9, fr;q=.9, xxx-yyy, sr;q=.8" };
1311:
1312: public void TestAcceptLanguage() {
1313: for (int i = 0; i < (ACCEPT_LANGUAGE_HTTP.length); i++) {
1314: Boolean expectBoolean = new Boolean(
1315: ACCEPT_LANGUAGE_TESTS[i][1]);
1316: String expectLocale = ACCEPT_LANGUAGE_TESTS[i][0];
1317:
1318: logln("#" + i + ": expecting: " + expectLocale + " ("
1319: + expectBoolean + ")");
1320:
1321: boolean r[] = { false };
1322: ULocale n = ULocale.acceptLanguage(ACCEPT_LANGUAGE_HTTP[i],
1323: r);
1324: if ((n == null) && (expectLocale != null)) {
1325: errln("result was null! line #" + i);
1326: continue;
1327: }
1328: if (((n == null) && (expectLocale == null))
1329: || (n.toString().equals(expectLocale))) {
1330: logln(" locale: OK.");
1331: } else {
1332: errln("expected " + expectLocale + " but got "
1333: + n.toString());
1334: }
1335: if (expectBoolean.equals(new Boolean(r[0]))) {
1336: logln(" bool: OK.");
1337: } else {
1338: errln("bool: not OK, was "
1339: + new Boolean(r[0]).toString() + " expected "
1340: + expectBoolean.toString());
1341: }
1342: }
1343: }
1344:
1345: private ULocale[] StringToULocaleArray(String acceptLanguageList) {
1346: //following code is copied from
1347: //ULocale.acceptLanguage(String acceptLanguageList, ULocale[] availableLocales, boolean[] fallback)
1348: class ULocaleAcceptLanguageQ implements Comparable {
1349: private double q;
1350: private double serial;
1351:
1352: public ULocaleAcceptLanguageQ(double theq, int theserial) {
1353: q = theq;
1354: serial = theserial;
1355: }
1356:
1357: public int compareTo(Object o) {
1358: ULocaleAcceptLanguageQ other = (ULocaleAcceptLanguageQ) o;
1359: if (q > other.q) { // reverse - to sort in descending order
1360: return -1;
1361: } else if (q < other.q) {
1362: return 1;
1363: }
1364: if (serial < other.serial) {
1365: return -1;
1366: } else if (serial > other.serial) {
1367: return 1;
1368: } else {
1369: return 0; // same object
1370: }
1371: }
1372: }
1373:
1374: // 1st: parse out the acceptLanguageList into an array
1375:
1376: TreeMap map = new TreeMap();
1377:
1378: final int l = acceptLanguageList.length();
1379: int n;
1380: int last = -1;
1381: for (n = 0; n < l; n++) {
1382: int itemEnd = acceptLanguageList.indexOf(',', n);
1383: if (itemEnd == -1) {
1384: itemEnd = l;
1385: }
1386: int paramEnd = acceptLanguageList.indexOf(';', n);
1387: double q = 1.0;
1388:
1389: if ((paramEnd != -1) && (paramEnd < itemEnd)) {
1390: /* semicolon (;) is closer than end (,) */
1391: int t = paramEnd + 1;
1392: while (Character.isSpace(acceptLanguageList.charAt(t))) {
1393: t++;
1394: }
1395: if (acceptLanguageList.charAt(t) == 'q') {
1396: t++;
1397: }
1398: while (Character.isSpace(acceptLanguageList.charAt(t))) {
1399: t++;
1400: }
1401: if (acceptLanguageList.charAt(t) == '=') {
1402: t++;
1403: }
1404: while (Character.isSpace(acceptLanguageList.charAt(t))) {
1405: t++;
1406: }
1407: try {
1408: String val = acceptLanguageList.substring(t,
1409: itemEnd).trim();
1410: q = Double.parseDouble(val);
1411: } catch (NumberFormatException nfe) {
1412: q = 1.0;
1413: }
1414: } else {
1415: q = 1.0; //default
1416: paramEnd = itemEnd;
1417: }
1418:
1419: String loc = acceptLanguageList.substring(n, paramEnd)
1420: .trim();
1421: int serial = map.size();
1422: ULocaleAcceptLanguageQ entry = new ULocaleAcceptLanguageQ(
1423: q, serial);
1424: map.put(entry, new ULocale(ULocale.canonicalize(loc))); // sort in reverse order.. 1.0, 0.9, 0.8 .. etc
1425: n = itemEnd; // get next item. (n++ will skip over delimiter)
1426: }
1427:
1428: // 2. pull out the map
1429: ULocale acceptList[] = (ULocale[]) map.values().toArray(
1430: new ULocale[map.size()]);
1431: return acceptList;
1432: }
1433:
1434: public void TestAcceptLanguage2() {
1435: for (int i = 0; i < (ACCEPT_LANGUAGE_HTTP.length); i++) {
1436: Boolean expectBoolean = new Boolean(
1437: ACCEPT_LANGUAGE_TESTS[i][1]);
1438: String expectLocale = ACCEPT_LANGUAGE_TESTS[i][0];
1439:
1440: logln("#" + i + ": expecting: " + expectLocale + " ("
1441: + expectBoolean + ")");
1442:
1443: boolean r[] = { false };
1444: ULocale n = ULocale.acceptLanguage(
1445: StringToULocaleArray(ACCEPT_LANGUAGE_HTTP[i]), r);
1446: if ((n == null) && (expectLocale != null)) {
1447: errln("result was null! line #" + i);
1448: continue;
1449: }
1450: if (((n == null) && (expectLocale == null))
1451: || (n.toString().equals(expectLocale))) {
1452: logln(" locale: OK.");
1453: } else {
1454: errln("expected " + expectLocale + " but got "
1455: + n.toString());
1456: }
1457: if (expectBoolean.equals(new Boolean(r[0]))) {
1458: logln(" bool: OK.");
1459: } else {
1460: errln("bool: not OK, was "
1461: + new Boolean(r[0]).toString() + " expected "
1462: + expectBoolean.toString());
1463: }
1464: }
1465: }
1466:
1467: public void TestJB3962() {
1468: ULocale loc = new ULocale("de_CH");
1469: String disp = loc.getDisplayName(ULocale.GERMAN);
1470: if (!disp.equals("Deutsch (Schweiz)")) {
1471: errln("Did not get the expected display name for de_CH locale. Got: "
1472: + prettify(disp));
1473: }
1474: }
1475: }
|