Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/LocaleTest.java
38813 views
/*1* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/**23* @test24* @bug 4052404 4052440 4084688 4092475 4101316 4105828 4107014 4107953 411061325* 4118587 4118595 4122371 4126371 4126880 4135316 4135752 4139504 4139940 414395126* 4147315 4147317 4147552 4335196 4778440 4940539 5010672 6475525 6544471 662754927* 6786276 7066203 7085757 825508628* @summary test Locales29*/30/*31*32*33* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved34* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved35*36* Portions copyright (c) 2007 Sun Microsystems, Inc.37* All Rights Reserved.38*39* The original version of this source code and documentation40* is copyrighted and owned by Taligent, Inc., a wholly-owned41* subsidiary of IBM. These materials are provided under terms42* of a License Agreement between Taligent and Sun. This technology43* is protected by multiple US and International patents.44*45* This notice and attribution to Taligent may not be removed.46* Taligent is a registered trademark of Taligent, Inc.47*48* Permission to use, copy, modify, and distribute this software49* and its documentation for NON-COMMERCIAL purposes and without50* fee is hereby granted provided that this copyright notice51* appears in all copies. Please refer to the file "copyright.html"52* for further important copyright and licensing information.53*54* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF55* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED56* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A57* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR58* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR59* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.60*61*/6263import java.text.*;64import java.util.Locale;65import java.util.MissingResourceException;66import java.util.Date;67import java.util.Calendar;68import java.io.*;6970public class LocaleTest extends LocaleTestFmwk {71public LocaleTest() {72}7374private int ENGLISH = 0;75private int FRENCH = 1;76private int CROATIAN = 2;77private int GREEK = 3;78private int NORWEGIAN = 4;79private int ITALIAN = 5;80private int DUMMY = 6;81private int MAX_LOCALES = 6;8283private int LANG = 0;84private int CTRY = 1;85private int VAR = 2;86private int NAME = 3;87private int LANG3 = 4;88private int CTRY3 = 5;89private int LCID = 6;90private int DLANG_EN = 7;91private int DCTRY_EN = 8;92private int DVAR_EN = 9;93private int DNAME_EN = 10;94private int DLANG_FR = 11;95private int DCTRY_FR = 12;96private int DVAR_FR = 13;97private int DNAME_FR = 14;98private int DLANG_HR = 15;99private int DCTRY_HR = 16;100private int DVAR_HR = 17;101private int DNAME_HR = 18;102private int DLANG_EL = 19;103private int DCTRY_EL = 20;104private int DVAR_EL = 21;105private int DNAME_EL = 22;106private int DLANG_ROOT = 23;107private int DCTRY_ROOT = 24;108private int DVAR_ROOT = 25;109private int DNAME_ROOT = 26;110111private String[][] dataTable = {112// language code113{ "en", "fr", "hr", "el", "no", "it", "xx" },114// country code115{ "US", "FR", "HR", "GR", "NO", "", "YY" },116// variant code117{ "", "", "", "", "NY", "", "" },118// full name119{ "en_US", "fr_FR", "hr_HR", "el_GR", "no_NO_NY", "it", "xx_YY" },120// ISO-3 language121{ "eng", "fra", "hrv", "ell", "nor", "ita", "" },122// ISO-3 country123{ "USA", "FRA", "HRV", "GRC", "NOR", "", "" },124// LCID (not currently public)125{ "0409", "040c", "041a", "0408", "0814", "", "" },126127// display language (English)128{ "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },129// display country (English)130{ "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },131// display variant (English)132{ "", "", "", "", "Nynorsk", "", ""},133// display name (English)134// Updated no_NO_NY English display name for new pattern-based algorithm135// (part of Euro support).136{ "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },137138// display langage (French)139{ "anglais", "fran\u00e7ais", "croate", "grec", "norv\u00e9gien", "italien", "xx" },140// display country (French)141{ "Etats-Unis", "France", "Croatie", "Gr\u00e8ce", "Norv\u00e8ge", "", "YY" },142// display variant (French)143{ "", "", "", "", "", "", "" },144// display name (French)145{ "anglais (Etats-Unis)", "fran\u00e7ais (France)", "croate (Croatie)", "grec (Gr\u00e8ce)", "norv\u00e9gien (Norv\u00e8ge,Nynorsk)", "italien", "xx (YY)" },146147// display langage (Croatian)148{ "", "", "hrvatski", "", "", "", "xx" },149// display country (Croatian)150{ "", "", "Hrvatska", "", "", "", "YY" },151// display variant (Croatian)152{ "", "", "", "", "", "", ""},153// display name (Croatian)154{ "", "", "hrvatski (Hrvatska)", "", "", "", "xx (YY)" },155156// display langage (Greek)157{ "\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" },158// display country (Greek)159{ "\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" },160// display variant (Greek)161{ "", "", "", "", "", "", "" },162// display name (Greek)163{ "\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)" },164165// display langage (<root>)166{ "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },167// display country (<root>)168{ "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },169// display variant (<root>)170{ "", "", "", "", "Nynorsk", "", ""},171// display name (<root>)172{ "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },173};174175public static void main(String[] args) throws Exception {176new LocaleTest().run(args);177}178179public void TestBasicGetters() {180for (int i = 0; i <= MAX_LOCALES; i++) {181Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);182logln("Testing " + testLocale + "...");183184if (!testLocale.getLanguage().equals(dataTable[LANG][i]))185errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "186+ dataTable[LANG][i]);187if (!testLocale.getCountry().equals(dataTable[CTRY][i]))188errln(" Country code mismatch: " + testLocale.getCountry() + " versus "189+ dataTable[CTRY][i]);190if (!testLocale.getVariant().equals(dataTable[VAR][i]))191errln(" Variant code mismatch: " + testLocale.getVariant() + " versus "192+ dataTable[VAR][i]);193if (!testLocale.toString().equals(dataTable[NAME][i]))194errln(" Locale name mismatch: " + testLocale.toString() + " versus "195+ dataTable[NAME][i]);196}197198logln("Same thing without variant codes...");199for (int i = 0; i <= MAX_LOCALES; i++) {200Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i]);201logln("Testing " + testLocale + "...");202203if (!testLocale.getLanguage().equals(dataTable[LANG][i]))204errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "205+ dataTable[LANG][i]);206if (!testLocale.getCountry().equals(dataTable[CTRY][i]))207errln(" Country code mismatch: " + testLocale.getCountry() + " versus "208+ dataTable[CTRY][i]);209if (!testLocale.getVariant().equals(""))210errln(" Variant code mismatch: " + testLocale.getVariant() + " versus \"\"");211}212}213214public void TestSimpleResourceInfo() {215for (int i = 0; i <= MAX_LOCALES; i++) {216if (dataTable[LANG][i].equals("xx"))217continue;218219Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);220logln("Testing " + testLocale + "...");221222if (!testLocale.getISO3Language().equals(dataTable[LANG3][i]))223errln(" ISO-3 language code mismatch: " + testLocale.getISO3Language()224+ " versus " + dataTable[LANG3][i]);225if (!testLocale.getISO3Country().equals(dataTable[CTRY3][i]))226errln(" ISO-3 country code mismatch: " + testLocale.getISO3Country()227+ " versus " + dataTable[CTRY3][i]);228/*229// getLCID() is currently private230if (!String.valueOf(testLocale.getLCID()).equals(dataTable[LCID][i]))231errln(" LCID mismatch: " + testLocale.getLCID() + " versus "232+ dataTable[LCID][i]);233*/234}235}236237/*238* @bug 4101316239* @bug 4084688 (This bug appears to be a duplicate of something, because it was fixed240* between 1.1.5 and 1.1.6, but I included a new test for it anyway)241* @bug 4052440 Stop falling back to the default locale.242*/243public void TestDisplayNames() {244Locale saveDefault = Locale.getDefault();245Locale english = new Locale("en", "US");246Locale french = new Locale("fr", "FR");247Locale croatian = new Locale("hr", "HR");248Locale greek = new Locale("el", "GR");249250Locale.setDefault(english);251logln("With default = en_US...");252logln(" In default locale...");253doTestDisplayNames(null, DLANG_EN, false);254logln(" In locale = en_US...");255doTestDisplayNames(english, DLANG_EN, false);256logln(" In locale = fr_FR...");257doTestDisplayNames(french, DLANG_FR, false);258logln(" In locale = hr_HR...");259doTestDisplayNames(croatian, DLANG_HR, false);260logln(" In locale = el_GR...");261doTestDisplayNames(greek, DLANG_EL, false);262263Locale.setDefault(french);264logln("With default = fr_FR...");265logln(" In default locale...");266doTestDisplayNames(null, DLANG_FR, true);267logln(" In locale = en_US...");268doTestDisplayNames(english, DLANG_EN, true);269logln(" In locale = fr_FR...");270doTestDisplayNames(french, DLANG_FR, true);271logln(" In locale = hr_HR...");272doTestDisplayNames(croatian, DLANG_HR, true);273logln(" In locale = el_GR...");274doTestDisplayNames(greek, DLANG_EL, true);275276Locale.setDefault(saveDefault);277}278279private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {280if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))281errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());282else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))283errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());284285for (int i = 0; i <= MAX_LOCALES; i++) {286Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);287logln(" Testing " + testLocale + "...");288289String testLang;290String testCtry;291String testVar;292String testName;293294if (inLocale == null) {295testLang = testLocale.getDisplayLanguage();296testCtry = testLocale.getDisplayCountry();297testVar = testLocale.getDisplayVariant();298testName = testLocale.getDisplayName();299}300else {301testLang = testLocale.getDisplayLanguage(inLocale);302testCtry = testLocale.getDisplayCountry(inLocale);303testVar = testLocale.getDisplayVariant(inLocale);304testName = testLocale.getDisplayName(inLocale);305}306307String expectedLang;308String expectedCtry;309String expectedVar;310String expectedName;311312expectedLang = dataTable[compareIndex][i];313if (expectedLang.equals("") && defaultIsFrench)314expectedLang = dataTable[DLANG_EN][i];315if (expectedLang.equals(""))316expectedLang = dataTable[DLANG_ROOT][i];317318expectedCtry = dataTable[compareIndex + 1][i];319if (expectedCtry.equals("") && defaultIsFrench)320expectedCtry = dataTable[DCTRY_EN][i];321if (expectedCtry.equals(""))322expectedCtry = dataTable[DCTRY_ROOT][i];323324expectedVar = dataTable[compareIndex + 2][i];325if (expectedVar.equals("") && defaultIsFrench)326expectedVar = dataTable[DVAR_EN][i];327if (expectedVar.equals(""))328expectedVar = dataTable[DVAR_ROOT][i];329330expectedName = dataTable[compareIndex + 3][i];331if (expectedName.equals("") && defaultIsFrench)332expectedName = dataTable[DNAME_EN][i];333if (expectedName.equals(""))334expectedName = dataTable[DNAME_ROOT][i];335336if (!testLang.equals(expectedLang))337errln("Display language mismatch: " + testLang + " versus " + expectedLang);338if (!testCtry.equals(expectedCtry))339errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);340if (!testVar.equals(expectedVar))341errln("Display variant mismatch: " + testVar + " versus " + expectedVar);342if (!testName.equals(expectedName))343errln("Display name mismatch: " + testName + " versus " + expectedName);344}345}346347public void TestSimpleObjectStuff() {348Locale test1 = new Locale("aa", "AA");349Locale test2 = new Locale("aa", "AA");350Locale test3 = (Locale)test1.clone();351Locale test4 = new Locale("zz", "ZZ");352353if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3)354errln("Some of the test variables point to the same locale!");355356if (test3 == null)357errln("clone() failed to produce a valid object!");358359if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3))360errln("clone() or equals() failed: objects that should compare equal don't");361362if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4))363errln("equals() failed: objects that shouldn't compare equal do");364365int hash1 = test1.hashCode();366int hash2 = test2.hashCode();367int hash3 = test3.hashCode();368369if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3)370errln("hashCode() failed: objects that should have the same hash code don't");371}372373/**374* @bug 4011756 4011380375*/376public void TestISO3Fallback() {377Locale test = new Locale("xx", "YY", "");378boolean gotException = false;379String result = "";380381try {382result = test.getISO3Language();383}384catch (MissingResourceException e) {385gotException = true;386}387if (!gotException)388errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");389390gotException = false;391try {392result = test.getISO3Country();393}394catch (MissingResourceException e) {395gotException = true;396}397if (!gotException)398errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");399}400401/**402* @bug 4106155 4118587 7066203 7085757403*/404public void TestGetLangsAndCountries() {405// It didn't seem right to just do an exhaustive test of everything here, so I check406// for the following things:407// 1) Does each list have the right total number of entries?408// 2) Does each list contain certain language and country codes we think are important409// (the G7 countries, plus a couple others)?410// 3) Does each list have every entry formatted correctly? (i.e., two characters,411// all lower case for the language codes, all upper case for the country codes)412// 4) Is each list in sorted order?413String[] test = Locale.getISOLanguages();414String[] spotCheck1 = { "en", "es", "fr", "de", "it", "ja", "ko", "zh", "th",415"he", "id", "iu", "ug", "yi", "za" };416417if (test.length != 188)418errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length);419else {420for (int i = 0; i < spotCheck1.length; i++) {421int j;422for (j = 0; j < test.length; j++)423if (test[j].equals(spotCheck1[i]))424break;425if (j == test.length || !test[j].equals(spotCheck1[i]))426errln("Couldn't find " + spotCheck1[i] + " in language list.");427}428}429for (int i = 0; i < test.length; i++) {430if (!test[i].equals(test[i].toLowerCase()))431errln(test[i] + " is not all lower case.");432if (test[i].length() != 2)433errln(test[i] + " is not two characters long.");434if (i > 0 && test[i].compareTo(test[i - 1]) <= 0)435errln(test[i] + " appears in an out-of-order position in the list.");436}437438test = Locale.getISOCountries();439String[] spotCheck2 = { "US", "CA", "GB", "FR", "DE", "IT", "JP", "KR", "CN", "TW", "TH" };440441442if (test.length != 250)443errln("Expected getISOCountries to return 250 countries; it returned " + test.length);444else {445for (int i = 0; i < spotCheck2.length; i++) {446int j;447for (j = 0; j < test.length; j++)448if (test[j].equals(spotCheck2[i]))449break;450if (j == test.length || !test[j].equals(spotCheck2[i]))451errln("Couldn't find " + spotCheck2[i] + " in country list.");452}453}454for (int i = 0; i < test.length; i++) {455if (!test[i].equals(test[i].toUpperCase()))456errln(test[i] + " is not all upper case.");457if (test[i].length() != 2)458errln(test[i] + " is not two characters long.");459if (i > 0 && test[i].compareTo(test[i - 1]) <= 0)460errln(test[i] + " appears in an out-of-order position in the list.");461}462}463464/**465* @bug 4126880466*/467void Test4126880() {468String[] test;469470test = Locale.getISOCountries();471test[0] = "SUCKER!!!";472test = Locale.getISOCountries();473if (test[0].equals("SUCKER!!!"))474errln("Changed internal country code list!");475476test = Locale.getISOLanguages();477test[0] = "HAHAHAHA!!!";478test = Locale.getISOLanguages();479if (test[0].equals("HAHAHAHA!!!")) // Fixed typo480errln("Changes internal language code list!");481}482483/**484* @bug 4107014485*/486public void TestGetAvailableLocales() {487Locale[] locales = Locale.getAvailableLocales();488if (locales == null || locales.length == 0)489errln("Locale.getAvailableLocales() returned no installed locales!");490else {491logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales.");492for (int i = 0; i < locales.length; i++)493logln(locales[i].toString());494}495}496497/**498* @bug 4135316499*/500public void TestBug4135316() {501Locale[] locales1 = Locale.getAvailableLocales();502Locale[] locales2 = Locale.getAvailableLocales();503if (locales1 == locales2)504errln("Locale.getAvailableLocales() doesn't clone its internal storage!");505}506507/**508* @bug 4107953509*/510/*511test commented out pending API-change approval512public void TestGetLanguagesForCountry() {513String[] languages = Locale.getLanguagesForCountry("US");514515if (!searchStringArrayFor("en", languages))516errln("Didn't get en as a language for US");517518languages = Locale.getLanguagesForCountry("FR");519if (!searchStringArrayFor("fr", languages))520errln("Didn't get fr as a language for FR");521522languages = Locale.getLanguagesForCountry("CH");523if (!searchStringArrayFor("fr", languages))524errln("Didn't get fr as a language for CH");525if (!searchStringArrayFor("it", languages))526errln("Didn't get it as a language for CH");527if (!searchStringArrayFor("de", languages))528errln("Didn't get de as a language for CH");529530languages = Locale.getLanguagesForCountry("JP");531if (!searchStringArrayFor("ja", languages))532errln("Didn't get ja as a language for JP");533}534*/535536private boolean searchStringArrayFor(String s, String[] array) {537for (int i = 0; i < array.length; i++)538if (s.equals(array[i]))539return true;540return false;541}542/**543* @bug 4110613544*/545public void TestSerialization() throws ClassNotFoundException, OptionalDataException,546IOException, StreamCorruptedException547{548ObjectOutputStream ostream;549ByteArrayOutputStream obstream;550byte[] bytes = null;551552obstream = new ByteArrayOutputStream();553ostream = new ObjectOutputStream(obstream);554555Locale test1 = new Locale("zh", "TW", "");556int dummy = test1.hashCode(); // fill in the cached hash-code value557ostream.writeObject(test1);558559bytes = obstream.toByteArray();560561ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));562563Locale test2 = (Locale)(istream.readObject());564565if (!test1.equals(test2) || test1.hashCode() != test2.hashCode())566errln("Locale failed to deserialize correctly.");567}568569/**570* @bug 4118587571*/572public void TestSimpleDisplayNames() {573// This test is different from TestDisplayNames because TestDisplayNames checks574// fallback behavior, combination of language and country names to form locale575// names, and other stuff like that. This test just checks specific language576// and country codes to make sure we have the correct names for them.577String[] languageCodes = { "he", "id", "iu", "ug", "yi", "za" };578String[] languageNames = {"Hebrew", "Indonesian", "Inuktitut", "Uyghur", "Yiddish",579"Zhuang" };580581for (int i = 0; i < languageCodes.length; i++) {582String test = (new Locale(languageCodes[i], "", "")).getDisplayLanguage(Locale.US);583if (!test.equals(languageNames[i]))584errln("Got wrong display name for " + languageCodes[i] + ": Expected \"" +585languageNames[i] + "\", got \"" + test + "\".");586}587}588589/**590* @bug 4118595591*/592public void TestUninstalledISO3Names() {593// This test checks to make sure getISO3Language and getISO3Country work right594// even for locales that are not installed.595String[] iso2Languages = { "am", "ba", "fy", "mr", "rn", "ss", "tw", "zu" };596String[] iso3Languages = { "amh", "bak", "fry", "mar", "run", "ssw", "twi", "zul" };597598for (int i = 0; i < iso2Languages.length; i++) {599String test = (new Locale(iso2Languages[i], "", "")).getISO3Language();600if (!test.equals(iso3Languages[i]))601errln("Got wrong ISO3 code for " + iso2Languages[i] + ": Expected \"" +602iso3Languages[i] + "\", got \"" + test + "\".");603}604605String[] iso2Countries = { "AF", "BW", "KZ", "MO", "MN", "SB", "TC", "ZW" };606String[] iso3Countries = { "AFG", "BWA", "KAZ", "MAC", "MNG", "SLB", "TCA", "ZWE" };607608for (int i = 0; i < iso2Countries.length; i++) {609String test = (new Locale("", iso2Countries[i], "")).getISO3Country();610if (!test.equals(iso3Countries[i]))611errln("Got wrong ISO3 code for " + iso2Countries[i] + ": Expected \"" +612iso3Countries[i] + "\", got \"" + test + "\".");613}614}615616/**617* @bug 4052404 4778440618*/619public void TestChangedISO639Codes() {620Locale hebrewOld = new Locale("iw", "IL", "");621Locale hebrewNew = new Locale("he", "IL", "");622Locale yiddishOld = new Locale("ji", "IL", "");623Locale yiddishNew = new Locale("yi", "IL", "");624Locale indonesianOld = new Locale("in", "", "");625Locale indonesianNew = new Locale("id", "", "");626627if (!hebrewNew.getLanguage().equals("iw"))628errln("Got back wrong language code for Hebrew: expected \"iw\", got \"" +629hebrewNew.getLanguage() + "\"");630if (!yiddishNew.getLanguage().equals("ji"))631errln("Got back wrong language code for Yiddish: expected \"ji\", got \"" +632yiddishNew.getLanguage() + "\"");633if (!indonesianNew.getLanguage().equals("in"))634errln("Got back wrong language code for Indonesian: expected \"in\", got \"" +635indonesianNew.getLanguage() + "\"");636}637638/**639* @bug 4092475640* I could not reproduce this bug. I'm pretty convinced it was fixed with the641* big locale-data reorg of 10/28/97. The lookup logic for language and country642* display names was also changed at that time in that check-in. --rtg 3/20/98643644* This test is not designed to work in any other locale but en_US.645* Most of the LocaleElements do not contain display names for other languages,646* so this test fails (bug 4289223) when run under different locales. For example,647* LocaleElements_es as of kestrel does not have a localized name for Japanese, so648* the getDisplayName method asks the default locale for a display name. The Japanese649* localized name for "Japanese" does not equal "Japanese" so this test fails for es650* display names if run under a ja locale. Eventually, he LocaleElements should probably651* be updated to contain more localized language and region display names.652* 1999-11-19 joconner653*654*/655public void TestAtypicalLocales() {656Locale[] localesToTest = { new Locale("de", "CA"),657new Locale("ja", "ZA"),658new Locale("ru", "MX"),659new Locale("en", "FR"),660new Locale("es", "DE"),661new Locale("", "HR"),662new Locale("", "SE"),663new Locale("", "DO"),664new Locale("", "BE") };665String[] englishDisplayNames = { "German (Canada)",666"Japanese (South Africa)",667"Russian (Mexico)",668"English (France)",669"Spanish (Germany)",670"Croatia",671"Sweden",672"Dominican Republic",673"Belgium" };674String[] frenchDisplayNames = { "allemand (Canada)",675"japonais (Afrique du Sud)",676"russe (Mexique)",677"anglais (France)",678"espagnol (Allemagne)",679"Croatie",680"Su\u00e8de",681"R\u00e9publique Dominicaine",682"Belgique" };683String[] spanishDisplayNames = { "alem\u00E1n (Canad\u00E1)",684"japon\u00E9s (Sud\u00E1frica)",685"ruso (M\u00e9xico)",686"ingl\u00E9s (Francia)",687"espa\u00f1ol (Alemania)",688"Croacia",689"Suecia",690"Rep\u00fablica Dominicana",691"B\u00E9lgica" };692693694// save the default locale and set to the new default to en_US695Locale defaultLocale = Locale.getDefault();696Locale.setDefault(Locale.US);697698for (int i = 0; i < localesToTest.length; i++) {699String name = localesToTest[i].getDisplayName(Locale.US);700logln(name);701if (!name.equals(englishDisplayNames[i]))702errln("Lookup in English failed: expected \"" + englishDisplayNames[i]703+ "\", got \"" + name + "\"");704}705706for (int i = 0; i < localesToTest.length; i++) {707String name = localesToTest[i].getDisplayName(new Locale("es", "ES"));708logln(name);709if (!name.equals(spanishDisplayNames[i]))710errln("Lookup in Spanish failed: expected \"" + spanishDisplayNames[i]711+ "\", got \"" + name + "\"");712}713714for (int i = 0; i < localesToTest.length; i++) {715String name = localesToTest[i].getDisplayName(Locale.FRANCE);716logln(name);717if (!name.equals(frenchDisplayNames[i]))718errln("Lookup in French failed: expected \"" + frenchDisplayNames[i]719+ "\", got \"" + name + "\"");720}721722// restore the default locale for other tests723Locale.setDefault(defaultLocale);724}725726/**727* @bug 4126371728*/729public void TestNullDefault() {730// why on earth anyone would ever try to do this is beyond me, but we should731// definitely make sure we don't let them732boolean gotException = false;733try {734Locale.setDefault(null);735}736catch (NullPointerException e) {737// all other exception types propagate through here back to the test harness738gotException = true;739}740if (Locale.getDefault() == null)741errln("Locale.getDefault() allowed us to set default to NULL!");742if (!gotException)743errln("Trying to set default locale to NULL didn't throw exception!");744}745746/**747* @bug 4135752748* This would be better tested by the LocaleDataTest. Will move it when I749* get the LocaleDataTest working again.750*/751public void TestThaiCurrencyFormat() {752DecimalFormat thaiCurrency = (DecimalFormat)NumberFormat.getCurrencyInstance(753new Locale("th", "TH"));754if (!thaiCurrency.getPositivePrefix().equals("\u0e3f"))755errln("Thai currency prefix wrong: expected \"\u0e3f\", got \"" +756thaiCurrency.getPositivePrefix() + "\"");757if (!thaiCurrency.getPositiveSuffix().equals(""))758errln("Thai currency suffix wrong: expected \"\", got \"" +759thaiCurrency.getPositiveSuffix() + "\"");760}761762/**763* @bug 4122371764* Confirm that Euro support works. This test is pretty rudimentary; all it does765* is check that any locales with the EURO variant format a number using the766* Euro currency symbol.767*768* ASSUME: All locales encode the Euro character "\u20AC".769* If this is changed to use the single-character Euro symbol, this770* test must be updated.771*772* DON'T ASSUME: Any specific countries support the Euro. Instead,773* iterate through all locales.774*/775public void TestEuroSupport() {776final String EURO_VARIANT = "EURO";777final String EURO_CURRENCY = "\u20AC"; // Look for this string in formatted Euro currency778779Locale[] locales = NumberFormat.getAvailableLocales();780for (int i=0; i<locales.length; ++i) {781Locale loc = locales[i];782if (loc.getVariant().indexOf(EURO_VARIANT) >= 0) {783NumberFormat nf = NumberFormat.getCurrencyInstance(loc);784String pos = nf.format(271828.182845);785String neg = nf.format(-271828.182845);786if (pos.indexOf(EURO_CURRENCY) >= 0 &&787neg.indexOf(EURO_CURRENCY) >= 0) {788logln("Ok: " + loc.toString() +789": " + pos + " / " + neg);790}791else {792errln("Fail: " + loc.toString() +793" formats without " + EURO_CURRENCY +794": " + pos + " / " + neg +795"\n*** THIS FAILURE MAY ONLY MEAN THAT LOCALE DATA HAS CHANGED ***");796}797}798}799}800801/**802* @bug 4139504803* toString() doesn't work with language_VARIANT.804*/805public void TestToString() {806Object[] DATA = {807new Locale("xx", "", ""), "xx",808new Locale("", "YY", ""), "_YY",809new Locale("", "", "ZZ"), "",810new Locale("xx", "YY", ""), "xx_YY",811new Locale("xx", "", "ZZ"), "xx__ZZ",812new Locale("", "YY", "ZZ"), "_YY_ZZ",813new Locale("xx", "YY", "ZZ"), "xx_YY_ZZ",814};815for (int i=0; i<DATA.length; i+=2) {816Locale loc = (Locale)DATA[i];817String fmt = (String)DATA[i+1];818if (!loc.toString().equals(fmt)) {819errln("Fail: Locale.toString(" + fmt + ")=>" + loc);820}821}822}823824/**825* @bug 4105828826* Currency symbol in zh is wrong. We will test this at the NumberFormat827* end to test the whole pipe.828*/829public void Test4105828() {830Locale[] LOC = { Locale.CHINESE, new Locale("zh", "CN", ""),831new Locale("zh", "TW", ""), new Locale("zh", "HK", "") };832for (int i=0; i<LOC.length; ++i) {833NumberFormat fmt = NumberFormat.getPercentInstance(LOC[i]);834String result = fmt.format(1);835if (!result.equals("100%")) {836errln("Percent for " + LOC[i] + " should be 100%, got " + result);837}838}839}840841/**842* @bug 4139940843* Couldn't reproduce this bug -- probably was fixed earlier.844*845* ORIGINAL BUG REPORT:846* -- basically, hungarian for monday shouldn't have an \u00f4847* (o circumflex)in it instead it should be an o with 2 inclined848* (right) lines over it..849*850* You may wonder -- why do all this -- why not just add a line to851* LocaleData? Well, I could see by inspection that the locale file had the852* right character in it, so I wanted to check the rest of the pipeline -- a853* very remote possibility, but I wanted to be sure. The other possibility854* is that something is wrong with the font mapping subsystem, but we can't855* test that here.856*/857public void Test4139940() {858Locale mylocale=new Locale("hu", "", "");859Date mydate = new Date(98,3,13); // A Monday860DateFormat df_full = new SimpleDateFormat("EEEE", mylocale);861String str = df_full.format(mydate);862// Make sure that o circumflex (\u00F4) is NOT there, and863// o double acute (\u0151) IS.864if (str.indexOf('\u0151') < 0 || str.indexOf('\u00F4') >= 0)865errln("Fail: Monday in Hungarian is wrong");866}867868/**869* @bug 4143951870* Russian first day of week should be Monday. Confirmed.871*/872public void Test4143951() {873Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));874if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {875errln("Fail: First day of week in Russia should be Monday");876}877}878879/**880* @bug 4147315881* java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.882* Should throw an exception for unknown locales883*/884public void Test4147315() {885// Try with codes that are the wrong length but happen to match text886// at a valid offset in the mapping table887Locale locale = new Locale("aaa", "CCC");888889try {890String result = locale.getISO3Country();891892errln("ERROR: getISO3Country() returns: " + result +893" for locale '" + locale + "' rather than exception" );894} catch(MissingResourceException e) { }895}896897/**898* @bug 4147317 4940539899* java.util.Locale.getISO3Language() works wrong for non ISO-639 codes.900* Should throw an exception for unknown locales, except they have three901* letter language codes.902*/903public void Test4147317() {904// Try a three letter language code, and check whether it is905// returned as is.906Locale locale = new Locale("aaa", "CCC");907908String result = locale.getISO3Language();909if (!result.equals("aaa")) {910errln("ERROR: getISO3Language() returns: " + result +911" for locale '" + locale + "' rather than returning it as is" );912}913914// Try an invalid two letter language code, and check whether it915// throws a MissingResourceException.916locale = new Locale("zz", "CCC");917918try {919result = locale.getISO3Language();920921errln("ERROR: getISO3Language() returns: " + result +922" for locale '" + locale + "' rather than exception" );923} catch(MissingResourceException e) { }924}925926/*927* @bug 4147552 4778440928*/929public void Test4147552() {930Locale[] locales = { new Locale("no", "NO"), new Locale("no", "NO", "B"),931new Locale("no", "NO", "NY") };932String[] englishDisplayNames = { "Norwegian (Norway)",933"Norwegian (Norway,Bokm\u00e5l)",934"Norwegian (Norway,Nynorsk)" };935String[] norwegianDisplayNames = { "norsk (Norge)",936"norsk (Norge,bokm\u00e5l)", "norsk (Norge,nynorsk)" };937938for (int i = 0; i < locales.length; i++) {939Locale loc = locales[i];940if (!loc.getDisplayName(Locale.US).equals(englishDisplayNames[i]))941errln("English display-name mismatch: expected " +942englishDisplayNames[i] + ", got " + loc.getDisplayName());943if (!loc.getDisplayName(loc).equals(norwegianDisplayNames[i]))944errln("Norwegian display-name mismatch: expected " +945norwegianDisplayNames[i] + ", got " +946loc.getDisplayName(loc));947}948}949950static String escapeUnicode(String s) {951StringBuffer buf = new StringBuffer();952for (int i=0; i<s.length(); ++i) {953char c = s.charAt(i);954if (c >= 0x20 && c <= 0x7F) buf.append(c);955else {956buf.append("\\u");957String h = "000" + Integer.toHexString(c);958if (h.length() > 4) h = h.substring(h.length() - 4);959buf.append(h);960}961}962return buf.toString();963}964}965966967