1 /*
   2  * Copyright (c) 2007, 2022, 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 4052404 4052440 4084688 4092475 4101316 4105828 4107014 4107953 4110613
  26  * 4118587 4118595 4122371 4126371 4126880 4135316 4135752 4139504 4139940 4143951
  27  * 4147315 4147317 4147552 4335196 4778440 4940539 5010672 6475525 6544471 6627549
  28  * 6786276 7066203 7085757 8008577 8030696 8170840 8255086 8263202 8287868
  29  * @summary test Locales
  30  * @library /java/text/testlib
  31  * @modules jdk.localedata
  32  * @run main/othervm -Djava.locale.providers=COMPAT,SPI LocaleTest
  33  * @run main/othervm -Djava.locale.providers=COMPAT,SPI -Djava.locale.useOldISOCodes=true LocaleTest
  34  */
  35 /*
  36  * This file is available under and governed by the GNU General Public
  37  * License version 2 only, as published by the Free Software Foundation.
  38  * However, the following notice accompanied the original version of this
  39  * file and, per its terms, should not be removed:
  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 import java.io.ByteArrayInputStream;
  72 import java.io.ByteArrayOutputStream;
  73 import java.io.IOException;
  74 import java.io.ObjectInputStream;
  75 import java.io.ObjectOutputStream;
  76 import java.io.OptionalDataException;
  77 import java.io.StreamCorruptedException;
  78 import java.text.DateFormat;
  79 import java.text.DecimalFormat;
  80 import java.text.NumberFormat;
  81 import java.text.SimpleDateFormat;
  82 import java.util.Arrays;
  83 import java.util.Calendar;
  84 import java.util.Date;
  85 import java.util.List;
  86 import java.util.Locale;
  87 import java.util.MissingResourceException;
  88 
  89 public class LocaleTest extends IntlTest {
  90     public LocaleTest() {
  91     }
  92 
  93     private int ENGLISH = 0;
  94     private int FRENCH = 1;
  95     private int CROATIAN = 2;
  96     private int GREEK = 3;
  97     private int NORWEGIAN = 4;
  98     private int ITALIAN = 5;
  99     private int DUMMY = 6;
 100     private int MAX_LOCALES = 6;
 101 
 102     private int LANG = 0;
 103     private int CTRY = 1;
 104     private int VAR = 2;
 105     private int NAME = 3;
 106     private int LANG3 = 4;
 107     private int CTRY3 = 5;
 108     private int LCID = 6;
 109     private int DLANG_EN = 7;
 110     private int DCTRY_EN = 8;
 111     private int DVAR_EN = 9;
 112     private int DNAME_EN = 10;
 113     private int DLANG_FR = 11;
 114     private int DCTRY_FR = 12;
 115     private int DVAR_FR = 13;
 116     private int DNAME_FR = 14;
 117     private int DLANG_HR = 15;
 118     private int DCTRY_HR = 16;
 119     private int DVAR_HR = 17;
 120     private int DNAME_HR = 18;
 121     private int DLANG_EL = 19;
 122     private int DCTRY_EL = 20;
 123     private int DVAR_EL = 21;
 124     private int DNAME_EL = 22;
 125     private int DLANG_ROOT = 23;
 126     private int DCTRY_ROOT = 24;
 127     private int DVAR_ROOT = 25;
 128     private int DNAME_ROOT = 26;
 129 
 130     private String[][] dataTable = {
 131         // language code
 132         {   "en",   "fr",   "hr",   "el",   "no",   "it",   "xx"    },
 133         // country code
 134         {   "US",   "FR",   "HR",   "GR",   "NO",   "",   "YY"    },
 135         // variant code
 136         {   "",     "",     "",     "",     "NY",   "",   ""    },
 137         // full name
 138         {   "en_US",    "fr_FR",    "hr_HR",    "el_GR",    "no_NO_NY", "it",   "xx_YY"  },
 139         // ISO-3 language
 140         {   "eng",  "fra",  "hrv",  "ell",  "nor",  "ita",  ""   },
 141         // ISO-3 country
 142         {   "USA",  "FRA",  "HRV",  "GRC",  "NOR",  "",     ""   },
 143         // LCID (not currently public)
 144         {   "0409", "040c", "041a", "0408", "0814", "",     ""  },
 145 
 146         // display language (English)
 147         {   "English",  "French",   "Croatian", "Greek",    "Norwegian",    "Italian",  "xx" },
 148         // display country (English)
 149         {   "United States",    "France",   "Croatia",  "Greece",   "Norway",   "",     "YY" },
 150         // display variant (English)
 151         {   "",     "",     "",     "",     "Nynorsk",   "",     ""},
 152         // display name (English)
 153         // Updated no_NO_NY English display name for new pattern-based algorithm
 154         // (part of Euro support).
 155         {   "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },
 156 
 157         // display langage (French)
 158         {   "anglais",  "fran\u00e7ais",   "croate", "grec",    "norv\u00e9gien",    "italien", "xx" },
 159         // display country (French)
 160         {   "\u00c9tats-Unis",    "France",   "Croatie",  "Gr\u00e8ce",   "Norv\u00e8ge", "",     "YY" },
 161         // display variant (French)
 162         {   "",     "",     "",     "",     "",     "",    "" },
 163         // display name (French)
 164         {   "anglais (\u00c9tats-Unis)", "fran\u00e7ais (France)", "croate (Croatie)", "grec (Gr\u00e8ce)", "norv\u00e9gien (Norv\u00e8ge,Nynorsk)", "italien", "xx (YY)" },
 165 
 166         // display langage (Croatian)
 167         {   "",  "", "hrvatski", "",    "", "", "xx" },
 168         // display country (Croatian)
 169         {   "",    "",   "Hrvatska",  "",   "", "", "YY" },
 170         // display variant (Croatian)
 171         {   "",     "",     "",     "",     "", "", ""},
 172         // display name (Croatian)
 173         {   "", "", "hrvatski (Hrvatska)", "", "", "", "xx (YY)" },
 174 
 175         // display langage (Greek)
 176         {   "\u0391\u03b3\u03b3\u03bb\u03b9\u03ba\u03ac",  "\u0393\u03b1\u03bb\u03bb\u03b9\u03ba\u03ac", "\u039a\u03c1\u03bf\u03b1\u03c4\u03b9\u03ba\u03ac", "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac",    "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03b9\u03ba\u03ac", "\u0399\u03c4\u03b1\u03bb\u03b9\u03ba\u03ac", "xx" },
 177         // display country (Greek)
 178         {   "\u0397\u03bd\u03c9\u03bc\u03ad\u03bd\u03b5\u03c2 \u03a0\u03bf\u03bb\u03b9\u03c4\u03b5\u03af\u03b5\u03c2",    "\u0393\u03b1\u03bb\u03bb\u03af\u03b1",   "\u039a\u03c1\u03bf\u03b1\u03c4\u03af\u03b1",  "\u0395\u03bb\u03bb\u03ac\u03b4\u03b1",   "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03af\u03b1", "", "YY" },
 179         // display variant (Greek)
 180         {   "",     "",     "",     "",     "", "", "" },
 181         // display name (Greek)
 182         {   "\u0391\u03b3\u03b3\u03bb\u03b9\u03ba\u03ac (\u0397\u03bd\u03c9\u03bc\u03ad\u03bd\u03b5\u03c2 \u03a0\u03bf\u03bb\u03b9\u03c4\u03b5\u03af\u03b5\u03c2)", "\u0393\u03b1\u03bb\u03bb\u03b9\u03ba\u03ac (\u0393\u03b1\u03bb\u03bb\u03af\u03b1)", "\u039a\u03c1\u03bf\u03b1\u03c4\u03b9\u03ba\u03ac (\u039a\u03c1\u03bf\u03b1\u03c4\u03af\u03b1)", "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac (\u0395\u03bb\u03bb\u03ac\u03b4\u03b1)", "\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03b9\u03ba\u03ac (\u039d\u03bf\u03c1\u03b2\u03b7\u03b3\u03af\u03b1,Nynorsk)", "\u0399\u03c4\u03b1\u03bb\u03b9\u03ba\u03ac", "xx (YY)" },
 183 
 184         // display langage (<root>)
 185         {   "English",  "French",   "Croatian", "Greek",    "Norwegian",  "Italian",  "xx" },
 186         // display country (<root>)
 187         {   "United States",    "France",   "Croatia",  "Greece",   "Norway",  "",     "YY" },
 188         // display variant (<root>)
 189         {   "",     "",     "",     "",     "Nynorsk",   "",     ""},
 190         // display name (<root>)
 191         {   "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },
 192     };
 193 
 194     public static void main(String[] args) throws Exception {
 195         new LocaleTest().run(args);
 196     }
 197 
 198     public void TestBasicGetters() {
 199         for (int i = 0; i <= MAX_LOCALES; i++) {
 200             Locale testLocale = Locale.of(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
 201             logln("Testing " + testLocale + "...");
 202 
 203             if (!testLocale.getLanguage().equals(dataTable[LANG][i])) {
 204                 errln("  Language code mismatch: " + testLocale.getLanguage() + " versus "
 205                         + dataTable[LANG][i]);
 206             }
 207             if (!testLocale.getCountry().equals(dataTable[CTRY][i])) {
 208                 errln("  Country code mismatch: " + testLocale.getCountry() + " versus "
 209                         + dataTable[CTRY][i]);
 210             }
 211             if (!testLocale.getVariant().equals(dataTable[VAR][i])) {
 212                 errln("  Variant code mismatch: " + testLocale.getVariant() + " versus "
 213                         + dataTable[VAR][i]);
 214             }
 215             if (!testLocale.toString().equals(dataTable[NAME][i])) {
 216                 errln("  Locale name mismatch: " + testLocale.toString() + " versus "
 217                         + dataTable[NAME][i]);
 218             }
 219         }
 220 
 221         logln("Same thing without variant codes...");
 222         for (int i = 0; i <= MAX_LOCALES; i++) {
 223             Locale testLocale = Locale.of(dataTable[LANG][i], dataTable[CTRY][i]);
 224             logln("Testing " + testLocale + "...");
 225 
 226             if (!testLocale.getLanguage().equals(dataTable[LANG][i])) {
 227                 errln("  Language code mismatch: " + testLocale.getLanguage() + " versus "
 228                         + dataTable[LANG][i]);
 229             }
 230             if (!testLocale.getCountry().equals(dataTable[CTRY][i])) {
 231                 errln("  Country code mismatch: " + testLocale.getCountry() + " versus "
 232                         + dataTable[CTRY][i]);
 233             }
 234             if (!testLocale.getVariant().equals("")) {
 235                 errln("  Variant code mismatch: " + testLocale.getVariant() + " versus \"\"");
 236             }
 237         }
 238     }
 239 
 240     public void TestSimpleResourceInfo() {
 241         for (int i = 0; i <= MAX_LOCALES; i++) {
 242             if (dataTable[LANG][i].equals("xx")) {
 243                 continue;
 244             }
 245 
 246             Locale testLocale = Locale.of(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
 247             logln("Testing " + testLocale + "...");
 248 
 249             if (!testLocale.getISO3Language().equals(dataTable[LANG3][i])) {
 250                 errln("  ISO-3 language code mismatch: " + testLocale.getISO3Language()
 251                         + " versus " + dataTable[LANG3][i]);
 252             }
 253             if (!testLocale.getISO3Country().equals(dataTable[CTRY3][i])) {
 254                 errln("  ISO-3 country code mismatch: " + testLocale.getISO3Country()
 255                         + " versus " + dataTable[CTRY3][i]);
 256             }
 257 /*
 258             // getLCID() is currently private
 259             if (!String.valueOf(testLocale.getLCID()).equals(dataTable[LCID][i]))
 260                 errln("  LCID mismatch: " + testLocale.getLCID() + " versus "
 261                             + dataTable[LCID][i]);
 262 */
 263         }
 264     }
 265 
 266     /*
 267      * @bug 4101316
 268      * @bug 4084688 (This bug appears to be a duplicate of something, because it was fixed
 269      *              between 1.1.5 and 1.1.6, but I included a new test for it anyway)
 270      * @bug 4052440 Stop falling back to the default locale.
 271      */
 272     public void TestDisplayNames() {
 273         Locale saveDefault = Locale.getDefault();
 274         Locale english = Locale.US;
 275         Locale french = Locale.FRANCE;
 276         Locale croatian = Locale.of("hr", "HR");
 277         Locale greek = Locale.of("el", "GR");
 278 
 279         Locale.setDefault(english);
 280         logln("With default = en_US...");
 281         logln("  In default locale...");
 282         doTestDisplayNames(null, DLANG_EN, false);
 283         logln("  In locale = en_US...");
 284         doTestDisplayNames(english, DLANG_EN, false);
 285         logln("  In locale = fr_FR...");
 286         doTestDisplayNames(french, DLANG_FR, false);
 287         logln("  In locale = hr_HR...");
 288         doTestDisplayNames(croatian, DLANG_HR, false);
 289         logln("  In locale = el_GR...");
 290         doTestDisplayNames(greek, DLANG_EL, false);
 291 
 292         Locale.setDefault(french);
 293         logln("With default = fr_FR...");
 294         logln("  In default locale...");
 295         doTestDisplayNames(null, DLANG_FR, true);
 296         logln("  In locale = en_US...");
 297         doTestDisplayNames(english, DLANG_EN, true);
 298         logln("  In locale = fr_FR...");
 299         doTestDisplayNames(french, DLANG_FR, true);
 300         logln("  In locale = hr_HR...");
 301         doTestDisplayNames(croatian, DLANG_HR, true);
 302         logln("  In locale = el_GR...");
 303         doTestDisplayNames(greek, DLANG_EL, true);
 304 
 305         Locale.setDefault(saveDefault);
 306     }
 307 
 308     private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
 309         String language = Locale.getDefault().getLanguage();
 310 
 311         if (defaultIsFrench && !language.equals("fr")) {
 312             errln("Default locale should be French, but it's really " + language);
 313         } else if (!defaultIsFrench && !language.equals("en")) {
 314             errln("Default locale should be English, but it's really " + language);
 315         }
 316 
 317         for (int i = 0; i <= MAX_LOCALES; i++) {
 318             Locale testLocale = Locale.of(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
 319             logln("  Testing " + testLocale + "...");
 320 
 321             String testLang;
 322             String testCtry;
 323             String testVar;
 324             String testName;
 325 
 326             if (inLocale == null) {
 327                 testLang = testLocale.getDisplayLanguage();
 328                 testCtry = testLocale.getDisplayCountry();
 329                 testVar = testLocale.getDisplayVariant();
 330                 testName = testLocale.getDisplayName();
 331             } else {
 332                 testLang = testLocale.getDisplayLanguage(inLocale);
 333                 testCtry = testLocale.getDisplayCountry(inLocale);
 334                 testVar = testLocale.getDisplayVariant(inLocale);
 335                 testName = testLocale.getDisplayName(inLocale);
 336             }
 337 
 338             String expectedLang;
 339             String expectedCtry;
 340             String expectedVar;
 341             String expectedName;
 342 
 343             expectedLang = dataTable[compareIndex][i];
 344             if (expectedLang.equals("") && defaultIsFrench) {
 345                 expectedLang = dataTable[DLANG_EN][i];
 346             }
 347             if (expectedLang.equals("")) {
 348                 expectedLang = dataTable[DLANG_ROOT][i];
 349             }
 350 
 351             expectedCtry = dataTable[compareIndex + 1][i];
 352             if (expectedCtry.equals("") && defaultIsFrench) {
 353                 expectedCtry = dataTable[DCTRY_EN][i];
 354             }
 355             if (expectedCtry.equals("")) {
 356                 expectedCtry = dataTable[DCTRY_ROOT][i];
 357             }
 358 
 359             expectedVar = dataTable[compareIndex + 2][i];
 360             if (expectedVar.equals("") && defaultIsFrench) {
 361                 expectedVar = dataTable[DVAR_EN][i];
 362             }
 363             if (expectedVar.equals("")) {
 364                 expectedVar = dataTable[DVAR_ROOT][i];
 365             }
 366 
 367             expectedName = dataTable[compareIndex + 3][i];
 368             if (expectedName.equals("") && defaultIsFrench) {
 369                 expectedName = dataTable[DNAME_EN][i];
 370             }
 371             if (expectedName.equals("")) {
 372                 expectedName = dataTable[DNAME_ROOT][i];
 373             }
 374 
 375             if (!testLang.equals(expectedLang)) {
 376                 errln("Display language mismatch: " + testLang + " versus " + expectedLang);
 377             }
 378             if (!testCtry.equals(expectedCtry)) {
 379                 errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
 380             }
 381             if (!testVar.equals(expectedVar)) {
 382                 errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
 383             }
 384             if (!testName.equals(expectedName)) {
 385                 errln("Display name mismatch: " + testName + " versus " + expectedName);
 386             }
 387         }
 388     }
 389 
 390     @SuppressWarnings("deprecation")
 391     public void TestSimpleObjectStuff() {
 392         Locale test1 = new Locale("aa", "AA");
 393         Locale test2 = new Locale("aa", "AA");
 394         Locale test3 = (Locale) test1.clone();
 395         Locale test4 = Locale.of("zz", "ZZ");
 396 
 397         if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3) {
 398             errln("Some of the test variables point to the same locale!");
 399         }
 400 
 401         if (test3 == null) {
 402             errln("clone() failed to produce a valid object!");
 403         }
 404 
 405         if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3)) {
 406             errln("clone() or equals() failed: objects that should compare equal don't");
 407         }
 408 
 409         if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4)) {
 410             errln("equals() failed: objects that shouldn't compare equal do");
 411         }
 412 
 413         int hash1 = test1.hashCode();
 414         int hash2 = test2.hashCode();
 415         int hash3 = test3.hashCode();
 416 
 417         if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3) {
 418             errln("hashCode() failed: objects that should have the same hash code don't");
 419         }
 420     }
 421 
 422     /**
 423      * @bug 4011756 4011380
 424      */
 425     public void TestISO3Fallback() {
 426         Locale test = Locale.of("xx", "YY");
 427         boolean gotException = false;
 428         String result = "";
 429 
 430         try {
 431             result = test.getISO3Language();
 432         } catch (MissingResourceException e) {
 433             gotException = true;
 434         }
 435         if (!gotException) {
 436             errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");
 437         }
 438 
 439         gotException = false;
 440         try {
 441             result = test.getISO3Country();
 442         } catch (MissingResourceException e) {
 443             gotException = true;
 444         }
 445         if (!gotException) {
 446             errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");
 447         }
 448     }
 449 
 450     /**
 451      * @bug 4106155 4118587 7066203 7085757
 452      */
 453     public void TestGetLangsAndCountries() {
 454         // It didn't seem right to just do an exhaustive test of everything here, so I check
 455         // for the following things:
 456         // 1) Does each list have the right total number of entries?
 457         // 2) Does each list contain certain language and country codes we think are important
 458         //     (the G7 countries, plus a couple others)?
 459         // 3) Does each list have every entry formatted correctly? (i.e., two characters,
 460         //     all lower case for the language codes, all upper case for the country codes)
 461         // 4) Is each list in sorted order?
 462         String[] test = Locale.getISOLanguages();
 463         String[] spotCheck1 = {"en", "es", "fr", "de", "it", "ja", "ko", "zh", "th",
 464             "he", "id", "iu", "ug", "yi", "za"};
 465 
 466         if (test.length != 188) {
 467             errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length);
 468         } else {
 469             for (int i = 0; i < spotCheck1.length; i++) {
 470                 int j;
 471                 for (j = 0; j < test.length; j++) {
 472                     if (test[j].equals(spotCheck1[i])) {
 473                         break;
 474                     }
 475                 }
 476                 if (j == test.length || !test[j].equals(spotCheck1[i])) {
 477                     errln("Couldn't find " + spotCheck1[i] + " in language list.");
 478                 }
 479             }
 480         }
 481         for (int i = 0; i < test.length; i++) {
 482             if (!test[i].equals(test[i].toLowerCase())) {
 483                 errln(test[i] + " is not all lower case.");
 484             }
 485             if (test[i].length() != 2) {
 486                 errln(test[i] + " is not two characters long.");
 487             }
 488             if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {
 489                 errln(test[i] + " appears in an out-of-order position in the list.");
 490             }
 491         }
 492 
 493         test = Locale.getISOCountries();
 494         String[] spotCheck2 = {"US", "CA", "GB", "FR", "DE", "IT", "JP", "KR", "CN", "TW", "TH"};
 495 
 496 
 497         if (test.length != 249) {
 498             errln("Expected getISOCountries to return 249 countries; it returned " + test.length);
 499         } else {
 500             for (int i = 0; i < spotCheck2.length; i++) {
 501                 int j;
 502                 for (j = 0; j < test.length; j++) {
 503                     if (test[j].equals(spotCheck2[i])) {
 504                         break;
 505                     }
 506                 }
 507                 if (j == test.length || !test[j].equals(spotCheck2[i])) {
 508                     errln("Couldn't find " + spotCheck2[i] + " in country list.");
 509                 }
 510             }
 511         }
 512         for (int i = 0; i < test.length; i++) {
 513             if (!test[i].equals(test[i].toUpperCase())) {
 514                 errln(test[i] + " is not all upper case.");
 515             }
 516             if (test[i].length() != 2) {
 517                 errln(test[i] + " is not two characters long.");
 518             }
 519             if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {
 520                 errln(test[i] + " appears in an out-of-order position in the list.");
 521             }
 522         }
 523     }
 524 
 525     /**
 526      * @bug 4126880
 527      */
 528     void Test4126880() {
 529         String[] test;
 530 
 531         test = Locale.getISOCountries();
 532         test[0] = "SUCKER!!!";
 533         test = Locale.getISOCountries();
 534         if (test[0].equals("SUCKER!!!")) {
 535             errln("Changed internal country code list!");
 536         }
 537 
 538         test = Locale.getISOLanguages();
 539         test[0] = "HAHAHAHA!!!";
 540         test = Locale.getISOLanguages();
 541         if (test[0].equals("HAHAHAHA!!!")) { // Fixed typo
 542             errln("Changes internal language code list!");
 543         }
 544     }
 545 
 546     /**
 547      * @bug 4107014
 548      */
 549     public void TestGetAvailableLocales() {
 550         Locale[] locales = Locale.getAvailableLocales();
 551         if (locales == null || locales.length == 0) {
 552             errln("Locale.getAvailableLocales() returned no installed locales!");
 553         } else {
 554             logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales.");
 555             for (int i = 0; i < locales.length; i++) {
 556                 logln(locales[i].toString());
 557             }
 558         }
 559     }
 560 
 561     /**
 562      * @bug 4135316
 563      */
 564     public void TestBug4135316() {
 565         Locale[] locales1 = Locale.getAvailableLocales();
 566         Locale[] locales2 = Locale.getAvailableLocales();
 567         if (locales1 == locales2) {
 568             errln("Locale.getAvailableLocales() doesn't clone its internal storage!");
 569         }
 570     }
 571 
 572     /**
 573      * @bug 4107953
 574      */
 575 /*
 576 test commented out pending API-change approval
 577     public void TestGetLanguagesForCountry() {
 578         String[] languages = Locale.getLanguagesForCountry("US");
 579 
 580         if (!searchStringArrayFor("en", languages))
 581             errln("Didn't get en as a language for US");
 582 
 583         languages = Locale.getLanguagesForCountry("FR");
 584         if (!searchStringArrayFor("fr", languages))
 585             errln("Didn't get fr as a language for FR");
 586 
 587         languages = Locale.getLanguagesForCountry("CH");
 588         if (!searchStringArrayFor("fr", languages))
 589             errln("Didn't get fr as a language for CH");
 590         if (!searchStringArrayFor("it", languages))
 591             errln("Didn't get it as a language for CH");
 592         if (!searchStringArrayFor("de", languages))
 593             errln("Didn't get de as a language for CH");
 594 
 595         languages = Locale.getLanguagesForCountry("JP");
 596         if (!searchStringArrayFor("ja", languages))
 597             errln("Didn't get ja as a language for JP");
 598     }
 599 */
 600 
 601     private boolean searchStringArrayFor(String s, String[] array) {
 602         for (int i = 0; i < array.length; i++)
 603             if (s.equals(array[i]))
 604                 return true;
 605         return false;
 606     }
 607     /**
 608      * @bug 4110613
 609      */
 610     public void TestSerialization() throws ClassNotFoundException, OptionalDataException,
 611             IOException, StreamCorruptedException {
 612         ObjectOutputStream ostream;
 613         ByteArrayOutputStream obstream;
 614         byte[] bytes = null;
 615 
 616         obstream = new ByteArrayOutputStream();
 617         ostream = new ObjectOutputStream(obstream);
 618 
 619         Locale test1 = Locale.of("zh", "TW");
 620         int dummy = test1.hashCode();   // fill in the cached hash-code value
 621         ostream.writeObject(test1);
 622 
 623         bytes = obstream.toByteArray();
 624 
 625         ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
 626 
 627         Locale test2 = (Locale) (istream.readObject());
 628 
 629         if (!test1.equals(test2) || test1.hashCode() != test2.hashCode()) {
 630             errln("Locale failed to deserialize correctly.");
 631         }
 632     }
 633 
 634     /**
 635      * @bug 4118587
 636      */
 637     public void TestSimpleDisplayNames() {
 638         // This test is different from TestDisplayNames because TestDisplayNames checks
 639         // fallback behavior, combination of language and country names to form locale
 640         // names, and other stuff like that.  This test just checks specific language
 641         // and country codes to make sure we have the correct names for them.
 642         String[] languageCodes = {"he", "id", "iu", "ug", "yi", "za"};
 643         String[] languageNames = {"Hebrew", "Indonesian", "Inuktitut", "Uyghur", "Yiddish",
 644             "Zhuang"};
 645 
 646         for (int i = 0; i < languageCodes.length; i++) {
 647             String test = (Locale.of(languageCodes[i])).getDisplayLanguage(Locale.US);
 648             if (!test.equals(languageNames[i])) {
 649                 errln("Got wrong display name for " + languageCodes[i] + ": Expected \""
 650                         + languageNames[i] + "\", got \"" + test + "\".");
 651             }
 652         }
 653     }
 654 
 655     /**
 656      * @bug 4118595
 657      */
 658     public void TestUninstalledISO3Names() {
 659         // This test checks to make sure getISO3Language and getISO3Country work right
 660         // even for locales that are not installed.
 661         String[] iso2Languages = {"am", "ba", "fy", "mr", "rn", "ss", "tw", "zu"};
 662         String[] iso3Languages = {"amh", "bak", "fry", "mar", "run", "ssw", "twi", "zul"};
 663 
 664         for (int i = 0; i < iso2Languages.length; i++) {
 665             String test = (Locale.of(iso2Languages[i])).getISO3Language();
 666             if (!test.equals(iso3Languages[i])) {
 667                 errln("Got wrong ISO3 code for " + iso2Languages[i] + ": Expected \""
 668                         + iso3Languages[i] + "\", got \"" + test + "\".");
 669             }
 670         }
 671 
 672         String[] iso2Countries = {"AF", "BW", "KZ", "MO", "MN", "SB", "TC", "ZW"};
 673         String[] iso3Countries = {"AFG", "BWA", "KAZ", "MAC", "MNG", "SLB", "TCA", "ZWE"};
 674 
 675         for (int i = 0; i < iso2Countries.length; i++) {
 676             String test = (Locale.of("", iso2Countries[i])).getISO3Country();
 677             if (!test.equals(iso3Countries[i])) {
 678                 errln("Got wrong ISO3 code for " + iso2Countries[i] + ": Expected \""
 679                         + iso3Countries[i] + "\", got \"" + test + "\".");
 680             }
 681         }
 682     }
 683 
 684     /**
 685      * @bug 4052404 4778440 8263202
 686      */
 687     public void TestChangedISO639Codes() {
 688         Locale hebrewOld = Locale.of("iw", "IL");
 689         Locale hebrewNew = Locale.of("he", "IL");
 690         Locale yiddishOld = Locale.of("ji", "IL");
 691         Locale yiddishNew = Locale.of("yi", "IL");
 692         Locale indonesianOld = Locale.of("in");
 693         Locale indonesianNew = Locale.of("id");
 694 
 695         if ("true".equalsIgnoreCase(System.getProperty("java.locale.useOldISOCodes"))) {
 696             if (!hebrewNew.getLanguage().equals("iw")) {
 697                 errln("Got back wrong language code for new Hebrew: expected \"iw\", got \""
 698                         + hebrewNew.getLanguage() + "\"");
 699             }
 700             if (!yiddishNew.getLanguage().equals("ji")) {
 701                 errln("Got back wrong language code for new Yiddish: expected \"ji\", got \""
 702                         + yiddishNew.getLanguage() + "\"");
 703             }
 704             if (!indonesianNew.getLanguage().equals("in")) {
 705                 errln("Got back wrong language code for new Indonesian: expected \"in\", got \""
 706                         + indonesianNew.getLanguage() + "\"");
 707             }
 708         } else {
 709             if (!hebrewOld.getLanguage().equals("he")) {
 710                 errln("Got back wrong language code for old Hebrew: expected \"he\", got \""
 711                         + hebrewNew.getLanguage() + "\"");
 712             }
 713             if (!yiddishOld.getLanguage().equals("yi")) {
 714                 errln("Got back wrong language code for old Yiddish: expected \"yi\", got \""
 715                         + yiddishNew.getLanguage() + "\"");
 716             }
 717             if (!indonesianOld.getLanguage().equals("id")) {
 718                 errln("Got back wrong language code for old Indonesian: expected \"id\", got \""
 719                         + indonesianNew.getLanguage() + "\"");
 720             }
 721         }
 722 
 723     }
 724 
 725     /**
 726      * @bug 4092475
 727      * I could not reproduce this bug.  I'm pretty convinced it was fixed with the
 728      * big locale-data reorg of 10/28/97.  The lookup logic for language and country
 729      * display names was also changed at that time in that check-in.    --rtg 3/20/98
 730 
 731      * This test is not designed to work in any other locale but en_US.
 732      * Most of the LocaleElements do not contain display names for other languages,
 733      * so this test fails (bug 4289223) when run under different locales. For example,
 734      * LocaleElements_es as of kestrel does not have a localized name for Japanese, so
 735      * the getDisplayName method asks the default locale for a display name. The Japanese
 736      * localized name for "Japanese" does not equal "Japanese" so this test fails for es
 737      * display names if run under a ja locale. Eventually, he LocaleElements should probably
 738      * be updated to contain more localized language and region display names.
 739      * 1999-11-19 joconner
 740      *
 741      */
 742     public void TestAtypicalLocales() {
 743         Locale[] localesToTest = { Locale.of("de", "CA"),
 744                                    Locale.of("ja", "ZA"),
 745                                    Locale.of("ru", "MX"),
 746                                    Locale.of("en", "FR"),
 747                                    Locale.of("es", "DE"),
 748                                    Locale.of("", "HR"),
 749                                    Locale.of("", "SE"),
 750                                    Locale.of("", "DO"),
 751                                    Locale.of("", "BE") };
 752         String[] englishDisplayNames = { "German (Canada)",
 753                                          "Japanese (South Africa)",
 754                                          "Russian (Mexico)",
 755                                          "English (France)",
 756                                          "Spanish (Germany)",
 757                                          "Croatia",
 758                                          "Sweden",
 759                                          "Dominican Republic",
 760                                          "Belgium" };
 761         String[] frenchDisplayNames = { "allemand (Canada)",
 762                                         "japonais (Afrique du Sud)",
 763                                         "russe (Mexique)",
 764                                          "anglais (France)",
 765                                          "espagnol (Allemagne)",
 766                                         "Croatie",
 767                                         "Su\u00e8de",
 768                                         "R\u00e9publique dominicaine",
 769                                         "Belgique" };
 770         String[] spanishDisplayNames = { "alem\u00E1n (Canad\u00E1)",
 771                                          "japon\u00E9s (Sud\u00E1frica)",
 772                                          "ruso (M\u00e9xico)",
 773                                          "ingl\u00E9s (Francia)",
 774                                          "espa\u00f1ol (Alemania)",
 775                                          "Croacia",
 776                                          "Suecia",
 777                                          "Rep\u00fablica Dominicana",
 778                                          "B\u00E9lgica" };
 779 
 780 
 781         // save the default locale and set to the new default to en_US
 782         Locale defaultLocale = Locale.getDefault();
 783         Locale.setDefault(Locale.US);
 784 
 785         for (int i = 0; i < localesToTest.length; i++) {
 786             String name = localesToTest[i].getDisplayName(Locale.US);
 787             logln(name);
 788             if (!name.equals(englishDisplayNames[i])) {
 789                 errln("Lookup in English failed: expected \"" + englishDisplayNames[i]
 790                         + "\", got \"" + name + "\"");
 791             }
 792         }
 793 
 794         for (int i = 0; i < localesToTest.length; i++) {
 795             String name = localesToTest[i].getDisplayName(Locale.of("es", "ES"));
 796             logln(name);
 797             if (!name.equals(spanishDisplayNames[i])) {
 798                 errln("Lookup in Spanish failed: expected \"" + spanishDisplayNames[i]
 799                         + "\", got \"" + name + "\"");
 800             }
 801         }
 802 
 803         for (int i = 0; i < localesToTest.length; i++) {
 804             String name = localesToTest[i].getDisplayName(Locale.FRANCE);
 805             logln(name);
 806             if (!name.equals(frenchDisplayNames[i])) {
 807                 errln("Lookup in French failed: expected \"" + frenchDisplayNames[i]
 808                         + "\", got \"" + name + "\"");
 809             }
 810         }
 811 
 812         // restore the default locale for other tests
 813         Locale.setDefault(defaultLocale);
 814     }
 815 
 816     /**
 817      * @bug 4126371
 818      */
 819     public void TestNullDefault() {
 820         // why on earth anyone would ever try to do this is beyond me, but we should
 821         // definitely make sure we don't let them
 822         boolean gotException = false;
 823         try {
 824             Locale.setDefault(null);
 825         } catch (NullPointerException e) {
 826             // all other exception types propagate through here back to the test harness
 827             gotException = true;
 828         }
 829         if (Locale.getDefault() == null) {
 830             errln("Locale.getDefault() allowed us to set default to NULL!");
 831         }
 832         if (!gotException) {
 833             errln("Trying to set default locale to NULL didn't throw exception!");
 834         }
 835     }
 836 
 837     /**
 838      * @bug 4135752
 839      * This would be better tested by the LocaleDataTest.  Will move it when I
 840      * get the LocaleDataTest working again.
 841      */
 842     public void TestThaiCurrencyFormat() {
 843         DecimalFormat thaiCurrency = (DecimalFormat) NumberFormat.getCurrencyInstance(
 844                 Locale.of("th", "TH"));
 845         if (!thaiCurrency.getPositivePrefix().equals("\u0e3f")) {
 846             errln("Thai currency prefix wrong: expected \"\u0e3f\", got \""
 847                     + thaiCurrency.getPositivePrefix() + "\"");
 848         }
 849         if (!thaiCurrency.getPositiveSuffix().equals("")) {
 850             errln("Thai currency suffix wrong: expected \"\", got \""
 851                     + thaiCurrency.getPositiveSuffix() + "\"");
 852         }
 853     }
 854 
 855     /**
 856      * @bug 4122371
 857      * Confirm that Euro support works.  This test is pretty rudimentary; all it does
 858      * is check that any locales with the EURO variant format a number using the
 859      * Euro currency symbol.
 860      *
 861      * ASSUME: All locales encode the Euro character "\u20AC".
 862      * If this is changed to use the single-character Euro symbol, this
 863      * test must be updated.
 864      *
 865      * DON'T ASSUME: Any specific countries support the Euro.  Instead,
 866      * iterate through all locales.
 867      */
 868     public void TestEuroSupport() {
 869         final String EURO_VARIANT = "EURO";
 870         final String EURO_CURRENCY = "\u20AC"; // Look for this string in formatted Euro currency
 871 
 872         Locale[] locales = NumberFormat.getAvailableLocales();
 873         for (int i = 0; i < locales.length; ++i) {
 874             Locale loc = locales[i];
 875             if (loc.getVariant().indexOf(EURO_VARIANT) >= 0) {
 876                 NumberFormat nf = NumberFormat.getCurrencyInstance(loc);
 877                 String pos = nf.format(271828.182845);
 878                 String neg = nf.format(-271828.182845);
 879                 if (pos.indexOf(EURO_CURRENCY) >= 0
 880                         && neg.indexOf(EURO_CURRENCY) >= 0) {
 881                     logln("Ok: " + loc.toString()
 882                             + ": " + pos + " / " + neg);
 883                 } else {
 884                     errln("Fail: " + loc.toString()
 885                             + " formats without " + EURO_CURRENCY
 886                             + ": " + pos + " / " + neg
 887                             + "\n*** THIS FAILURE MAY ONLY MEAN THAT LOCALE DATA HAS CHANGED ***");
 888                 }
 889             }
 890         }
 891     }
 892 
 893     /**
 894      * @bug 4139504
 895      * toString() doesn't work with language_VARIANT.
 896      */
 897     public void TestToString() {
 898         Object[] DATA = {
 899             Locale.of("xx", "", ""), "xx",
 900             Locale.of("", "YY", ""), "_YY",
 901             Locale.of("", "", "ZZ"), "",
 902             Locale.of("xx", "YY", ""), "xx_YY",
 903             Locale.of("xx", "", "ZZ"), "xx__ZZ",
 904             Locale.of("", "YY", "ZZ"), "_YY_ZZ",
 905             Locale.of("xx", "YY", "ZZ"), "xx_YY_ZZ",
 906         };
 907         for (int i = 0; i < DATA.length; i += 2) {
 908             Locale loc = (Locale) DATA[i];
 909             String fmt = (String) DATA[i + 1];
 910             if (!loc.toString().equals(fmt)) {
 911                 errln("Fail: Locale.toString(" + fmt + ")=>" + loc);
 912             }
 913         }
 914     }
 915 
 916     /**
 917      * @bug 4105828
 918      * Currency symbol in zh is wrong.  We will test this at the NumberFormat
 919      * end to test the whole pipe.
 920      */
 921     public void Test4105828() {
 922         Locale[] LOC = {Locale.CHINESE, Locale.of("zh", "CN"),
 923             Locale.of("zh", "TW"), Locale.of("zh", "HK")};
 924         for (int i = 0; i < LOC.length; ++i) {
 925             NumberFormat fmt = NumberFormat.getPercentInstance(LOC[i]);
 926             String result = fmt.format(1);
 927             if (!result.equals("100%")) {
 928                 errln("Percent for " + LOC[i] + " should be 100%, got " + result);
 929             }
 930         }
 931     }
 932 
 933     /**
 934      * @bug 4139940
 935      * Couldn't reproduce this bug -- probably was fixed earlier.
 936      *
 937      * ORIGINAL BUG REPORT:
 938      * -- basically, hungarian for monday shouldn't have an \u00f4
 939      * (o circumflex)in it instead it should be an o with 2 inclined
 940      * (right) lines over it..
 941      *
 942      * You may wonder -- why do all this -- why not just add a line to
 943      * LocaleData?  Well, I could see by inspection that the locale file had the
 944      * right character in it, so I wanted to check the rest of the pipeline -- a
 945      * very remote possibility, but I wanted to be sure.  The other possibility
 946      * is that something is wrong with the font mapping subsystem, but we can't
 947      * test that here.
 948      */
 949     public void Test4139940() {
 950         Locale mylocale = Locale.of("hu");
 951         @SuppressWarnings("deprecation")
 952         Date mydate = new Date(98, 3, 13); // A Monday
 953         DateFormat df_full = new SimpleDateFormat("EEEE", mylocale);
 954         String str = df_full.format(mydate);
 955         // Make sure that o circumflex (\u00F4) is NOT there, and
 956         // o double acute (\u0151) IS.
 957         if (str.indexOf('\u0151') < 0 || str.indexOf('\u00F4') >= 0) {
 958             errln("Fail: Monday in Hungarian is wrong");
 959         }
 960     }
 961 
 962     /**
 963      * @bug 4143951
 964      * Russian first day of week should be Monday. Confirmed.
 965      */
 966     public void Test4143951() {
 967         Calendar cal = Calendar.getInstance(Locale.of("ru"));
 968         if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
 969             errln("Fail: First day of week in Russia should be Monday");
 970         }
 971     }
 972 
 973     /**
 974      * @bug 4147315
 975      * java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.
 976      * Should throw an exception for unknown locales
 977      */
 978     public void Test4147315() {
 979         // Try with codes that are the wrong length but happen to match text
 980         // at a valid offset in the mapping table
 981         Locale locale = Locale.of("aaa", "CCC");
 982 
 983         try {
 984             String result = locale.getISO3Country();
 985 
 986             errln("ERROR: getISO3Country() returns: " + result
 987                     + " for locale '" + locale + "' rather than exception");
 988         } catch (MissingResourceException e) {
 989         }
 990     }
 991 
 992     /**
 993      * @bug 4147317 4940539
 994      * java.util.Locale.getISO3Language() works wrong for non ISO-639 codes.
 995      * Should throw an exception for unknown locales, except they have three
 996      * letter language codes.
 997      */
 998     public void Test4147317() {
 999         // Try a three letter language code, and check whether it is
1000         // returned as is.
1001         Locale locale = Locale.of("aaa", "CCC");
1002 
1003         String result = locale.getISO3Language();
1004         if (!result.equals("aaa")) {
1005             errln("ERROR: getISO3Language() returns: " + result
1006                     + " for locale '" + locale + "' rather than returning it as is");
1007         }
1008 
1009         // Try an invalid two letter language code, and check whether it
1010         // throws a MissingResourceException.
1011         locale = Locale.of("zz", "CCC");
1012 
1013         try {
1014             result = locale.getISO3Language();
1015 
1016             errln("ERROR: getISO3Language() returns: " + result
1017                     + " for locale '" + locale + "' rather than exception");
1018         } catch (MissingResourceException e) {
1019         }
1020     }
1021 
1022     /*
1023      * @bug 4147552 4778440 8030696
1024      */
1025     public void Test4147552() {
1026         Locale[] locales = {Locale.of("no", "NO"), Locale.of("no", "NO", "B"),
1027             Locale.of("no", "NO", "NY"), Locale.of("nb", "NO"),
1028             Locale.of("nn", "NO")};
1029         String[] englishDisplayNames = {"Norwegian (Norway)",
1030             "Norwegian (Norway,Bokm\u00e5l)",
1031             "Norwegian (Norway,Nynorsk)",
1032             "Norwegian Bokm\u00e5l (Norway)",
1033             "Norwegian Nynorsk (Norway)"};
1034         String[] norwegianDisplayNames = {"norsk (Norge)",
1035             "norsk (Norge,bokm\u00e5l)", "norsk (Noreg,nynorsk)",
1036             "norsk bokm\u00e5l (Norge)", "norsk nynorsk (Noreg)"};
1037 
1038         for (int i = 0; i < locales.length; i++) {
1039             Locale loc = locales[i];
1040             if (!loc.getDisplayName(Locale.US).equals(englishDisplayNames[i])) {
1041                 errln("English display-name mismatch: expected "
1042                         + englishDisplayNames[i] + ", got " + loc.getDisplayName());
1043             }
1044             if (!loc.getDisplayName(loc).equals(norwegianDisplayNames[i])) {
1045                 errln("Norwegian display-name mismatch: expected "
1046                         + norwegianDisplayNames[i] + ", got "
1047                         + loc.getDisplayName(loc));
1048             }
1049         }
1050     }
1051 
1052     /*
1053      * @bug 8030696
1054      */
1055     public void Test8030696() {
1056         List<Locale> av = Arrays.asList(Locale.getAvailableLocales());
1057         if (!av.contains(Locale.of("nb", "NO"))
1058                 || !av.contains(Locale.of("nn", "NO"))) {
1059             errln("\"nb-NO\" and/or \"nn-NO\" locale(s) not returned from getAvailableLocales().");
1060         }
1061     }
1062 
1063     static String escapeUnicode(String s) {
1064         StringBuffer buf = new StringBuffer();
1065         for (int i = 0; i < s.length(); ++i) {
1066             char c = s.charAt(i);
1067             if (c >= 0x20 && c <= 0x7F) {
1068                 buf.append(c);
1069             } else {
1070                 buf.append("\\u");
1071                 String h = "000" + Integer.toHexString(c);
1072                 if (h.length() > 4) {
1073                     h = h.substring(h.length() - 4);
1074                 }
1075                 buf.append(h);
1076             }
1077         }
1078         return buf.toString();
1079     }
1080 }