0N/A/*
3909N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 4052473 4052679 4055602 4066550 4067619 4068012 4068073 4070174 4070452
0N/A * 4070178 4070450 4070695 4070725 4070795 4071003 4071183 4071782 4072013
0N/A * 4072388 4072773 4075404 4084356 4087238 4092361 4094033 4094371 4098518
0N/A * 4099810 4103218 4103220 4103861 4112136 4113638 4113654 4117054 4122468
0N/A * 4122840 4139860 4156708 4175306 4215747 4209960 4290801 4900884 4942982
0N/A * 4518811 4945388 4936845 4794068 4461740 4965260 4984277 4826794 5032580
0N/A * 5102005 5074431 6182685 6208712 6277020 6245766 6351682 6386647 6379382
0N/A * 6414459 6455680 6498742 6558863 6488119 6547501 6497154 6558856 6481177
0N/A * 6379214 6485516 6486607 4225362 4494727 6533691 6531591 6531593 6570259
2055N/A * 6509039 6609737 6610748 6645271 6507067 6873931 6450945 6645268 6646611
2313N/A * 6645405 6650730 6910489 6573250 6870908 6585666 6716626 6914413 6916787
5479N/A * 6919624 6998391 7019267 7020960 7025837 7020583 7036905 7066203 7189611
5520N/A * 7171028
0N/A * @summary Verify locale data
0N/A *
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
0N/A *
0N/A * Portions copyright (c) 2007 Sun Microsystems, Inc.
0N/A * All Rights Reserved.
0N/A *
0N/A * The original version of this source code and documentation
0N/A * is copyrighted and owned by Taligent, Inc., a wholly-owned
0N/A * subsidiary of IBM. These materials are provided under terms
0N/A * of a License Agreement between Taligent and Sun. This technology
0N/A * is protected by multiple US and International patents.
0N/A *
0N/A * This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A * Permission to use, copy, modify, and distribute this software
0N/A * and its documentation for NON-COMMERCIAL purposes and without
0N/A * fee is hereby granted provided that this copyright notice
0N/A * appears in all copies. Please refer to the file "copyright.html"
0N/A * for further important copyright and licensing information.
0N/A *
0N/A * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
0N/A * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
0N/A * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
0N/A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
0N/A * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
0N/A * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
0N/A *
0N/A */
0N/A
0N/A/* This test is a generalized test for verifying changes to the locale data.
0N/A * It is driven by an external file that specifies the particular pieces of locale
0N/A * data to check. That file is in .properties file format: a series of key/value
0N/A * pairs delimited by newline characters, with the keys separated from the values
0N/A * by = signs. The keys are similar in syntax to a Unix pathname, with keys at
0N/A * successive levels of containment in the resource-data hierarchy separated by
0N/A * slashes. The file is in ISO 8859-1 encoding, with control characters and
0N/A * non-ASCII characters denoted with backslash-u escape sequences. The program also allows
0N/A * blank lines and comment lines to be interspersed with the data. Comment lines
0N/A * begin with '#'.
0N/A *
0N/A * A data file for this test would look something like this:<pre>
0N/A * FormatData//MonthNames/0=January
0N/A * FormatData//MonthNames/1=February
0N/A * LocaleNames//US=United States
0N/A * LocaleNames//FR=France
0N/A * FormatData/fr_FR/MonthNames/0=janvier
0N/A * FormatData/fr_FR/MonthNames/1=f\u00e9vrier
0N/A * LocaleNames/fr_FR/US=\u00c9tats-Unis
0N/A * LocaleNames/fr_FR/FR=France</pre>
0N/A *
3699N/A * You can use language tag with '-' in locale field like this:<pre>
3699N/A * LocaleNames/sr-Latn/SR=Surinam
3699N/A * FormatData/sr-Latn-BA/DayNames/2=utorak</pre>
3699N/A *
0N/A * The command-line syntax of this test is
0N/A * <tt>java LocaleDataTest [-w] [{ -s | <filename> }]</tt>
0N/A *
0N/A * This program always sends its results to standard output. If -w is not specified,
0N/A * this program prints out only the differences between the data file and the actual
0N/A * resource data. If -w is specified, the program prints out every entry, comment,
0N/A * and blank line from the data file. Where there is a difference between the data
0N/A * file and the resource data, the data is the data from the resources. This feature
0N/A * can be used to quickly generate a new data file.
0N/A *
0N/A * The user can specify an optional filename or -s. If the user specifies a filename,
0N/A * the program uses that file as the data file. If the user specifies -s, the program
0N/A * reads its input from standard input rather than from a file. If the user specifies
0N/A * neither, the program reads its input from a file called LocaleData in the same
0N/A * directory the program itself resides in.
0N/A *
0N/A * The -nothrow option prevents the program from throwing an exception when it
0N/A * gets an error. -w implies -nothrow.
0N/A *
0N/A * Other command-line options can be specified, but are ignored.
0N/A *
0N/A * It's important to note what this test will NOT test. Certain changes to the locale
0N/A * data are meant to have certain effects on the internationalization frameworks. For
0N/A * instance, we could ensure round-trip formatting/parsing integrity for the full
0N/A * date/time format of SimpleDateFormat by making sure that the full date and time
0N/A * patterns include sufficient data. The test of this is not whether changes were
0N/A * made to the locale data; it's whether using this data gives round-trip integrity.
0N/A * Likewise, changing the currency patterns to use \u00a4 instead of local currency
0N/A * symbols isn't something that can be tested by this test; instead, you want to
0N/A * actually format currency values and make sure the proper currency symbol was used.
0N/A *
0N/A * This test by itself doesn't do an exhaustive comparison of locale data. It is
0N/A * possible to do this manually, however: Use the GenerateKeyList tool to produce
0N/A * a complete list of keys for the two versions of the locales you want to compare,
0N/A * and then diff them. This will flag additions and deletions. Generate a data file
0N/A * for the base version of the data using the -w option and the output from
0N/A * GenerateKeyList, and then use the resultant file as the data file when you run
0N/A * this test against the new version of the data.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.text.*;
0N/Aimport java.util.Locale;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.ResourceBundle.Control;
0N/Aimport java.util.MissingResourceException;
0N/A
0N/Apublic class LocaleDataTest
0N/A{
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A // set up our flags and our input and output streams based on the
0N/A // command-line arguments (exceptions generated here will propagate out
0N/A // to the environment)
0N/A BufferedReader in = null;
0N/A PrintWriter out = null;
0N/A boolean writeNewFile = false;
0N/A boolean doThrow = true;
0N/A
0N/A for (int i = 0; i < args.length; i++) {
0N/A if (args[i].equals("-w")) {
0N/A writeNewFile = true;
0N/A doThrow = false;
0N/A }
0N/A
0N/A else if (args[i].equals("-nothrow"))
0N/A doThrow = false;
0N/A
0N/A else if (args[i].equals("-s") && in == null)
0N/A in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
0N/A "ISO8859_1")));
0N/A else if (!args[i].startsWith("-") && in == null)
0N/A in = new BufferedReader(new EscapeReader(new InputStreamReader(new
0N/A FileInputStream(args[i]), "ISO8859_1")));
0N/A }
0N/A if (in == null) {
0N/A File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
0N/A in = new BufferedReader(new EscapeReader(new InputStreamReader(new
0N/A FileInputStream(localeData), "ISO8859_1")));
0N/A }
0N/A out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
0N/A "ISO8859_1")), true);
0N/A
0N/A // perform the actual test
0N/A int errorCount = doTest(in, out, writeNewFile);
0N/A
0N/A // write out the error count, and throw an exception out into the environment
0N/A // if there were any errors
0N/A if (errorCount != 0) {
0N/A if (!writeNewFile)
0N/A out.println("Test failed. " + errorCount + " errors.");
0N/A if (doThrow)
0N/A throw new Exception("Test failed. " + errorCount + " errors.");
0N/A }
0N/A else if (!writeNewFile)
0N/A out.println("Test passed.");
0N/A
0N/A in.close();
0N/A out.close();
0N/A }
0N/A
0N/A static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
0N/A throws Exception {
0N/A int errorCount = 0;
0N/A
0N/A String key = null;
0N/A String expectedValue = null;
0N/A String line = in.readLine();
0N/A while (line != null) {
0N/A if (line.startsWith("#") || line.length() == 0) {
0N/A if (writeNewFile)
0N/A out.println(line);
0N/A }
0N/A
0N/A else {
0N/A int index = line.indexOf("=");
0N/A if (index == -1) {
0N/A key = line;
0N/A expectedValue = "";
0N/A }
0N/A else {
0N/A key = line.substring(0, index);
0N/A if (index + 1 == line.length())
0N/A expectedValue = "";
0N/A else
0N/A expectedValue = line.substring(index + 1);
0N/A }
0N/A if (!processLine(key, expectedValue, out, writeNewFile))
0N/A ++errorCount;
0N/A }
0N/A line = in.readLine();
0N/A }
0N/A return errorCount;
0N/A }
0N/A
0N/A static boolean processLine(String key, String expectedValue, PrintWriter out,
0N/A boolean writeNewFile) throws Exception {
0N/A String rbName, localeName, resTag, qualifier;
0N/A String language = "", country = "", variant = "";
0N/A int index, oldIndex;
0N/A
0N/A index = key.indexOf("/");
0N/A if (index == -1 || index + 1 == key.length())
0N/A throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
0N/A rbName = key.substring(0, index);
0N/A
0N/A oldIndex = index + 1;
0N/A index = key.indexOf("/", oldIndex);
0N/A if (index == -1 || index + 1 == key.length())
0N/A throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
0N/A localeName = key.substring(oldIndex, index);
3699N/A boolean use_tag = localeName.indexOf("-") != -1;
3699N/A
3699N/A if (use_tag == false && localeName.length() > 0) {
0N/A language = localeName.substring(0, 2);
0N/A if (localeName.length() > 3) {
0N/A country = localeName.substring(3, 5);
0N/A if (localeName.length() > 5)
0N/A variant = localeName.substring(6);
0N/A }
0N/A }
0N/A
0N/A oldIndex = index + 1;
0N/A index = key.indexOf("/", oldIndex);
0N/A if (index == -1)
0N/A index = key.length();
0N/A resTag = key.substring(oldIndex, index);
0N/A
2048N/A // TimeZone name may have "/" in it, for example "Asia/Taipei", so use "Asia\/Taipei in LocaleData.
2048N/A if(resTag.endsWith("\\")) {
2048N/A resTag = resTag.substring(0, resTag.length() - 1);
2048N/A oldIndex = index;
2048N/A index = key.indexOf("/", oldIndex + 1);
2048N/A if (index == -1) index = key.length();
2048N/A resTag += key.substring(oldIndex, index);
2048N/A }
2048N/A
0N/A if (index < key.length() - 1)
0N/A qualifier = key.substring(index + 1);
0N/A else
0N/A qualifier = "";
0N/A
0N/A String retrievedValue = null;
0N/A Object resource = null;
0N/A try {
0N/A String fullName = null;
0N/A if (rbName.equals("CalendarData")
0N/A || rbName.equals("CurrencyNames")
0N/A || rbName.equals("LocaleNames")
0N/A || rbName.equals("TimeZoneNames")) {
0N/A fullName = "sun.util.resources." + rbName;
0N/A } else {
0N/A fullName = "sun.text.resources." + rbName;
0N/A }
3699N/A Locale locale;
3699N/A if (use_tag) {
3699N/A locale = Locale.forLanguageTag(localeName);
3699N/A } else {
3699N/A locale = new Locale(language, country, variant);
3699N/A }
0N/A ResourceBundle bundle = ResourceBundle.getBundle(fullName,
3699N/A locale,
0N/A ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
0N/A resource = bundle.getObject(resTag);
0N/A }
0N/A catch (MissingResourceException e) {
0N/A }
0N/A
0N/A if (resource != null) {
0N/A if (resource instanceof String) {
0N/A retrievedValue = (String)resource;
0N/A }
0N/A else if (resource instanceof String[]) {
0N/A int element = Integer.valueOf(qualifier).intValue();
0N/A String[] stringList = (String[])resource;
0N/A if (element >= 0 || element < stringList.length)
0N/A retrievedValue = stringList[element];
0N/A }
0N/A else if (resource instanceof String[][]) {
0N/A String[][] stringArray = (String[][])resource;
0N/A int slash = qualifier.indexOf("/");
0N/A if (slash == -1) {
0N/A for (int i = 0; i < stringArray.length; i++) {
0N/A if (stringArray[i][0].equals(qualifier))
0N/A retrievedValue = stringArray[i][1];
0N/A }
0N/A }
0N/A else {
0N/A int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
0N/A int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
0N/A if (row >= 0 || row < stringArray.length || column >= 0 || column <
0N/A stringArray[row].length)
0N/A retrievedValue = stringArray[row][column];
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
0N/A if (retrievedValue == null)
0N/A retrievedValue = "<MISSING!>";
0N/A
0N/A if (writeNewFile)
0N/A out.println(key + "=" + retrievedValue);
0N/A else {
0N/A out.println("Mismatch in " + key + ":");
0N/A out.println(" file = \"" + expectedValue + "\"");
0N/A out.println(" jvm = \"" + retrievedValue + "\"");
0N/A }
0N/A return false;
0N/A }
0N/A else {
0N/A if (writeNewFile)
0N/A out.println(key + "=" + expectedValue);
0N/A }
0N/A return true;
0N/A }
0N/A}
0N/A
0N/Aclass EscapeReader extends FilterReader {
0N/A public EscapeReader(Reader in) {
0N/A super(in);
0N/A }
0N/A
0N/A public int read() throws IOException {
0N/A if (buffer != null) {
0N/A String b = buffer.toString();
0N/A int result = b.charAt(0);
0N/A if (b.length() > 1)
0N/A buffer = new StringBuffer(b.substring(1));
0N/A else
0N/A buffer = null;
0N/A return result;
0N/A }
0N/A else {
0N/A int result = super.read();
0N/A if (result != '\\')
0N/A return result;
0N/A else {
0N/A buffer = new StringBuffer();
0N/A result = super.read();
0N/A buffer.append((char)result);
0N/A if (result == 'u') {
0N/A for (int i = 0; i < 4; i++) {
0N/A result = super.read();
0N/A if (result == -1)
0N/A break;
0N/A buffer.append((char)result);
0N/A }
0N/A String number = buffer.toString().substring(1);
0N/A result = Integer.parseInt(number, 16);
0N/A buffer = null;
0N/A return result;
0N/A }
0N/A return '\\';
0N/A }
0N/A }
0N/A }
0N/A
0N/A public int read(char[] cbuf, int start, int len) throws IOException {
0N/A int p = start;
0N/A int end = start + len;
0N/A int c = 0;
0N/A while (c != -1 && p < end) {
0N/A c = read();
0N/A if (c != -1)
0N/A cbuf[p++] = (char)c;
0N/A }
0N/A if (c == -1 && p == start)
0N/A return -1;
0N/A else
0N/A return p - start;
0N/A }
0N/A
0N/A private StringBuffer buffer = null;
0N/A}
0N/A
0N/Aclass EscapeWriter extends FilterWriter {
0N/A public EscapeWriter(Writer out) {
0N/A super(out);
0N/A }
0N/A
0N/A public void write(int c) throws IOException {
0N/A if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
0N/A super.write(c);
0N/A else {
0N/A super.write('\\');
0N/A super.write('u');
0N/A String number = Integer.toHexString(c);
0N/A if (number.length() < 4)
0N/A number = zeros.substring(0, 4 - number.length()) + number;
0N/A super.write(number.charAt(0));
0N/A super.write(number.charAt(1));
0N/A super.write(number.charAt(2));
0N/A super.write(number.charAt(3));
0N/A }
0N/A }
0N/A
0N/A public void write(char[] cbuf, int off, int len) throws IOException {
0N/A int end = off + len;
0N/A while (off < end)
0N/A write(cbuf[off++]);
0N/A }
0N/A
0N/A public void write(String str, int off, int len) throws IOException {
0N/A int end = off + len;
0N/A while (off < end)
0N/A write(str.charAt(off++));
0N/A }
0N/A
0N/A private static String zeros = "0000";
0N/A}