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