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