Print this page
rev 5615 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o Jigsaw. by Naoto Sato and Masayoshi Okutsu)
Split |
Close |
Expand all |
Collapse all |
--- old/test/sun/text/resources/LocaleDataTest.java
+++ new/test/sun/text/resources/LocaleDataTest.java
1 1 /*
2 2 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23 /*
24 24 * @test
25 25 * @bug 4052473 4052679 4055602 4066550 4067619 4068012 4068073 4070174 4070452
26 26 * 4070178 4070450 4070695 4070725 4070795 4071003 4071183 4071782 4072013
27 27 * 4072388 4072773 4075404 4084356 4087238 4092361 4094033 4094371 4098518
28 28 * 4099810 4103218 4103220 4103861 4112136 4113638 4113654 4117054 4122468
29 29 * 4122840 4139860 4156708 4175306 4215747 4209960 4290801 4900884 4942982
30 30 * 4518811 4945388 4936845 4794068 4461740 4965260 4984277 4826794 5032580
31 31 * 5102005 5074431 6182685 6208712 6277020 6245766 6351682 6386647 6379382
32 32 * 6414459 6455680 6498742 6558863 6488119 6547501 6497154 6558856 6481177
33 33 * 6379214 6485516 6486607 4225362 4494727 6533691 6531591 6531593 6570259
34 34 * 6509039 6609737 6610748 6645271 6507067 6873931 6450945 6645268 6646611
35 35 * 6645405 6650730 6910489 6573250 6870908 6585666 6716626 6914413 6916787
36 36 * 6919624 6998391 7019267 7020960 7025837 7020583 7036905 7066203 7101495
37 37 * 7003124 7085757 7028073 7171028
38 38 * @summary Verify locale data
39 39 *
40 40 */
41 41
42 42 /*
43 43 *
44 44 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
45 45 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
46 46 *
47 47 * Portions copyright (c) 2007 Sun Microsystems, Inc.
48 48 * All Rights Reserved.
49 49 *
50 50 * The original version of this source code and documentation
51 51 * is copyrighted and owned by Taligent, Inc., a wholly-owned
52 52 * subsidiary of IBM. These materials are provided under terms
53 53 * of a License Agreement between Taligent and Sun. This technology
54 54 * is protected by multiple US and International patents.
55 55 *
56 56 * This notice and attribution to Taligent may not be removed.
57 57 * Taligent is a registered trademark of Taligent, Inc.
58 58 *
59 59 * Permission to use, copy, modify, and distribute this software
60 60 * and its documentation for NON-COMMERCIAL purposes and without
61 61 * fee is hereby granted provided that this copyright notice
62 62 * appears in all copies. Please refer to the file "copyright.html"
63 63 * for further important copyright and licensing information.
64 64 *
65 65 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
66 66 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
67 67 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
68 68 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
69 69 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
70 70 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
71 71 *
72 72 */
73 73
74 74 /* This test is a generalized test for verifying changes to the locale data.
75 75 * It is driven by an external file that specifies the particular pieces of locale
76 76 * data to check. That file is in .properties file format: a series of key/value
77 77 * pairs delimited by newline characters, with the keys separated from the values
78 78 * by = signs. The keys are similar in syntax to a Unix pathname, with keys at
79 79 * successive levels of containment in the resource-data hierarchy separated by
80 80 * slashes. The file is in ISO 8859-1 encoding, with control characters and
81 81 * non-ASCII characters denoted with backslash-u escape sequences. The program also allows
82 82 * blank lines and comment lines to be interspersed with the data. Comment lines
83 83 * begin with '#'.
84 84 *
85 85 * A data file for this test would look something like this:<pre>
86 86 * FormatData//MonthNames/0=January
87 87 * FormatData//MonthNames/1=February
88 88 * LocaleNames//US=United States
89 89 * LocaleNames//FR=France
90 90 * FormatData/fr_FR/MonthNames/0=janvier
91 91 * FormatData/fr_FR/MonthNames/1=f\u00e9vrier
92 92 * LocaleNames/fr_FR/US=\u00c9tats-Unis
93 93 * LocaleNames/fr_FR/FR=France</pre>
94 94 *
95 95 * You can use language tag with '-' in locale field like this:<pre>
96 96 * LocaleNames/sr-Latn/SR=Surinam
97 97 * FormatData/sr-Latn-BA/DayNames/2=utorak</pre>
98 98 *
99 99 * The command-line syntax of this test is
100 100 * <tt>java LocaleDataTest [-w] [{ -s | <filename> }]</tt>
101 101 *
102 102 * This program always sends its results to standard output. If -w is not specified,
103 103 * this program prints out only the differences between the data file and the actual
104 104 * resource data. If -w is specified, the program prints out every entry, comment,
105 105 * and blank line from the data file. Where there is a difference between the data
106 106 * file and the resource data, the data is the data from the resources. This feature
107 107 * can be used to quickly generate a new data file.
108 108 *
109 109 * The user can specify an optional filename or -s. If the user specifies a filename,
110 110 * the program uses that file as the data file. If the user specifies -s, the program
111 111 * reads its input from standard input rather than from a file. If the user specifies
112 112 * neither, the program reads its input from a file called LocaleData in the same
113 113 * directory the program itself resides in.
114 114 *
115 115 * The -nothrow option prevents the program from throwing an exception when it
116 116 * gets an error. -w implies -nothrow.
117 117 *
118 118 * Other command-line options can be specified, but are ignored.
119 119 *
120 120 * It's important to note what this test will NOT test. Certain changes to the locale
121 121 * data are meant to have certain effects on the internationalization frameworks. For
122 122 * instance, we could ensure round-trip formatting/parsing integrity for the full
123 123 * date/time format of SimpleDateFormat by making sure that the full date and time
124 124 * patterns include sufficient data. The test of this is not whether changes were
125 125 * made to the locale data; it's whether using this data gives round-trip integrity.
126 126 * Likewise, changing the currency patterns to use \u00a4 instead of local currency
127 127 * symbols isn't something that can be tested by this test; instead, you want to
128 128 * actually format currency values and make sure the proper currency symbol was used.
129 129 *
130 130 * This test by itself doesn't do an exhaustive comparison of locale data. It is
131 131 * possible to do this manually, however: Use the GenerateKeyList tool to produce
132 132 * a complete list of keys for the two versions of the locales you want to compare,
133 133 * and then diff them. This will flag additions and deletions. Generate a data file
134 134 * for the base version of the data using the -w option and the output from
135 135 * GenerateKeyList, and then use the resultant file as the data file when you run
136 136 * this test against the new version of the data.
137 137 */
↓ open down ↓ |
137 lines elided |
↑ open up ↑ |
138 138
139 139 import java.io.*;
140 140 import java.text.*;
141 141 import java.util.Locale;
142 142 import java.util.ResourceBundle;
143 143 import java.util.ResourceBundle.Control;
144 144 import java.util.MissingResourceException;
145 145
146 146 public class LocaleDataTest
147 147 {
148 + static final String TEXT_RESOURCES_PACKAGE ="sun.text.resources";
149 + static final String UTIL_RESOURCES_PACKAGE ="sun.util.resources";
150 +
148 151 public static void main(String[] args) throws Exception {
149 152
150 153 // set up our flags and our input and output streams based on the
151 154 // command-line arguments (exceptions generated here will propagate out
152 155 // to the environment)
153 156 BufferedReader in = null;
154 157 PrintWriter out = null;
155 158 boolean writeNewFile = false;
156 159 boolean doThrow = true;
157 160
158 161 for (int i = 0; i < args.length; i++) {
159 162 if (args[i].equals("-w")) {
160 163 writeNewFile = true;
161 164 doThrow = false;
162 165 }
163 166
164 167 else if (args[i].equals("-nothrow"))
165 168 doThrow = false;
166 169
167 170 else if (args[i].equals("-s") && in == null)
168 171 in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
169 172 "ISO8859_1")));
170 173 else if (!args[i].startsWith("-") && in == null)
171 174 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
172 175 FileInputStream(args[i]), "ISO8859_1")));
173 176 }
174 177 if (in == null) {
175 178 File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
176 179 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
177 180 FileInputStream(localeData), "ISO8859_1")));
178 181 }
179 182 out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
180 183 "ISO8859_1")), true);
181 184
182 185 // perform the actual test
183 186 int errorCount = doTest(in, out, writeNewFile);
184 187
185 188 // write out the error count, and throw an exception out into the environment
186 189 // if there were any errors
187 190 if (errorCount != 0) {
188 191 if (!writeNewFile)
189 192 out.println("Test failed. " + errorCount + " errors.");
190 193 if (doThrow)
191 194 throw new Exception("Test failed. " + errorCount + " errors.");
192 195 }
193 196 else if (!writeNewFile)
194 197 out.println("Test passed.");
195 198
196 199 in.close();
197 200 out.close();
198 201 }
199 202
200 203 static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
201 204 throws Exception {
202 205 int errorCount = 0;
203 206
204 207 String key = null;
205 208 String expectedValue = null;
206 209 String line = in.readLine();
207 210 while (line != null) {
208 211 if (line.startsWith("#") || line.length() == 0) {
209 212 if (writeNewFile)
210 213 out.println(line);
211 214 }
212 215
213 216 else {
214 217 int index = line.indexOf("=");
215 218 if (index == -1) {
216 219 key = line;
217 220 expectedValue = "";
218 221 }
219 222 else {
220 223 key = line.substring(0, index);
221 224 if (index + 1 == line.length())
222 225 expectedValue = "";
223 226 else
224 227 expectedValue = line.substring(index + 1);
225 228 }
226 229 if (!processLine(key, expectedValue, out, writeNewFile))
227 230 ++errorCount;
228 231 }
229 232 line = in.readLine();
230 233 }
231 234 return errorCount;
232 235 }
233 236
234 237 static boolean processLine(String key, String expectedValue, PrintWriter out,
235 238 boolean writeNewFile) throws Exception {
236 239 String rbName, localeName, resTag, qualifier;
237 240 String language = "", country = "", variant = "";
238 241 int index, oldIndex;
239 242
240 243 index = key.indexOf("/");
241 244 if (index == -1 || index + 1 == key.length())
242 245 throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
243 246 rbName = key.substring(0, index);
244 247
245 248 oldIndex = index + 1;
246 249 index = key.indexOf("/", oldIndex);
247 250 if (index == -1 || index + 1 == key.length())
248 251 throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
249 252 localeName = key.substring(oldIndex, index);
250 253 boolean use_tag = localeName.indexOf("-") != -1;
251 254
252 255 if (use_tag == false && localeName.length() > 0) {
253 256 language = localeName.substring(0, 2);
254 257 if (localeName.length() > 3) {
255 258 country = localeName.substring(3, 5);
256 259 if (localeName.length() > 5)
257 260 variant = localeName.substring(6);
258 261 }
259 262 }
260 263
261 264 oldIndex = index + 1;
262 265 index = key.indexOf("/", oldIndex);
263 266 if (index == -1)
264 267 index = key.length();
265 268 resTag = key.substring(oldIndex, index);
266 269
267 270 // TimeZone name may have "/" in it, for example "Asia/Taipei", so use "Asia\/Taipei in LocaleData.
268 271 if(resTag.endsWith("\\")) {
269 272 resTag = resTag.substring(0, resTag.length() - 1);
270 273 oldIndex = index;
271 274 index = key.indexOf("/", oldIndex + 1);
272 275 if (index == -1) index = key.length();
273 276 resTag += key.substring(oldIndex, index);
274 277 }
275 278
276 279 if (index < key.length() - 1)
277 280 qualifier = key.substring(index + 1);
278 281 else
↓ open down ↓ |
121 lines elided |
↑ open up ↑ |
279 282 qualifier = "";
280 283
281 284 String retrievedValue = null;
282 285 Object resource = null;
283 286 try {
284 287 String fullName = null;
285 288 if (rbName.equals("CalendarData")
286 289 || rbName.equals("CurrencyNames")
287 290 || rbName.equals("LocaleNames")
288 291 || rbName.equals("TimeZoneNames")) {
289 - fullName = "sun.util.resources." + rbName;
292 + fullName = UTIL_RESOURCES_PACKAGE + "." + rbName;
290 293 } else {
291 - fullName = "sun.text.resources." + rbName;
294 + fullName = TEXT_RESOURCES_PACKAGE + "." + rbName;
292 295 }
293 296 Locale locale;
294 297 if (use_tag) {
295 298 locale = Locale.forLanguageTag(localeName);
296 299 } else {
297 300 locale = new Locale(language, country, variant);
298 301 }
299 302 ResourceBundle bundle = ResourceBundle.getBundle(fullName,
300 - locale,
301 - ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
303 + locale,
304 + JRELocaleResourceBundleControl.INSTANCE);
302 305 resource = bundle.getObject(resTag);
303 306 }
304 307 catch (MissingResourceException e) {
305 308 }
306 309
307 310 if (resource != null) {
308 311 if (resource instanceof String) {
309 312 retrievedValue = (String)resource;
310 313 }
311 314 else if (resource instanceof String[]) {
312 315 int element = Integer.valueOf(qualifier).intValue();
313 316 String[] stringList = (String[])resource;
314 - if (element >= 0 || element < stringList.length)
317 + if (element >= 0 && element < stringList.length)
315 318 retrievedValue = stringList[element];
316 319 }
317 320 else if (resource instanceof String[][]) {
318 321 String[][] stringArray = (String[][])resource;
319 322 int slash = qualifier.indexOf("/");
320 323 if (slash == -1) {
321 324 for (int i = 0; i < stringArray.length; i++) {
322 325 if (stringArray[i][0].equals(qualifier))
323 326 retrievedValue = stringArray[i][1];
324 327 }
325 328 }
326 329 else {
327 330 int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
328 331 int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
329 - if (row >= 0 || row < stringArray.length || column >= 0 || column <
332 + if (row >= 0 && row < stringArray.length && column >= 0 && column <
330 333 stringArray[row].length)
331 334 retrievedValue = stringArray[row][column];
332 335 }
333 336 }
334 337 }
335 338
336 339 if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
337 340 if (retrievedValue == null)
338 341 retrievedValue = "<MISSING!>";
339 342
340 343 if (writeNewFile)
341 344 out.println(key + "=" + retrievedValue);
342 345 else {
343 346 out.println("Mismatch in " + key + ":");
344 347 out.println(" file = \"" + expectedValue + "\"");
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
345 348 out.println(" jvm = \"" + retrievedValue + "\"");
346 349 }
347 350 return false;
348 351 }
349 352 else {
350 353 if (writeNewFile)
351 354 out.println(key + "=" + expectedValue);
352 355 }
353 356 return true;
354 357 }
358 +
359 + private static class JRELocaleResourceBundleControl extends ResourceBundle.Control {
360 + static final JRELocaleResourceBundleControl INSTANCE = new JRELocaleResourceBundleControl();
361 +
362 + private JRELocaleResourceBundleControl() {
363 + }
364 +
365 + @Override
366 + public Locale getFallbackLocale(String baseName, Locale locale) {
367 + if (baseName == null || locale == null) {
368 + throw new NullPointerException();
369 + }
370 + return null;
371 + }
372 +
373 + private static final String CLDR = ".cldr";
374 +
375 + /**
376 + * Changes baseName to its per-language package name and
377 + * calls the super class implementation. For example,
378 + * if the baseName is "sun.text.resources.FormatData" and locale is ja_JP,
379 + * the baseName is changed to "sun.text.resources.ja.FormatData". If
380 + * baseName contains "cldr", such as "sun.text.resources.cldr.FormatData",
381 + * the name is changed to "sun.text.resources.cldr.jp.FormatData".
382 + */
383 + @Override
384 + public String toBundleName(String baseName, Locale locale) {
385 + String newBaseName = baseName;
386 + String lang = locale.getLanguage();
387 + if (lang.length() > 0) {
388 + if (baseName.startsWith(UTIL_RESOURCES_PACKAGE)
389 + || baseName.startsWith(TEXT_RESOURCES_PACKAGE)) {
390 + // Assume the lengths are the same.
391 + if (UTIL_RESOURCES_PACKAGE.length()
392 + != TEXT_RESOURCES_PACKAGE.length()) {
393 + throw new InternalError("The resources package names have different lengths.");
394 + }
395 + int index = TEXT_RESOURCES_PACKAGE.length();
396 + if (baseName.indexOf(CLDR, index) > 0) {
397 + index += CLDR.length();
398 + }
399 + newBaseName = baseName.substring(0, index + 1) + lang
400 + + baseName.substring(index);
401 + }
402 + }
403 + return super.toBundleName(newBaseName, locale);
404 + }
405 + }
355 406 }
356 407
357 408 class EscapeReader extends FilterReader {
358 409 public EscapeReader(Reader in) {
359 410 super(in);
360 411 }
361 412
362 413 public int read() throws IOException {
363 414 if (buffer != null) {
364 415 String b = buffer.toString();
365 416 int result = b.charAt(0);
366 417 if (b.length() > 1)
367 418 buffer = new StringBuffer(b.substring(1));
368 419 else
369 420 buffer = null;
370 421 return result;
371 422 }
372 423 else {
373 424 int result = super.read();
374 425 if (result != '\\')
375 426 return result;
376 427 else {
377 428 buffer = new StringBuffer();
378 429 result = super.read();
379 430 buffer.append((char)result);
380 431 if (result == 'u') {
381 432 for (int i = 0; i < 4; i++) {
382 433 result = super.read();
383 434 if (result == -1)
384 435 break;
385 436 buffer.append((char)result);
386 437 }
387 438 String number = buffer.toString().substring(1);
388 439 result = Integer.parseInt(number, 16);
389 440 buffer = null;
390 441 return result;
391 442 }
392 443 return '\\';
393 444 }
394 445 }
395 446 }
396 447
397 448 public int read(char[] cbuf, int start, int len) throws IOException {
398 449 int p = start;
399 450 int end = start + len;
400 451 int c = 0;
401 452 while (c != -1 && p < end) {
402 453 c = read();
403 454 if (c != -1)
404 455 cbuf[p++] = (char)c;
405 456 }
406 457 if (c == -1 && p == start)
407 458 return -1;
408 459 else
409 460 return p - start;
410 461 }
411 462
412 463 private StringBuffer buffer = null;
413 464 }
414 465
415 466 class EscapeWriter extends FilterWriter {
416 467 public EscapeWriter(Writer out) {
417 468 super(out);
418 469 }
419 470
420 471 public void write(int c) throws IOException {
421 472 if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
422 473 super.write(c);
423 474 else {
424 475 super.write('\\');
425 476 super.write('u');
426 477 String number = Integer.toHexString(c);
427 478 if (number.length() < 4)
428 479 number = zeros.substring(0, 4 - number.length()) + number;
429 480 super.write(number.charAt(0));
430 481 super.write(number.charAt(1));
431 482 super.write(number.charAt(2));
432 483 super.write(number.charAt(3));
433 484 }
434 485 }
435 486
436 487 public void write(char[] cbuf, int off, int len) throws IOException {
437 488 int end = off + len;
438 489 while (off < end)
439 490 write(cbuf[off++]);
440 491 }
441 492
442 493 public void write(String str, int off, int len) throws IOException {
443 494 int end = off + len;
444 495 while (off < end)
445 496 write(str.charAt(off++));
446 497 }
447 498
448 499 private static String zeros = "0000";
449 500 }
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX