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