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