2 **********************************************************************
3 * Copyright (c) 2004-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: January 14 2004
9 **********************************************************************
11 package com.ibm.icu.dev.test.util;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.Locale;
21 import java.util.TreeMap;
23 import com.ibm.icu.dev.test.TestFmwk;
24 import com.ibm.icu.lang.UCharacter;
25 import com.ibm.icu.text.DateFormat;
26 import com.ibm.icu.text.DecimalFormat;
27 import com.ibm.icu.text.DisplayContext;
28 import com.ibm.icu.text.LocaleDisplayNames;
29 import com.ibm.icu.text.LocaleDisplayNames.DialectHandling;
30 import com.ibm.icu.text.NumberFormat;
31 import com.ibm.icu.text.NumberFormat.SimpleNumberFormatFactory;
32 import com.ibm.icu.text.SimpleDateFormat;
33 import com.ibm.icu.util.Calendar;
34 import com.ibm.icu.util.IllformedLocaleException;
35 import com.ibm.icu.util.LocaleData;
36 import com.ibm.icu.util.ULocale;
37 import com.ibm.icu.util.ULocale.Builder;
38 import com.ibm.icu.util.ULocale.Category;
39 import com.ibm.icu.util.UResourceBundle;
40 import com.ibm.icu.util.VersionInfo;
42 public class ULocaleTest extends TestFmwk {
44 private static final boolean JAVA7_OR_LATER = (VersionInfo.javaVersion().compareTo(VersionInfo.getInstance(1, 7)) >= 0);
46 public static void main(String[] args) throws Exception {
47 new ULocaleTest().run(args);
50 public void TestCalendar() {
51 // TODO The CalendarFactory mechanism is not public,
52 // so we can't test it yet. If it becomes public,
55 // class CFactory implements CalendarFactory {
58 // public CFactory(Locale locale, Calendar prototype) {
62 // public Calendar create(TimeZone tz, Locale locale) {
63 // // ignore tz -- not relevant to this test
64 // return locale.equals(loc) ?
65 // (Calendar) proto.clone() : null;
67 // public String factoryName() {
72 checkService("en_US_BROOKLYN", new ServiceFacade() {
73 public Object create(ULocale req) {
74 return Calendar.getInstance(req);
76 // }, null, new Registrar() {
77 // public Object register(ULocale loc, Object prototype) {
78 // CFactory f = new CFactory(loc, (Calendar) prototype);
79 // return Calendar.register(f, loc);
81 // public boolean unregister(Object key) {
82 // return Calendar.unregister(key);
87 // Currency getLocale API is obsolete in 3.2. Since it now returns ULocale.ROOT,
88 // and this is not equal to the requested locale zh_TW_TAIPEI, the
89 // checkService call would always fail. So we now omit the test.
91 public void TestCurrency() {
92 checkService("zh_TW_TAIPEI", new ServiceFacade() {
93 public Object create(ULocale req) {
94 return Currency.getInstance(req);
96 }, null, new Registrar() {
97 public Object register(ULocale loc, Object prototype) {
98 return Currency.registerInstance((Currency) prototype, loc);
100 public boolean unregister(Object key) {
101 return Currency.unregister(key);
107 public void TestDateFormat() {
108 checkService("de_CH_ZURICH", new ServiceFacade() {
109 public Object create(ULocale req) {
110 return DateFormat.getDateInstance(DateFormat.DEFAULT, req);
113 public Object get(Object parent) {
114 return ((SimpleDateFormat) parent).getDateFormatSymbols();
119 public void TestNumberFormat() {
120 class NFactory extends SimpleNumberFormatFactory {
123 public NFactory(ULocale loc, NumberFormat fmt) {
128 public NumberFormat createFormat(ULocale loc, int formatType) {
129 return (NumberFormat) (locale.equals(loc) ?
130 proto.clone() : null);
134 checkService("fr_FR_NICE", new ServiceFacade() {
135 public Object create(ULocale req) {
136 return NumberFormat.getInstance(req);
139 public Object get(Object parent) {
140 return ((DecimalFormat) parent).getDecimalFormatSymbols();
143 public Object register(ULocale loc, Object prototype) {
144 NFactory f = new NFactory(loc, (NumberFormat) prototype);
145 return NumberFormat.registerFactory(f);
147 public boolean unregister(Object key) {
148 return NumberFormat.unregister(key);
153 public void TestSetULocaleKeywords() {
154 ULocale uloc = new ULocale("en_Latn_US");
155 uloc = uloc.setKeywordValue("Foo", "FooValue");
156 if (!"en_Latn_US@foo=FooValue".equals(uloc.getName())) {
157 errln("failed to add foo keyword, got: " + uloc.getName());
159 uloc = uloc.setKeywordValue("Bar", "BarValue");
160 if (!"en_Latn_US@bar=BarValue;foo=FooValue".equals(uloc.getName())) {
161 errln("failed to add bar keyword, got: " + uloc.getName());
163 uloc = uloc.setKeywordValue("BAR", "NewBarValue");
164 if (!"en_Latn_US@bar=NewBarValue;foo=FooValue".equals(uloc.getName())) {
165 errln("failed to change bar keyword, got: " + uloc.getName());
167 uloc = uloc.setKeywordValue("BaR", null);
168 if (!"en_Latn_US@foo=FooValue".equals(uloc.getName())) {
169 errln("failed to delete bar keyword, got: " + uloc.getName());
171 uloc = uloc.setKeywordValue(null, null);
172 if (!"en_Latn_US".equals(uloc.getName())) {
173 errln("failed to delete all keywords, got: " + uloc.getName());
180 public void TestJavaLocaleCompatibility() {
181 Locale backupDefault = Locale.getDefault();
182 ULocale orgUlocDefault = ULocale.getDefault();
184 // Java Locale for ja_JP with Japanese calendar
185 Locale jaJPJP = new Locale("ja", "JP", "JP");
186 Locale jaJP = new Locale("ja", "JP");
187 // Java Locale for th_TH with Thai digits
188 Locale thTHTH = new Locale("th", "TH", "TH");
190 Calendar cal = Calendar.getInstance(jaJPJP);
191 String caltype = cal.getType();
192 if (!caltype.equals("japanese")) {
193 errln("FAIL: Invalid calendar type: " + caltype + " /expected: japanese");
196 cal = Calendar.getInstance(jaJP);
197 caltype = cal.getType();
198 if (!caltype.equals("gregorian")) {
199 errln("FAIL: Invalid calendar type: " + caltype + " /expected: gregorian");
203 Locale.setDefault(jaJPJP);
204 ULocale defUloc = ULocale.getDefault();
205 if (JAVA7_OR_LATER) {
206 if (!defUloc.toString().equals("ja_JP_JP@calendar=japanese")) {
207 errln("FAIL: Invalid default ULocale: " + defUloc + " /expected: ja_JP_JP@calendar=japanese");
210 if (!defUloc.toString().equals("ja_JP@calendar=japanese")) {
211 errln("FAIL: Invalid default ULocale: " + defUloc + " /expected: ja_JP@calendar=japanese");
214 // Check calendar type
215 cal = Calendar.getInstance();
216 caltype = cal.getType();
217 if (!caltype.equals("japanese")) {
218 errln("FAIL: Invalid calendar type: " + caltype + " /expected: japanese");
220 Locale.setDefault(backupDefault);
222 // Set default via ULocale
223 ULocale ujaJP_calJP = new ULocale("ja_JP@calendar=japanese");
224 ULocale.setDefault(ujaJP_calJP);
225 if (!JAVA7_OR_LATER && !Locale.getDefault().equals(jaJPJP)) {
226 errln("FAIL: ULocale#setDefault failed to set Java Locale ja_JP_JP /actual: " + Locale.getDefault());
228 // Ticket#6672 - missing keywords
229 defUloc = ULocale.getDefault();
230 if (!defUloc.equals(ujaJP_calJP)) {
231 errln("FAIL: ULocale#getDefault returned " + defUloc + " /expected: ja_JP@calendar=japanese");
233 // Set a incompatible base locale via Locale#setDefault
234 Locale.setDefault(Locale.US);
235 defUloc = ULocale.getDefault();
236 if (defUloc.equals(ujaJP_calJP)) {
237 errln("FAIL: ULocale#getDefault returned " + defUloc + " /expected: " + ULocale.forLocale(Locale.US));
240 Locale.setDefault(backupDefault);
242 // We also want to map ICU locale ja@calendar=japanese to Java ja_JP_JP
243 ULocale.setDefault(new ULocale("ja@calendar=japanese"));
244 if (!JAVA7_OR_LATER && !Locale.getDefault().equals(jaJPJP)) {
245 errln("FAIL: ULocale#setDefault failed to set Java Locale ja_JP_JP /actual: " + Locale.getDefault());
247 Locale.setDefault(backupDefault);
250 Locale noNONY = new Locale("no", "NO", "NY");
251 Locale.setDefault(noNONY);
252 defUloc = ULocale.getDefault();
253 if (!defUloc.toString().equals("nn_NO")) {
254 errln("FAIL: Invalid default ULocale: " + defUloc + " /expected: nn_NO");
256 Locale.setDefault(backupDefault);
258 // Java th_TH_TH -> ICU th_TH@numbers=thai
259 ULocale.setDefault(new ULocale("th@numbers=thai"));
260 if (!JAVA7_OR_LATER && !Locale.getDefault().equals(thTHTH)) {
261 errln("FAIL: ULocale#setDefault failed to set Java Locale th_TH_TH /actual: " + Locale.getDefault());
263 Locale.setDefault(backupDefault);
265 // Set default via ULocale
266 ULocale.setDefault(new ULocale("nn_NO"));
267 if (!JAVA7_OR_LATER && !Locale.getDefault().equals(noNONY)) {
268 errln("FAIL: ULocale#setDefault failed to set Java Locale no_NO_NY /actual: " + Locale.getDefault());
270 Locale.setDefault(backupDefault);
272 // We also want to map ICU locale nn to Java no_NO_NY
273 ULocale.setDefault(new ULocale("nn"));
274 if (!JAVA7_OR_LATER && !Locale.getDefault().equals(noNONY)) {
275 errln("FAIL: ULocale#setDefault failed to set Java Locale no_NO_NY /actual: " + Locale.getDefault());
277 Locale.setDefault(backupDefault);
279 // Make sure default ULocale is restored
280 if (!ULocale.getDefault().equals(orgUlocDefault)) {
281 errln("FAIL: Original default ULocale is not restored - " + ULocale.getDefault() + ", expected(orginal) - " + orgUlocDefault);
285 // ================= Infrastructure =================
288 * Compare two locale IDs. If they are equal, return 0. If `string'
289 * starts with `prefix' plus an additional element, that is, string ==
290 * prefix + '_' + x, then return 1. Otherwise return a value < 0.
292 static int loccmp(String string, String prefix) {
293 int slen = string.length(),
294 plen = prefix.length();
295 /* 'root' is "less than" everything */
296 if (prefix.equals("root")) {
297 return string.equals("root") ? 0 : 1;
299 // ON JAVA (only -- not on C -- someone correct me if I'm wrong)
300 // consider "" to be an alternate name for "root".
302 return slen == 0 ? 0 : 1;
304 if (!string.startsWith(prefix)) return -1; /* mismatch */
305 if (slen == plen) return 0;
306 if (string.charAt(plen) == '_') return 1;
307 return -2; /* false match, e.g. "en_USX" cmp "en_US" */
311 * Check the relationship between requested locales, and report problems.
312 * The caller specifies the expected relationships between requested
313 * and valid (expReqValid) and between valid and actual (expValidActual).
314 * Possible values are:
315 * "gt" strictly greater than, e.g., en_US > en
316 * "ge" greater or equal, e.g., en >= en
317 * "eq" equal, e.g., en == en
319 void checklocs(String label,
324 String expValidActual) {
325 String valid = validLoc.toString();
326 String actual = actualLoc.toString();
327 int reqValid = loccmp(req, valid);
328 int validActual = loccmp(valid, actual);
329 boolean reqOK = (expReqValid.equals("gt") && reqValid > 0) ||
330 (expReqValid.equals("ge") && reqValid >= 0) ||
331 (expReqValid.equals("eq") && reqValid == 0);
332 boolean valOK = (expValidActual.equals("gt") && validActual > 0) ||
333 (expValidActual.equals("ge") && validActual >= 0) ||
334 (expValidActual.equals("eq") && validActual == 0);
335 if (reqOK && valOK) {
336 logln("Ok: " + label + "; req=" + req + ", valid=" + valid +
337 ", actual=" + actual);
339 errln("FAIL: " + label + "; req=" + req + ", valid=" + valid +
340 ", actual=" + actual +
341 (reqOK ? "" : "\n req !" + expReqValid + " valid") +
342 (valOK ? "" : "\n val !" + expValidActual + " actual"));
347 * Interface used by checkService defining a protocol to create an
348 * object, given a requested locale.
350 interface ServiceFacade {
351 Object create(ULocale requestedLocale);
355 * Interface used by checkService defining a protocol to get a
356 * contained subobject, given its parent object.
358 interface Subobject {
359 Object get(Object parent);
363 * Interface used by checkService defining a protocol to register
364 * and unregister a service object prototype.
366 interface Registrar {
367 Object register(ULocale loc, Object prototype);
368 boolean unregister(Object key);
372 * Use reflection to call getLocale() on the given object to
373 * determine both the valid and the actual locale. Verify these
376 void checkObject(String requestedLocale, Object obj,
377 String expReqValid, String expValidActual) {
378 Class[] getLocaleParams = new Class[] { ULocale.Type.class };
380 Class cls = obj.getClass();
381 Method getLocale = cls.getMethod("getLocale", getLocaleParams);
382 ULocale valid = (ULocale) getLocale.invoke(obj, new Object[] {
383 ULocale.VALID_LOCALE });
384 ULocale actual = (ULocale) getLocale.invoke(obj, new Object[] {
385 ULocale.ACTUAL_LOCALE });
386 checklocs(cls.getName(), requestedLocale,
387 valid.toLocale(), actual.toLocale(),
388 expReqValid, expValidActual);
391 // Make the following exceptions _specific_ -- do not
392 // catch(Exception), since that will catch the exception
393 // that errln throws.
394 catch(NoSuchMethodException e1) {
395 // no longer an error, Currency has no getLocale
396 // errln("FAIL: reflection failed: " + e1);
397 } catch(SecurityException e2) {
398 errln("FAIL: reflection failed: " + e2);
399 } catch(IllegalAccessException e3) {
400 errln("FAIL: reflection failed: " + e3);
401 } catch(IllegalArgumentException e4) {
402 errln("FAIL: reflection failed: " + e4);
403 } catch(InvocationTargetException e5) {
404 // no longer an error, Currency has no getLocale
405 // errln("FAIL: reflection failed: " + e5);
410 * Verify the correct getLocale() behavior for the given service.
411 * @param requestedLocale the locale to request. This MUST BE
412 * FAKE. In other words, it should be something like
413 * en_US_FAKEVARIANT so this method can verify correct fallback
415 * @param svc a factory object that can create the object to be
416 * tested. This isn't necessary here (one could just pass in the
417 * object) but is required for the overload of this method that
420 void checkService(String requestedLocale, ServiceFacade svc) {
421 checkService(requestedLocale, svc, null, null);
425 * Verify the correct getLocale() behavior for the given service.
426 * @param requestedLocale the locale to request. This MUST BE
427 * FAKE. In other words, it should be something like
428 * en_US_FAKEVARIANT so this method can verify correct fallback
430 * @param svc a factory object that can create the object to be
432 * @param sub an object that can be used to retrieve a subobject
433 * which should also be tested. May be null.
434 * @param reg an object that supplies the registration and
435 * unregistration functionality to be tested. May be null.
437 void checkService(String requestedLocale, ServiceFacade svc,
438 Subobject sub, Registrar reg) {
439 ULocale req = new ULocale(requestedLocale);
440 Object obj = svc.create(req);
441 checkObject(requestedLocale, obj, "gt", "ge");
443 Object subobj = sub.get(obj);
444 checkObject(requestedLocale, subobj, "gt", "ge");
447 logln("Info: Registering service");
448 Object key = reg.register(req, obj);
449 Object objReg = svc.create(req);
450 checkObject(requestedLocale, objReg, "eq", "eq");
452 Object subobj = sub.get(obj);
453 // Assume subobjects don't come from services, so
454 // their metadata should be structured normally.
455 checkObject(requestedLocale, subobj, "gt", "ge");
457 logln("Info: Unregistering service");
458 if (!reg.unregister(key)) {
459 errln("FAIL: unregister failed");
461 Object objUnreg = svc.create(req);
462 checkObject(requestedLocale, objUnreg, "gt", "ge");
465 private static final int LOCALE_SIZE = 9;
466 private static final String[][] rawData2 = new String[][]{
468 { "en", "fr", "ca", "el", "no", "zh", "de", "es", "ja" },
470 { "", "", "", "", "", "Hans", "", "", "" },
472 { "US", "FR", "ES", "GR", "NO", "CN", "DE", "", "JP" },
474 { "", "", "", "", "NY", "", "", "", "" },
476 { "en_US", "fr_FR", "ca_ES",
477 "el_GR", "no_NO_NY", "zh_Hans_CN",
478 "de_DE@collation=phonebook", "es@collation=traditional", "ja_JP@calendar=japanese" },
480 { "eng", "fra", "cat", "ell", "nor", "zho", "deu", "spa", "jpn" },
482 { "USA", "FRA", "ESP", "GRC", "NOR", "CHN", "DEU", "", "JPN" },
484 { "409", "40c", "403", "408", "814", "804", "407", "a", "411" },
486 /* display language (English) */
487 { "English", "French", "Catalan", "Greek", "Norwegian", "Chinese", "German", "Spanish", "Japanese" },
488 /* display script code (English) */
489 { "", "", "", "", "", "Simplified Han", "", "", "" },
490 /* display country (English) */
491 { "United States", "France", "Spain", "Greece", "Norway", "China", "Germany", "", "Japan" },
492 /* display variant (English) */
493 { "", "", "", "", "NY", "", "", "", "" },
494 /* display name (English) */
495 { "English (United States)", "French (France)", "Catalan (Spain)",
496 "Greek (Greece)", "Norwegian (Norway, NY)", "Chinese (Simplified Han, China)",
497 "German (Germany, Collation=Phonebook Sort Order)", "Spanish (Collation=Traditional)", "Japanese (Japan, Calendar=Japanese Calendar)" },
499 /* display language (French) */
500 { "anglais", "fran\\u00E7ais", "catalan", "grec", "norv\\u00E9gien", "chinois", "allemand", "espagnol", "japonais" },
501 /* display script code (French) */
502 { "", "", "", "", "", "Hans", "", "", "" },
503 /* display country (French) */
504 { "\\u00C9tats-Unis", "France", "Espagne", "Gr\\u00E8ce", "Norv\\u00E8ge", "Chine", "Allemagne", "", "Japon" },
505 /* display variant (French) */
506 { "", "", "", "", "NY", "", "", "", "" },
507 /* display name (French) */
508 { "anglais (\\u00C9tats-Unis)", "fran\\u00E7ais (France)", "catalan (Espagne)",
509 "grec (Gr\\u00E8ce)", "norv\\u00E9gien (Norv\\u00E8ge, NY)", "chinois (Hans, Chine)",
510 "allemand (Allemagne, Ordonnancement=Ordre de l'annuaire)", "espagnol (Ordonnancement=Ordre traditionnel)", "japonais (Japon, Calendrier=Calendrier japonais)" },
512 /* display language (Catalan) */
513 { "angl\\u00E8s", "franc\\u00E8s", "catal\\u00E0", "grec", "noruec", "xin\\u00E9s", "alemany", "espanyol", "japon\\u00E8s" },
514 /* display script code (Catalan) */
515 { "", "", "", "", "", "Hans", "", "", "" },
516 /* display country (Catalan) */
517 { "Estats Units", "Fran\\u00E7a", "Espanya", "Gr\\u00E8cia", "Noruega", "Xina", "Alemanya", "", "Jap\\u00F3" },
518 /* display variant (Catalan) */
519 { "", "", "", "", "NY", "", "", "", "" },
520 /* display name (Catalan) */
521 { "angl\\u00E8s (Estats Units)", "franc\\u00E8s (Fran\\u00E7a)", "catal\\u00E0 (Espanya)",
522 "grec (Gr\\u00E8cia)", "noruec (Noruega, NY)", "xin\\u00E9s (Hans, Xina)",
523 "alemany (Alemanya, COLLATION=PHONEBOOK)", "espanyol (COLLATION=TRADITIONAL)", "japon\\u00E8s (Jap\\u00F3, CALENDAR=JAPANESE)" },
525 /* display language (Greek) */
527 "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac",
528 "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac",
529 "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac",
530 "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac",
531 "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac",
532 "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC",
533 "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC",
534 "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC",
535 "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC"
537 /* display script code (Greek) */
538 { "", "", "", "", "", "Hans", "", "", "" },
539 /* display country (Greek) */
541 "\\u0397\\u03bd\\u03c9\\u03bc\\u03ad\\u03bd\\u03b5\\u03c2 \\u03a0\\u03bf\\u03bb\\u03b9\\u03c4\\u03b5\\u03af\\u03b5\\u03c2",
542 "\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1",
543 "\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1",
544 "\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1",
545 "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1",
546 "\\u039A\\u03AF\\u03BD\\u03B1",
547 "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1",
549 "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1"
551 /* display variant (Greek) */
552 { "", "", "", "", "NY", "", "", "", "" }, /* TODO: currently there is no translation for NY in Greek fix this test when we have it */
553 /* display name (Greek) */
555 "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac (\\u0397\\u03bd\\u03c9\\u03bc\\u03ad\\u03bd\\u03b5\\u03c2 \\u03a0\\u03bf\\u03bb\\u03b9\\u03c4\\u03b5\\u03af\\u03b5\\u03c2)",
556 "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac (\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1)",
557 "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac (\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1)",
558 "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac (\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1)",
559 "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac (\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1, NY)",
560 "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC (Hans, \\u039A\\u03AF\\u03BD\\u03B1)",
561 "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1, COLLATION=PHONEBOOK)",
562 "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (COLLATION=TRADITIONAL)",
563 "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC (\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1, CALENDAR=JAPANESE)"
566 // private static final int ENGLISH = 0;
567 // private static final int FRENCH = 1;
568 // private static final int CATALAN = 2;
569 // private static final int GREEK = 3;
570 // private static final int NORWEGIAN = 4;
571 private static final int LANG = 0;
572 private static final int SCRIPT = 1;
573 private static final int CTRY = 2;
574 private static final int VAR = 3;
575 private static final int NAME = 4;
576 // private static final int LANG3 = 5;
577 // private static final int CTRY3 = 6;
578 // private static final int LCID = 7;
579 // private static final int DLANG_EN = 8;
580 // private static final int DSCRIPT_EN = 9;
581 // private static final int DCTRY_EN = 10;
582 // private static final int DVAR_EN = 11;
583 // private static final int DNAME_EN = 12;
584 // private static final int DLANG_FR = 13;
585 // private static final int DSCRIPT_FR = 14;
586 // private static final int DCTRY_FR = 15;
587 // private static final int DVAR_FR = 16;
588 // private static final int DNAME_FR = 17;
589 // private static final int DLANG_CA = 18;
590 // private static final int DSCRIPT_CA = 19;
591 // private static final int DCTRY_CA = 20;
592 // private static final int DVAR_CA = 21;
593 // private static final int DNAME_CA = 22;
594 // private static final int DLANG_EL = 23;
595 // private static final int DSCRIPT_EL = 24;
596 // private static final int DCTRY_EL = 25;
597 // private static final int DVAR_EL = 26;
598 // private static final int DNAME_EL = 27;
600 public void TestBasicGetters() {
602 logln("Testing Basic Getters\n");
603 for (i = 0; i < LOCALE_SIZE; i++) {
604 String testLocale=(rawData2[NAME][i]);
605 logln("Testing "+ testLocale+".....\n");
607 String lang =ULocale.getLanguage(testLocale);
608 if (0 !=lang.compareTo(rawData2[LANG][i])) {
609 errln(" Language code mismatch: "+lang+" versus "+ rawData2[LANG][i]);
612 String ctry=ULocale.getCountry(testLocale);
613 if (0 !=ctry.compareTo(rawData2[CTRY][i])) {
614 errln(" Country code mismatch: "+ctry+" versus "+ rawData2[CTRY][i]);
617 String var=ULocale.getVariant(testLocale);
618 if (0 !=var.compareTo(rawData2[VAR][i])) {
619 errln(" Variant code mismatch: "+var+" versus "+ rawData2[VAR][i]);
622 String name = ULocale.getName(testLocale);
623 if (0 !=name.compareTo(rawData2[NAME][i])) {
624 errln(" Name mismatch: "+name+" versus "+ rawData2[NAME][i]);
630 public void TestPrefixes() {
631 // POSIX ids are no longer handled by getName, so POSIX failures are ignored
632 final String [][] testData = new String[][]{
633 /* null canonicalize() column means "expect same as getName()" */
634 {"sv", "", "FI", "AL", "sv-fi-al", "sv_FI_AL", null},
635 {"en", "", "GB", "", "en-gb", "en_GB", null},
636 {"i-hakka", "", "MT", "XEMXIJA", "i-hakka_MT_XEMXIJA", "i-hakka_MT_XEMXIJA", null},
637 {"i-hakka", "", "CN", "", "i-hakka_CN", "i-hakka_CN", null},
638 {"i-hakka", "", "MX", "", "I-hakka_MX", "i-hakka_MX", null},
639 {"x-klingon", "", "US", "SANJOSE", "X-KLINGON_us_SANJOSE", "x-klingon_US_SANJOSE", null},
641 {"de", "", "", "1901", "de-1901", "de__1901", null},
642 {"mr", "", "", "", "mr.utf8", "mr.utf8", "mr"},
643 {"de", "", "TV", "", "de-tv.koi8r", "de_TV.koi8r", "de_TV"},
644 {"x-piglatin", "", "ML", "", "x-piglatin_ML.MBE", "x-piglatin_ML.MBE", "x-piglatin_ML"}, /* Multibyte English */
645 {"i-cherokee", "","US", "", "i-Cherokee_US.utf7", "i-cherokee_US.utf7", "i-cherokee_US"},
646 {"x-filfli", "", "MT", "FILFLA", "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA"},
647 {"no", "", "NO", "NY_B", "no-no-ny.utf32@B", "no_NO_NY.utf32@B", "no_NO_NY_B"},
648 {"no", "", "NO", "B", "no-no.utf32@B", "no_NO.utf32@B", "no_NO_B"},
649 {"no", "", "", "NY", "no__ny", "no__NY", null},
650 {"no", "", "", "NY", "no@ny", "no@ny", "no__NY"},
651 {"el", "Latn", "", "", "el-latn", "el_Latn", null},
652 {"en", "Cyrl", "RU", "", "en-cyrl-ru", "en_Cyrl_RU", null},
653 {"zh", "Hant", "TW", "STROKE", "zh-hant_TW_STROKE", "zh_Hant_TW_STROKE", "zh_Hant_TW@collation=stroke"},
654 {"zh", "Hant", "CN", "STROKE", "zh-hant_CN_STROKE", "zh_Hant_CN_STROKE", "zh_Hant_CN@collation=stroke"},
655 {"zh", "Hant", "TW", "PINYIN", "zh-hant_TW_PINYIN", "zh_Hant_TW_PINYIN", "zh_Hant_TW@collation=pinyin"},
656 {"qq", "Qqqq", "QQ", "QQ", "qq_Qqqq_QQ_QQ", "qq_Qqqq_QQ_QQ", null},
657 {"qq", "Qqqq", "", "QQ", "qq_Qqqq__QQ", "qq_Qqqq__QQ", null},
658 {"ab", "Cdef", "GH", "IJ", "ab_cdef_gh_ij", "ab_Cdef_GH_IJ", null}, /* total garbage */
661 {"", "", "", "", "@FOO=bar", "@foo=bar", null},
662 {"", "", "", "", "_@FOO=bar", "@foo=bar", null},
663 {"", "", "", "", "__@FOO=bar", "@foo=bar", null},
664 {"", "", "", "FOO", "__foo@FOO=bar", "__FOO@foo=bar", null}, // we have some of these prefixes
667 String loc, buf,buf1;
668 final String [] testTitles = {
669 "ULocale.getLanguage()",
670 "ULocale.getScript()",
671 "ULocale.getCountry()",
672 "ULocale.getVariant()",
679 for(int row=0;row<testData.length;row++) {
680 loc = testData[row][NAME];
681 logln("Test #"+row+": "+loc);
683 uloc = new ULocale(loc);
685 for(int n=0;n<=(NAME+2);n++) {
686 if(n==NAME) continue;
690 buf = ULocale.getLanguage(loc);
691 buf1 = uloc.getLanguage();
695 buf = ULocale.getScript(loc);
696 buf1 = uloc.getScript();
700 buf = ULocale.getCountry(loc);
701 buf1 = uloc.getCountry();
705 buf = ULocale.getVariant(loc);
710 buf = ULocale.getName(loc);
711 buf1 = uloc.getName();
715 buf = ULocale.canonicalize(loc);
716 buf1 = ULocale.createCanonical(loc).getName();
724 logln("#"+row+": "+testTitles[n]+" on "+loc+": -> ["+buf+"]");
726 String expected = testData[row][n];
727 if (expected == null && n == (NAME+2)) {
728 expected = testData[row][NAME+1];
731 // ignore POSIX failures in getName, we don't spec behavior in this case
733 (expected.indexOf('.') != -1 ||
734 expected.indexOf('@') != -1)) {
738 if(buf.compareTo(expected)!=0) {
739 errln("#"+row+": "+testTitles[n]+" on "+loc+": -> ["+buf+"] (expected '"+expected+"'!)");
741 if(buf1.compareTo(expected)!=0) {
742 errln("#"+row+": "+testTitles[n]+" on ULocale object "+loc+": -> ["+buf1+"] (expected '"+expected+"'!)");
748 public void TestObsoleteNames(){
749 final String[][] tests = new String[][]{
750 /* locale, language3, language2, Country3, country2 */
751 { "eng_USA", "eng", "en", "USA", "US" },
752 { "kok", "kok", "kok", "", "" },
753 { "in", "ind", "in", "", "" },
754 { "id", "ind", "id", "", "" }, /* NO aliasing */
755 { "sh", "srp", "sh", "", "" },
756 { "zz_CS", "", "zz", "SCG", "CS" },
757 { "zz_FX", "", "zz", "FXX", "FX" },
758 { "zz_RO", "", "zz", "ROU", "RO" },
759 { "zz_TP", "", "zz", "TMP", "TP" },
760 { "zz_TL", "", "zz", "TLS", "TL" },
761 { "zz_ZR", "", "zz", "ZAR", "ZR" },
762 { "zz_FXX", "", "zz", "FXX", "FX" }, /* no aliasing. Doesn't go to PS(PSE). */
763 { "zz_ROM", "", "zz", "ROU", "RO" },
764 { "zz_ROU", "", "zz", "ROU", "RO" },
765 { "zz_ZAR", "", "zz", "ZAR", "ZR" },
766 { "zz_TMP", "", "zz", "TMP", "TP" },
767 { "zz_TLS", "", "zz", "TLS", "TL" },
768 { "zz_YUG", "", "zz", "YUG", "YU" },
769 { "mlt_PSE", "mlt", "mt", "PSE", "PS" },
770 { "iw", "heb", "iw", "", "" },
771 { "ji", "yid", "ji", "", "" },
772 { "jw", "jaw", "jw", "", "" },
773 { "sh", "srp", "sh", "", "" },
774 { "", "", "", "", "" }
777 for(int i=0;i<tests.length;i++){
778 String locale = tests[i][0];
779 logln("** Testing : "+ locale);
781 ULocale uloc = new ULocale(locale);
783 buff = ULocale.getISO3Language(locale);
784 if(buff.compareTo(tests[i][1])!=0){
785 errln("FAIL: ULocale.getISO3Language("+locale+")=="+
786 buff+",\t expected "+tests[i][1]);
788 logln(" ULocale.getISO3Language("+locale+")=="+buff);
791 buff1 = uloc.getISO3Language();
792 if(buff1.compareTo(tests[i][1])!=0){
793 errln("FAIL: ULocale.getISO3Language("+locale+")=="+
794 buff+",\t expected "+tests[i][1]);
796 logln(" ULocale.getISO3Language("+locale+")=="+buff);
799 buff = ULocale.getLanguage(locale);
800 if(buff.compareTo(tests[i][2])!=0){
801 errln("FAIL: ULocale.getLanguage("+locale+")=="+
802 buff+",\t expected "+tests[i][2]);
804 logln(" ULocale.getLanguage("+locale+")=="+buff);
807 buff = ULocale.getISO3Country(locale);
808 if(buff.compareTo(tests[i][3])!=0){
809 errln("FAIL: ULocale.getISO3Country("+locale+")=="+
810 buff+",\t expected "+tests[i][3]);
812 logln(" ULocale.getISO3Country("+locale+")=="+buff);
815 buff1 = uloc.getISO3Country();
816 if(buff1.compareTo(tests[i][3])!=0){
817 errln("FAIL: ULocale.getISO3Country("+locale+")=="+
818 buff+",\t expected "+tests[i][3]);
820 logln(" ULocale.getISO3Country("+locale+")=="+buff);
823 buff = ULocale.getCountry(locale);
824 if(buff.compareTo(tests[i][4])!=0){
825 errln("FAIL: ULocale.getCountry("+locale+")=="+
826 buff+",\t expected "+tests[i][4]);
828 logln(" ULocale.getCountry("+locale+")=="+buff);
832 if (ULocale.getLanguage("iw_IL").compareTo( ULocale.getLanguage("he_IL"))==0) {
833 errln("he,iw ULocale.getLanguage mismatch");
836 String buff = ULocale.getLanguage("kok_IN");
837 if(buff.compareTo("kok")!=0){
838 errln("ULocale.getLanguage(\"kok\") failed. Expected: kok Got: "+buff);
841 public void TestCanonicalization(){
842 final String[][]testCases = new String[][]{
843 { "ca_ES_PREEURO", "ca_ES_PREEURO", "ca_ES@currency=ESP" },
844 { "de_AT_PREEURO", "de_AT_PREEURO", "de_AT@currency=ATS" },
845 { "de_DE_PREEURO", "de_DE_PREEURO", "de_DE@currency=DEM" },
846 { "de_LU_PREEURO", "de_LU_PREEURO", "de_LU@currency=EUR" },
847 { "el_GR_PREEURO", "el_GR_PREEURO", "el_GR@currency=GRD" },
848 { "en_BE_PREEURO", "en_BE_PREEURO", "en_BE@currency=BEF" },
849 { "en_IE_PREEURO", "en_IE_PREEURO", "en_IE@currency=IEP" },
850 { "es_ES_PREEURO", "es_ES_PREEURO", "es_ES@currency=ESP" },
851 { "eu_ES_PREEURO", "eu_ES_PREEURO", "eu_ES@currency=ESP" },
852 { "fi_FI_PREEURO", "fi_FI_PREEURO", "fi_FI@currency=FIM" },
853 { "fr_BE_PREEURO", "fr_BE_PREEURO", "fr_BE@currency=BEF" },
854 { "fr_FR_PREEURO", "fr_FR_PREEURO", "fr_FR@currency=FRF" },
855 { "fr_LU_PREEURO", "fr_LU_PREEURO", "fr_LU@currency=LUF" },
856 { "ga_IE_PREEURO", "ga_IE_PREEURO", "ga_IE@currency=IEP" },
857 { "gl_ES_PREEURO", "gl_ES_PREEURO", "gl_ES@currency=ESP" },
858 { "it_IT_PREEURO", "it_IT_PREEURO", "it_IT@currency=ITL" },
859 { "nl_BE_PREEURO", "nl_BE_PREEURO", "nl_BE@currency=BEF" },
860 { "nl_NL_PREEURO", "nl_NL_PREEURO", "nl_NL@currency=NLG" },
861 { "pt_PT_PREEURO", "pt_PT_PREEURO", "pt_PT@currency=PTE" },
862 { "de__PHONEBOOK", "de__PHONEBOOK", "de@collation=phonebook" },
863 { "de_PHONEBOOK", "de__PHONEBOOK", "de@collation=phonebook" },
864 { "en_GB_EURO", "en_GB_EURO", "en_GB@currency=EUR" },
865 { "en_GB@EURO", null, "en_GB@currency=EUR" }, /* POSIX ID */
866 { "es__TRADITIONAL", "es__TRADITIONAL", "es@collation=traditional" },
867 { "hi__DIRECT", "hi__DIRECT", "hi@collation=direct" },
868 { "ja_JP_TRADITIONAL", "ja_JP_TRADITIONAL", "ja_JP@calendar=japanese" },
869 { "th_TH_TRADITIONAL", "th_TH_TRADITIONAL", "th_TH@calendar=buddhist" },
870 { "zh_TW_STROKE", "zh_TW_STROKE", "zh_TW@collation=stroke" },
871 { "zh__PINYIN", "zh__PINYIN", "zh@collation=pinyin" },
872 { "zh@collation=pinyin", "zh@collation=pinyin", "zh@collation=pinyin" },
873 { "zh_CN@collation=pinyin", "zh_CN@collation=pinyin", "zh_CN@collation=pinyin" },
874 { "zh_CN_CA@collation=pinyin", "zh_CN_CA@collation=pinyin", "zh_CN_CA@collation=pinyin" },
875 { "en_US_POSIX", "en_US_POSIX", "en_US_POSIX" },
876 { "hy_AM_REVISED", "hy_AM_REVISED", "hy_AM_REVISED" },
877 { "no_NO_NY", "no_NO_NY", "no_NO_NY" /* not: "nn_NO" [alan ICU3.0] */ },
878 { "no@ny", null, "no__NY" /* not: "nn" [alan ICU3.0] */ }, /* POSIX ID */
879 { "no-no.utf32@B", null, "no_NO_B" /* not: "nb_NO_B" [alan ICU3.0] */ }, /* POSIX ID */
880 { "qz-qz@Euro", null, "qz_QZ@currency=EUR" }, /* qz-qz uses private use iso codes */
881 { "en-BOONT", "en__BOONT", "en__BOONT" }, /* registered name */
882 { "de-1901", "de__1901", "de__1901" }, /* registered name */
883 { "de-1906", "de__1906", "de__1906" }, /* registered name */
884 { "sr-SP-Cyrl", "sr_SP_CYRL", "sr_Cyrl_RS" }, /* .NET name */
885 { "sr-SP-Latn", "sr_SP_LATN", "sr_Latn_RS" }, /* .NET name */
886 { "sr_YU_CYRILLIC", "sr_YU_CYRILLIC", "sr_Cyrl_RS" }, /* Linux name */
887 { "uz-UZ-Cyrl", "uz_UZ_CYRL", "uz_Cyrl_UZ" }, /* .NET name */
888 { "uz-UZ-Latn", "uz_UZ_LATN", "uz_Latn_UZ" }, /* .NET name */
889 { "zh-CHS", "zh_CHS", "zh_Hans" }, /* .NET name */
890 { "zh-CHT", "zh_CHT", "zh_Hant" }, /* .NET name This may change back to zh_Hant */
892 /* posix behavior that used to be performed by getName */
893 { "mr.utf8", null, "mr" },
894 { "de-tv.koi8r", null, "de_TV" },
895 { "x-piglatin_ML.MBE", null, "x-piglatin_ML" },
896 { "i-cherokee_US.utf7", null, "i-cherokee_US" },
897 { "x-filfli_MT_FILFLA.gb-18030", null, "x-filfli_MT_FILFLA" },
898 { "no-no-ny.utf8@B", null, "no_NO_NY_B" /* not: "nn_NO" [alan ICU3.0] */ }, /* @ ignored unless variant is empty */
900 /* fleshing out canonicalization */
901 /* sort keywords, ';' is separator so not present at end in canonical form */
902 { "en_Hant_IL_VALLEY_GIRL@currency=EUR;calendar=Japanese;", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" },
903 /* already-canonical ids are not changed */
904 { "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" },
905 /* PRE_EURO and EURO conversions don't affect other keywords */
907 { "es_ES_PREEURO@CALendar=Japanese", "es_ES_PREEURO@calendar=Japanese", "es_ES@calendar=Japanese;currency=ESP" },
908 { "es_ES_EURO@SHOUT=zipeedeedoodah", "es_ES_EURO@shout=zipeedeedoodah", "es_ES@currency=EUR;shout=zipeedeedoodah" },
910 /* currency keyword overrides PRE_EURO and EURO currency */
912 { "es_ES_PREEURO@currency=EUR", "es_ES_PREEURO@currency=EUR", "es_ES@currency=EUR" },
913 { "es_ES_EURO@currency=ESP", "es_ES_EURO@currency=ESP", "es_ES@currency=ESP" },
915 /* norwegian is just too weird, if we handle things in their full generality */
916 /* this is a negative test to show that we DO NOT handle 'lang=no,var=NY' specially. */
917 { "no-Hant-GB_NY@currency=$$$", "no_Hant_GB_NY@currency=$$$", "no_Hant_GB_NY@currency=$$$" /* not: "nn_Hant_GB@currency=$$$" [alan ICU3.0] */ },
919 /* test cases reflecting internal resource bundle usage */
920 /* root is just a language */
921 { "root@kw=foo", "root@kw=foo", "root@kw=foo" },
922 /* level 2 canonicalization should not touch basename when there are keywords and it is null */
923 { "@calendar=gregorian", "@calendar=gregorian", "@calendar=gregorian" },
926 for(int i = 0; i< testCases.length;i++){
927 String[] testCase = testCases[i];
928 String source = testCase[0];
929 String level1Expected = testCase[1];
930 String level2Expected = testCase[2];
932 if (level1Expected != null) { // null means we have no expectations for how this case is handled
933 String level1 = ULocale.getName(source);
934 if (!level1.equals(level1Expected)) {
935 errln("ULocale.getName error for: '" + source +
936 "' expected: '" + level1Expected + "' but got: '" + level1 + "'");
938 logln("Ulocale.getName for: '" + source + "' returned: '" + level1 + "'");
941 logln("ULocale.getName skipped: '" + source + "'");
944 if (level2Expected != null) {
945 String level2 = ULocale.canonicalize(source);
946 if(!level2.equals(level2Expected)){
947 errln("ULocale.getName error for: '" + source +
948 "' expected: '" + level2Expected + "' but got: '" + level2 + "'");
950 logln("Ulocale.canonicalize for: '" + source + "' returned: '" + level2 + "'");
953 logln("ULocale.canonicalize skipped: '" + source + "'");
958 public void TestGetAvailable(){
959 ULocale[] locales = ULocale.getAvailableLocales();
960 if(locales.length<10){
961 errln("Did not get the correct result from getAvailableLocales");
963 if(!locales[locales.length-1].getName().equals("zu_ZA")){
964 errln("Did not get the expected result");
968 public void TestDisplayNames() {
969 // consistency check, also check that all data is available
971 ULocale[] locales = ULocale.getAvailableLocales();
972 for (int i = 0; i < locales.length; ++i) {
973 ULocale l = locales[i];
974 String name = l.getDisplayName();
976 logln(l + " --> " + name +
977 ", " + l.getDisplayName(ULocale.GERMAN) +
978 ", " + l.getDisplayName(ULocale.FRANCE));
980 String language = l.getDisplayLanguage();
981 String script = l.getDisplayScriptInContext();
982 String country = l.getDisplayCountry();
983 String variant = l.getDisplayVariant();
985 checkName(name, language, script, country, variant, ULocale.getDefault());
987 for (int j = 0; j < locales.length; ++j) {
988 ULocale dl = locales[j];
990 name = l.getDisplayName(dl);
991 language = l.getDisplayLanguage(dl);
992 script = l.getDisplayScriptInContext(dl);
993 country = l.getDisplayCountry(dl);
994 variant = l.getDisplayVariant(dl);
996 if (!checkName(name, language, script, country, variant, dl)) {
1004 ULocale[] locales = {
1005 ULocale.US, ULocale.GERMANY, ULocale.FRANCE
1008 "Chinese (China)", "Chinesisch (China)", "chinois (Chine)"
1011 "Simplified Chinese (China)", "Chinesisch (vereinfacht) (China)", "chinois simplifi\u00E9 (Chine)"
1013 ULocale locale = new ULocale("zh_CN");
1014 ULocale locale2 = new ULocale("zh_Hans_CN");
1016 for (int i = 0; i < locales.length; ++i) {
1017 String name = locale.getDisplayName(locales[i]);
1018 if (!names[i].equals(name)) {
1019 errln("expected '" + names[i] + "' but got '" + name + "'");
1022 for (int i = 0; i < locales.length; ++i) {
1023 String name = locale2.getDisplayNameWithDialect(locales[i]);
1024 if (!names2[i].equals(name)) {
1025 errln("expected '" + names2[i] + "' but got '" + name + "'");
1029 // test use of context
1031 class TestContextItem {
1032 public String displayLocale;
1033 public DisplayContext dialectHandling;
1034 public DisplayContext capitalization;
1035 public String localeToBeNamed;
1036 public String result;
1037 public TestContextItem(String dLoc, DisplayContext dia, DisplayContext cap, String locToName, String res) {
1038 displayLocale = dLoc;
1039 dialectHandling = dia;
1040 capitalization = cap;
1041 localeToBeNamed = locToName;
1045 final TestContextItem[] items = {
1046 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en", "engelsk" ),
1047 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en", "Engelsk" ),
1048 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en", "Engelsk" ),
1049 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en_US", "engelsk (USA)" ),
1050 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en_US", "Engelsk (USA)" ),
1051 new TestContextItem( "da", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en_US", "Engelsk (USA)" ),
1052 new TestContextItem( "da", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en_US", "amerikansk engelsk" ),
1053 new TestContextItem( "da", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en_US", "Amerikansk engelsk" ),
1054 new TestContextItem( "da", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en_US", "Amerikansk engelsk" ),
1055 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en", "ingl\u00E9s" ),
1056 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en", "Ingl\u00E9s" ),
1057 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en", "Ingl\u00E9s" ),
1058 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en_US", "ingl\u00E9s (Estados Unidos)" ),
1059 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en_US", "Ingl\u00E9s (Estados Unidos)" ),
1060 new TestContextItem( "es", DisplayContext.STANDARD_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en_US", "Ingl\u00E9s (Estados Unidos)" ),
1061 new TestContextItem( "es", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "en_US", "ingl\u00E9s estadounidense" ),
1062 new TestContextItem( "es", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "en_US", "Ingl\u00E9s estadounidense" ),
1063 new TestContextItem( "es", DisplayContext.DIALECT_NAMES, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "en_US", "Ingl\u00E9s estadounidense" ),
1065 for (TestContextItem item: items) {
1066 ULocale locale = new ULocale(item.displayLocale);
1067 LocaleDisplayNames ldn = LocaleDisplayNames.getInstance(locale, item.dialectHandling, item.capitalization);
1068 DisplayContext dialectHandling = ldn.getContext(DisplayContext.Type.DIALECT_HANDLING);
1069 DisplayContext capitalization = ldn.getContext(DisplayContext.Type.CAPITALIZATION);
1070 if (dialectHandling != item.dialectHandling || capitalization != item.capitalization) {
1071 errln("FAIL: displayLocale: " + item.displayLocale + ", dialectHandling: " + item.dialectHandling +
1072 ", capitalization: " + item.capitalization + ", localeToName: " + item.localeToBeNamed +
1073 ", => read back dialectHandling: " + dialectHandling + ", capitalization: " + capitalization);
1075 String result = ldn.localeDisplayName(item.localeToBeNamed);
1076 if (!result.equals(item.result)) {
1077 errln("FAIL: displayLocale: " + item.displayLocale + ", dialectHandling: " + item.dialectHandling +
1078 ", capitalization: " + item.capitalization + ", localeToName: " + item.localeToBeNamed +
1079 ", => expected result: " + item.result + ", got: " + result);
1086 private boolean checkName(String name, String language, String script, String country, String variant, ULocale dl) {
1087 if (!checkInclusion(dl, name, language, "language")) {
1090 if (!checkInclusion(dl, name, script, "script")) {
1093 if (!checkInclusion(dl, name, country, "country")) {
1096 if (!checkInclusion(dl, name, variant, "variant")) {
1102 private boolean checkInclusion(ULocale dl, String name, String substring, String substringName) {
1103 if (substring.length() > 0 && name.indexOf(substring) == -1) {
1104 String country2 = substring.replace('(', '[').replace(')',']').replace('(', '[').replace(')',']');
1105 if (name.indexOf(country2) == -1) {
1106 errln("loc: " + dl + " name '" + name + "' does not contain " +
1108 " '" + substring + "'");
1115 public void TestCoverage() {
1119 String localeID="zh_CN";
1120 String name, language, script, country, variant;
1121 logln("Covering APIs with signature displayXXX(String, String)");
1122 for (i = 0; i < LOCALE_SIZE; i++) {
1124 String testLocale=(rawData2[NAME][i]);
1126 logln("Testing "+ testLocale+".....");
1127 name = ULocale.getDisplayName(localeID, testLocale);
1128 language = ULocale.getDisplayLanguage(localeID, testLocale);
1129 script = ULocale.getDisplayScriptInContext(localeID, testLocale);
1130 country = ULocale.getDisplayCountry(localeID, testLocale);
1131 variant = ULocale.getDisplayVariant(localeID, testLocale);
1132 if (!checkName(name, language, script, country, variant, new ULocale(testLocale))) {
1137 logln("Covering APIs with signature displayXXX(String, ULocale)\n");
1138 for (j = 0; j < LOCALE_SIZE; j++) {
1139 String testLocale=(rawData2[NAME][j]);
1140 ULocale loc = new ULocale(testLocale);
1142 logln("Testing "+ testLocale+".....");
1143 name = ULocale.getDisplayName(localeID, loc);
1144 language = ULocale.getDisplayLanguage(localeID, loc);
1145 script = ULocale.getDisplayScriptInContext(localeID, loc);
1146 country = ULocale.getDisplayCountry(localeID, loc);
1147 variant = ULocale.getDisplayVariant(localeID, loc);
1149 if (!checkName(name, language, script, country, variant, loc)) {
1154 ULocale loc1 = new ULocale("en_US_BROOKLYN");
1155 ULocale loc2 = new ULocale("en","US","BROOKLYN");
1156 if (!loc2.equals(loc1)){
1157 errln("ULocale.ULocale(String a, String b, String c)");
1160 ULocale loc3 = new ULocale("en_US");
1161 ULocale loc4 = new ULocale("en","US");
1162 if (!loc4.equals(loc3)){
1163 errln("ULocale.ULocale(String a, String b)");
1166 ULocale loc5 = (ULocale) loc4.clone();
1167 if (!loc5.equals(loc4)){
1168 errln("ULocale.clone should get the same ULocale");
1170 ULocale.getISOCountries(); // To check the result ?!
1173 public void TestBamBm() {
1174 // "bam" shouldn't be there since the official code is 'bm'
1175 String[] isoLanguages = ULocale.getISOLanguages();
1176 for (int i = 0; i < isoLanguages.length; ++i) {
1177 if ("bam".equals(isoLanguages[i])) {
1180 if (i > 0 && isoLanguages[i].compareTo(isoLanguages[i-1]) <= 0) {
1181 errln("language list out of order: '" + isoLanguages[i] + " <= " + isoLanguages[i-1]);
1186 public void TestDisplayKeyword() {
1187 //prepare testing data
1189 String[] data = {"en_US@collation=phonebook;calendar=islamic-civil",
1190 "zh_Hans@collation=pinyin;calendar=chinese",
1191 "foo_Bar_BAZ@collation=traditional;calendar=buddhist"};
1193 for (int i = 0; i < data.length; i++) {
1194 String localeID = data[i];
1196 logln("Testing locale " + localeID + " ...");
1197 ULocale loc = new ULocale(localeID);
1199 Iterator it = loc.getKeywords();
1200 Iterator it2 = ULocale.getKeywords(localeID);
1201 //it and it2 are not equal here. No way to verify their equivalence yet.
1202 while(it.hasNext()) {
1203 String key = (String)it.next();
1204 String key2 = (String)it2.next();
1205 if (!key.equals(key2)) {
1206 errln("FAIL: static and non-static getKeywords returned different results.");
1209 //To verify display of Keyword
1210 // display the above key in English
1211 String s0 = ULocale.getDisplayKeyword(key); //display in default locale
1212 String s1 = ULocale.getDisplayKeyword(key, ULocale.US);
1213 String s2 = ULocale.getDisplayKeyword(key, "en_US");
1214 if (!s1.equals(s2)) {
1215 errln ("FAIL: one of the getDisplayKeyword methods failed.");
1217 if (ULocale.getDefault().equals(ULocale.US) && !s1.equals(s0)) {
1218 errln ("FAIL: getDisplayKeyword methods failed for the default locale.");
1220 if (!s1.equals(h[0].get(key))) {
1221 errln("Locale " + localeID + " getDisplayKeyword for key: " + key +
1222 " in English expected \"" + h[0].get(key) + "\" saw \"" + s1 + "\" instead");
1224 logln("OK: getDisplayKeyword for key: " + key + " in English got " + s1);
1227 // display the key in S-Chinese
1228 s1 = ULocale.getDisplayKeyword(key, ULocale.CHINA);
1229 s2 = ULocale.getDisplayKeyword(key, "zh_Hans");
1230 if (!s1.equals(s2)) {
1231 errln ("one of the getDisplayKeyword methods failed.");
1233 if (!s1.equals(h[1].get(key))) {
1234 errln("Locale " + localeID + " getDisplayKeyword for key: " + key +
1235 " in Chinese expected \"" + h[1].get(key) + "\" saw \"" + s1 + "\" instead");
1237 logln("OK: getDisplayKeyword for key: " + key + " in Chinese got " + s1);
1240 //To verify display of Keyword values
1241 String type = loc.getKeywordValue(key);
1242 // display type in English
1243 String ss0 = loc.getDisplayKeywordValue(key);
1244 String ss1 = loc.getDisplayKeywordValue(key, ULocale.US);
1245 String ss2 = ULocale.getDisplayKeywordValue(localeID, key, "en_US");
1246 String ss3 = ULocale.getDisplayKeywordValue(localeID, key, ULocale.US);
1247 if (!ss1.equals(ss2) || !ss1.equals(ss3)) {
1248 errln ("FAIL: one of the getDisplayKeywordValue methods failed.");
1250 if (ULocale.getDefault().equals(ULocale.US) && !ss1.equals(ss0)) {
1251 errln ("FAIL: getDisplayKeyword methods failed for the default locale.");
1253 if (!ss1.equals(h[0].get(type))) {
1254 errln(" Locale " + localeID + " getDisplayKeywordValue for key: " + key +
1255 " in English expected \"" + h[0].get(type) + "\" saw \"" + ss1 + "\" instead");
1257 logln("OK: getDisplayKeywordValue for key: " + key + " in English got " + ss1);
1260 // display type in Chinese
1261 ss0 = loc.getDisplayKeywordValue(key);
1262 ss1 = loc.getDisplayKeywordValue(key, ULocale.CHINA);
1263 ss2 = ULocale.getDisplayKeywordValue(localeID, key, "zh_Hans");
1264 ss3 = ULocale.getDisplayKeywordValue(localeID, key, ULocale.CHINA);
1265 if (!ss1.equals(ss2) || !ss1.equals(ss3)) {
1266 errln ("one of the getDisplayKeywordValue methods failed.");
1268 if (!ss1.equals(h[1].get(type))) {
1269 errln("Locale " + localeID + " getDisplayKeywordValue for key: " + key +
1270 " in Chinese expected \"" + h[1].get(type) + "\" saw \"" + ss1 + "\" instead");
1272 logln("OK: getDisplayKeywordValue for key: " + key + " in Chinese got " + ss1);
1278 public void TestDisplayWithKeyword() {
1279 // Note, this test depends on locale display data for the U.S. and Taiwan.
1280 // If the data changes (in particular, the keyTypePattern may change for Taiwan),
1281 // this test will break.
1282 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
1283 DialectHandling.DIALECT_NAMES);
1284 LocaleDisplayNames tdn = LocaleDisplayNames.getInstance(ULocale.TAIWAN,
1285 DialectHandling.DIALECT_NAMES);
1286 String name = dn.localeDisplayName("de@collation=phonebook");
1287 String target = "German (Phonebook Sort Order)";
1288 assertEquals("collation", target, name);
1290 name = tdn.localeDisplayName("de@collation=phonebook");
1291 target = "德文(電話簿排序)"; // \u5FB7\u6587\uFF08\u96FB\u8A71\u7C3F\u6392\u5E8F\uFF09
1292 assertEquals("collation", target, name);
1294 name = dn.localeDisplayName("de@currency=XYZ");
1295 target = "German (Currency: XYZ)";
1296 assertEquals("currency", target, name);
1298 name = dn.localeDisplayName("de@collation=phonebook;currency=XYZ");
1299 target = "German (Phonebook Sort Order, Currency: XYZ)";
1300 assertEquals("currency", target, name);
1302 name = dn.localeDisplayName("de_Latn_DE");
1303 target = "German (Latin, Germany)";
1304 assertEquals("currency", target, name);
1306 name = tdn.localeDisplayName("de@currency=XYZ");
1307 target = "德文(貨幣:XYZ)"; // \u5FB7\u6587\uFF08\u8CA8\u5E63: XYZ\uFF09
1308 assertEquals("currency", target, name);
1310 name = tdn.localeDisplayName("de@collation=phonebook;currency=XYZ");
1311 target = "德文(電話簿排序,貨幣:XYZ)"; // \u5FB7\u6587\uFF08\u96FB\u8A71\u7C3F\u6392\u5E8F\uFF09,\u5FB7\u6587\uFF08\u8CA8\u5E63: XYZ\uFF09
1312 assertEquals("collation", target, name);
1314 name = dn.localeDisplayName("de@foo=bar");
1315 target = "German (foo=bar)";
1316 assertEquals("foo", target, name);
1318 name = tdn.localeDisplayName("de@foo=bar");
1319 target = "德文(foo=bar)"; // \u5FB7\u6587\uFF08foo=bar\uFF09
1320 assertEquals("foo", target, name);
1322 ULocale locale = ULocale.forLanguageTag("de-x-foobar");
1323 name = dn.localeDisplayName(locale);
1324 target = "German (Private-Use: foobar)";
1325 assertEquals("foobar", target, name);
1327 name = tdn.localeDisplayName(locale);
1328 target = "德文(私人使用:foobar)"; // \u5FB7\u6587\uFF08\u79C1\u4EBA\u4F7F\u7528: foobar\uFF09
1329 assertEquals("foobar", target, name);
1332 private void initHashtable() {
1333 h[0] = new HashMap<String, String>();
1334 h[1] = new HashMap<String, String>();
1336 //display in English
1337 h[0].put("collation", "Sort Order");
1338 h[0].put("calendar", "Calendar");
1339 h[0].put("currency", "Currency");
1340 h[0].put("phonebook", "Phonebook Sort Order");
1341 h[0].put("pinyin", "Pinyin Sort Order");
1342 h[0].put("traditional", "Traditional Sort Order");
1343 h[0].put("stroke", "Stroke Order");
1344 h[0].put("japanese", "Japanese Calendar");
1345 h[0].put("buddhist", "Buddhist Calendar");
1346 h[0].put("islamic", "Islamic Calendar");
1347 h[0].put("islamic-civil", "Islamic Calendar (tabular, civil epoch)" );
1348 h[0].put("hebrew", "Hebrew Calendar");
1349 h[0].put("chinese", "Chinese Calendar");
1350 h[0].put("gregorian", "Gregorian Calendar" );
1352 //display in S-Chinese
1353 h[1].put("collation", "\u6392\u5E8F");
1354 h[1].put("calendar", "\u65E5\u5386");
1355 h[1].put("currency", "\u8D27\u5E01");
1356 h[1].put("phonebook", "\u7535\u8BDD\u7C3F\u6392\u5E8F\u987A\u5E8F");
1357 h[1].put("pinyin", "\u62FC\u97F3\u6392\u5E8F");
1358 h[1].put("stroke", "\u7B14\u5212\u987A\u5E8F");
1359 h[1].put("traditional", "\u4F20\u7EDF\u6392\u5E8F\u987A\u5E8F");
1360 h[1].put("japanese", "\u65E5\u672C\u65E5\u5386");
1361 h[1].put("buddhist", "\u4F5B\u6559\u65E5\u5386");
1362 h[1].put("islamic", "\u4F0A\u65AF\u5170\u65E5\u5386");
1363 h[1].put("islamic-civil", "\u4F0A\u65AF\u5170\u5E0C\u5409\u6765\u65E5\u5386");
1364 h[1].put("hebrew", "\u5E0C\u4F2F\u6765\u65E5\u5386");
1365 h[1].put("chinese", "\u519C\u5386");
1366 h[1].put("gregorian", "\u516C\u5386");
1369 //Hashtables for storing expected display of keys/types of locale in English and Chinese
1370 private static Map[] h = new Map[2];
1372 private static final String ACCEPT_LANGUAGE_TESTS[][] = {
1373 /*# result fallback? */
1374 /*0*/ { "mt_MT", "false" },
1375 /*1*/ { "en", "false" },
1376 /*2*/ { "en", "true" }, // fell back from en-zzz to en
1377 /*3*/ { null, "true" },
1378 /*4*/ { "es", "false" },
1379 /*5*/ { "de", "false" },
1380 /*6*/ { "zh_TW", "false" },
1381 /*7*/ { "zh", "true" },
1384 private static final String ACCEPT_LANGUAGE_HTTP[] = {
1385 /*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",
1386 /*1*/ "ja;q=0.5, en;q=0.8, tlh",
1387 /*2*/ "en-zzz, de-lx;q=0.8",
1388 /*3*/ "mga-ie;q=0.9, tlh",
1389 /*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, "+
1390 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1391 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1392 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1393 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1394 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1395 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1396 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1397 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1398 "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
1400 /*5*/ "de;q=.9, fr;q=.9, xxx-yyy, sr;q=.8",
1406 public void TestAcceptLanguage() {
1407 for(int i = 0 ; i < (ACCEPT_LANGUAGE_HTTP.length); i++) {
1408 Boolean expectBoolean = new Boolean(ACCEPT_LANGUAGE_TESTS[i][1]);
1409 String expectLocale=ACCEPT_LANGUAGE_TESTS[i][0];
1411 logln("#" + i + ": expecting: " + expectLocale + " (" + expectBoolean + ")");
1413 boolean r[] = { false };
1414 ULocale n = ULocale.acceptLanguage(ACCEPT_LANGUAGE_HTTP[i], r);
1415 if((n==null)&&(expectLocale!=null)) {
1416 errln("result was null! line #" + i);
1419 if(((n==null)&&(expectLocale==null)) || (n.toString().equals(expectLocale))) {
1420 logln(" locale: OK." );
1422 errln("expected " + expectLocale + " but got " + n.toString());
1424 if(expectBoolean.equals(new Boolean(r[0]))) {
1425 logln(" bool: OK.");
1427 errln("bool: not OK, was " + new Boolean(r[0]).toString() + " expected " + expectBoolean.toString());
1432 private ULocale[] StringToULocaleArray(String acceptLanguageList){
1433 //following code is copied from
1434 //ULocale.acceptLanguage(String acceptLanguageList, ULocale[] availableLocales, boolean[] fallback)
1435 class ULocaleAcceptLanguageQ implements Comparable {
1437 private double serial;
1438 public ULocaleAcceptLanguageQ(double theq, int theserial) {
1442 public int compareTo(Object o) {
1443 ULocaleAcceptLanguageQ other = (ULocaleAcceptLanguageQ) o;
1444 if(q > other.q) { // reverse - to sort in descending order
1446 } else if(q < other.q) {
1449 if(serial < other.serial) {
1451 } else if(serial > other.serial) {
1454 return 0; // same object
1459 // 1st: parse out the acceptLanguageList into an array
1461 TreeMap map = new TreeMap();
1463 final int l = acceptLanguageList.length();
1466 int itemEnd = acceptLanguageList.indexOf(',',n);
1470 int paramEnd = acceptLanguageList.indexOf(';',n);
1473 if((paramEnd != -1) && (paramEnd < itemEnd)) {
1474 /* semicolon (;) is closer than end (,) */
1475 int t = paramEnd + 1;
1476 while(UCharacter.isWhitespace(acceptLanguageList.charAt(t))) {
1479 if(acceptLanguageList.charAt(t)=='q') {
1482 while(UCharacter.isWhitespace(acceptLanguageList.charAt(t))) {
1485 if(acceptLanguageList.charAt(t)=='=') {
1488 while(UCharacter.isWhitespace(acceptLanguageList.charAt(t))) {
1492 String val = acceptLanguageList.substring(t,itemEnd).trim();
1493 q = Double.parseDouble(val);
1494 } catch (NumberFormatException nfe) {
1502 String loc = acceptLanguageList.substring(n,paramEnd).trim();
1503 int serial = map.size();
1504 ULocaleAcceptLanguageQ entry = new ULocaleAcceptLanguageQ(q,serial);
1505 map.put(entry, new ULocale(ULocale.canonicalize(loc))); // sort in reverse order.. 1.0, 0.9, 0.8 .. etc
1506 n = itemEnd; // get next item. (n++ will skip over delimiter)
1509 // 2. pull out the map
1510 ULocale acceptList[] = (ULocale[])map.values().toArray(new ULocale[map.size()]);
1514 public void TestAcceptLanguage2() {
1515 for(int i = 0 ; i < (ACCEPT_LANGUAGE_HTTP.length); i++) {
1516 Boolean expectBoolean = new Boolean(ACCEPT_LANGUAGE_TESTS[i][1]);
1517 String expectLocale=ACCEPT_LANGUAGE_TESTS[i][0];
1519 logln("#" + i + ": expecting: " + expectLocale + " (" + expectBoolean + ")");
1521 boolean r[] = { false };
1522 ULocale n = ULocale.acceptLanguage(StringToULocaleArray(ACCEPT_LANGUAGE_HTTP[i]), r);
1523 if((n==null)&&(expectLocale!=null)) {
1524 errln("result was null! line #" + i);
1527 if(((n==null)&&(expectLocale==null)) || (n.toString().equals(expectLocale))) {
1528 logln(" locale: OK." );
1530 errln("expected " + expectLocale + " but got " + n.toString());
1532 if(expectBoolean.equals(new Boolean(r[0]))) {
1533 logln(" bool: OK.");
1535 errln("bool: not OK, was " + new Boolean(r[0]).toString() + " expected " + expectBoolean.toString());
1540 public void TestOrientation() {
1542 String toTest [][] = {
1543 { "ar", "right-to-left", "top-to-bottom" },
1544 { "ar_Arab", "right-to-left", "top-to-bottom" },
1545 { "fa", "right-to-left", "top-to-bottom" },
1546 { "he", "right-to-left", "top-to-bottom" },
1547 { "ps", "right-to-left", "top-to-bottom" },
1548 { "ur", "right-to-left", "top-to-bottom" },
1549 { "en", "left-to-right", "top-to-bottom" }
1552 for (int i = 0; i < toTest.length; ++i) {
1553 ULocale loc = new ULocale(toTest[i][0]);
1554 String co = loc.getCharacterOrientation();
1555 String lo = loc.getLineOrientation();
1556 if (!co.equals(toTest[i][1])) {
1557 errln("Locale \"" + toTest[i][0] + "\" should have \"" + toTest[i][1] + "\" character orientation, but got \'" + co + "\"");
1559 else if (!lo.equals(toTest[i][2])) {
1560 errln("Locale \"" + toTest[i][0] + "\" should have \"" + toTest[i][2] + "\" line orientation, but got \'" + lo + "\"");
1566 public void TestJB3962(){
1567 ULocale loc = new ULocale("de_CH");
1568 String disp = loc.getDisplayName(ULocale.GERMAN);
1569 if(!disp.equals("Deutsch (Schweiz)")){
1570 errln("Did not get the expected display name for de_CH locale. Got: "+ prettify(disp));
1574 public void TestAddLikelySubtags() {
1576 {"en", "en_Latn_US"},
1577 {"en_US_BOSTON", "en_Latn_US_BOSTON"},
1578 {"th@calendar=buddhist", "th_Thai_TH@calendar=buddhist"},
1579 {"ar_ZZ", "ar_Arab_EG"},
1580 {"zh", "zh_Hans_CN"},
1581 {"zh_TW", "zh_Hant_TW"},
1582 {"zh_HK", "zh_Hant_HK"},
1583 {"zh_Hant", "zh_Hant_TW"},
1584 {"zh_Zzzz_CN", "zh_Hans_CN"},
1585 {"und_US", "en_Latn_US"},
1586 {"und_HK", "zh_Hant_HK"},
1587 /* Not yet implemented
1588 {"art_lojban", "arg_lojban"},
1589 {"zh_cmn_Hans", "zh_cmn_Hans"},
1592 for (int i = 0; i < data.length; i++) {
1593 ULocale org = new ULocale(data[i][0]);
1594 ULocale res = ULocale.addLikelySubtags(org);
1595 if (!res.toString().equals(data[i][1])) {
1596 errln("Original: " + data[i][0] + " Expected: " + data[i][1] + " - but got " + res.toString());
1600 String[][] basic_maximize_data = {
1639 "en_Latn_US_POSIX_1901"
1641 "en_Latn__POSIX_1901",
1642 "en_Latn_US_POSIX_1901"
1645 "en_Latn_US_POSIX_1901"
1648 "de_Latn_DE_POSIX_1901"
1655 for (int i = 0; i < basic_maximize_data.length; i++) {
1656 ULocale org = new ULocale(basic_maximize_data[i][0]);
1657 ULocale res = ULocale.addLikelySubtags(org);
1658 String exp = basic_maximize_data[i][1];
1659 if (exp.length() == 0) {
1660 if (!org.equals(res)) {
1661 errln("Original: " + basic_maximize_data[i][0] + " expected: " + exp + " - but got " + res.toString());
1664 else if (!res.toString().equals(exp)) {
1665 errln("Original: " + basic_maximize_data[i][0] + " expected: " + exp + " - but got " + res.toString());
1669 String[][] basic_minimize_data = {
1674 "en_Latn_US_POSIX_1901",
1677 "en_Zzzz_US_POSIX_1901",
1680 "de_Latn_DE_POSIX_1901",
1688 for (int i = 0; i < basic_minimize_data.length; i++) {
1689 ULocale org = new ULocale(basic_minimize_data[i][0]);
1690 ULocale res = ULocale.minimizeSubtags(org);
1691 String exp = basic_minimize_data[i][1];
1692 if (exp.length() == 0) {
1693 if (!org.equals(res)) {
1694 errln("Original: " + basic_minimize_data[i][0] + " expected: " + exp + " - but got " + res.toString());
1697 else if (!res.toString().equals(exp)) {
1698 errln("Original: " + basic_minimize_data[i][0] + " expected: " + exp + " - but got " + res.toString());
1702 String[][] full_data = {
1706 /* "REMOVE-LIKELY" */
3770 for (int i = 0; i < full_data.length; i++) {
3771 ULocale org = new ULocale(full_data[i][0]);
3772 ULocale res = ULocale.addLikelySubtags(org);
3773 String exp = full_data[i][1];
3774 if (exp.length() == 0) {
3775 if (!org.equals(res)) {
3776 errln("Original: " + full_data[i][0] + " expected: " + exp + " - but got " + res.toString());
3779 else if (!res.toString().equals(exp)) {
3780 errln("Original: " + full_data[i][0] + " expected: " + exp + " - but got " + res.toString());
3784 for (int i = 0; i < full_data.length; i++) {
3785 String maximal = full_data[i][1];
3787 if (maximal.length() > 0) {
3788 ULocale org = new ULocale(maximal);
3789 ULocale res = ULocale.minimizeSubtags(org);
3790 String exp = full_data[i][2];
3791 if (exp.length() == 0) {
3792 if (!org.equals(res)) {
3793 errln("Original: " + full_data[i][1] + " expected: " + exp + " - but got " + res.toString());
3796 else if (!res.toString().equals(exp)) {
3797 errln("Original: " + full_data[i][1] + " expected: " + exp + " - but got " + res.toString());
3802 public void TestCLDRVersion() {
3803 //VersionInfo zeroVersion = VersionInfo.getInstance(0, 0, 0, 0);
3804 VersionInfo testExpect;
3805 VersionInfo testCurrent;
3806 VersionInfo cldrVersion;
3808 cldrVersion = LocaleData.getCLDRVersion();
3810 this.logln("uloc_getCLDRVersion() returned: '"+cldrVersion+"'");
3812 // why isn't this public for tests somewhere?
3813 final ClassLoader testLoader = ICUResourceBundleTest.class.getClassLoader();
3814 UResourceBundle bundle = (UResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", ULocale.ROOT, testLoader);
3816 testExpect = VersionInfo.getInstance(bundle.getString("ExpectCLDRVersionAtLeast"));
3817 testCurrent = VersionInfo.getInstance(bundle.getString("CurrentCLDRVersion"));
3820 logln("(data) ExpectCLDRVersionAtLeast { "+testExpect+"");
3821 if(cldrVersion.compareTo(testExpect)<0) {
3822 errln("CLDR version is too old, expect at least "+testExpect+".");
3825 int r = cldrVersion.compareTo(testCurrent);
3827 logln("CLDR version is behind 'current' (for testdata/root.txt) "+testCurrent+". Some things may fail.\n");
3828 } else if ( r > 0) {
3829 logln("CLDR version is ahead of 'current' (for testdata/root.txt) "+testCurrent+". Some things may fail.\n");
3831 // CLDR version is OK.
3835 public void TestToLanguageTag() {
3836 final String[][] locale_to_langtag = {
3841 {"sr_Latn_SR", "sr-Latn-SR"},
3842 {"en_US_POSIX@ca=japanese", "en-US-u-ca-japanese-va-posix"},
3843 {"en__POSIX", "en-u-va-posix"},
3844 {"en_US_POSIX_VAR", "en-US-posix-x-lvariant-var"}, // variant POSIX_VAR is processed as regular variant
3845 {"en_US_VAR_POSIX", "en-US-x-lvariant-var-posix"}, // variant VAR_POSIX is processed as regular variant
3846 {"en_US_POSIX@va=posix2", "en-US-u-va-posix2"}, // if keyword va=xxx already exists, variant POSIX is simply dropped
3847 {"und_555", "und-555"},
3850 {"_Latn", "und-Latn"},
3852 {"und_FR", "und-FR"},
3853 {"th_TH_TH", "th-TH-x-lvariant-th"},
3855 {"foooobarrr", "und"},
3856 {"aa_BB_CYRL", "aa-BB-x-lvariant-cyrl"},
3857 {"en_US_1234", "en-US-1234"},
3858 {"en_US_VARIANTA_VARIANTB", "en-US-varianta-variantb"},
3859 {"en_US_VARIANTB_VARIANTA", "en-US-variantb-varianta"},
3860 {"ja__9876_5432", "ja-9876-5432"},
3861 {"zh_Hant__VAR", "zh-Hant-x-lvariant-var"},
3862 {"es__BADVARIANT_GOODVAR", "es"},
3863 {"es__GOODVAR_BAD_BADVARIANT", "es-goodvar-x-lvariant-bad"},
3864 {"en@calendar=gregorian", "en-u-ca-gregory"},
3865 {"de@collation=phonebook;calendar=gregorian", "de-u-ca-gregory-co-phonebk"},
3866 {"th@numbers=thai;z=extz;x=priv-use;a=exta", "th-a-exta-u-nu-thai-z-extz-x-priv-use"},
3867 {"en@timezone=America/New_York;calendar=japanese", "en-u-ca-japanese-tz-usnyc"},
3868 {"en@timezone=US/Eastern", "en-u-tz-usnyc"},
3869 {"en@x=x-y-z;a=a-b-c", "en-x-x-y-z"},
3870 {"it@collation=badcollationtype;colStrength=identical;cu=usd-eur", "it-u-ks-identic"},
3871 {"en_US_POSIX", "en-US-u-va-posix"},
3872 {"en_US_POSIX@calendar=japanese;currency=EUR","en-US-u-ca-japanese-cu-eur-va-posix"},
3873 {"@x=elmer", "x-elmer"},
3874 {"_US@x=elmer", "und-US-x-elmer"},
3877 for (int i = 0; i < locale_to_langtag.length; i++) {
3878 ULocale loc = new ULocale(locale_to_langtag[i][0]);
3879 String langtag = loc.toLanguageTag();
3880 if (!langtag.equals(locale_to_langtag[i][1])) {
3881 errln("FAIL: toLanguageTag returned language tag [" + langtag + "] for locale ["
3882 + loc + "] - expected: [" + locale_to_langtag[i][1] + "]");
3887 public void TestForLanguageTag() {
3888 final Integer NOERROR = Integer.valueOf(-1);
3890 final Object[][] langtag_to_locale = {
3891 {"en", "en", NOERROR},
3892 {"en-us", "en_US", NOERROR},
3893 {"und-us", "_US", NOERROR},
3894 {"und-latn", "_Latn", NOERROR},
3895 {"en-us-posix", "en_US_POSIX", NOERROR},
3896 {"de-de_euro", "de", Integer.valueOf(3)},
3897 {"kok-in", "kok_IN", NOERROR},
3898 {"123", "", Integer.valueOf(0)},
3899 {"en_us", "", Integer.valueOf(0)},
3900 {"en-latn-x", "en_Latn", Integer.valueOf(8)},
3901 {"art-lojban", "jbo", NOERROR},
3902 {"zh-hakka", "hak", NOERROR},
3903 {"zh-cmn-CH", "cmn_CH", NOERROR},
3904 {"xxx-yy", "xxx_YY", NOERROR},
3905 {"fr-234", "fr_234", NOERROR},
3906 {"i-default", "en@x=i-default", NOERROR},
3907 {"i-test", "", Integer.valueOf(0)},
3908 {"ja-jp-jp", "ja_JP", Integer.valueOf(6)},
3909 {"bogus", "bogus", NOERROR},
3910 {"boguslang", "", Integer.valueOf(0)},
3911 {"EN-lATN-us", "en_Latn_US", NOERROR},
3912 {"und-variant-1234", "__VARIANT_1234", NOERROR},
3913 {"und-varzero-var1-vartwo", "__VARZERO", Integer.valueOf(12)},
3914 {"en-u-ca-gregory", "en@calendar=gregorian", NOERROR},
3915 {"en-U-cu-USD", "en@currency=usd", NOERROR},
3916 {"en-us-u-va-posix", "en_US_POSIX", NOERROR},
3917 {"en-us-u-ca-gregory-va-posix", "en_US_POSIX@calendar=gregorian", NOERROR},
3918 {"en-us-posix-u-va-posix", "en_US_POSIX@va=posix", NOERROR},
3919 {"en-us-u-va-posix2", "en_US@va=posix2", NOERROR},
3920 {"en-us-vari1-u-va-posix", "en_US_VARI1@va=posix", NOERROR},
3921 {"ar-x-1-2-3", "ar@x=1-2-3", NOERROR},
3922 {"fr-u-nu-latn-cu-eur", "fr@currency=eur;numbers=latn", NOERROR},
3923 {"de-k-kext-u-co-phonebk-nu-latn", "de@collation=phonebook;k=kext;numbers=latn", NOERROR},
3924 {"ja-u-cu-jpy-ca-jp", "ja@calendar=yes;currency=jpy;jp=yes", NOERROR},
3925 {"en-us-u-tz-usnyc", "en_US@timezone=America/New_York", NOERROR},
3926 {"und-a-abc-def", "@a=abc-def", NOERROR},
3927 {"zh-u-ca-chinese-x-u-ca-chinese", "zh@calendar=chinese;x=u-ca-chinese", NOERROR},
3928 {"fr--FR", "fr", Integer.valueOf(3)},
3929 {"fr-", "fr", Integer.valueOf(3)},
3930 {"x-elmer", "@x=elmer", NOERROR},
3931 {"en-US-u-attr1-attr2-ca-gregory", "en_US@attribute=attr1-attr2;calendar=gregorian", NOERROR},
3932 {"sr-u-kn", "sr@colnumeric=yes", NOERROR},
3933 {"de-u-kn-co-phonebk", "de@collation=phonebook;colnumeric=yes", NOERROR},
3934 {"en-u-attr2-attr1-kn-kb", "en@attribute=attr1-attr2;colbackwards=yes;colnumeric=yes", NOERROR},
3935 {"ja-u-ijkl-efgh-abcd-ca-japanese-xx-yyy-zzz-kn", "ja@attribute=abcd-efgh-ijkl;calendar=japanese;colnumeric=yes;xx=yyy-zzz", NOERROR},
3937 {"de-u-xc-xphonebk-co-phonebk-ca-buddhist-mo-very-lo-extensi-xd-that-de-should-vc-probably-xz-killthebuffer",
3938 "de@calendar=buddhist;collation=phonebook;de=should;lo=extensi;mo=very;vc=probably;xc=xphonebk;xd=that;xz=yes", Integer.valueOf(92)},
3942 for (int i = 0; i < langtag_to_locale.length; i++) {
3943 String tag = (String)langtag_to_locale[i][0];
3944 ULocale expected = new ULocale((String)langtag_to_locale[i][1]);
3945 ULocale loc = ULocale.forLanguageTag(tag);
3947 if (!loc.equals(expected)) {
3948 errln("FAIL: forLanguageTag returned locale [" + loc + "] for language tag [" + tag
3949 + "] - expected: [" + expected + "]");
3953 // Use locale builder to check errors
3954 for (int i = 0; i < langtag_to_locale.length; i++) {
3955 String tag = (String)langtag_to_locale[i][0];
3956 ULocale expected = new ULocale((String)langtag_to_locale[i][1]);
3957 int errorIdx = ((Integer)langtag_to_locale[i][2]).intValue();
3960 Builder bld = new Builder();
3961 bld.setLanguageTag(tag);
3962 ULocale loc = bld.build();
3964 if (!loc.equals(expected)) {
3965 errln("FAIL: forLanguageTag returned locale [" + loc + "] for language tag [" + tag
3966 + "] - expected: [" + expected + "]");
3968 if (errorIdx != NOERROR.intValue()) {
3969 errln("FAIL: Builder.setLanguageTag should throw an exception for input tag [" + tag + "]");
3971 } catch (IllformedLocaleException ifle) {
3972 if (ifle.getErrorIndex() != errorIdx) {
3973 errln("FAIL: Builder.setLanguageTag returned error index " + ifle.getErrorIndex()
3974 + " for input language tag [" + tag + "] expected: " + errorIdx);
3981 * Test that if you use any locale without keyword that you will get a NULL
3982 * string returned and not throw and exception.
3984 public void Test4735()
3987 new ULocale("und").getDisplayKeywordValue("calendar",ULocale.GERMAN);
3988 new ULocale("en").getDisplayKeywordValue("calendar",ULocale.GERMAN);
3989 } catch (Exception e) {
3990 errln("Unexpected exception: " + e.getMessage());
3994 public void TestGetFallback() {
3995 // Testing static String getFallback(String)
3996 final String[][] TESTIDS =
3998 {"en_US", "en", "", ""}, // ULocale.getFallback("") should return ""
3999 {"EN_us_Var", "en_US", "en", ""}, // Case is always normalized
4000 {"de_DE@collation=phonebook", "de@collation=phonebook", "@collation=phonebook", "@collation=phonebook"}, // Keyword is preserved
4001 {"en__POSIX", "en", ""}, // Trailing empty segment should be truncated
4002 {"_US_POSIX", "_US", ""}, // Same as above
4003 {"root", ""}, // No canonicalization
4006 for (String[] chain : TESTIDS) {
4007 for (int i = 1; i < chain.length; i++) {
4008 String fallback = ULocale.getFallback(chain[i-1]);
4009 assertEquals("getFallback(\"" + chain[i-1] + "\")", chain[i], fallback);
4013 // Testing ULocale getFallback()
4014 final ULocale[][] TESTLOCALES =
4016 {new ULocale("en_US"), new ULocale("en"), ULocale.ROOT, null},
4017 {new ULocale("en__POSIX"), new ULocale("en"), ULocale.ROOT, null},
4018 {new ULocale("de_DE@collation=phonebook"), new ULocale("de@collation=phonebook"), new ULocale("@collation=phonebook"), null},
4019 {new ULocale("_US_POSIX"), new ULocale("_US"), ULocale.ROOT, null},
4020 {new ULocale("root"), ULocale.ROOT, null},
4023 for(ULocale[] chain : TESTLOCALES) {
4024 for (int i = 1; i < chain.length; i++) {
4025 ULocale fallback = chain[i-1].getFallback();
4026 assertEquals("ULocale(" + chain[i-1] + ").getFallback()", chain[i], fallback);
4031 public void TestExtension() {
4032 String[][] TESTCASES = {
4033 // {"<langtag>", "<ext key1>", "<ext val1>", "<ext key2>", "<ext val2>", ....},
4035 {"en-a-exta-b-extb", "a", "exta", "b", "extb"},
4036 {"en-b-extb-a-exta", "a", "exta", "b", "extb"},
4037 {"de-x-a-bc-def", "x", "a-bc-def"},
4038 {"ja-JP-u-cu-jpy-ca-japanese-x-java", "u", "ca-japanese-cu-jpy", "x", "java"},
4041 for (String[] testcase : TESTCASES) {
4042 ULocale loc = ULocale.forLanguageTag(testcase[0]);
4044 int nExtensions = (testcase.length - 1) / 2;
4046 Set<Character> keys = loc.getExtensionKeys();
4047 if (keys.size() != nExtensions) {
4048 errln("Incorrect number of extensions: returned="
4049 + keys.size() + ", expected=" + nExtensions
4050 + ", locale=" + testcase[0]);
4053 for (int i = 0; i < nExtensions; i++) {
4054 String kstr = testcase[i/2 + 1];
4055 String ext = loc.getExtension(Character.valueOf(kstr.charAt(0)));
4056 if (ext == null || !ext.equals(testcase[i/2 + 2])) {
4057 errln("Incorrect extension value: key="
4058 + kstr + ", returned=" + ext + ", expected=" + testcase[i/2 + 2]
4059 + ", locale=" + testcase[0]);
4064 // Exception handling
4065 boolean sawException = false;
4067 ULocale l = ULocale.forLanguageTag("en-US-a-exta");
4068 l.getExtension('$');
4069 } catch (IllegalArgumentException e) {
4070 sawException = true;
4072 if (!sawException) {
4073 errln("getExtension must throw an exception on illegal input key");
4077 public void TestUnicodeLocaleExtension() {
4078 String[][] TESTCASES = {
4079 //"<langtag>", "<attr1>,<attr2>,...", "<key1>,<key2>,...", "<type1>", "<type2>", ...},
4081 {"en-a-ext1-x-privuse", null, null},
4082 {"en-u-attr1-attr2", "attr1,attr2", null},
4083 {"ja-u-ca-japanese-cu-jpy", null, "ca,cu", "japanese", "jpy"},
4084 {"th-TH-u-number-attr-nu-thai-ca-buddhist", "attr,number", "ca,nu", "buddhist", "thai"},
4087 for (String[] testcase : TESTCASES) {
4088 ULocale loc = ULocale.forLanguageTag(testcase[0]);
4090 Set<String> expectedAttributes = new HashSet<String>();
4091 if (testcase[1] != null) {
4092 String[] attrs = testcase[1].split(",");
4093 for (String s : attrs) {
4094 expectedAttributes.add(s);
4098 Map<String, String> expectedKeywords = new HashMap<String, String>();
4099 if (testcase[2] != null) {
4100 String[] ukeys = testcase[2].split(",");
4101 for (int i = 0; i < ukeys.length; i++) {
4102 expectedKeywords.put(ukeys[i], testcase[i + 3]);
4107 Set<String> attributes = loc.getUnicodeLocaleAttributes();
4108 if (attributes.size() != expectedAttributes.size()) {
4109 errln("Incorrect number for Unicode locale attributes: returned="
4110 + attributes.size() + ", expected=" + expectedAttributes.size()
4111 + ", locale=" + testcase[0]);
4113 if (!attributes.containsAll(expectedAttributes) || !expectedAttributes.containsAll(attributes)) {
4114 errln("Incorrect set of attributes for locale " + testcase[0]);
4118 Set<String> keys = loc.getUnicodeLocaleKeys();
4119 Set<String> expectedKeys = expectedKeywords.keySet();
4120 if (keys.size() != expectedKeys.size()) {
4121 errln("Incorrect number for Unicode locale keys: returned="
4122 + keys.size() + ", expected=" + expectedKeys.size()
4123 + ", locale=" + testcase[0]);
4126 for (String expKey : expectedKeys) {
4127 String type = loc.getUnicodeLocaleType(expKey);
4128 String expType = expectedKeywords.get(expKey);
4130 if (type == null || !expType.equals(type)) {
4131 errln("Incorrect Unicode locale type: key="
4132 + expKey + ", returned=" + type + ", expected=" + expType
4133 + ", locale=" + testcase[0]);
4138 // Exception handling
4139 boolean sawException = false;
4141 ULocale l = ULocale.forLanguageTag("en-US-u-ca-gregory");
4142 l.getUnicodeLocaleType("$%");
4143 } catch (IllegalArgumentException e) {
4144 sawException = true;
4146 if (!sawException) {
4147 errln("getUnicodeLocaleType must throw an exception on illegal input key");
4151 public void TestForLocale() {
4153 {new Locale(""), ""},
4154 {new Locale("en", "US"), "en_US"},
4155 {new Locale("en", "US", "POSIX"), "en_US_POSIX"},
4156 {new Locale("", "US"), "_US"},
4157 {new Locale("en", "", "POSIX"), "en__POSIX"},
4158 {new Locale("no", "NO", "NY"), "nn_NO"},
4159 {new Locale("en", "BOGUS"), "en__BOGUS"}, // ill-formed country is mapped to variant - see #8383 and #8384
4162 for (int i = 0; i < DATA.length; i++) {
4163 ULocale uloc = ULocale.forLocale((Locale) DATA[i][0]);
4164 assertEquals("forLocale with " + DATA[i][0], DATA[i][1], uloc.getName());
4167 if (JAVA7_OR_LATER) {
4168 Object[][] DATA7 = {
4169 {new Locale("ja", "JP", "JP"), "ja_JP_JP@calendar=japanese"},
4170 {new Locale("th", "TH", "TH"), "th_TH_TH@numbers=thai"},
4172 for (int i = 0; i < DATA7.length; i++) {
4173 ULocale uloc = ULocale.forLocale((Locale) DATA7[i][0]);
4174 assertEquals("forLocale with " + DATA7[i][0], DATA7[i][1], uloc.getName());
4178 Method localeForLanguageTag = Locale.class.getMethod("forLanguageTag", String.class);
4180 String[][] DATA7EXT = {
4181 {"en-Latn-US", "en_Latn_US"},
4182 {"zh-Hant-TW", "zh_Hant_TW"},
4183 {"und-US-u-cu-usd", "_US@currency=usd"},
4184 {"th-TH-u-ca-buddhist-nu-thai", "th_TH@calendar=buddhist;numbers=thai"},
4185 {"en-US-u-va-POSIX", "en_US_POSIX"},
4186 {"de-DE-u-co-phonebk", "de_DE@collation=phonebook"},
4187 {"en-a-exta-b-extb-x-privu", "en@a=exta;b=extb;x=privu"},
4188 {"fr-u-attr1-attr2-cu-eur", "fr@attribute=attr1-attr2;currency=eur"},
4191 for (int i = 0; i < DATA7EXT.length; i++) {
4192 Locale loc = (Locale) localeForLanguageTag.invoke(null, DATA7EXT[i][0]);
4193 ULocale uloc = ULocale.forLocale(loc);
4194 assertEquals("forLocale with " + loc, DATA7EXT[i][1], uloc.getName());
4196 } catch (Exception e) {
4197 throw new RuntimeException(e);
4201 Object[][] DATA6 = {
4202 {new Locale("ja", "JP", "JP"), "ja_JP@calendar=japanese"},
4203 {new Locale("th", "TH", "TH"), "th_TH@numbers=thai"},
4205 for (int i = 0; i < DATA6.length; i++) {
4206 ULocale uloc = ULocale.forLocale((Locale) DATA6[i][0]);
4207 assertEquals("forLocale with " + DATA6[i][0], DATA6[i][1], uloc.getName());
4212 public void TestToLocale() {
4214 {"", new Locale("")},
4215 {"en_US", new Locale("en", "US")},
4216 {"_US", new Locale("", "US")},
4217 {"en__POSIX", new Locale("en", "", "POSIX")},
4220 for (int i = 0; i < DATA.length; i++) {
4221 Locale loc = new ULocale((String) DATA[i][0]).toLocale();
4222 assertEquals("toLocale with " + DATA[i][0], DATA[i][1], loc);
4225 if (JAVA7_OR_LATER) {
4226 Object[][] DATA7 = {
4227 {"nn_NO", new Locale("nn", "NO")},
4228 {"no_NO_NY", new Locale("no", "NO", "NY")},
4230 for (int i = 0; i < DATA7.length; i++) {
4231 Locale loc = new ULocale((String) DATA7[i][0]).toLocale();
4232 assertEquals("toLocale with " + DATA7[i][0], DATA7[i][1], loc);
4236 Method localeForLanguageTag = Locale.class.getMethod("forLanguageTag", String.class);
4238 String[][] DATA7EXT = {
4239 {"en_Latn_US", "en-Latn-US"},
4240 {"zh_Hant_TW", "zh-Hant-TW"},
4241 {"ja_JP@calendar=japanese", "ja-JP-u-ca-japanese"},
4242 {"ja_JP_JP@calendar=japanese", "ja-JP-u-ca-japanese-x-lvariant-JP"},
4243 {"th_TH@numbers=thai", "th-TH-u-nu-thai"},
4244 {"th_TH_TH@numbers=thai", "th-TH-u-nu-thai-x-lvariant-TH"},
4245 {"de@collation=phonebook", "de-u-co-phonebk"},
4246 {"en@a=exta;b=extb;x=privu", "en-a-exta-b-extb-x-privu"},
4247 {"fr@attribute=attr1-attr2;currency=eur", "fr-u-attr1-attr2-cu-eur"},
4250 for (int i = 0; i < DATA7EXT.length; i++) {
4251 Locale loc = new ULocale((String) DATA7EXT[i][0]).toLocale();
4252 Locale expected = (Locale) localeForLanguageTag.invoke(null, DATA7EXT[i][1]);
4253 assertEquals("toLocale with " + DATA7EXT[i][0], expected, loc);
4255 } catch (Exception e) {
4256 throw new RuntimeException(e);
4260 Object[][] DATA6 = {
4261 {"nn_NO", new Locale("no", "NO", "NY")},
4262 {"no_NO_NY", new Locale("no", "NO", "NY")},
4263 {"ja_JP@calendar=japanese", new Locale("ja", "JP", "JP")},
4264 {"th_TH@numbers=thai", new Locale("th", "TH", "TH")},
4266 for (int i = 0; i < DATA6.length; i++) {
4267 Locale loc = new ULocale((String) DATA6[i][0]).toLocale();
4268 assertEquals("toLocale with " + DATA6[i][0], DATA6[i][1], loc);
4273 public void TestCategoryDefault() {
4274 Locale backupDefault = Locale.getDefault();
4276 ULocale orgDefault = ULocale.getDefault();
4278 // Setting a category default won't change default ULocale
4279 ULocale uJaJp = new ULocale("ja_JP");
4280 ULocale uDeDePhonebook = new ULocale("de_DE@collation=phonebook");
4282 ULocale.setDefault(Category.DISPLAY, uJaJp);
4283 ULocale.setDefault(Category.FORMAT, uDeDePhonebook);
4285 if (!ULocale.getDefault().equals(orgDefault)) {
4286 errln("FAIL: Default ULocale is " + ULocale.getDefault() + ", expected: " + orgDefault);
4289 if (!ULocale.getDefault(Category.DISPLAY).equals(uJaJp)) {
4290 errln("FAIL: DISPLAY ULocale is " + ULocale.getDefault(Category.DISPLAY) + ", expected: " + uJaJp);
4293 if (!ULocale.getDefault(Category.FORMAT).equals(uDeDePhonebook)) {
4294 errln("FAIL: FORMAT ULocale is " + ULocale.getDefault(Category.FORMAT) + ", expected: " + uDeDePhonebook);
4297 // Setting ULocale default will overrides category defaults
4298 ULocale uFrFr = new ULocale("fr_FR");
4300 ULocale.setDefault(uFrFr);
4302 if (!ULocale.getDefault(Category.DISPLAY).equals(uFrFr)) {
4303 errln("FAIL: DISPLAY ULocale is " + ULocale.getDefault(Category.DISPLAY) + ", expected: " + uFrFr);
4306 if (!ULocale.getDefault(Category.FORMAT).equals(uFrFr)) {
4307 errln("FAIL: FORMAT ULocale is " + ULocale.getDefault(Category.FORMAT) + ", expected: " + uFrFr);
4310 // Setting Locale default will updates ULocale default and category defaults
4311 Locale arEg = new Locale("ar", "EG");
4312 ULocale uArEg = ULocale.forLocale(arEg);
4314 Locale.setDefault(arEg);
4316 if (!ULocale.getDefault().equals(uArEg)) {
4317 errln("FAIL: Default ULocale is " + ULocale.getDefault() + ", expected: " + uArEg);
4320 if (!ULocale.getDefault(Category.DISPLAY).equals(uArEg)) {
4321 errln("FAIL: DISPLAY ULocale is " + ULocale.getDefault(Category.DISPLAY) + ", expected: " + uArEg);
4324 if (!ULocale.getDefault(Category.FORMAT).equals(uArEg)) {
4325 errln("FAIL: FORMAT ULocale is " + ULocale.getDefault(Category.FORMAT) + ", expected: " + uArEg);
4329 Locale.setDefault(backupDefault);