Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/text/resources/LocaleDataTest.java
41149 views
1
/*
2
* Copyright (c) 2007, 2020, 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 4052473 4052679 4055602 4066550 4067619 4068012 4068073 4070174 4070452
26
* 4070178 4070450 4070695 4070725 4070795 4071003 4071183 4071782 4072013
27
* 4072388 4072773 4075404 4084356 4087238 4092361 4094033 4094371 4098518
28
* 4099810 4103218 4103220 4103861 4112136 4113638 4113654 4117054 4122468
29
* 4122840 4139860 4156708 4175306 4215747 4209960 4290801 4900884 4942982
30
* 4518811 4945388 4936845 4794068 4461740 4965260 4984277 4826794 5032580
31
* 5102005 5074431 6182685 6208712 6277020 6245766 6351682 6386647 6379382
32
* 6414459 6455680 6498742 6558863 6488119 6547501 6497154 6558856 6481177
33
* 6379214 6485516 6486607 4225362 4494727 6533691 6531591 6531593 6570259
34
* 6509039 6609737 6610748 6645271 6507067 6873931 6450945 6645268 6646611
35
* 6645405 6650730 6910489 6573250 6870908 6585666 6716626 6914413 6916787
36
* 6919624 6998391 7019267 7020960 7025837 7020583 7036905 7066203 7101495
37
* 7003124 7085757 7028073 7171028 7189611 8000983 7195759 8004489 8006509
38
* 7114053 7074882 7040556 8008577 8013836 8021121 6192407 6931564 8027695
39
* 8017142 8037343 8055222 8042126 8074791 8075173 8080774 8129361 8134916
40
* 8145136 8145952 8164784 8037111 8081643 7037368 8178872 8185841 8190918
41
* 8187946 8195478 8181157 8179071 8193552 8202026 8204269 8202537 8208746
42
* 8209775 8221432 8227127 8230284 8231273 8233579 8234288 8250665 8255086
43
* 8251317
44
* @summary Verify locale data
45
* @modules java.base/sun.util.resources
46
* @modules jdk.localedata
47
* @run main LocaleDataTest
48
* @run main LocaleDataTest -cldr
49
*
50
*/
51
52
/*
53
*
54
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
55
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
56
*
57
* Portions copyright (c) 2007 Sun Microsystems, Inc.
58
* All Rights Reserved.
59
*
60
* The original version of this source code and documentation
61
* is copyrighted and owned by Taligent, Inc., a wholly-owned
62
* subsidiary of IBM. These materials are provided under terms
63
* of a License Agreement between Taligent and Sun. This technology
64
* is protected by multiple US and International patents.
65
*
66
* This notice and attribution to Taligent may not be removed.
67
* Taligent is a registered trademark of Taligent, Inc.
68
*
69
* Permission to use, copy, modify, and distribute this software
70
* and its documentation for NON-COMMERCIAL purposes and without
71
* fee is hereby granted provided that this copyright notice
72
* appears in all copies. Please refer to the file "copyright.html"
73
* for further important copyright and licensing information.
74
*
75
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
76
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
77
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
78
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
79
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
80
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
81
*
82
*/
83
84
/* This test is a generalized test for verifying changes to the locale data.
85
* It is driven by an external file that specifies the particular pieces of locale
86
* data to check. That file is in .properties file format: a series of key/value
87
* pairs delimited by newline characters, with the keys separated from the values
88
* by = signs. The keys are similar in syntax to a Unix pathname, with keys at
89
* successive levels of containment in the resource-data hierarchy separated by
90
* slashes. The file is in ISO 8859-1 encoding, with control characters and
91
* non-ASCII characters denoted with backslash-u escape sequences. The program also allows
92
* blank lines and comment lines to be interspersed with the data. Comment lines
93
* begin with '#'.
94
*
95
* A data file for this test would look something like this:<pre>
96
* FormatData//MonthNames/0=January
97
* FormatData//MonthNames/1=February
98
* LocaleNames//US=United States
99
* LocaleNames//FR=France
100
* FormatData/fr_FR/MonthNames/0=janvier
101
* FormatData/fr_FR/MonthNames/1=f\u00e9vrier
102
* LocaleNames/fr_FR/US=\u00c9tats-Unis
103
* LocaleNames/fr_FR/FR=France</pre>
104
*
105
* Second field which designates locale is in the form of:
106
* 1) Legacy locale notation using '_' as a locale component(language/country/variant) separator.
107
* language is a mandatory component. country and variant are optional, however,
108
* variant cannot exist without country. So for example, while "ja"/"ja_JP"/"ja_JP_JP" are valid,
109
* "_JP"/"ja__JP" are invalid.
110
*
111
* 2) BCP47 language tag notation in which we can specify language tag with '-' as a subtag
112
* separator. Language tag can be specified with '-' in locale field like this:
113
* <pre>LocaleNames/sr-Latn/SR=Surinam
114
* FormatData/sr-Latn-BA/DayNames/2=utorak</pre>
115
*
116
* The command-line syntax of this test is
117
* <tt>java --add-exports java.base/sun.util.resources=ALL-UNNAMED LocaleDataTest.java [-w] [{ -s | <filename> }] [-cldr]</tt>
118
*
119
* This program always sends its results to standard output. If -w is not specified,
120
* this program prints out only the differences between the data file and the actual
121
* resource data. If -w is specified, the program prints out every entry, comment,
122
* and blank line from the data file. Where there is a difference between the data
123
* file and the resource data, the data is the data from the resources. This feature
124
* can be used to quickly generate a new data file.
125
*
126
* The user can specify an optional filename or -s. If the user specifies a filename,
127
* the program uses that file as the data file. If the user specifies -s, the program
128
* reads its input from standard input rather than from a file. If the user specifies
129
* neither, the program reads its input from a file called LocaleData in the same
130
* directory the program itself resides in.
131
*
132
* The -nothrow option prevents the program from throwing an exception when it
133
* gets an error. -w implies -nothrow.
134
*
135
* -cldr option specifies to test CLDR locale data. The default data file name for this
136
* option is "LocaleData.cldr".
137
*
138
* Other command-line options can be specified, but are ignored.
139
*
140
* It's important to note what this test will NOT test. Certain changes to the locale
141
* data are meant to have certain effects on the internationalization frameworks. For
142
* instance, we could ensure round-trip formatting/parsing integrity for the full
143
* date/time format of SimpleDateFormat by making sure that the full date and time
144
* patterns include sufficient data. The test of this is not whether changes were
145
* made to the locale data; it's whether using this data gives round-trip integrity.
146
* Likewise, changing the currency patterns to use \u00a4 instead of local currency
147
* symbols isn't something that can be tested by this test; instead, you want to
148
* actually format currency values and make sure the proper currency symbol was used.
149
*
150
* This test by itself doesn't do an exhaustive comparison of locale data. It is
151
* possible to do this manually, however: Use the GenerateKeyList tool to produce
152
* a complete list of keys for the two versions of the locales you want to compare,
153
* and then diff them. This will flag additions and deletions. Generate a data file
154
* for the base version of the data using the -w option and the output from
155
* GenerateKeyList, and then use the resultant file as the data file when you run
156
* this test against the new version of the data.
157
*/
158
159
import java.io.BufferedReader;
160
import java.io.File;
161
import java.io.FileInputStream;
162
import java.io.FilterReader;
163
import java.io.FilterWriter;
164
import java.io.IOException;
165
import java.io.InputStreamReader;
166
import java.io.OutputStreamWriter;
167
import java.io.PrintWriter;
168
import java.io.Reader;
169
import java.io.Writer;
170
import java.util.Locale;
171
import java.util.MissingResourceException;
172
import java.util.ResourceBundle;
173
import sun.util.resources.LocaleData;
174
175
public class LocaleDataTest
176
{
177
static final String TEXT_RESOURCES_PACKAGE ="sun.text.resources";
178
static final String UTIL_RESOURCES_PACKAGE ="sun.util.resources";
179
static final String DEFAULT_DATAFILE ="LocaleData";
180
static String cldrSuffix = "";
181
182
public static void main(String[] args) throws Exception {
183
184
// set up our flags and our input and output streams based on the
185
// command-line arguments (exceptions generated here will propagate out
186
// to the environment)
187
BufferedReader in = null;
188
PrintWriter out = null;
189
boolean writeNewFile = false;
190
boolean doThrow = true;
191
192
for (int i = 0; i < args.length; i++) {
193
if (args[i].equals("-w")) {
194
writeNewFile = true;
195
doThrow = false;
196
}
197
198
else if (args[i].equals("-nothrow"))
199
doThrow = false;
200
201
else if (args[i].equals("-cldr")) {
202
cldrSuffix = ".cldr";
203
}
204
205
else if (args[i].equals("-s") && in == null)
206
in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
207
"ISO8859_1")));
208
else if (!args[i].startsWith("-") && in == null)
209
in = new BufferedReader(new EscapeReader(new InputStreamReader(new
210
FileInputStream(args[i]), "ISO8859_1")));
211
}
212
if (in == null) {
213
File localeData = new File(System.getProperty("test.src", "."), DEFAULT_DATAFILE + cldrSuffix);
214
in = new BufferedReader(new EscapeReader(new InputStreamReader(new
215
FileInputStream(localeData), "ISO8859_1")));
216
}
217
out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
218
"ISO8859_1")), true);
219
220
// perform the actual test
221
int errorCount = doTest(in, out, writeNewFile);
222
223
// write out the error count, and throw an exception out into the environment
224
// if there were any errors
225
if (errorCount != 0) {
226
if (!writeNewFile)
227
out.println("Test failed. " + errorCount + " errors.");
228
if (doThrow)
229
throw new Exception("Test failed. " + errorCount + " errors.");
230
}
231
else if (!writeNewFile)
232
out.println("Test passed.");
233
234
in.close();
235
out.close();
236
}
237
238
static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
239
throws Exception {
240
int errorCount = 0;
241
242
String key = null;
243
String expectedValue = null;
244
String line = in.readLine();
245
while (line != null) {
246
if (line.startsWith("#") || line.length() == 0) {
247
if (writeNewFile)
248
out.println(line);
249
}
250
251
else {
252
int index = line.indexOf("=");
253
if (index == -1) {
254
key = line;
255
expectedValue = "";
256
}
257
else {
258
key = line.substring(0, index);
259
if (index + 1 == line.length())
260
expectedValue = "";
261
else
262
expectedValue = line.substring(index + 1);
263
}
264
if (!processLine(key, expectedValue, out, writeNewFile))
265
++errorCount;
266
}
267
line = in.readLine();
268
}
269
return errorCount;
270
}
271
272
static boolean processLine(String key, String expectedValue, PrintWriter out,
273
boolean writeNewFile) throws Exception {
274
String rbName, localeName, resTag, qualifier;
275
String language = "", country = "", variant = "";
276
int index, oldIndex;
277
278
index = key.indexOf("/");
279
if (index == -1 || index + 1 == key.length())
280
throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
281
rbName = key.substring(0, index);
282
283
oldIndex = index + 1;
284
index = key.indexOf("/", oldIndex);
285
if (index == -1 || index + 1 == key.length())
286
throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
287
localeName = key.substring(oldIndex, index);
288
boolean use_tag = localeName.indexOf("-") != -1;
289
if (use_tag == false && localeName.length() > 0) {
290
String[] locDetails = localeName.split("_");
291
switch (locDetails.length) {
292
case 1:
293
language = locDetails[0];
294
break;
295
case 2:
296
language = locDetails[0];
297
country = locDetails[1];
298
break;
299
case 3:
300
language = locDetails[0];
301
country = locDetails[1];
302
variant = locDetails[2];
303
break;
304
default:
305
throw new Exception("locale not specified properly " + locDetails);
306
}
307
}
308
oldIndex = index + 1;
309
index = key.indexOf("/", oldIndex);
310
if (index == -1)
311
index = key.length();
312
resTag = key.substring(oldIndex, index);
313
314
// TimeZone name may have "/" in it, for example "Asia/Taipei", so use "Asia\/Taipei in LocaleData.
315
if(resTag.endsWith("\\")) {
316
resTag = resTag.substring(0, resTag.length() - 1);
317
oldIndex = index;
318
index = key.indexOf("/", oldIndex + 1);
319
if (index == -1) index = key.length();
320
resTag += key.substring(oldIndex, index);
321
}
322
323
if (index < key.length() - 1)
324
qualifier = key.substring(index + 1);
325
else
326
qualifier = "";
327
328
String retrievedValue = null;
329
Object resource = null;
330
try {
331
String fullName = null;
332
if (rbName.equals("CalendarData")
333
|| rbName.equals("CurrencyNames")
334
|| rbName.equals("LocaleNames")
335
|| rbName.equals("TimeZoneNames")) {
336
fullName = UTIL_RESOURCES_PACKAGE + cldrSuffix + "." + rbName;
337
} else {
338
fullName = TEXT_RESOURCES_PACKAGE + cldrSuffix + "." + rbName;
339
}
340
Locale locale;
341
if (use_tag) {
342
locale = Locale.forLanguageTag(localeName);
343
} else {
344
locale = new Locale(language, country, variant);
345
}
346
ResourceBundle bundle = LocaleData.getBundle(fullName, locale);
347
resource = bundle.getObject(resTag);
348
}
349
catch (MissingResourceException e) {
350
}
351
352
if (resource != null) {
353
if (resource instanceof String) {
354
retrievedValue = (String)resource;
355
}
356
else if (resource instanceof String[]) {
357
int element = Integer.valueOf(qualifier).intValue();
358
String[] stringList = (String[])resource;
359
if (element >= 0 && element < stringList.length)
360
retrievedValue = stringList[element];
361
}
362
else if (resource instanceof String[][]) {
363
String[][] stringArray = (String[][])resource;
364
int slash = qualifier.indexOf("/");
365
if (slash == -1) {
366
for (int i = 0; i < stringArray.length; i++) {
367
if (stringArray[i][0].equals(qualifier))
368
retrievedValue = stringArray[i][1];
369
}
370
}
371
else {
372
int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
373
int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
374
if (row >= 0 && row < stringArray.length && column >= 0 && column <
375
stringArray[row].length)
376
retrievedValue = stringArray[row][column];
377
}
378
}
379
}
380
381
if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
382
if (retrievedValue == null)
383
retrievedValue = "<MISSING!>";
384
385
if (writeNewFile)
386
out.println(key + "=" + retrievedValue);
387
else {
388
out.println("Mismatch in " + key + ":");
389
out.println(" file = \"" + expectedValue + "\"");
390
out.println(" jvm = \"" + retrievedValue + "\"");
391
}
392
return false;
393
}
394
else {
395
if (writeNewFile)
396
out.println(key + "=" + expectedValue);
397
}
398
return true;
399
}
400
}
401
402
class EscapeReader extends FilterReader {
403
public EscapeReader(Reader in) {
404
super(in);
405
}
406
407
public int read() throws IOException {
408
if (buffer != null) {
409
String b = buffer.toString();
410
int result = b.charAt(0);
411
if (b.length() > 1)
412
buffer = new StringBuffer(b.substring(1));
413
else
414
buffer = null;
415
return result;
416
}
417
else {
418
int result = super.read();
419
if (result != '\\')
420
return result;
421
else {
422
buffer = new StringBuffer();
423
result = super.read();
424
buffer.append((char)result);
425
if (result == 'u') {
426
for (int i = 0; i < 4; i++) {
427
result = super.read();
428
if (result == -1)
429
break;
430
buffer.append((char)result);
431
}
432
String number = buffer.toString().substring(1);
433
result = Integer.parseInt(number, 16);
434
buffer = null;
435
return result;
436
}
437
return '\\';
438
}
439
}
440
}
441
442
public int read(char[] cbuf, int start, int len) throws IOException {
443
int p = start;
444
int end = start + len;
445
int c = 0;
446
while (c != -1 && p < end) {
447
c = read();
448
if (c != -1)
449
cbuf[p++] = (char)c;
450
}
451
if (c == -1 && p == start)
452
return -1;
453
else
454
return p - start;
455
}
456
457
private StringBuffer buffer = null;
458
}
459
460
class EscapeWriter extends FilterWriter {
461
public EscapeWriter(Writer out) {
462
super(out);
463
}
464
465
public void write(int c) throws IOException {
466
if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
467
super.write(c);
468
else {
469
super.write('\\');
470
super.write('u');
471
String number = Integer.toHexString(c);
472
if (number.length() < 4)
473
number = zeros.substring(0, 4 - number.length()) + number;
474
super.write(number.charAt(0));
475
super.write(number.charAt(1));
476
super.write(number.charAt(2));
477
super.write(number.charAt(3));
478
}
479
}
480
481
public void write(char[] cbuf, int off, int len) throws IOException {
482
int end = off + len;
483
while (off < end)
484
write(cbuf[off++]);
485
}
486
487
public void write(String str, int off, int len) throws IOException {
488
int end = off + len;
489
while (off < end)
490
write(str.charAt(off++));
491
}
492
493
private static String zeros = "0000";
494
}
495
496