blob: 99fa44b78429046d1470181352c62b94a3d9160b [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23/**
24 * @test
25 * @bug 4052404 4052440 4084688 4092475 4101316 4105828 4107014 4107953 4110613
26 * 4118587 4118595 4122371 4126371 4126880 4135316 4135752 4139504 4139940 4143951
naoto4082d6b2009-01-21 13:58:46 -080027 * 4147315 4147317 4147552 4335196 4778440 5010672 6475525 6544471 6627549 6786276
duke6e45e102007-12-01 00:00:00 +000028 * @summary test Locales
29 */
30/*
31 *
32 *
33 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
34 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
35 *
36 * Portions copyright (c) 2007 Sun Microsystems, Inc.
37 * All Rights Reserved.
38 *
39 * The original version of this source code and documentation
40 * is copyrighted and owned by Taligent, Inc., a wholly-owned
41 * subsidiary of IBM. These materials are provided under terms
42 * of a License Agreement between Taligent and Sun. This technology
43 * is protected by multiple US and International patents.
44 *
45 * This notice and attribution to Taligent may not be removed.
46 * Taligent is a registered trademark of Taligent, Inc.
47 *
48 * Permission to use, copy, modify, and distribute this software
49 * and its documentation for NON-COMMERCIAL purposes and without
50 * fee is hereby granted provided that this copyright notice
51 * appears in all copies. Please refer to the file "copyright.html"
52 * for further important copyright and licensing information.
53 *
54 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
55 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
56 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
57 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
58 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
59 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
60 *
61 */
62
63import java.text.*;
64import java.util.Locale;
65import java.util.MissingResourceException;
66import java.util.Date;
67import java.util.Calendar;
68import java.io.*;
69
70public class LocaleTest extends LocaleTestFmwk {
71 public LocaleTest() {
72 }
73
74 private int ENGLISH = 0;
75 private int FRENCH = 1;
76 private int CROATIAN = 2;
77 private int GREEK = 3;
78 private int NORWEGIAN = 4;
79 private int ITALIAN = 5;
80 private int DUMMY = 6;
81 private int MAX_LOCALES = 6;
82
83 private int LANG = 0;
84 private int CTRY = 1;
85 private int VAR = 2;
86 private int NAME = 3;
87 private int LANG3 = 4;
88 private int CTRY3 = 5;
89 private int LCID = 6;
90 private int DLANG_EN = 7;
91 private int DCTRY_EN = 8;
92 private int DVAR_EN = 9;
93 private int DNAME_EN = 10;
94 private int DLANG_FR = 11;
95 private int DCTRY_FR = 12;
96 private int DVAR_FR = 13;
97 private int DNAME_FR = 14;
98 private int DLANG_HR = 15;
99 private int DCTRY_HR = 16;
100 private int DVAR_HR = 17;
101 private int DNAME_HR = 18;
102 private int DLANG_EL = 19;
103 private int DCTRY_EL = 20;
104 private int DVAR_EL = 21;
105 private int DNAME_EL = 22;
106 private int DLANG_ROOT = 23;
107 private int DCTRY_ROOT = 24;
108 private int DVAR_ROOT = 25;
109 private int DNAME_ROOT = 26;
110
111 private String[][] dataTable = {
112 // language code
113 { "en", "fr", "hr", "el", "no", "it", "xx" },
114 // country code
115 { "US", "FR", "HR", "GR", "NO", "", "YY" },
116 // variant code
117 { "", "", "", "", "NY", "", "" },
118 // full name
119 { "en_US", "fr_FR", "hr_HR", "el_GR", "no_NO_NY", "it", "xx_YY" },
120 // ISO-3 language
121 { "eng", "fra", "hrv", "ell", "nor", "ita", "" },
122 // ISO-3 country
123 { "USA", "FRA", "HRV", "GRC", "NOR", "", "" },
124 // LCID (not currently public)
125 { "0409", "040c", "041a", "0408", "0814", "", "" },
126
127 // display language (English)
128 { "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },
129 // display country (English)
130 { "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },
131 // display variant (English)
132 { "", "", "", "", "Nynorsk", "", ""},
133 // display name (English)
134 // Updated no_NO_NY English display name for new pattern-based algorithm
135 // (part of Euro support).
136 { "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },
137
138 // display langage (French)
139 { "anglais", "fran\u00e7ais", "croate", "grec", "norv\u00e9gien", "italien", "xx" },
140 // display country (French)
141 { "Etats-Unis", "France", "Croatie", "Gr\u00e8ce", "Norv\u00e8ge", "", "YY" },
142 // display variant (French)
143 { "", "", "", "", "", "", "" },
144 // display name (French)
145 { "anglais (Etats-Unis)", "fran\u00e7ais (France)", "croate (Croatie)", "grec (Gr\u00e8ce)", "norv\u00e9gien (Norv\u00e8ge,Nynorsk)", "italien", "xx (YY)" },
146
147 // display langage (Croatian)
148 { "", "", "hrvatski", "", "", "", "xx" },
149 // display country (Croatian)
150 { "", "", "Hrvatska", "", "", "", "YY" },
151 // display variant (Croatian)
152 { "", "", "", "", "", "", ""},
153 // display name (Croatian)
154 { "", "", "hrvatski (Hrvatska)", "", "", "", "xx (YY)" },
155
156 // display langage (Greek)
157 { "\u0391\u03b3\u03b3\u03bb\u03b9\u03ba\u03ac", "\u0393\u03b1\u03bb\u03bb\u03b9\u03ba\u03ac", "\u039a\u03c1\u03bf\u03b1\u03c4\u03b9\u03ba\u03ac", "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac", "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03b9\u03ba\u03ac", "\u0399\u03c4\u03b1\u03bb\u03b9\u03ba\u03ac", "xx" },
158 // display country (Greek)
159 { "\u0397\u03bd\u03c9\u03bc\u03ad\u03bd\u03b5\u03c2 \u03a0\u03bf\u03bb\u03b9\u03c4\u03b5\u03af\u03b5\u03c2", "\u0393\u03b1\u03bb\u03bb\u03af\u03b1", "\u039a\u03c1\u03bf\u03b1\u03c4\u03af\u03b1", "\u0395\u03bb\u03bb\u03ac\u03b4\u03b1", "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03af\u03b1", "", "YY" },
160 // display variant (Greek)
161 { "", "", "", "", "", "", "" },
162 // display name (Greek)
163 { "\u0391\u03b3\u03b3\u03bb\u03b9\u03ba\u03ac (\u0397\u03bd\u03c9\u03bc\u03ad\u03bd\u03b5\u03c2 \u03a0\u03bf\u03bb\u03b9\u03c4\u03b5\u03af\u03b5\u03c2)", "\u0393\u03b1\u03bb\u03bb\u03b9\u03ba\u03ac (\u0393\u03b1\u03bb\u03bb\u03af\u03b1)", "\u039a\u03c1\u03bf\u03b1\u03c4\u03b9\u03ba\u03ac (\u039a\u03c1\u03bf\u03b1\u03c4\u03af\u03b1)", "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac (\u0395\u03bb\u03bb\u03ac\u03b4\u03b1)", "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03b9\u03ba\u03ac (\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03af\u03b1,Nynorsk)", "\u0399\u03c4\u03b1\u03bb\u03b9\u03ba\u03ac", "xx (YY)" },
164
165 // display langage (<root>)
166 { "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },
167 // display country (<root>)
168 { "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },
169 // display variant (<root>)
170 { "", "", "", "", "Nynorsk", "", ""},
171 // display name (<root>)
172 { "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },
173 };
174
175 public static void main(String[] args) throws Exception {
176 new LocaleTest().run(args);
177 }
178
179 public void TestBasicGetters() {
180 for (int i = 0; i <= MAX_LOCALES; i++) {
181 Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
182 logln("Testing " + testLocale + "...");
183
184 if (!testLocale.getLanguage().equals(dataTable[LANG][i]))
185 errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "
186 + dataTable[LANG][i]);
187 if (!testLocale.getCountry().equals(dataTable[CTRY][i]))
188 errln(" Country code mismatch: " + testLocale.getCountry() + " versus "
189 + dataTable[CTRY][i]);
190 if (!testLocale.getVariant().equals(dataTable[VAR][i]))
191 errln(" Variant code mismatch: " + testLocale.getVariant() + " versus "
192 + dataTable[VAR][i]);
193 if (!testLocale.toString().equals(dataTable[NAME][i]))
194 errln(" Locale name mismatch: " + testLocale.toString() + " versus "
195 + dataTable[NAME][i]);
196 }
197
198 logln("Same thing without variant codes...");
199 for (int i = 0; i <= MAX_LOCALES; i++) {
200 Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i]);
201 logln("Testing " + testLocale + "...");
202
203 if (!testLocale.getLanguage().equals(dataTable[LANG][i]))
204 errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "
205 + dataTable[LANG][i]);
206 if (!testLocale.getCountry().equals(dataTable[CTRY][i]))
207 errln(" Country code mismatch: " + testLocale.getCountry() + " versus "
208 + dataTable[CTRY][i]);
209 if (!testLocale.getVariant().equals(""))
210 errln(" Variant code mismatch: " + testLocale.getVariant() + " versus \"\"");
211 }
212 }
213
214 public void TestSimpleResourceInfo() {
215 for (int i = 0; i <= MAX_LOCALES; i++) {
216 if (dataTable[LANG][i].equals("xx"))
217 continue;
218
219 Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
220 logln("Testing " + testLocale + "...");
221
222 if (!testLocale.getISO3Language().equals(dataTable[LANG3][i]))
223 errln(" ISO-3 language code mismatch: " + testLocale.getISO3Language()
224 + " versus " + dataTable[LANG3][i]);
225 if (!testLocale.getISO3Country().equals(dataTable[CTRY3][i]))
226 errln(" ISO-3 country code mismatch: " + testLocale.getISO3Country()
227 + " versus " + dataTable[CTRY3][i]);
228/*
229 // getLCID() is currently private
230 if (!String.valueOf(testLocale.getLCID()).equals(dataTable[LCID][i]))
231 errln(" LCID mismatch: " + testLocale.getLCID() + " versus "
232 + dataTable[LCID][i]);
233*/
234 }
235 }
236
237 /*
238 * @bug 4101316
239 * @bug 4084688 (This bug appears to be a duplicate of something, because it was fixed
240 * between 1.1.5 and 1.1.6, but I included a new test for it anyway)
241 * @bug 4052440 Stop falling back to the default locale.
242 */
243 public void TestDisplayNames() {
244 Locale saveDefault = Locale.getDefault();
245 Locale english = new Locale("en", "US");
246 Locale french = new Locale("fr", "FR");
247 Locale croatian = new Locale("hr", "HR");
248 Locale greek = new Locale("el", "GR");
249
250 Locale.setDefault(english);
251 logln("With default = en_US...");
252 logln(" In default locale...");
253 doTestDisplayNames(null, DLANG_EN, false);
254 logln(" In locale = en_US...");
255 doTestDisplayNames(english, DLANG_EN, false);
256 logln(" In locale = fr_FR...");
257 doTestDisplayNames(french, DLANG_FR, false);
258 logln(" In locale = hr_HR...");
259 doTestDisplayNames(croatian, DLANG_HR, false);
260 logln(" In locale = el_GR...");
261 doTestDisplayNames(greek, DLANG_EL, false);
262
263 Locale.setDefault(french);
264 logln("With default = fr_FR...");
265 logln(" In default locale...");
266 doTestDisplayNames(null, DLANG_FR, true);
267 logln(" In locale = en_US...");
268 doTestDisplayNames(english, DLANG_EN, true);
269 logln(" In locale = fr_FR...");
270 doTestDisplayNames(french, DLANG_FR, true);
271 logln(" In locale = hr_HR...");
272 doTestDisplayNames(croatian, DLANG_HR, true);
273 logln(" In locale = el_GR...");
274 doTestDisplayNames(greek, DLANG_EL, true);
275
276 Locale.setDefault(saveDefault);
277 }
278
279 private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
280 if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))
281 errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());
282 else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))
283 errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());
284
285 for (int i = 0; i <= MAX_LOCALES; i++) {
286 Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
287 logln(" Testing " + testLocale + "...");
288
289 String testLang;
290 String testCtry;
291 String testVar;
292 String testName;
293
294 if (inLocale == null) {
295 testLang = testLocale.getDisplayLanguage();
296 testCtry = testLocale.getDisplayCountry();
297 testVar = testLocale.getDisplayVariant();
298 testName = testLocale.getDisplayName();
299 }
300 else {
301 testLang = testLocale.getDisplayLanguage(inLocale);
302 testCtry = testLocale.getDisplayCountry(inLocale);
303 testVar = testLocale.getDisplayVariant(inLocale);
304 testName = testLocale.getDisplayName(inLocale);
305 }
306
307 String expectedLang;
308 String expectedCtry;
309 String expectedVar;
310 String expectedName;
311
312 expectedLang = dataTable[compareIndex][i];
313 if (expectedLang.equals("") && defaultIsFrench)
314 expectedLang = dataTable[DLANG_EN][i];
315 if (expectedLang.equals(""))
316 expectedLang = dataTable[DLANG_ROOT][i];
317
318 expectedCtry = dataTable[compareIndex + 1][i];
319 if (expectedCtry.equals("") && defaultIsFrench)
320 expectedCtry = dataTable[DCTRY_EN][i];
321 if (expectedCtry.equals(""))
322 expectedCtry = dataTable[DCTRY_ROOT][i];
323
324 expectedVar = dataTable[compareIndex + 2][i];
325 if (expectedVar.equals("") && defaultIsFrench)
326 expectedVar = dataTable[DVAR_EN][i];
327 if (expectedVar.equals(""))
328 expectedVar = dataTable[DVAR_ROOT][i];
329
330 expectedName = dataTable[compareIndex + 3][i];
331 if (expectedName.equals("") && defaultIsFrench)
332 expectedName = dataTable[DNAME_EN][i];
333 if (expectedName.equals(""))
334 expectedName = dataTable[DNAME_ROOT][i];
335
336 if (!testLang.equals(expectedLang))
337 errln("Display language mismatch: " + testLang + " versus " + expectedLang);
338 if (!testCtry.equals(expectedCtry))
339 errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
340 if (!testVar.equals(expectedVar))
341 errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
342 if (!testName.equals(expectedName))
343 errln("Display name mismatch: " + testName + " versus " + expectedName);
344 }
345 }
346
347 public void TestSimpleObjectStuff() {
348 Locale test1 = new Locale("aa", "AA");
349 Locale test2 = new Locale("aa", "AA");
350 Locale test3 = (Locale)test1.clone();
351 Locale test4 = new Locale("zz", "ZZ");
352
353 if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3)
354 errln("Some of the test variables point to the same locale!");
355
356 if (test3 == null)
357 errln("clone() failed to produce a valid object!");
358
359 if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3))
360 errln("clone() or equals() failed: objects that should compare equal don't");
361
362 if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4))
363 errln("equals() failed: objects that shouldn't compare equal do");
364
365 int hash1 = test1.hashCode();
366 int hash2 = test2.hashCode();
367 int hash3 = test3.hashCode();
368
369 if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3)
370 errln("hashCode() failed: objects that should have the same hash code don't");
371 }
372
373 /**
374 * @bug 4011756 4011380
375 */
376 public void TestISO3Fallback() {
377 Locale test = new Locale("xx", "YY", "");
378 boolean gotException = false;
379 String result = "";
380
381 try {
382 result = test.getISO3Language();
383 }
384 catch (MissingResourceException e) {
385 gotException = true;
386 }
387 if (!gotException)
388 errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");
389
390 gotException = false;
391 try {
392 result = test.getISO3Country();
393 }
394 catch (MissingResourceException e) {
395 gotException = true;
396 }
397 if (!gotException)
398 errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");
399 }
400
401 /**
402 * @bug 4106155 4118587
403 */
404 public void TestGetLangsAndCountries() {
405 // It didn't seem right to just do an exhaustive test of everything here, so I check
406 // for the following things:
407 // 1) Does each list have the right total number of entries?
408 // 2) Does each list contain certain language and country codes we think are important
409 // (the G7 countries, plus a couple others)?
410 // 3) Does each list have every entry formatted correctly? (i.e., two characters,
411 // all lower case for the language codes, all upper case for the country codes)
412 // 4) Is each list in sorted order?
413 String[] test = Locale.getISOLanguages();
414 String[] spotCheck1 = { "en", "es", "fr", "de", "it", "ja", "ko", "zh", "th",
415 "he", "id", "iu", "ug", "yi", "za" };
416
417 if (test.length != 188)
418 errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length);
419 else {
420 for (int i = 0; i < spotCheck1.length; i++) {
421 int j;
422 for (j = 0; j < test.length; j++)
423 if (test[j].equals(spotCheck1[i]))
424 break;
425 if (j == test.length || !test[j].equals(spotCheck1[i]))
426 errln("Couldn't find " + spotCheck1[i] + " in language list.");
427 }
428 }
429 for (int i = 0; i < test.length; i++) {
430 if (!test[i].equals(test[i].toLowerCase()))
431 errln(test[i] + " is not all lower case.");
432 if (test[i].length() != 2)
433 errln(test[i] + " is not two characters long.");
434 if (i > 0 && test[i].compareTo(test[i - 1]) <= 0)
435 errln(test[i] + " appears in an out-of-order position in the list.");
436 }
437
438 test = Locale.getISOCountries();
439 String[] spotCheck2 = { "US", "CA", "GB", "FR", "DE", "IT", "JP", "KR", "CN", "TW", "TH" };
440
441
naoto4082d6b2009-01-21 13:58:46 -0800442 if (test.length != 246)
443 errln("Expected getISOCountries to return 246 countries; it returned " + test.length);
duke6e45e102007-12-01 00:00:00 +0000444 else {
445 for (int i = 0; i < spotCheck2.length; i++) {
446 int j;
447 for (j = 0; j < test.length; j++)
448 if (test[j].equals(spotCheck2[i]))
449 break;
450 if (j == test.length || !test[j].equals(spotCheck2[i]))
451 errln("Couldn't find " + spotCheck2[i] + " in country list.");
452 }
453 }
454 for (int i = 0; i < test.length; i++) {
455 if (!test[i].equals(test[i].toUpperCase()))
456 errln(test[i] + " is not all upper case.");
457 if (test[i].length() != 2)
458 errln(test[i] + " is not two characters long.");
459 if (i > 0 && test[i].compareTo(test[i - 1]) <= 0)
460 errln(test[i] + " appears in an out-of-order position in the list.");
461 }
462 }
463
464 /**
465 * @bug 4126880
466 */
467 void Test4126880() {
468 String[] test;
469
470 test = Locale.getISOCountries();
471 test[0] = "SUCKER!!!";
472 test = Locale.getISOCountries();
473 if (test[0].equals("SUCKER!!!"))
474 errln("Changed internal country code list!");
475
476 test = Locale.getISOLanguages();
477 test[0] = "HAHAHAHA!!!";
478 test = Locale.getISOLanguages();
479 if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
480 errln("Changes internal language code list!");
481 }
482
483 /**
484 * @bug 4107014
485 */
486 public void TestGetAvailableLocales() {
487 Locale[] locales = Locale.getAvailableLocales();
488 if (locales == null || locales.length == 0)
489 errln("Locale.getAvailableLocales() returned no installed locales!");
490 else {
491 logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales.");
492 for (int i = 0; i < locales.length; i++)
493 logln(locales[i].toString());
494 }
495 }
496
497 /**
498 * @bug 4135316
499 */
500 public void TestBug4135316() {
501 Locale[] locales1 = Locale.getAvailableLocales();
502 Locale[] locales2 = Locale.getAvailableLocales();
503 if (locales1 == locales2)
504 errln("Locale.getAvailableLocales() doesn't clone its internal storage!");
505 }
506
507 /**
508 * @bug 4107953
509 */
510/*
511test commented out pending API-change approval
512 public void TestGetLanguagesForCountry() {
513 String[] languages = Locale.getLanguagesForCountry("US");
514
515 if (!searchStringArrayFor("en", languages))
516 errln("Didn't get en as a language for US");
517
518 languages = Locale.getLanguagesForCountry("FR");
519 if (!searchStringArrayFor("fr", languages))
520 errln("Didn't get fr as a language for FR");
521
522 languages = Locale.getLanguagesForCountry("CH");
523 if (!searchStringArrayFor("fr", languages))
524 errln("Didn't get fr as a language for CH");
525 if (!searchStringArrayFor("it", languages))
526 errln("Didn't get it as a language for CH");
527 if (!searchStringArrayFor("de", languages))
528 errln("Didn't get de as a language for CH");
529
530 languages = Locale.getLanguagesForCountry("JP");
531 if (!searchStringArrayFor("ja", languages))
532 errln("Didn't get ja as a language for JP");
533 }
534*/
535
536 private boolean searchStringArrayFor(String s, String[] array) {
537 for (int i = 0; i < array.length; i++)
538 if (s.equals(array[i]))
539 return true;
540 return false;
541 }
542 /**
543 * @bug 4110613
544 */
545 public void TestSerialization() throws ClassNotFoundException, OptionalDataException,
546 IOException, StreamCorruptedException
547 {
548 ObjectOutputStream ostream;
549 ByteArrayOutputStream obstream;
550 byte[] bytes = null;
551
552 obstream = new ByteArrayOutputStream();
553 ostream = new ObjectOutputStream(obstream);
554
555 Locale test1 = new Locale("zh", "TW", "");
556 int dummy = test1.hashCode(); // fill in the cached hash-code value
557 ostream.writeObject(test1);
558
559 bytes = obstream.toByteArray();
560
561 ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
562
563 Locale test2 = (Locale)(istream.readObject());
564
565 if (!test1.equals(test2) || test1.hashCode() != test2.hashCode())
566 errln("Locale failed to deserialize correctly.");
567 }
568
569 /**
570 * @bug 4118587
571 */
572 public void TestSimpleDisplayNames() {
573 // This test is different from TestDisplayNames because TestDisplayNames checks
574 // fallback behavior, combination of language and country names to form locale
575 // names, and other stuff like that. This test just checks specific language
576 // and country codes to make sure we have the correct names for them.
577 String[] languageCodes = { "he", "id", "iu", "ug", "yi", "za" };
578 String[] languageNames = { "Hebrew", "Indonesian", "Inuktitut", "Uighur", "Yiddish",
579 "Zhuang" };
580
581 for (int i = 0; i < languageCodes.length; i++) {
582 String test = (new Locale(languageCodes[i], "", "")).getDisplayLanguage(Locale.US);
583 if (!test.equals(languageNames[i]))
584 errln("Got wrong display name for " + languageCodes[i] + ": Expected \"" +
585 languageNames[i] + "\", got \"" + test + "\".");
586 }
587 }
588
589 /**
590 * @bug 4118595
591 */
592 public void TestUninstalledISO3Names() {
593 // This test checks to make sure getISO3Language and getISO3Country work right
594 // even for locales that are not installed.
595 String[] iso2Languages = { "am", "ba", "fy", "mr", "rn", "ss", "tw", "zu" };
596 String[] iso3Languages = { "amh", "bak", "fry", "mar", "run", "ssw", "twi", "zul" };
597
598 for (int i = 0; i < iso2Languages.length; i++) {
599 String test = (new Locale(iso2Languages[i], "", "")).getISO3Language();
600 if (!test.equals(iso3Languages[i]))
601 errln("Got wrong ISO3 code for " + iso2Languages[i] + ": Expected \"" +
602 iso3Languages[i] + "\", got \"" + test + "\".");
603 }
604
605 String[] iso2Countries = { "AF", "BW", "KZ", "MO", "MN", "SB", "TC", "ZW" };
606 String[] iso3Countries = { "AFG", "BWA", "KAZ", "MAC", "MNG", "SLB", "TCA", "ZWE" };
607
608 for (int i = 0; i < iso2Countries.length; i++) {
609 String test = (new Locale("", iso2Countries[i], "")).getISO3Country();
610 if (!test.equals(iso3Countries[i]))
611 errln("Got wrong ISO3 code for " + iso2Countries[i] + ": Expected \"" +
612 iso3Countries[i] + "\", got \"" + test + "\".");
613 }
614 }
615
616 /**
617 * @bug 4052404 4778440
618 */
619 public void TestChangedISO639Codes() {
620 Locale hebrewOld = new Locale("iw", "IL", "");
621 Locale hebrewNew = new Locale("he", "IL", "");
622 Locale yiddishOld = new Locale("ji", "IL", "");
623 Locale yiddishNew = new Locale("yi", "IL", "");
624 Locale indonesianOld = new Locale("in", "", "");
625 Locale indonesianNew = new Locale("id", "", "");
626
627 if (!hebrewNew.getLanguage().equals("iw"))
628 errln("Got back wrong language code for Hebrew: expected \"iw\", got \"" +
629 hebrewNew.getLanguage() + "\"");
630 if (!yiddishNew.getLanguage().equals("ji"))
631 errln("Got back wrong language code for Yiddish: expected \"ji\", got \"" +
632 yiddishNew.getLanguage() + "\"");
633 if (!indonesianNew.getLanguage().equals("in"))
634 errln("Got back wrong language code for Indonesian: expected \"in\", got \"" +
635 indonesianNew.getLanguage() + "\"");
636 }
637
638 /**
639 * @bug 4092475
640 * I could not reproduce this bug. I'm pretty convinced it was fixed with the
641 * big locale-data reorg of 10/28/97. The lookup logic for language and country
642 * display names was also changed at that time in that check-in. --rtg 3/20/98
643
644 * This test is not designed to work in any other locale but en_US.
645 * Most of the LocaleElements do not contain display names for other languages,
646 * so this test fails (bug 4289223) when run under different locales. For example,
647 * LocaleElements_es as of kestrel does not have a localized name for Japanese, so
648 * the getDisplayName method asks the default locale for a display name. The Japanese
649 * localized name for "Japanese" does not equal "Japanese" so this test fails for es
650 * display names if run under a ja locale. Eventually, he LocaleElements should probably
651 * be updated to contain more localized language and region display names.
652 * 1999-11-19 joconner
653 *
654 */
655 public void TestAtypicalLocales() {
656 Locale[] localesToTest = { new Locale("de", "CA"),
657 new Locale("ja", "ZA"),
658 new Locale("ru", "MX"),
659 new Locale("en", "FR"),
660 new Locale("es", "DE"),
661 new Locale("", "HR"),
662 new Locale("", "SE"),
663 new Locale("", "DO"),
664 new Locale("", "BE") };
665 String[] englishDisplayNames = { "German (Canada)",
666 "Japanese (South Africa)",
667 "Russian (Mexico)",
668 "English (France)",
669 "Spanish (Germany)",
670 "Croatia",
671 "Sweden",
672 "Dominican Republic",
673 "Belgium" };
674 String[] frenchDisplayNames = { "allemand (Canada)",
675 "japonais (Afrique du Sud)",
676 "russe (Mexique)",
677 "anglais (France)",
678 "espagnol (Allemagne)",
679 "Croatie",
680 "Su\u00e8de",
681 "R\u00e9publique Dominicaine",
682 "Belgique" };
683 String[] spanishDisplayNames = { "alem\u00E1n (Canad\u00E1)",
684 "japon\u00E9s (Sud\u00E1frica)",
685 "ruso (M\u00e9xico)",
686 "ingl\u00E9s (Francia)",
687 "espa\u00f1ol (Alemania)",
688 "Croacia",
689 "Suecia",
690 "Rep\u00fablica Dominicana",
691 "B\u00E9lgica" };
692
693
694 // save the default locale and set to the new default to en_US
695 Locale defaultLocale = Locale.getDefault();
696 Locale.setDefault(Locale.US);
697
698 for (int i = 0; i < localesToTest.length; i++) {
699 String name = localesToTest[i].getDisplayName(Locale.US);
700 logln(name);
701 if (!name.equals(englishDisplayNames[i]))
702 errln("Lookup in English failed: expected \"" + englishDisplayNames[i]
703 + "\", got \"" + name + "\"");
704 }
705
706 for (int i = 0; i < localesToTest.length; i++) {
707 String name = localesToTest[i].getDisplayName(new Locale("es", "ES"));
708 logln(name);
709 if (!name.equals(spanishDisplayNames[i]))
710 errln("Lookup in Spanish failed: expected \"" + spanishDisplayNames[i]
711 + "\", got \"" + name + "\"");
712 }
713
714 for (int i = 0; i < localesToTest.length; i++) {
715 String name = localesToTest[i].getDisplayName(Locale.FRANCE);
716 logln(name);
717 if (!name.equals(frenchDisplayNames[i]))
718 errln("Lookup in French failed: expected \"" + frenchDisplayNames[i]
719 + "\", got \"" + name + "\"");
720 }
721
722 // restore the default locale for other tests
723 Locale.setDefault(defaultLocale);
724 }
725
726 /**
727 * @bug 4126371
728 */
729 public void TestNullDefault() {
730 // why on earth anyone would ever try to do this is beyond me, but we should
731 // definitely make sure we don't let them
732 boolean gotException = false;
733 try {
734 Locale.setDefault(null);
735 }
736 catch (NullPointerException e) {
737 // all other exception types propagate through here back to the test harness
738 gotException = true;
739 }
740 if (Locale.getDefault() == null)
741 errln("Locale.getDefault() allowed us to set default to NULL!");
742 if (!gotException)
743 errln("Trying to set default locale to NULL didn't throw exception!");
744 }
745
746 /**
747 * @bug 4135752
748 * This would be better tested by the LocaleDataTest. Will move it when I
749 * get the LocaleDataTest working again.
750 */
751 public void TestThaiCurrencyFormat() {
752 DecimalFormat thaiCurrency = (DecimalFormat)NumberFormat.getCurrencyInstance(
753 new Locale("th", "TH"));
754 if (!thaiCurrency.getPositivePrefix().equals("\u0e3f"))
755 errln("Thai currency prefix wrong: expected \"\u0e3f\", got \"" +
756 thaiCurrency.getPositivePrefix() + "\"");
757 if (!thaiCurrency.getPositiveSuffix().equals(""))
758 errln("Thai currency suffix wrong: expected \"\", got \"" +
759 thaiCurrency.getPositiveSuffix() + "\"");
760 }
761
762 /**
763 * @bug 4122371
764 * Confirm that Euro support works. This test is pretty rudimentary; all it does
765 * is check that any locales with the EURO variant format a number using the
766 * Euro currency symbol.
767 *
768 * ASSUME: All locales encode the Euro character "\u20AC".
769 * If this is changed to use the single-character Euro symbol, this
770 * test must be updated.
771 *
772 * DON'T ASSUME: Any specific countries support the Euro. Instead,
773 * iterate through all locales.
774 */
775 public void TestEuroSupport() {
776 final String EURO_VARIANT = "EURO";
777 final String EURO_CURRENCY = "\u20AC"; // Look for this string in formatted Euro currency
778
779 Locale[] locales = NumberFormat.getAvailableLocales();
780 for (int i=0; i<locales.length; ++i) {
781 Locale loc = locales[i];
782 if (loc.getVariant().indexOf(EURO_VARIANT) >= 0) {
783 NumberFormat nf = NumberFormat.getCurrencyInstance(loc);
784 String pos = nf.format(271828.182845);
785 String neg = nf.format(-271828.182845);
786 if (pos.indexOf(EURO_CURRENCY) >= 0 &&
787 neg.indexOf(EURO_CURRENCY) >= 0) {
788 logln("Ok: " + loc.toString() +
789 ": " + pos + " / " + neg);
790 }
791 else {
792 errln("Fail: " + loc.toString() +
793 " formats without " + EURO_CURRENCY +
794 ": " + pos + " / " + neg +
795 "\n*** THIS FAILURE MAY ONLY MEAN THAT LOCALE DATA HAS CHANGED ***");
796 }
797 }
798 }
799 }
800
801 /**
802 * @bug 4139504
803 * toString() doesn't work with language_VARIANT.
804 */
805 public void TestToString() {
806 Object[] DATA = {
807 new Locale("xx", "", ""), "xx",
808 new Locale("", "YY", ""), "_YY",
809 new Locale("", "", "ZZ"), "",
810 new Locale("xx", "YY", ""), "xx_YY",
811 new Locale("xx", "", "ZZ"), "xx__ZZ",
812 new Locale("", "YY", "ZZ"), "_YY_ZZ",
813 new Locale("xx", "YY", "ZZ"), "xx_YY_ZZ",
814 };
815 for (int i=0; i<DATA.length; i+=2) {
816 Locale loc = (Locale)DATA[i];
817 String fmt = (String)DATA[i+1];
818 if (!loc.toString().equals(fmt)) {
819 errln("Fail: Locale.toString(" + fmt + ")=>" + loc);
820 }
821 }
822 }
823
824 /**
825 * @bug 4105828
826 * Currency symbol in zh is wrong. We will test this at the NumberFormat
827 * end to test the whole pipe.
828 */
829 public void Test4105828() {
830 Locale[] LOC = { Locale.CHINESE, new Locale("zh", "CN", ""),
831 new Locale("zh", "TW", ""), new Locale("zh", "HK", "") };
832 for (int i=0; i<LOC.length; ++i) {
833 NumberFormat fmt = NumberFormat.getPercentInstance(LOC[i]);
834 String result = fmt.format(1);
835 if (!result.equals("100%")) {
836 errln("Percent for " + LOC[i] + " should be 100%, got " + result);
837 }
838 }
839 }
840
841 /**
842 * @bug 4139940
843 * Couldn't reproduce this bug -- probably was fixed earlier.
844 *
845 * ORIGINAL BUG REPORT:
846 * -- basically, hungarian for monday shouldn't have an \u00f4
847 * (o circumflex)in it instead it should be an o with 2 inclined
848 * (right) lines over it..
849 *
850 * You may wonder -- why do all this -- why not just add a line to
851 * LocaleData? Well, I could see by inspection that the locale file had the
852 * right character in it, so I wanted to check the rest of the pipeline -- a
853 * very remote possibility, but I wanted to be sure. The other possibility
854 * is that something is wrong with the font mapping subsystem, but we can't
855 * test that here.
856 */
857 public void Test4139940() {
858 Locale mylocale=new Locale("hu", "", "");
859 Date mydate = new Date(98,3,13); // A Monday
860 DateFormat df_full = new SimpleDateFormat("EEEE", mylocale);
861 String str = df_full.format(mydate);
862 // Make sure that o circumflex (\u00F4) is NOT there, and
863 // o double acute (\u0151) IS.
864 if (str.indexOf('\u0151') < 0 || str.indexOf('\u00F4') >= 0)
865 errln("Fail: Monday in Hungarian is wrong");
866 }
867
868 /**
869 * @bug 4143951
870 * Russian first day of week should be Monday. Confirmed.
871 */
872 public void Test4143951() {
873 Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
874 if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
875 errln("Fail: First day of week in Russia should be Monday");
876 }
877 }
878
879 /**
880 * @bug 4147315
881 * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
882 * Should throw an exception for unknown locales
883 */
884 public void Test4147315() {
885 // Try with codes that are the wrong length but happen to match text
886 // at a valid offset in the mapping table
887 Locale locale = new Locale("aaa", "CCC");
888
889 try {
890 String result = locale.getISO3Country();
891
892 errln("ERROR: getISO3Country() returns: " + result +
893 " for locale '" + locale + "' rather than exception" );
894 } catch(MissingResourceException e) { }
895 }
896
897 /**
898 * @bug 4147317
899 * java.util.Locale.getISO3Language() works wrong for non ISO-3166 codes.
900 * Should throw an exception for unknown locales
901 */
902 public void Test4147317() {
903 // Try with codes that are the wrong length but happen to match text
904 // at a valid offset in the mapping table
905 Locale locale = new Locale("aaa", "CCC");
906
907 try {
908 String result = locale.getISO3Language();
909
910 errln("ERROR: getISO3Language() returns: " + result +
911 " for locale '" + locale + "' rather than exception" );
912 } catch(MissingResourceException e) { }
913 }
914
915 /*
916 * @bug 4147552 4778440
917 */
918 public void Test4147552() {
919 Locale[] locales = { new Locale("no", "NO"), new Locale("no", "NO", "B"),
920 new Locale("no", "NO", "NY") };
921 String[] englishDisplayNames = { "Norwegian (Norway)",
922 "Norwegian (Norway,Bokm\u00e5l)",
923 "Norwegian (Norway,Nynorsk)" };
924 String[] norwegianDisplayNames = { "norsk (Norge)",
925 "norsk (Norge,bokm\u00e5l)", "norsk (Norge,nynorsk)" };
926
927 for (int i = 0; i < locales.length; i++) {
928 Locale loc = locales[i];
929 if (!loc.getDisplayName(Locale.US).equals(englishDisplayNames[i]))
930 errln("English display-name mismatch: expected " +
931 englishDisplayNames[i] + ", got " + loc.getDisplayName());
932 if (!loc.getDisplayName(loc).equals(norwegianDisplayNames[i]))
933 errln("Norwegian display-name mismatch: expected " +
934 norwegianDisplayNames[i] + ", got " +
935 loc.getDisplayName(loc));
936 }
937 }
938
939 static String escapeUnicode(String s) {
940 StringBuffer buf = new StringBuffer();
941 for (int i=0; i<s.length(); ++i) {
942 char c = s.charAt(i);
943 if (c >= 0x20 && c <= 0x7F) buf.append(c);
944 else {
945 buf.append("\\u");
946 String h = "000" + Integer.toHexString(c);
947 if (h.length() > 4) h = h.substring(h.length() - 4);
948 buf.append(h);
949 }
950 }
951 return buf.toString();
952 }
953}