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