author | mikejwre |
Thu, 03 Dec 2009 12:52:46 -0800 | |
changeset 4400 | 6f7b7cb74a72 |
parent 891 | 8ef167bd8428 |
child 4600 | d49895808acd |
permissions | -rw-r--r-- |
2 | 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 |
|
891
8ef167bd8428
6509039: Swedish localization has incorrect am/pm markers in FormatData_sv
jtusla
parents:
2
diff
changeset
|
34 |
* 6509039 |
2 | 35 |
* @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 |
||
132 |
import java.io.*; |
|
133 |
import java.text.*; |
|
134 |
import java.util.Locale; |
|
135 |
import java.util.ResourceBundle; |
|
136 |
import java.util.ResourceBundle.Control; |
|
137 |
import java.util.MissingResourceException; |
|
138 |
||
139 |
public 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 |
||
258 |
if (index < key.length() - 1) |
|
259 |
qualifier = key.substring(index + 1); |
|
260 |
else |
|
261 |
qualifier = ""; |
|
262 |
||
263 |
String retrievedValue = null; |
|
264 |
Object resource = null; |
|
265 |
try { |
|
266 |
String fullName = null; |
|
267 |
if (rbName.equals("CalendarData") |
|
268 |
|| rbName.equals("CurrencyNames") |
|
269 |
|| rbName.equals("LocaleNames") |
|
270 |
|| rbName.equals("TimeZoneNames")) { |
|
271 |
fullName = "sun.util.resources." + rbName; |
|
272 |
} else { |
|
273 |
fullName = "sun.text.resources." + rbName; |
|
274 |
} |
|
275 |
ResourceBundle bundle = ResourceBundle.getBundle(fullName, |
|
276 |
new Locale(language, country, variant), |
|
277 |
ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT)); |
|
278 |
resource = bundle.getObject(resTag); |
|
279 |
} |
|
280 |
catch (MissingResourceException e) { |
|
281 |
} |
|
282 |
||
283 |
if (resource != null) { |
|
284 |
if (resource instanceof String) { |
|
285 |
retrievedValue = (String)resource; |
|
286 |
} |
|
287 |
else if (resource instanceof String[]) { |
|
288 |
int element = Integer.valueOf(qualifier).intValue(); |
|
289 |
String[] stringList = (String[])resource; |
|
290 |
if (element >= 0 || element < stringList.length) |
|
291 |
retrievedValue = stringList[element]; |
|
292 |
} |
|
293 |
else if (resource instanceof String[][]) { |
|
294 |
String[][] stringArray = (String[][])resource; |
|
295 |
int slash = qualifier.indexOf("/"); |
|
296 |
if (slash == -1) { |
|
297 |
for (int i = 0; i < stringArray.length; i++) { |
|
298 |
if (stringArray[i][0].equals(qualifier)) |
|
299 |
retrievedValue = stringArray[i][1]; |
|
300 |
} |
|
301 |
} |
|
302 |
else { |
|
303 |
int row = Integer.valueOf(qualifier.substring(0, slash)).intValue(); |
|
304 |
int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue(); |
|
305 |
if (row >= 0 || row < stringArray.length || column >= 0 || column < |
|
306 |
stringArray[row].length) |
|
307 |
retrievedValue = stringArray[row][column]; |
|
308 |
} |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
if (retrievedValue == null || !retrievedValue.equals(expectedValue)) { |
|
313 |
if (retrievedValue == null) |
|
314 |
retrievedValue = "<MISSING!>"; |
|
315 |
||
316 |
if (writeNewFile) |
|
317 |
out.println(key + "=" + retrievedValue); |
|
318 |
else { |
|
319 |
out.println("Mismatch in " + key + ":"); |
|
320 |
out.println(" file = \"" + expectedValue + "\""); |
|
321 |
out.println(" jvm = \"" + retrievedValue + "\""); |
|
322 |
} |
|
323 |
return false; |
|
324 |
} |
|
325 |
else { |
|
326 |
if (writeNewFile) |
|
327 |
out.println(key + "=" + expectedValue); |
|
328 |
} |
|
329 |
return true; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
class EscapeReader extends FilterReader { |
|
334 |
public EscapeReader(Reader in) { |
|
335 |
super(in); |
|
336 |
} |
|
337 |
||
338 |
public int read() throws IOException { |
|
339 |
if (buffer != null) { |
|
340 |
String b = buffer.toString(); |
|
341 |
int result = b.charAt(0); |
|
342 |
if (b.length() > 1) |
|
343 |
buffer = new StringBuffer(b.substring(1)); |
|
344 |
else |
|
345 |
buffer = null; |
|
346 |
return result; |
|
347 |
} |
|
348 |
else { |
|
349 |
int result = super.read(); |
|
350 |
if (result != '\\') |
|
351 |
return result; |
|
352 |
else { |
|
353 |
buffer = new StringBuffer(); |
|
354 |
result = super.read(); |
|
355 |
buffer.append((char)result); |
|
356 |
if (result == 'u') { |
|
357 |
for (int i = 0; i < 4; i++) { |
|
358 |
result = super.read(); |
|
359 |
if (result == -1) |
|
360 |
break; |
|
361 |
buffer.append((char)result); |
|
362 |
} |
|
363 |
String number = buffer.toString().substring(1); |
|
364 |
result = Integer.parseInt(number, 16); |
|
365 |
buffer = null; |
|
366 |
return result; |
|
367 |
} |
|
368 |
return '\\'; |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
public int read(char[] cbuf, int start, int len) throws IOException { |
|
374 |
int p = start; |
|
375 |
int end = start + len; |
|
376 |
int c = 0; |
|
377 |
while (c != -1 && p < end) { |
|
378 |
c = read(); |
|
379 |
if (c != -1) |
|
380 |
cbuf[p++] = (char)c; |
|
381 |
} |
|
382 |
if (c == -1 && p == start) |
|
383 |
return -1; |
|
384 |
else |
|
385 |
return p - start; |
|
386 |
} |
|
387 |
||
388 |
private StringBuffer buffer = null; |
|
389 |
} |
|
390 |
||
391 |
class EscapeWriter extends FilterWriter { |
|
392 |
public EscapeWriter(Writer out) { |
|
393 |
super(out); |
|
394 |
} |
|
395 |
||
396 |
public void write(int c) throws IOException { |
|
397 |
if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n') |
|
398 |
super.write(c); |
|
399 |
else { |
|
400 |
super.write('\\'); |
|
401 |
super.write('u'); |
|
402 |
String number = Integer.toHexString(c); |
|
403 |
if (number.length() < 4) |
|
404 |
number = zeros.substring(0, 4 - number.length()) + number; |
|
405 |
super.write(number.charAt(0)); |
|
406 |
super.write(number.charAt(1)); |
|
407 |
super.write(number.charAt(2)); |
|
408 |
super.write(number.charAt(3)); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
public void write(char[] cbuf, int off, int len) throws IOException { |
|
413 |
int end = off + len; |
|
414 |
while (off < end) |
|
415 |
write(cbuf[off++]); |
|
416 |
} |
|
417 |
||
418 |
public void write(String str, int off, int len) throws IOException { |
|
419 |
int end = off + len; |
|
420 |
while (off < end) |
|
421 |
write(str.charAt(off++)); |
|
422 |
} |
|
423 |
||
424 |
private static String zeros = "0000"; |
|
425 |
} |