Path: blob/master/test/jdk/java/util/Locale/LocaleTest.java
41149 views
/*1* Copyright (c) 2007, 2021, 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 8008577 8030696 8170840 8255086 826320228* @summary test Locales29* @library /java/text/testlib30* @modules jdk.localedata31* @run main/othervm -Djava.locale.providers=COMPAT,SPI LocaleTest32* @run main/othervm -Djava.locale.providers=COMPAT,SPI -Djava.locale.useOldISOCodes=true LocaleTest33*/34/*35*36*37* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved38* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved39*40* Portions copyright (c) 2007 Sun Microsystems, Inc.41* All Rights Reserved.42*43* The original version of this source code and documentation44* is copyrighted and owned by Taligent, Inc., a wholly-owned45* subsidiary of IBM. These materials are provided under terms46* of a License Agreement between Taligent and Sun. This technology47* is protected by multiple US and International patents.48*49* This notice and attribution to Taligent may not be removed.50* Taligent is a registered trademark of Taligent, Inc.51*52* Permission to use, copy, modify, and distribute this software53* and its documentation for NON-COMMERCIAL purposes and without54* fee is hereby granted provided that this copyright notice55* appears in all copies. Please refer to the file "copyright.html"56* for further important copyright and licensing information.57*58* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF59* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED60* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A61* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR62* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR63* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.64*65*/6667import java.io.ByteArrayInputStream;68import java.io.ByteArrayOutputStream;69import java.io.IOException;70import java.io.ObjectInputStream;71import java.io.ObjectOutputStream;72import java.io.OptionalDataException;73import java.io.StreamCorruptedException;74import java.text.DateFormat;75import java.text.DecimalFormat;76import java.text.NumberFormat;77import java.text.SimpleDateFormat;78import java.util.Arrays;79import java.util.Calendar;80import java.util.Date;81import java.util.List;82import java.util.Locale;83import java.util.MissingResourceException;8485public class LocaleTest extends IntlTest {86public LocaleTest() {87}8889private int ENGLISH = 0;90private int FRENCH = 1;91private int CROATIAN = 2;92private int GREEK = 3;93private int NORWEGIAN = 4;94private int ITALIAN = 5;95private int DUMMY = 6;96private int MAX_LOCALES = 6;9798private int LANG = 0;99private int CTRY = 1;100private int VAR = 2;101private int NAME = 3;102private int LANG3 = 4;103private int CTRY3 = 5;104private int LCID = 6;105private int DLANG_EN = 7;106private int DCTRY_EN = 8;107private int DVAR_EN = 9;108private int DNAME_EN = 10;109private int DLANG_FR = 11;110private int DCTRY_FR = 12;111private int DVAR_FR = 13;112private int DNAME_FR = 14;113private int DLANG_HR = 15;114private int DCTRY_HR = 16;115private int DVAR_HR = 17;116private int DNAME_HR = 18;117private int DLANG_EL = 19;118private int DCTRY_EL = 20;119private int DVAR_EL = 21;120private int DNAME_EL = 22;121private int DLANG_ROOT = 23;122private int DCTRY_ROOT = 24;123private int DVAR_ROOT = 25;124private int DNAME_ROOT = 26;125126private String[][] dataTable = {127// language code128{ "en", "fr", "hr", "el", "no", "it", "xx" },129// country code130{ "US", "FR", "HR", "GR", "NO", "", "YY" },131// variant code132{ "", "", "", "", "NY", "", "" },133// full name134{ "en_US", "fr_FR", "hr_HR", "el_GR", "no_NO_NY", "it", "xx_YY" },135// ISO-3 language136{ "eng", "fra", "hrv", "ell", "nor", "ita", "" },137// ISO-3 country138{ "USA", "FRA", "HRV", "GRC", "NOR", "", "" },139// LCID (not currently public)140{ "0409", "040c", "041a", "0408", "0814", "", "" },141142// display language (English)143{ "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },144// display country (English)145{ "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },146// display variant (English)147{ "", "", "", "", "Nynorsk", "", ""},148// display name (English)149// Updated no_NO_NY English display name for new pattern-based algorithm150// (part of Euro support).151{ "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },152153// display langage (French)154{ "anglais", "fran\u00e7ais", "croate", "grec", "norv\u00e9gien", "italien", "xx" },155// display country (French)156{ "Etats-Unis", "France", "Croatie", "Gr\u00e8ce", "Norv\u00e8ge", "", "YY" },157// display variant (French)158{ "", "", "", "", "", "", "" },159// display name (French)160{ "anglais (Etats-Unis)", "fran\u00e7ais (France)", "croate (Croatie)", "grec (Gr\u00e8ce)", "norv\u00e9gien (Norv\u00e8ge,Nynorsk)", "italien", "xx (YY)" },161162// display langage (Croatian)163{ "", "", "hrvatski", "", "", "", "xx" },164// display country (Croatian)165{ "", "", "Hrvatska", "", "", "", "YY" },166// display variant (Croatian)167{ "", "", "", "", "", "", ""},168// display name (Croatian)169{ "", "", "hrvatski (Hrvatska)", "", "", "", "xx (YY)" },170171// display langage (Greek)172{ "\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" },173// display country (Greek)174{ "\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" },175// display variant (Greek)176{ "", "", "", "", "", "", "" },177// display name (Greek)178{ "\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)" },179180// display langage (<root>)181{ "English", "French", "Croatian", "Greek", "Norwegian", "Italian", "xx" },182// display country (<root>)183{ "United States", "France", "Croatia", "Greece", "Norway", "", "YY" },184// display variant (<root>)185{ "", "", "", "", "Nynorsk", "", ""},186// display name (<root>)187{ "English (United States)", "French (France)", "Croatian (Croatia)", "Greek (Greece)", "Norwegian (Norway,Nynorsk)", "Italian", "xx (YY)" },188};189190public static void main(String[] args) throws Exception {191new LocaleTest().run(args);192}193194public void TestBasicGetters() {195for (int i = 0; i <= MAX_LOCALES; i++) {196Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);197logln("Testing " + testLocale + "...");198199if (!testLocale.getLanguage().equals(dataTable[LANG][i])) {200errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "201+ dataTable[LANG][i]);202}203if (!testLocale.getCountry().equals(dataTable[CTRY][i])) {204errln(" Country code mismatch: " + testLocale.getCountry() + " versus "205+ dataTable[CTRY][i]);206}207if (!testLocale.getVariant().equals(dataTable[VAR][i])) {208errln(" Variant code mismatch: " + testLocale.getVariant() + " versus "209+ dataTable[VAR][i]);210}211if (!testLocale.toString().equals(dataTable[NAME][i])) {212errln(" Locale name mismatch: " + testLocale.toString() + " versus "213+ dataTable[NAME][i]);214}215}216217logln("Same thing without variant codes...");218for (int i = 0; i <= MAX_LOCALES; i++) {219Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i]);220logln("Testing " + testLocale + "...");221222if (!testLocale.getLanguage().equals(dataTable[LANG][i])) {223errln(" Language code mismatch: " + testLocale.getLanguage() + " versus "224+ dataTable[LANG][i]);225}226if (!testLocale.getCountry().equals(dataTable[CTRY][i])) {227errln(" Country code mismatch: " + testLocale.getCountry() + " versus "228+ dataTable[CTRY][i]);229}230if (!testLocale.getVariant().equals("")) {231errln(" Variant code mismatch: " + testLocale.getVariant() + " versus \"\"");232}233}234}235236public void TestSimpleResourceInfo() {237for (int i = 0; i <= MAX_LOCALES; i++) {238if (dataTable[LANG][i].equals("xx")) {239continue;240}241242Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);243logln("Testing " + testLocale + "...");244245if (!testLocale.getISO3Language().equals(dataTable[LANG3][i])) {246errln(" ISO-3 language code mismatch: " + testLocale.getISO3Language()247+ " versus " + dataTable[LANG3][i]);248}249if (!testLocale.getISO3Country().equals(dataTable[CTRY3][i])) {250errln(" ISO-3 country code mismatch: " + testLocale.getISO3Country()251+ " versus " + dataTable[CTRY3][i]);252}253/*254// getLCID() is currently private255if (!String.valueOf(testLocale.getLCID()).equals(dataTable[LCID][i]))256errln(" LCID mismatch: " + testLocale.getLCID() + " versus "257+ dataTable[LCID][i]);258*/259}260}261262/*263* @bug 4101316264* @bug 4084688 (This bug appears to be a duplicate of something, because it was fixed265* between 1.1.5 and 1.1.6, but I included a new test for it anyway)266* @bug 4052440 Stop falling back to the default locale.267*/268public void TestDisplayNames() {269Locale saveDefault = Locale.getDefault();270Locale english = new Locale("en", "US");271Locale french = new Locale("fr", "FR");272Locale croatian = new Locale("hr", "HR");273Locale greek = new Locale("el", "GR");274275Locale.setDefault(english);276logln("With default = en_US...");277logln(" In default locale...");278doTestDisplayNames(null, DLANG_EN, false);279logln(" In locale = en_US...");280doTestDisplayNames(english, DLANG_EN, false);281logln(" In locale = fr_FR...");282doTestDisplayNames(french, DLANG_FR, false);283logln(" In locale = hr_HR...");284doTestDisplayNames(croatian, DLANG_HR, false);285logln(" In locale = el_GR...");286doTestDisplayNames(greek, DLANG_EL, false);287288Locale.setDefault(french);289logln("With default = fr_FR...");290logln(" In default locale...");291doTestDisplayNames(null, DLANG_FR, true);292logln(" In locale = en_US...");293doTestDisplayNames(english, DLANG_EN, true);294logln(" In locale = fr_FR...");295doTestDisplayNames(french, DLANG_FR, true);296logln(" In locale = hr_HR...");297doTestDisplayNames(croatian, DLANG_HR, true);298logln(" In locale = el_GR...");299doTestDisplayNames(greek, DLANG_EL, true);300301Locale.setDefault(saveDefault);302}303304private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {305String language = Locale.getDefault().getLanguage();306307if (defaultIsFrench && !language.equals("fr")) {308errln("Default locale should be French, but it's really " + language);309} else if (!defaultIsFrench && !language.equals("en")) {310errln("Default locale should be English, but it's really " + language);311}312313for (int i = 0; i <= MAX_LOCALES; i++) {314Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);315logln(" Testing " + testLocale + "...");316317String testLang;318String testCtry;319String testVar;320String testName;321322if (inLocale == null) {323testLang = testLocale.getDisplayLanguage();324testCtry = testLocale.getDisplayCountry();325testVar = testLocale.getDisplayVariant();326testName = testLocale.getDisplayName();327} else {328testLang = testLocale.getDisplayLanguage(inLocale);329testCtry = testLocale.getDisplayCountry(inLocale);330testVar = testLocale.getDisplayVariant(inLocale);331testName = testLocale.getDisplayName(inLocale);332}333334String expectedLang;335String expectedCtry;336String expectedVar;337String expectedName;338339expectedLang = dataTable[compareIndex][i];340if (expectedLang.equals("") && defaultIsFrench) {341expectedLang = dataTable[DLANG_EN][i];342}343if (expectedLang.equals("")) {344expectedLang = dataTable[DLANG_ROOT][i];345}346347expectedCtry = dataTable[compareIndex + 1][i];348if (expectedCtry.equals("") && defaultIsFrench) {349expectedCtry = dataTable[DCTRY_EN][i];350}351if (expectedCtry.equals("")) {352expectedCtry = dataTable[DCTRY_ROOT][i];353}354355expectedVar = dataTable[compareIndex + 2][i];356if (expectedVar.equals("") && defaultIsFrench) {357expectedVar = dataTable[DVAR_EN][i];358}359if (expectedVar.equals("")) {360expectedVar = dataTable[DVAR_ROOT][i];361}362363expectedName = dataTable[compareIndex + 3][i];364if (expectedName.equals("") && defaultIsFrench) {365expectedName = dataTable[DNAME_EN][i];366}367if (expectedName.equals("")) {368expectedName = dataTable[DNAME_ROOT][i];369}370371if (!testLang.equals(expectedLang)) {372errln("Display language mismatch: " + testLang + " versus " + expectedLang);373}374if (!testCtry.equals(expectedCtry)) {375errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);376}377if (!testVar.equals(expectedVar)) {378errln("Display variant mismatch: " + testVar + " versus " + expectedVar);379}380if (!testName.equals(expectedName)) {381errln("Display name mismatch: " + testName + " versus " + expectedName);382}383}384}385386public void TestSimpleObjectStuff() {387Locale test1 = new Locale("aa", "AA");388Locale test2 = new Locale("aa", "AA");389Locale test3 = (Locale) test1.clone();390Locale test4 = new Locale("zz", "ZZ");391392if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3) {393errln("Some of the test variables point to the same locale!");394}395396if (test3 == null) {397errln("clone() failed to produce a valid object!");398}399400if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3)) {401errln("clone() or equals() failed: objects that should compare equal don't");402}403404if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4)) {405errln("equals() failed: objects that shouldn't compare equal do");406}407408int hash1 = test1.hashCode();409int hash2 = test2.hashCode();410int hash3 = test3.hashCode();411412if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3) {413errln("hashCode() failed: objects that should have the same hash code don't");414}415}416417/**418* @bug 4011756 4011380419*/420public void TestISO3Fallback() {421Locale test = new Locale("xx", "YY", "");422boolean gotException = false;423String result = "";424425try {426result = test.getISO3Language();427} catch (MissingResourceException e) {428gotException = true;429}430if (!gotException) {431errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");432}433434gotException = false;435try {436result = test.getISO3Country();437} catch (MissingResourceException e) {438gotException = true;439}440if (!gotException) {441errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");442}443}444445/**446* @bug 4106155 4118587 7066203 7085757447*/448public void TestGetLangsAndCountries() {449// It didn't seem right to just do an exhaustive test of everything here, so I check450// for the following things:451// 1) Does each list have the right total number of entries?452// 2) Does each list contain certain language and country codes we think are important453// (the G7 countries, plus a couple others)?454// 3) Does each list have every entry formatted correctly? (i.e., two characters,455// all lower case for the language codes, all upper case for the country codes)456// 4) Is each list in sorted order?457String[] test = Locale.getISOLanguages();458String[] spotCheck1 = {"en", "es", "fr", "de", "it", "ja", "ko", "zh", "th",459"he", "id", "iu", "ug", "yi", "za"};460461if (test.length != 188) {462errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length);463} else {464for (int i = 0; i < spotCheck1.length; i++) {465int j;466for (j = 0; j < test.length; j++) {467if (test[j].equals(spotCheck1[i])) {468break;469}470}471if (j == test.length || !test[j].equals(spotCheck1[i])) {472errln("Couldn't find " + spotCheck1[i] + " in language list.");473}474}475}476for (int i = 0; i < test.length; i++) {477if (!test[i].equals(test[i].toLowerCase())) {478errln(test[i] + " is not all lower case.");479}480if (test[i].length() != 2) {481errln(test[i] + " is not two characters long.");482}483if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {484errln(test[i] + " appears in an out-of-order position in the list.");485}486}487488test = Locale.getISOCountries();489String[] spotCheck2 = {"US", "CA", "GB", "FR", "DE", "IT", "JP", "KR", "CN", "TW", "TH"};490491492if (test.length != 249) {493errln("Expected getISOCountries to return 249 countries; it returned " + test.length);494} else {495for (int i = 0; i < spotCheck2.length; i++) {496int j;497for (j = 0; j < test.length; j++) {498if (test[j].equals(spotCheck2[i])) {499break;500}501}502if (j == test.length || !test[j].equals(spotCheck2[i])) {503errln("Couldn't find " + spotCheck2[i] + " in country list.");504}505}506}507for (int i = 0; i < test.length; i++) {508if (!test[i].equals(test[i].toUpperCase())) {509errln(test[i] + " is not all upper case.");510}511if (test[i].length() != 2) {512errln(test[i] + " is not two characters long.");513}514if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {515errln(test[i] + " appears in an out-of-order position in the list.");516}517}518}519520/**521* @bug 4126880522*/523void Test4126880() {524String[] test;525526test = Locale.getISOCountries();527test[0] = "SUCKER!!!";528test = Locale.getISOCountries();529if (test[0].equals("SUCKER!!!")) {530errln("Changed internal country code list!");531}532533test = Locale.getISOLanguages();534test[0] = "HAHAHAHA!!!";535test = Locale.getISOLanguages();536if (test[0].equals("HAHAHAHA!!!")) { // Fixed typo537errln("Changes internal language code list!");538}539}540541/**542* @bug 4107014543*/544public void TestGetAvailableLocales() {545Locale[] locales = Locale.getAvailableLocales();546if (locales == null || locales.length == 0) {547errln("Locale.getAvailableLocales() returned no installed locales!");548} else {549logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales.");550for (int i = 0; i < locales.length; i++) {551logln(locales[i].toString());552}553}554}555556/**557* @bug 4135316558*/559public void TestBug4135316() {560Locale[] locales1 = Locale.getAvailableLocales();561Locale[] locales2 = Locale.getAvailableLocales();562if (locales1 == locales2) {563errln("Locale.getAvailableLocales() doesn't clone its internal storage!");564}565}566567/**568* @bug 4107953569*/570/*571test commented out pending API-change approval572public void TestGetLanguagesForCountry() {573String[] languages = Locale.getLanguagesForCountry("US");574575if (!searchStringArrayFor("en", languages))576errln("Didn't get en as a language for US");577578languages = Locale.getLanguagesForCountry("FR");579if (!searchStringArrayFor("fr", languages))580errln("Didn't get fr as a language for FR");581582languages = Locale.getLanguagesForCountry("CH");583if (!searchStringArrayFor("fr", languages))584errln("Didn't get fr as a language for CH");585if (!searchStringArrayFor("it", languages))586errln("Didn't get it as a language for CH");587if (!searchStringArrayFor("de", languages))588errln("Didn't get de as a language for CH");589590languages = Locale.getLanguagesForCountry("JP");591if (!searchStringArrayFor("ja", languages))592errln("Didn't get ja as a language for JP");593}594*/595596private boolean searchStringArrayFor(String s, String[] array) {597for (int i = 0; i < array.length; i++)598if (s.equals(array[i]))599return true;600return false;601}602/**603* @bug 4110613604*/605public void TestSerialization() throws ClassNotFoundException, OptionalDataException,606IOException, StreamCorruptedException {607ObjectOutputStream ostream;608ByteArrayOutputStream obstream;609byte[] bytes = null;610611obstream = new ByteArrayOutputStream();612ostream = new ObjectOutputStream(obstream);613614Locale test1 = new Locale("zh", "TW", "");615int dummy = test1.hashCode(); // fill in the cached hash-code value616ostream.writeObject(test1);617618bytes = obstream.toByteArray();619620ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));621622Locale test2 = (Locale) (istream.readObject());623624if (!test1.equals(test2) || test1.hashCode() != test2.hashCode()) {625errln("Locale failed to deserialize correctly.");626}627}628629/**630* @bug 4118587631*/632public void TestSimpleDisplayNames() {633// This test is different from TestDisplayNames because TestDisplayNames checks634// fallback behavior, combination of language and country names to form locale635// names, and other stuff like that. This test just checks specific language636// and country codes to make sure we have the correct names for them.637String[] languageCodes = {"he", "id", "iu", "ug", "yi", "za"};638String[] languageNames = {"Hebrew", "Indonesian", "Inuktitut", "Uyghur", "Yiddish",639"Zhuang"};640641for (int i = 0; i < languageCodes.length; i++) {642String test = (new Locale(languageCodes[i], "", "")).getDisplayLanguage(Locale.US);643if (!test.equals(languageNames[i])) {644errln("Got wrong display name for " + languageCodes[i] + ": Expected \""645+ languageNames[i] + "\", got \"" + test + "\".");646}647}648}649650/**651* @bug 4118595652*/653public void TestUninstalledISO3Names() {654// This test checks to make sure getISO3Language and getISO3Country work right655// even for locales that are not installed.656String[] iso2Languages = {"am", "ba", "fy", "mr", "rn", "ss", "tw", "zu"};657String[] iso3Languages = {"amh", "bak", "fry", "mar", "run", "ssw", "twi", "zul"};658659for (int i = 0; i < iso2Languages.length; i++) {660String test = (new Locale(iso2Languages[i], "", "")).getISO3Language();661if (!test.equals(iso3Languages[i])) {662errln("Got wrong ISO3 code for " + iso2Languages[i] + ": Expected \""663+ iso3Languages[i] + "\", got \"" + test + "\".");664}665}666667String[] iso2Countries = {"AF", "BW", "KZ", "MO", "MN", "SB", "TC", "ZW"};668String[] iso3Countries = {"AFG", "BWA", "KAZ", "MAC", "MNG", "SLB", "TCA", "ZWE"};669670for (int i = 0; i < iso2Countries.length; i++) {671String test = (new Locale("", iso2Countries[i], "")).getISO3Country();672if (!test.equals(iso3Countries[i])) {673errln("Got wrong ISO3 code for " + iso2Countries[i] + ": Expected \""674+ iso3Countries[i] + "\", got \"" + test + "\".");675}676}677}678679/**680* @bug 4052404 4778440 8263202681*/682public void TestChangedISO639Codes() {683Locale hebrewOld = new Locale("iw", "IL", "");684Locale hebrewNew = new Locale("he", "IL", "");685Locale yiddishOld = new Locale("ji", "IL", "");686Locale yiddishNew = new Locale("yi", "IL", "");687Locale indonesianOld = new Locale("in", "", "");688Locale indonesianNew = new Locale("id", "", "");689690if ("true".equalsIgnoreCase(System.getProperty("java.locale.useOldISOCodes"))) {691if (!hebrewNew.getLanguage().equals("iw")) {692errln("Got back wrong language code for new Hebrew: expected \"iw\", got \""693+ hebrewNew.getLanguage() + "\"");694}695if (!yiddishNew.getLanguage().equals("ji")) {696errln("Got back wrong language code for new Yiddish: expected \"ji\", got \""697+ yiddishNew.getLanguage() + "\"");698}699if (!indonesianNew.getLanguage().equals("in")) {700errln("Got back wrong language code for new Indonesian: expected \"in\", got \""701+ indonesianNew.getLanguage() + "\"");702}703} else {704if (!hebrewOld.getLanguage().equals("he")) {705errln("Got back wrong language code for old Hebrew: expected \"he\", got \""706+ hebrewNew.getLanguage() + "\"");707}708if (!yiddishOld.getLanguage().equals("yi")) {709errln("Got back wrong language code for old Yiddish: expected \"yi\", got \""710+ yiddishNew.getLanguage() + "\"");711}712if (!indonesianOld.getLanguage().equals("id")) {713errln("Got back wrong language code for old Indonesian: expected \"id\", got \""714+ indonesianNew.getLanguage() + "\"");715}716}717718}719720/**721* @bug 4092475722* I could not reproduce this bug. I'm pretty convinced it was fixed with the723* big locale-data reorg of 10/28/97. The lookup logic for language and country724* display names was also changed at that time in that check-in. --rtg 3/20/98725726* This test is not designed to work in any other locale but en_US.727* Most of the LocaleElements do not contain display names for other languages,728* so this test fails (bug 4289223) when run under different locales. For example,729* LocaleElements_es as of kestrel does not have a localized name for Japanese, so730* the getDisplayName method asks the default locale for a display name. The Japanese731* localized name for "Japanese" does not equal "Japanese" so this test fails for es732* display names if run under a ja locale. Eventually, he LocaleElements should probably733* be updated to contain more localized language and region display names.734* 1999-11-19 joconner735*736*/737public void TestAtypicalLocales() {738Locale[] localesToTest = { new Locale("de", "CA"),739new Locale("ja", "ZA"),740new Locale("ru", "MX"),741new Locale("en", "FR"),742new Locale("es", "DE"),743new Locale("", "HR"),744new Locale("", "SE"),745new Locale("", "DO"),746new Locale("", "BE") };747String[] englishDisplayNames = { "German (Canada)",748"Japanese (South Africa)",749"Russian (Mexico)",750"English (France)",751"Spanish (Germany)",752"Croatia",753"Sweden",754"Dominican Republic",755"Belgium" };756String[] frenchDisplayNames = { "allemand (Canada)",757"japonais (Afrique du Sud)",758"russe (Mexique)",759"anglais (France)",760"espagnol (Allemagne)",761"Croatie",762"Su\u00e8de",763"R\u00e9publique Dominicaine",764"Belgique" };765String[] spanishDisplayNames = { "alem\u00E1n (Canad\u00E1)",766"japon\u00E9s (Sud\u00E1frica)",767"ruso (M\u00e9xico)",768"ingl\u00E9s (Francia)",769"espa\u00f1ol (Alemania)",770"Croacia",771"Suecia",772"Rep\u00fablica Dominicana",773"B\u00E9lgica" };774775776// save the default locale and set to the new default to en_US777Locale defaultLocale = Locale.getDefault();778Locale.setDefault(Locale.US);779780for (int i = 0; i < localesToTest.length; i++) {781String name = localesToTest[i].getDisplayName(Locale.US);782logln(name);783if (!name.equals(englishDisplayNames[i])) {784errln("Lookup in English failed: expected \"" + englishDisplayNames[i]785+ "\", got \"" + name + "\"");786}787}788789for (int i = 0; i < localesToTest.length; i++) {790String name = localesToTest[i].getDisplayName(new Locale("es", "ES"));791logln(name);792if (!name.equals(spanishDisplayNames[i])) {793errln("Lookup in Spanish failed: expected \"" + spanishDisplayNames[i]794+ "\", got \"" + name + "\"");795}796}797798for (int i = 0; i < localesToTest.length; i++) {799String name = localesToTest[i].getDisplayName(Locale.FRANCE);800logln(name);801if (!name.equals(frenchDisplayNames[i])) {802errln("Lookup in French failed: expected \"" + frenchDisplayNames[i]803+ "\", got \"" + name + "\"");804}805}806807// restore the default locale for other tests808Locale.setDefault(defaultLocale);809}810811/**812* @bug 4126371813*/814public void TestNullDefault() {815// why on earth anyone would ever try to do this is beyond me, but we should816// definitely make sure we don't let them817boolean gotException = false;818try {819Locale.setDefault(null);820} catch (NullPointerException e) {821// all other exception types propagate through here back to the test harness822gotException = true;823}824if (Locale.getDefault() == null) {825errln("Locale.getDefault() allowed us to set default to NULL!");826}827if (!gotException) {828errln("Trying to set default locale to NULL didn't throw exception!");829}830}831832/**833* @bug 4135752834* This would be better tested by the LocaleDataTest. Will move it when I835* get the LocaleDataTest working again.836*/837public void TestThaiCurrencyFormat() {838DecimalFormat thaiCurrency = (DecimalFormat) NumberFormat.getCurrencyInstance(839new Locale("th", "TH"));840if (!thaiCurrency.getPositivePrefix().equals("\u0e3f")) {841errln("Thai currency prefix wrong: expected \"\u0e3f\", got \""842+ thaiCurrency.getPositivePrefix() + "\"");843}844if (!thaiCurrency.getPositiveSuffix().equals("")) {845errln("Thai currency suffix wrong: expected \"\", got \""846+ thaiCurrency.getPositiveSuffix() + "\"");847}848}849850/**851* @bug 4122371852* Confirm that Euro support works. This test is pretty rudimentary; all it does853* is check that any locales with the EURO variant format a number using the854* Euro currency symbol.855*856* ASSUME: All locales encode the Euro character "\u20AC".857* If this is changed to use the single-character Euro symbol, this858* test must be updated.859*860* DON'T ASSUME: Any specific countries support the Euro. Instead,861* iterate through all locales.862*/863public void TestEuroSupport() {864final String EURO_VARIANT = "EURO";865final String EURO_CURRENCY = "\u20AC"; // Look for this string in formatted Euro currency866867Locale[] locales = NumberFormat.getAvailableLocales();868for (int i = 0; i < locales.length; ++i) {869Locale loc = locales[i];870if (loc.getVariant().indexOf(EURO_VARIANT) >= 0) {871NumberFormat nf = NumberFormat.getCurrencyInstance(loc);872String pos = nf.format(271828.182845);873String neg = nf.format(-271828.182845);874if (pos.indexOf(EURO_CURRENCY) >= 0875&& neg.indexOf(EURO_CURRENCY) >= 0) {876logln("Ok: " + loc.toString()877+ ": " + pos + " / " + neg);878} else {879errln("Fail: " + loc.toString()880+ " formats without " + EURO_CURRENCY881+ ": " + pos + " / " + neg882+ "\n*** THIS FAILURE MAY ONLY MEAN THAT LOCALE DATA HAS CHANGED ***");883}884}885}886}887888/**889* @bug 4139504890* toString() doesn't work with language_VARIANT.891*/892public void TestToString() {893Object[] DATA = {894new Locale("xx", "", ""), "xx",895new Locale("", "YY", ""), "_YY",896new Locale("", "", "ZZ"), "",897new Locale("xx", "YY", ""), "xx_YY",898new Locale("xx", "", "ZZ"), "xx__ZZ",899new Locale("", "YY", "ZZ"), "_YY_ZZ",900new Locale("xx", "YY", "ZZ"), "xx_YY_ZZ",901};902for (int i = 0; i < DATA.length; i += 2) {903Locale loc = (Locale) DATA[i];904String fmt = (String) DATA[i + 1];905if (!loc.toString().equals(fmt)) {906errln("Fail: Locale.toString(" + fmt + ")=>" + loc);907}908}909}910911/**912* @bug 4105828913* Currency symbol in zh is wrong. We will test this at the NumberFormat914* end to test the whole pipe.915*/916public void Test4105828() {917Locale[] LOC = {Locale.CHINESE, new Locale("zh", "CN", ""),918new Locale("zh", "TW", ""), new Locale("zh", "HK", "")};919for (int i = 0; i < LOC.length; ++i) {920NumberFormat fmt = NumberFormat.getPercentInstance(LOC[i]);921String result = fmt.format(1);922if (!result.equals("100%")) {923errln("Percent for " + LOC[i] + " should be 100%, got " + result);924}925}926}927928/**929* @bug 4139940930* Couldn't reproduce this bug -- probably was fixed earlier.931*932* ORIGINAL BUG REPORT:933* -- basically, hungarian for monday shouldn't have an \u00f4934* (o circumflex)in it instead it should be an o with 2 inclined935* (right) lines over it..936*937* You may wonder -- why do all this -- why not just add a line to938* LocaleData? Well, I could see by inspection that the locale file had the939* right character in it, so I wanted to check the rest of the pipeline -- a940* very remote possibility, but I wanted to be sure. The other possibility941* is that something is wrong with the font mapping subsystem, but we can't942* test that here.943*/944public void Test4139940() {945Locale mylocale = new Locale("hu", "", "");946@SuppressWarnings("deprecation")947Date mydate = new Date(98, 3, 13); // A Monday948DateFormat df_full = new SimpleDateFormat("EEEE", mylocale);949String str = df_full.format(mydate);950// Make sure that o circumflex (\u00F4) is NOT there, and951// o double acute (\u0151) IS.952if (str.indexOf('\u0151') < 0 || str.indexOf('\u00F4') >= 0) {953errln("Fail: Monday in Hungarian is wrong");954}955}956957/**958* @bug 4143951959* Russian first day of week should be Monday. Confirmed.960*/961public void Test4143951() {962Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));963if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {964errln("Fail: First day of week in Russia should be Monday");965}966}967968/**969* @bug 4147315970* java.util.Locale.getISO3Country() works wrong for non ISO-3166 codes.971* Should throw an exception for unknown locales972*/973public void Test4147315() {974// Try with codes that are the wrong length but happen to match text975// at a valid offset in the mapping table976Locale locale = new Locale("aaa", "CCC");977978try {979String result = locale.getISO3Country();980981errln("ERROR: getISO3Country() returns: " + result982+ " for locale '" + locale + "' rather than exception");983} catch (MissingResourceException e) {984}985}986987/**988* @bug 4147317 4940539989* java.util.Locale.getISO3Language() works wrong for non ISO-639 codes.990* Should throw an exception for unknown locales, except they have three991* letter language codes.992*/993public void Test4147317() {994// Try a three letter language code, and check whether it is995// returned as is.996Locale locale = new Locale("aaa", "CCC");997998String result = locale.getISO3Language();999if (!result.equals("aaa")) {1000errln("ERROR: getISO3Language() returns: " + result1001+ " for locale '" + locale + "' rather than returning it as is");1002}10031004// Try an invalid two letter language code, and check whether it1005// throws a MissingResourceException.1006locale = new Locale("zz", "CCC");10071008try {1009result = locale.getISO3Language();10101011errln("ERROR: getISO3Language() returns: " + result1012+ " for locale '" + locale + "' rather than exception");1013} catch (MissingResourceException e) {1014}1015}10161017/*1018* @bug 4147552 4778440 80306961019*/1020public void Test4147552() {1021Locale[] locales = {new Locale("no", "NO"), new Locale("no", "NO", "B"),1022new Locale("no", "NO", "NY"), new Locale("nb", "NO"),1023new Locale("nn", "NO")};1024String[] englishDisplayNames = {"Norwegian (Norway)",1025"Norwegian (Norway,Bokm\u00e5l)",1026"Norwegian (Norway,Nynorsk)",1027"Norwegian Bokm\u00e5l (Norway)",1028"Norwegian Nynorsk (Norway)"};1029String[] norwegianDisplayNames = {"norsk (Norge)",1030"norsk (Norge,bokm\u00e5l)", "norsk (Noreg,nynorsk)",1031"bokm\u00e5l (Norge)", "nynorsk (Noreg)"};10321033for (int i = 0; i < locales.length; i++) {1034Locale loc = locales[i];1035if (!loc.getDisplayName(Locale.US).equals(englishDisplayNames[i])) {1036errln("English display-name mismatch: expected "1037+ englishDisplayNames[i] + ", got " + loc.getDisplayName());1038}1039if (!loc.getDisplayName(loc).equals(norwegianDisplayNames[i])) {1040errln("Norwegian display-name mismatch: expected "1041+ norwegianDisplayNames[i] + ", got "1042+ loc.getDisplayName(loc));1043}1044}1045}10461047/*1048* @bug 80306961049*/1050public void Test8030696() {1051List<Locale> av = Arrays.asList(Locale.getAvailableLocales());1052if (!av.contains(new Locale("nb", "NO"))1053|| !av.contains(new Locale("nn", "NO"))) {1054errln("\"nb-NO\" and/or \"nn-NO\" locale(s) not returned from getAvailableLocales().");1055}1056}10571058static String escapeUnicode(String s) {1059StringBuffer buf = new StringBuffer();1060for (int i = 0; i < s.length(); ++i) {1061char c = s.charAt(i);1062if (c >= 0x20 && c <= 0x7F) {1063buf.append(c);1064} else {1065buf.append("\\u");1066String h = "000" + Integer.toHexString(c);1067if (h.length() > 4) {1068h = h.substring(h.length() - 4);1069}1070buf.append(h);1071}1072}1073return buf.toString();1074}1075}107610771078