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