blob: 48c741a6bad7d5aef8709bdca7d366b7d86756b0 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohairbf91ea12011-04-06 22:06:11 -07002 * Copyright (c) 2007, 2011, 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
yhuanga8e354a2011-03-30 22:46:41 -070036 * 6919624 6998391 7019267 7020960 7025837
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 *
katakai998ef022011-02-26 03:53:06 -080094 * You can use language tag with '-' in locale field like this:<pre>
95 * LocaleNames/sr-Latn/SR=Surinam
96 * FormatData/sr-Latn-BA/DayNames/2=utorak</pre>
97 *
duke6e45e102007-12-01 00:00:00 +000098 * The command-line syntax of this test is
99 * <tt>java LocaleDataTest [-w] [{ -s | <filename> }]</tt>
100 *
101 * This program always sends its results to standard output. If -w is not specified,
102 * this program prints out only the differences between the data file and the actual
103 * resource data. If -w is specified, the program prints out every entry, comment,
104 * and blank line from the data file. Where there is a difference between the data
105 * file and the resource data, the data is the data from the resources. This feature
106 * can be used to quickly generate a new data file.
107 *
108 * The user can specify an optional filename or -s. If the user specifies a filename,
109 * the program uses that file as the data file. If the user specifies -s, the program
110 * reads its input from standard input rather than from a file. If the user specifies
111 * neither, the program reads its input from a file called LocaleData in the same
112 * directory the program itself resides in.
113 *
114 * The -nothrow option prevents the program from throwing an exception when it
115 * gets an error. -w implies -nothrow.
116 *
117 * Other command-line options can be specified, but are ignored.
118 *
119 * It's important to note what this test will NOT test. Certain changes to the locale
120 * data are meant to have certain effects on the internationalization frameworks. For
121 * instance, we could ensure round-trip formatting/parsing integrity for the full
122 * date/time format of SimpleDateFormat by making sure that the full date and time
123 * patterns include sufficient data. The test of this is not whether changes were
124 * made to the locale data; it's whether using this data gives round-trip integrity.
125 * Likewise, changing the currency patterns to use \u00a4 instead of local currency
126 * symbols isn't something that can be tested by this test; instead, you want to
127 * actually format currency values and make sure the proper currency symbol was used.
128 *
129 * This test by itself doesn't do an exhaustive comparison of locale data. It is
130 * possible to do this manually, however: Use the GenerateKeyList tool to produce
131 * a complete list of keys for the two versions of the locales you want to compare,
132 * and then diff them. This will flag additions and deletions. Generate a data file
133 * for the base version of the data using the -w option and the output from
134 * GenerateKeyList, and then use the resultant file as the data file when you run
135 * this test against the new version of the data.
136 */
137
138import java.io.*;
139import java.text.*;
140import java.util.Locale;
141import java.util.ResourceBundle;
142import java.util.ResourceBundle.Control;
143import java.util.MissingResourceException;
144
145public class LocaleDataTest
146{
147 public static void main(String[] args) throws Exception {
148
149 // set up our flags and our input and output streams based on the
150 // command-line arguments (exceptions generated here will propagate out
151 // to the environment)
152 BufferedReader in = null;
153 PrintWriter out = null;
154 boolean writeNewFile = false;
155 boolean doThrow = true;
156
157 for (int i = 0; i < args.length; i++) {
158 if (args[i].equals("-w")) {
159 writeNewFile = true;
160 doThrow = false;
161 }
162
163 else if (args[i].equals("-nothrow"))
164 doThrow = false;
165
166 else if (args[i].equals("-s") && in == null)
167 in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
168 "ISO8859_1")));
169 else if (!args[i].startsWith("-") && in == null)
170 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
171 FileInputStream(args[i]), "ISO8859_1")));
172 }
173 if (in == null) {
174 File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
175 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
176 FileInputStream(localeData), "ISO8859_1")));
177 }
178 out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
179 "ISO8859_1")), true);
180
181 // perform the actual test
182 int errorCount = doTest(in, out, writeNewFile);
183
184 // write out the error count, and throw an exception out into the environment
185 // if there were any errors
186 if (errorCount != 0) {
187 if (!writeNewFile)
188 out.println("Test failed. " + errorCount + " errors.");
189 if (doThrow)
190 throw new Exception("Test failed. " + errorCount + " errors.");
191 }
192 else if (!writeNewFile)
193 out.println("Test passed.");
194
195 in.close();
196 out.close();
197 }
198
199 static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
200 throws Exception {
201 int errorCount = 0;
202
203 String key = null;
204 String expectedValue = null;
205 String line = in.readLine();
206 while (line != null) {
207 if (line.startsWith("#") || line.length() == 0) {
208 if (writeNewFile)
209 out.println(line);
210 }
211
212 else {
213 int index = line.indexOf("=");
214 if (index == -1) {
215 key = line;
216 expectedValue = "";
217 }
218 else {
219 key = line.substring(0, index);
220 if (index + 1 == line.length())
221 expectedValue = "";
222 else
223 expectedValue = line.substring(index + 1);
224 }
225 if (!processLine(key, expectedValue, out, writeNewFile))
226 ++errorCount;
227 }
228 line = in.readLine();
229 }
230 return errorCount;
231 }
232
233 static boolean processLine(String key, String expectedValue, PrintWriter out,
234 boolean writeNewFile) throws Exception {
235 String rbName, localeName, resTag, qualifier;
236 String language = "", country = "", variant = "";
237 int index, oldIndex;
238
239 index = key.indexOf("/");
240 if (index == -1 || index + 1 == key.length())
241 throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
242 rbName = key.substring(0, index);
243
244 oldIndex = index + 1;
245 index = key.indexOf("/", oldIndex);
246 if (index == -1 || index + 1 == key.length())
247 throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
248 localeName = key.substring(oldIndex, index);
katakai998ef022011-02-26 03:53:06 -0800249 boolean use_tag = localeName.indexOf("-") != -1;
250
251 if (use_tag == false && localeName.length() > 0) {
duke6e45e102007-12-01 00:00:00 +0000252 language = localeName.substring(0, 2);
253 if (localeName.length() > 3) {
254 country = localeName.substring(3, 5);
255 if (localeName.length() > 5)
256 variant = localeName.substring(6);
257 }
258 }
259
260 oldIndex = index + 1;
261 index = key.indexOf("/", oldIndex);
262 if (index == -1)
263 index = key.length();
264 resTag = key.substring(oldIndex, index);
265
yhuangbbd79a12009-12-08 21:14:04 -0800266 // TimeZone name may have "/" in it, for example "Asia/Taipei", so use "Asia\/Taipei in LocaleData.
267 if(resTag.endsWith("\\")) {
268 resTag = resTag.substring(0, resTag.length() - 1);
269 oldIndex = index;
270 index = key.indexOf("/", oldIndex + 1);
271 if (index == -1) index = key.length();
272 resTag += key.substring(oldIndex, index);
273 }
274
duke6e45e102007-12-01 00:00:00 +0000275 if (index < key.length() - 1)
276 qualifier = key.substring(index + 1);
277 else
278 qualifier = "";
279
280 String retrievedValue = null;
281 Object resource = null;
282 try {
283 String fullName = null;
284 if (rbName.equals("CalendarData")
285 || rbName.equals("CurrencyNames")
286 || rbName.equals("LocaleNames")
287 || rbName.equals("TimeZoneNames")) {
288 fullName = "sun.util.resources." + rbName;
289 } else {
290 fullName = "sun.text.resources." + rbName;
291 }
katakai998ef022011-02-26 03:53:06 -0800292 Locale locale;
293 if (use_tag) {
294 locale = Locale.forLanguageTag(localeName);
295 } else {
296 locale = new Locale(language, country, variant);
297 }
duke6e45e102007-12-01 00:00:00 +0000298 ResourceBundle bundle = ResourceBundle.getBundle(fullName,
katakai998ef022011-02-26 03:53:06 -0800299 locale,
duke6e45e102007-12-01 00:00:00 +0000300 ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
301 resource = bundle.getObject(resTag);
302 }
303 catch (MissingResourceException e) {
304 }
305
306 if (resource != null) {
307 if (resource instanceof String) {
308 retrievedValue = (String)resource;
309 }
310 else if (resource instanceof String[]) {
311 int element = Integer.valueOf(qualifier).intValue();
312 String[] stringList = (String[])resource;
313 if (element >= 0 || element < stringList.length)
314 retrievedValue = stringList[element];
315 }
316 else if (resource instanceof String[][]) {
317 String[][] stringArray = (String[][])resource;
318 int slash = qualifier.indexOf("/");
319 if (slash == -1) {
320 for (int i = 0; i < stringArray.length; i++) {
321 if (stringArray[i][0].equals(qualifier))
322 retrievedValue = stringArray[i][1];
323 }
324 }
325 else {
326 int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
327 int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
328 if (row >= 0 || row < stringArray.length || column >= 0 || column <
329 stringArray[row].length)
330 retrievedValue = stringArray[row][column];
331 }
332 }
333 }
334
335 if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
336 if (retrievedValue == null)
337 retrievedValue = "<MISSING!>";
338
339 if (writeNewFile)
340 out.println(key + "=" + retrievedValue);
341 else {
342 out.println("Mismatch in " + key + ":");
343 out.println(" file = \"" + expectedValue + "\"");
344 out.println(" jvm = \"" + retrievedValue + "\"");
345 }
346 return false;
347 }
348 else {
349 if (writeNewFile)
350 out.println(key + "=" + expectedValue);
351 }
352 return true;
353 }
354}
355
356class EscapeReader extends FilterReader {
357 public EscapeReader(Reader in) {
358 super(in);
359 }
360
361 public int read() throws IOException {
362 if (buffer != null) {
363 String b = buffer.toString();
364 int result = b.charAt(0);
365 if (b.length() > 1)
366 buffer = new StringBuffer(b.substring(1));
367 else
368 buffer = null;
369 return result;
370 }
371 else {
372 int result = super.read();
373 if (result != '\\')
374 return result;
375 else {
376 buffer = new StringBuffer();
377 result = super.read();
378 buffer.append((char)result);
379 if (result == 'u') {
380 for (int i = 0; i < 4; i++) {
381 result = super.read();
382 if (result == -1)
383 break;
384 buffer.append((char)result);
385 }
386 String number = buffer.toString().substring(1);
387 result = Integer.parseInt(number, 16);
388 buffer = null;
389 return result;
390 }
391 return '\\';
392 }
393 }
394 }
395
396 public int read(char[] cbuf, int start, int len) throws IOException {
397 int p = start;
398 int end = start + len;
399 int c = 0;
400 while (c != -1 && p < end) {
401 c = read();
402 if (c != -1)
403 cbuf[p++] = (char)c;
404 }
405 if (c == -1 && p == start)
406 return -1;
407 else
408 return p - start;
409 }
410
411 private StringBuffer buffer = null;
412}
413
414class EscapeWriter extends FilterWriter {
415 public EscapeWriter(Writer out) {
416 super(out);
417 }
418
419 public void write(int c) throws IOException {
420 if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
421 super.write(c);
422 else {
423 super.write('\\');
424 super.write('u');
425 String number = Integer.toHexString(c);
426 if (number.length() < 4)
427 number = zeros.substring(0, 4 - number.length()) + number;
428 super.write(number.charAt(0));
429 super.write(number.charAt(1));
430 super.write(number.charAt(2));
431 super.write(number.charAt(3));
432 }
433 }
434
435 public void write(char[] cbuf, int off, int len) throws IOException {
436 int end = off + len;
437 while (off < end)
438 write(cbuf[off++]);
439 }
440
441 public void write(String str, int off, int len) throws IOException {
442 int end = off + len;
443 while (off < end)
444 write(str.charAt(off++));
445 }
446
447 private static String zeros = "0000";
448}