1 /*
2 * Copyright (c) 2007, 2013, 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
39 * @summary Verify locale data
40 *
41 */
42
43 /*
44 *
45 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
46 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
47 *
48 * Portions copyright (c) 2007 Sun Microsystems, Inc.
49 * All Rights Reserved.
50 *
51 * The original version of this source code and documentation
52 * is copyrighted and owned by Taligent, Inc., a wholly-owned
53 * subsidiary of IBM. These materials are provided under terms
54 * of a License Agreement between Taligent and Sun. This technology
55 * is protected by multiple US and International patents.
56 *
57 * This notice and attribution to Taligent may not be removed.
58 * Taligent is a registered trademark of Taligent, Inc.
59 *
60 * Permission to use, copy, modify, and distribute this software
61 * and its documentation for NON-COMMERCIAL purposes and without
62 * fee is hereby granted provided that this copyright notice
63 * appears in all copies. Please refer to the file "copyright.html"
64 * for further important copyright and licensing information.
65 *
66 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
67 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
68 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
69 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
70 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
71 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
72 *
73 */
74
75 /* This test is a generalized test for verifying changes to the locale data.
76 * It is driven by an external file that specifies the particular pieces of locale
77 * data to check. That file is in .properties file format: a series of key/value
78 * pairs delimited by newline characters, with the keys separated from the values
79 * by = signs. The keys are similar in syntax to a Unix pathname, with keys at
80 * successive levels of containment in the resource-data hierarchy separated by
81 * slashes. The file is in ISO 8859-1 encoding, with control characters and
82 * non-ASCII characters denoted with backslash-u escape sequences. The program also allows
83 * blank lines and comment lines to be interspersed with the data. Comment lines
84 * begin with '#'.
85 *
86 * A data file for this test would look something like this:<pre>
87 * FormatData//MonthNames/0=January
88 * FormatData//MonthNames/1=February
89 * LocaleNames//US=United States
90 * LocaleNames//FR=France
91 * FormatData/fr_FR/MonthNames/0=janvier
92 * FormatData/fr_FR/MonthNames/1=f\u00e9vrier
93 * LocaleNames/fr_FR/US=\u00c9tats-Unis
94 * LocaleNames/fr_FR/FR=France</pre>
95 *
96 * You can use language tag with '-' in locale field like this:<pre>
97 * LocaleNames/sr-Latn/SR=Surinam
98 * FormatData/sr-Latn-BA/DayNames/2=utorak</pre>
99 *
100 * The command-line syntax of this test is
101 * <tt>java LocaleDataTest [-w] [{ -s | <filename> }]</tt>
102 *
103 * This program always sends its results to standard output. If -w is not specified,
104 * this program prints out only the differences between the data file and the actual
105 * resource data. If -w is specified, the program prints out every entry, comment,
106 * and blank line from the data file. Where there is a difference between the data
107 * file and the resource data, the data is the data from the resources. This feature
108 * can be used to quickly generate a new data file.
109 *
110 * The user can specify an optional filename or -s. If the user specifies a filename,
111 * the program uses that file as the data file. If the user specifies -s, the program
112 * reads its input from standard input rather than from a file. If the user specifies
113 * neither, the program reads its input from a file called LocaleData in the same
114 * directory the program itself resides in.
115 *
116 * The -nothrow option prevents the program from throwing an exception when it
117 * gets an error. -w implies -nothrow.
118 *
119 * Other command-line options can be specified, but are ignored.
120 *
121 * It's important to note what this test will NOT test. Certain changes to the locale
122 * data are meant to have certain effects on the internationalization frameworks. For
123 * instance, we could ensure round-trip formatting/parsing integrity for the full
124 * date/time format of SimpleDateFormat by making sure that the full date and time
125 * patterns include sufficient data. The test of this is not whether changes were
126 * made to the locale data; it's whether using this data gives round-trip integrity.
127 * Likewise, changing the currency patterns to use \u00a4 instead of local currency
128 * symbols isn't something that can be tested by this test; instead, you want to
129 * actually format currency values and make sure the proper currency symbol was used.
130 *
131 * This test by itself doesn't do an exhaustive comparison of locale data. It is
132 * possible to do this manually, however: Use the GenerateKeyList tool to produce
133 * a complete list of keys for the two versions of the locales you want to compare,
134 * and then diff them. This will flag additions and deletions. Generate a data file
135 * for the base version of the data using the -w option and the output from
136 * GenerateKeyList, and then use the resultant file as the data file when you run
137 * this test against the new version of the data.
138 */
139
140 import java.io.*;
141 import java.text.*;
142 import java.util.Locale;
143 import java.util.ResourceBundle;
144 import java.util.ResourceBundle.Control;
145 import java.util.MissingResourceException;
146
147 public class LocaleDataTest
148 {
149 static final String TEXT_RESOURCES_PACKAGE ="sun.text.resources";
150 static final String UTIL_RESOURCES_PACKAGE ="sun.util.resources";
151
152 public static void main(String[] args) throws Exception {
153
154 // set up our flags and our input and output streams based on the
155 // command-line arguments (exceptions generated here will propagate out
156 // to the environment)
157 BufferedReader in = null;
158 PrintWriter out = null;
159 boolean writeNewFile = false;
160 boolean doThrow = true;
161
162 for (int i = 0; i < args.length; i++) {
163 if (args[i].equals("-w")) {
164 writeNewFile = true;
165 doThrow = false;
166 }
167
168 else if (args[i].equals("-nothrow"))
169 doThrow = false;
170
171 else if (args[i].equals("-s") && in == null)
172 in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
173 "ISO8859_1")));
174 else if (!args[i].startsWith("-") && in == null)
175 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
176 FileInputStream(args[i]), "ISO8859_1")));
177 }
178 if (in == null) {
179 File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
180 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
181 FileInputStream(localeData), "ISO8859_1")));
182 }
183 out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
184 "ISO8859_1")), true);
185
186 // perform the actual test
187 int errorCount = doTest(in, out, writeNewFile);
188
189 // write out the error count, and throw an exception out into the environment
190 // if there were any errors
191 if (errorCount != 0) {
192 if (!writeNewFile)
193 out.println("Test failed. " + errorCount + " errors.");
194 if (doThrow)
195 throw new Exception("Test failed. " + errorCount + " errors.");
196 }
197 else if (!writeNewFile)
198 out.println("Test passed.");
199
200 in.close();
201 out.close();
202 }
203
204 static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
205 throws Exception {
206 int errorCount = 0;
207
208 String key = null;
209 String expectedValue = null;
210 String line = in.readLine();
211 while (line != null) {
212 if (line.startsWith("#") || line.length() == 0) {
213 if (writeNewFile)
214 out.println(line);
215 }
216
217 else {
218 int index = line.indexOf("=");
219 if (index == -1) {
220 key = line;
221 expectedValue = "";
222 }
223 else {
224 key = line.substring(0, index);
225 if (index + 1 == line.length())
226 expectedValue = "";
227 else
228 expectedValue = line.substring(index + 1);
229 }
230 if (!processLine(key, expectedValue, out, writeNewFile))
231 ++errorCount;
232 }
233 line = in.readLine();
234 }
235 return errorCount;
236 }
237
238 static boolean processLine(String key, String expectedValue, PrintWriter out,
239 boolean writeNewFile) throws Exception {
240 String rbName, localeName, resTag, qualifier;
241 String language = "", country = "", variant = "";
242 int index, oldIndex;
243
244 index = key.indexOf("/");
245 if (index == -1 || index + 1 == key.length())
246 throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
247 rbName = key.substring(0, index);
248
249 oldIndex = index + 1;
250 index = key.indexOf("/", oldIndex);
251 if (index == -1 || index + 1 == key.length())
252 throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
253 localeName = key.substring(oldIndex, index);
254 boolean use_tag = localeName.indexOf("-") != -1;
255
256 if (use_tag == false && localeName.length() > 0) {
257 language = localeName.substring(0, 2);
258 if (localeName.length() > 3) {
259 country = localeName.substring(3, 5);
260 if (localeName.length() > 5)
261 variant = localeName.substring(6);
262 }
263 }
264
265 oldIndex = index + 1;
266 index = key.indexOf("/", oldIndex);
267 if (index == -1)
268 index = key.length();
269 resTag = key.substring(oldIndex, index);
270
271 // TimeZone name may have "/" in it, for example "Asia/Taipei", so use "Asia\/Taipei in LocaleData.
272 if(resTag.endsWith("\\")) {
273 resTag = resTag.substring(0, resTag.length() - 1);
274 oldIndex = index;
275 index = key.indexOf("/", oldIndex + 1);
276 if (index == -1) index = key.length();
277 resTag += key.substring(oldIndex, index);
278 }
279
280 if (index < key.length() - 1)
281 qualifier = key.substring(index + 1);
282 else
283 qualifier = "";
284
285 String retrievedValue = null;
286 Object resource = null;
287 try {
288 String fullName = null;
289 if (rbName.equals("CalendarData")
290 || rbName.equals("CurrencyNames")
291 || rbName.equals("LocaleNames")
292 || rbName.equals("TimeZoneNames")) {
293 fullName = UTIL_RESOURCES_PACKAGE + "." + rbName;
294 } else {
295 fullName = TEXT_RESOURCES_PACKAGE + "." + rbName;
296 }
297 Locale locale;
298 if (use_tag) {
299 locale = Locale.forLanguageTag(localeName);
300 } else {
301 locale = new Locale(language, country, variant);
302 }
303 ResourceBundle bundle = ResourceBundle.getBundle(fullName,
304 locale,
305 JRELocaleResourceBundleControl.INSTANCE);
306 resource = bundle.getObject(resTag);
307 }
308 catch (MissingResourceException e) {
309 }
310
311 if (resource != null) {
312 if (resource instanceof String) {
313 retrievedValue = (String)resource;
314 }
315 else if (resource instanceof String[]) {
316 int element = Integer.valueOf(qualifier).intValue();
317 String[] stringList = (String[])resource;
318 if (element >= 0 && element < stringList.length)
319 retrievedValue = stringList[element];
320 }
321 else if (resource instanceof String[][]) {
322 String[][] stringArray = (String[][])resource;
323 int slash = qualifier.indexOf("/");
324 if (slash == -1) {
325 for (int i = 0; i < stringArray.length; i++) {
326 if (stringArray[i][0].equals(qualifier))
327 retrievedValue = stringArray[i][1];
328 }
329 }
330 else {
331 int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
332 int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
333 if (row >= 0 && row < stringArray.length && column >= 0 && column <
334 stringArray[row].length)
335 retrievedValue = stringArray[row][column];
336 }
337 }
338 }
339
340 if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
341 if (retrievedValue == null)
342 retrievedValue = "<MISSING!>";
343
344 if (writeNewFile)
345 out.println(key + "=" + retrievedValue);
346 else {
347 out.println("Mismatch in " + key + ":");
348 out.println(" file = \"" + expectedValue + "\"");
349 out.println(" jvm = \"" + retrievedValue + "\"");
350 }
351 return false;
352 }
353 else {
354 if (writeNewFile)
355 out.println(key + "=" + expectedValue);
356 }
357 return true;
358 }
359
360 private static class JRELocaleResourceBundleControl extends ResourceBundle.Control {
361 static final JRELocaleResourceBundleControl INSTANCE = new JRELocaleResourceBundleControl();
362
363 private JRELocaleResourceBundleControl() {
364 }
365
366 @Override
367 public Locale getFallbackLocale(String baseName, Locale locale) {
368 if (baseName == null || locale == null) {
369 throw new NullPointerException();
370 }
371 return null;
372 }
373
374 private static final String CLDR = ".cldr";
375
376 /**
377 * Changes baseName to its per-language package name and
378 * calls the super class implementation. For example,
379 * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
380 * the baseName is changed to "sun.text.resources.ja.FormatData". If
381 * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
382 * the name is changed to "sun.text.resources.cldr.jp.FormatData".
383 */
384 @Override
385 public String toBundleName(String baseName, Locale locale) {
386 String newBaseName = baseName;
387 String lang = locale.getLanguage();
388 if (lang.length() > 0) {
389 if (baseName.startsWith(UTIL_RESOURCES_PACKAGE)
390 || baseName.startsWith(TEXT_RESOURCES_PACKAGE)) {
391 // Assume the lengths are the same.
392 if (UTIL_RESOURCES_PACKAGE.length()
393 != TEXT_RESOURCES_PACKAGE.length()) {
394 throw new InternalError("The resources package names have different lengths.");
395 }
396 int index = TEXT_RESOURCES_PACKAGE.length();
397 if (baseName.indexOf(CLDR, index) > 0) {
398 index += CLDR.length();
399 }
400 newBaseName = baseName.substring(0, index + 1) + lang
401 + baseName.substring(index);
402 }
403 }
404 return super.toBundleName(newBaseName, locale);
405 }
406 }
407 }
408
409 class EscapeReader extends FilterReader {
410 public EscapeReader(Reader in) {
411 super(in);
412 }
413
414 public int read() throws IOException {
415 if (buffer != null) {
416 String b = buffer.toString();
417 int result = b.charAt(0);
418 if (b.length() > 1)
419 buffer = new StringBuffer(b.substring(1));
420 else
421 buffer = null;
422 return result;
423 }
424 else {
425 int result = super.read();
426 if (result != '\\')
427 return result;
428 else {
429 buffer = new StringBuffer();
430 result = super.read();
431 buffer.append((char)result);
432 if (result == 'u') {
433 for (int i = 0; i < 4; i++) {
434 result = super.read();
435 if (result == -1)
436 break;
437 buffer.append((char)result);
438 }
439 String number = buffer.toString().substring(1);
440 result = Integer.parseInt(number, 16);
441 buffer = null;
442 return result;
443 }
444 return '\\';
445 }
446 }
447 }
448
449 public int read(char[] cbuf, int start, int len) throws IOException {
450 int p = start;
451 int end = start + len;
452 int c = 0;
453 while (c != -1 && p < end) {
454 c = read();
455 if (c != -1)
456 cbuf[p++] = (char)c;
457 }
458 if (c == -1 && p == start)
459 return -1;
460 else
461 return p - start;
462 }
463
464 private StringBuffer buffer = null;
465 }
466
467 class EscapeWriter extends FilterWriter {
468 public EscapeWriter(Writer out) {
469 super(out);
470 }
471
472 public void write(int c) throws IOException {
473 if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
474 super.write(c);
475 else {
476 super.write('\\');
477 super.write('u');
478 String number = Integer.toHexString(c);
479 if (number.length() < 4)
480 number = zeros.substring(0, 4 - number.length()) + number;
481 super.write(number.charAt(0));
482 super.write(number.charAt(1));
483 super.write(number.charAt(2));
484 super.write(number.charAt(3));
485 }
486 }
487
488 public void write(char[] cbuf, int off, int len) throws IOException {
489 int end = off + len;
490 while (off < end)
491 write(cbuf[off++]);
492 }
493
494 public void write(String str, int off, int len) throws IOException {
495 int end = off + len;
496 while (off < end)
497 write(str.charAt(off++));
498 }
499
500 private static String zeros = "0000";
501 }
--- EOF ---