Locale.setDefault(english);
logln("With default = en_US...");
logln(" In default locale...");
doTestDisplayNames(null, DLANG_EN, false);
logln(" In locale = en_US...");
doTestDisplayNames(english, DLANG_EN, false);
logln(" In locale = fr_FR...");
doTestDisplayNames(french, DLANG_FR, false);
logln(" In locale = hr_HR...");
doTestDisplayNames(croatian, DLANG_HR, false);
logln(" In locale = el_GR...");
doTestDisplayNames(greek, DLANG_EL, false);
Locale.setDefault(french);
logln("With default = fr_FR...");
logln(" In default locale...");
doTestDisplayNames(null, DLANG_FR, true);
logln(" In locale = en_US...");
doTestDisplayNames(english, DLANG_EN, true);
logln(" In locale = fr_FR...");
doTestDisplayNames(french, DLANG_FR, true);
logln(" In locale = hr_HR...");
doTestDisplayNames(croatian, DLANG_HR, true);
logln(" In locale = el_GR...");
doTestDisplayNames(greek, DLANG_EL, true);
Locale.setDefault(saveDefault);
}
privatevoid doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
String language = Locale.getDefault().getLanguage();
if (defaultIsFrench && !language.equals("fr")) {
errln("Default locale should be French, but it's really " + language);
} elseif (!defaultIsFrench && !language.equals("en")) {
errln("Default locale should be English, but it's really " + language);
}
for (int i = 0; i <= MAX_LOCALES; i++) {
Locale testLocale = Locale.of(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
logln(" Testing " + testLocale + "...");
if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3) {
errln("Some of the test variables point to the same locale!");
}
if (test3 == null) {
errln("clone() failed to produce a valid object!");
}
if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3)) {
errln("clone() or equals() failed: objects that should compare equal don't");
}
if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4)) {
errln("equals() failed: objects that shouldn't compare equal do");
}
int hash1 = test1.hashCode(); int hash2 = test2.hashCode(); int hash3 = test3.hashCode();
if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3) {
errln("hashCode() failed: objects that should have the same hash code don't");
}
}
/** *@bug40117564011380
*/ publicvoid TestISO3Fallback() {
Locale test = Locale.of("xx", "YY"); boolean gotException = false;
String result = "";
try {
result = test.getISO3Language();
} catch (MissingResourceException e) {
gotException = true;
} if (!gotException) {
errln("getISO3Language() on xx_YY returned " + result + " instead of throwing an exception");
}
gotException = false; try {
result = test.getISO3Country();
} catch (MissingResourceException e) {
gotException = true;
} if (!gotException) {
errln("getISO3Country() on xx_YY returned " + result + " instead of throwing an exception");
}
}
/** *@bug4106155411858770662037085757
*/ publicvoid TestGetLangsAndCountries() { // It didn't seem right to just do an exhaustive test of everything here, so I check // for the following things: // 1) Does each list have the right total number of entries? // 2) Does each list contain certain language and country codes we think are important // (the G7 countries, plus a couple others)? // 3) Does each list have every entry formatted correctly? (i.e., two characters, // all lower case for the language codes, all upper case for the country codes) // 4) Is each list in sorted order?
String[] test = Locale.getISOLanguages();
String[] spotCheck1 = {"en", "es", "fr", "de", "it", "ja", "ko", "zh", "th", "he", "id", "iu", "ug", "yi", "za"};
if (test.length != 188) {
errln("Expected getISOLanguages() to return 188 languages; it returned " + test.length);
} else { for (int i = 0; i < spotCheck1.length; i++) { int j; for (j = 0; j < test.length; j++) { if (test[j].equals(spotCheck1[i])) { break;
}
} if (j == test.length || !test[j].equals(spotCheck1[i])) {
errln("Couldn't find " + spotCheck1[i] + " in language list.");
}
}
} for (int i = 0; i < test.length; i++) { if (!test[i].equals(test[i].toLowerCase())) {
errln(test[i] + " is not all lower case.");
} if (test[i].length() != 2) {
errln(test[i] + " is not two characters long.");
} if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {
errln(test[i] + " appears in an out-of-order position in the list.");
}
}
if (test.length != 249) {
errln("Expected getISOCountries to return 249 countries; it returned " + test.length);
} else { for (int i = 0; i < spotCheck2.length; i++) { int j; for (j = 0; j < test.length; j++) { if (test[j].equals(spotCheck2[i])) { break;
}
} if (j == test.length || !test[j].equals(spotCheck2[i])) {
errln("Couldn't find " + spotCheck2[i] + " in country list.");
}
}
} for (int i = 0; i < test.length; i++) { if (!test[i].equals(test[i].toUpperCase())) {
errln(test[i] + " is not all upper case.");
} if (test[i].length() != 2) {
errln(test[i] + " is not two characters long.");
} if (i > 0 && test[i].compareTo(test[i - 1]) <= 0) {
errln(test[i] + " appears in an out-of-order position in the list.");
}
}
}
test = Locale.getISOCountries();
test[0] = "SUCKER!!!";
test = Locale.getISOCountries(); if (test[0].equals("SUCKER!!!")) {
errln("Changed internal country code list!");
}
test = Locale.getISOLanguages();
test[0] = "HAHAHAHA!!!";
test = Locale.getISOLanguages(); if (test[0].equals("HAHAHAHA!!!")) { // Fixed typo
errln("Changes internal language code list!");
}
}
/** *@bug4107014
*/ publicvoid TestGetAvailableLocales() {
Locale[] locales = Locale.getAvailableLocales(); if (locales == null || locales.length == 0) {
errln("Locale.getAvailableLocales() returned no installed locales!");
} else {
logln("Locale.getAvailableLocales() returned a list of " + locales.length + " locales."); for (int i = 0; i < locales.length; i++) {
logln(locales[i].toString());
}
}
}
privateboolean searchStringArrayFor(String s, String[] array) { for (int i = 0; i < array.length; i++) if (s.equals(array[i])) returntrue; returnfalse;
} /** *@bug4110613
*/ publicvoid TestSerialization() throws ClassNotFoundException, OptionalDataException,
IOException, StreamCorruptedException {
ObjectOutputStream ostream;
ByteArrayOutputStream obstream; byte[] bytes = null;
obstream = new ByteArrayOutputStream();
ostream = new ObjectOutputStream(obstream);
Locale test1 = Locale.of("zh", "TW"); int dummy = test1.hashCode(); // fill in the cached hash-code value
ostream.writeObject(test1);
bytes = obstream.toByteArray();
ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Locale test2 = (Locale) (istream.readObject());
if (!test1.equals(test2) || test1.hashCode() != test2.hashCode()) {
errln("Locale failed to deserialize correctly.");
}
}
/** *@bug4118587
*/ publicvoid TestSimpleDisplayNames() { // This test is different from TestDisplayNames because TestDisplayNames checks // fallback behavior, combination of language and country names to form locale // names, and other stuff like that. This test just checks specific language // and country codes to make sure we have the correct names for them.
String[] languageCodes = {"he", "id", "iu", "ug", "yi", "za"};
String[] languageNames = {"Hebrew", "Indonesian", "Inuktitut", "Uyghur", "Yiddish", "Zhuang"};
for (int i = 0; i < languageCodes.length; i++) {
String test = (Locale.of(languageCodes[i])).getDisplayLanguage(Locale.US); if (!test.equals(languageNames[i])) {
errln("Got wrong display name for " + languageCodes[i] + ": Expected \""
+ languageNames[i] + "\", got \"" + test + "\".");
}
}
}
/** *@bug4118595
*/ publicvoid TestUninstalledISO3Names() { // This test checks to make sure getISO3Language and getISO3Country work right // even for locales that are not installed.
String[] iso2Languages = {"am", "ba", "fy", "mr", "rn", "ss", "tw", "zu"};
String[] iso3Languages = {"amh", "bak", "fry", "mar", "run", "ssw", "twi", "zul"};
for (int i = 0; i < iso2Languages.length; i++) {
String test = (Locale.of(iso2Languages[i])).getISO3Language(); if (!test.equals(iso3Languages[i])) {
errln("Got wrong ISO3 code for " + iso2Languages[i] + ": Expected \""
+ iso3Languages[i] + "\", got \"" + test + "\".");
}
}
if ("true".equalsIgnoreCase(System.getProperty("java.locale.useOldISOCodes"))) { if (!hebrewNew.getLanguage().equals("iw")) {
errln("Got back wrong language code for new Hebrew: expected \"iw\", got \""
+ hebrewNew.getLanguage() + "\"");
} if (!yiddishNew.getLanguage().equals("ji")) {
errln("Got back wrong language code for new Yiddish: expected \"ji\", got \""
+ yiddishNew.getLanguage() + "\"");
} if (!indonesianNew.getLanguage().equals("in")) {
errln("Got back wrong language code for new Indonesian: expected \"in\", got \""
+ indonesianNew.getLanguage() + "\"");
}
} else { if (!hebrewOld.getLanguage().equals("he")) {
errln("Got back wrong language code for old Hebrew: expected \"he\", got \""
+ hebrewNew.getLanguage() + "\"");
} if (!yiddishOld.getLanguage().equals("yi")) {
errln("Got back wrong language code for old Yiddish: expected \"yi\", got \""
+ yiddishNew.getLanguage() + "\"");
} if (!indonesianOld.getLanguage().equals("id")) {
errln("Got back wrong language code for old Indonesian: expected \"id\", got \""
+ indonesianNew.getLanguage() + "\"");
}
}
// save the default locale and set to the new default to en_US
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
for (int i = 0; i < localesToTest.length; i++) {
String name = localesToTest[i].getDisplayName(Locale.US);
logln(name); if (!name.equals(englishDisplayNames[i])) {
errln("Lookup in English failed: expected \"" + englishDisplayNames[i]
+ "\", got \"" + name + "\"");
}
}
for (int i = 0; i < localesToTest.length; i++) {
String name = localesToTest[i].getDisplayName(Locale.of("es", "ES"));
logln(name); if (!name.equals(spanishDisplayNames[i])) {
errln("Lookup in Spanish failed: expected \"" + spanishDisplayNames[i]
+ "\", got \"" + name + "\"");
}
}
for (int i = 0; i < localesToTest.length; i++) {
String name = localesToTest[i].getDisplayName(Locale.FRANCE);
logln(name); if (!name.equals(frenchDisplayNames[i])) {
errln("Lookup in French failed: expected \"" + frenchDisplayNames[i]
+ "\", got \"" + name + "\"");
}
}
// restore the default locale for other tests
Locale.setDefault(defaultLocale);
}
/** *@bug4126371
*/ publicvoid TestNullDefault() { // why on earth anyone would ever try to do this is beyond me, but we should // definitely make sure we don't let them boolean gotException = false; try {
Locale.setDefault(null);
} catch (NullPointerException e) { // all other exception types propagate through here back to the test harness
gotException = true;
} if (Locale.getDefault() == null) {
errln("Locale.getDefault() allowed us to set default to NULL!");
} if (!gotException) {
errln("Trying to set default locale to NULL didn't throw exception!");
}
}
/** *@bug4105828 *Currencysymbolinzhiswrong.WewilltestthisattheNumberFormat *endtotestthewholepipe.
*/ publicvoid Test4105828() {
Locale[] LOC = {Locale.CHINESE, Locale.of("zh", "CN"),
Locale.of("zh", "TW"), Locale.of("zh", "HK")}; for (int i = 0; i < LOC.length; ++i) {
NumberFormat fmt = NumberFormat.getPercentInstance(LOC[i]);
String result = fmt.format(1); if (!result.equals("100%")) {
errln("Percent for " + LOC[i] + " should be 100%, got " + result);
}
}
}
/** *@bug4139940 *Couldn'treproducethisbug--probablywasfixedearlier. * *ORIGINALBUGREPORT: *--basically,hungarianformondayshouldn'thavean\u00f4 *(ocircumflex)initinsteaditshouldbeanowith2inclined *(right)linesoverit.. * *Youmaywonder--whydoallthis--whynotjustaddalineto *LocaleData?Well,Icouldseebyinspectionthatthelocalefilehadthe *rightcharacterinit,soIwantedtochecktherestofthepipeline--a *veryremotepossibility,butIwantedtobesure.Theotherpossibility *isthatsomethingiswrongwiththefontmappingsubsystem,butwecan't *testthathere.
*/ publicvoid Test4139940() {
Locale mylocale = Locale.of("hu");
@SuppressWarnings("deprecation")
Date mydate = new Date(98, 3, 13); // A Monday
DateFormat df_full = new SimpleDateFormat("EEEE", mylocale);
String str = df_full.format(mydate); // Make sure that o circumflex (\u00F4) is NOT there, and // o double acute (\u0151) IS. if (str.indexOf('\u0151') < 0 || str.indexOf('\u00F4') >= 0) {
errln("Fail: Monday in Hungarian is wrong");
}
}
/** *@bug4143951 *RussianfirstdayofweekshouldbeMonday.Confirmed.
*/ publicvoid Test4143951() {
Calendar cal = Calendar.getInstance(Locale.of("ru")); if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
errln("Fail: First day of week in Russia should be Monday");
}
}
/** *@bug4147315 *java.util.Locale.getISO3Country()workswrongfornonISO-3166codes. *Shouldthrowanexceptionforunknownlocales
*/ publicvoid Test4147315() { // Try with codes that are the wrong length but happen to match text // at a valid offset in the mapping table
Locale locale = Locale.of("aaa", "CCC");
try {
String result = locale.getISO3Country();
errln("ERROR: getISO3Country() returns: " + result
+ " for locale '" + locale + "' rather than exception");
} catch (MissingResourceException e) {
}
}
/** *@bug41473174940539 *java.util.Locale.getISO3Language()workswrongfornonISO-639codes. *Shouldthrowanexceptionforunknownlocales,excepttheyhavethree *letterlanguagecodes.
*/ publicvoid Test4147317() { // Try a three letter language code, and check whether it is // returned as is.
Locale locale = Locale.of("aaa", "CCC");
String result = locale.getISO3Language(); if (!result.equals("aaa")) {
errln("ERROR: getISO3Language() returns: " + result
+ " for locale '" + locale + "' rather than returning it as is");
}
// Try an invalid two letter language code, and check whether it // throws a MissingResourceException.
locale = Locale.of("zz", "CCC");
try {
result = locale.getISO3Language();
errln("ERROR: getISO3Language() returns: " + result
+ " for locale '" + locale + "' rather than exception");
} catch (MissingResourceException e) {
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.