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