Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/resources/TimeZone/Bug4640234.java
38853 views
/*1* Copyright (c) 2007, 2011, 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*/2223/**24* @test25* @bug 4640234 4946057 4938151 4873691 502318126* @summary Verifies the translation of time zone names, this test will catch27* presence of country name for english and selected locales for all28* ISO country codes.29* The test program also displays which timezone, country and30* language names are not translated31*/323334/*35* 4946057 Localization for ISO country & language data.36* 4938151 Time zones not translated37* 4873691 Changes in TimeZone mapping38*/3940import java.text.MessageFormat;41import java.text.SimpleDateFormat;4243import java.util.Date;44import java.util.Locale;45import java.util.Enumeration;46import java.util.HashMap;47import java.util.Map;48import java.util.ResourceBundle;49import java.util.TimeZone;505152public class Bug4640234 {53static SimpleDateFormat sdfEn = new SimpleDateFormat("zzzz", Locale.US);54static SimpleDateFormat sdfEnShort = new SimpleDateFormat("z", Locale.US);55static Locale locEn = Locale.ENGLISH;5657static SimpleDateFormat sdfLoc;58static SimpleDateFormat sdfLocShort;59static Date date = new Date();6061// Define supported locales62static Locale[] locales2Test = new Locale[] {63new Locale("de"),64new Locale("es"),65new Locale("fr"),66new Locale("it"),67new Locale("ja"),68new Locale("ko"),69new Locale("sv"),70new Locale("zh", "CN"),71new Locale("zh", "TW")72};7374public static void main(String[] args) throws Exception {75Locale reservedLocale = Locale.getDefault();76try {77Locale.setDefault(Locale.ENGLISH);7879StringBuffer errors = new StringBuffer("");80StringBuffer warnings = new StringBuffer("");8182String[] timezones = TimeZone.getAvailableIDs();83String[] countries = locEn.getISOCountries();84String[] languages = locEn.getISOLanguages();8586ResourceBundle resEn = ResourceBundle.getBundle(87"sun.util.resources.LocaleNames", locEn);88Map<String, String> countryMapEn = getList(resEn, true);89Map<String, String> languageMapEn = getList(resEn, false);9091ResourceBundle resLoc;92Map<String, String> countryMap;93Map<String, String> languageMap;9495for (Locale locale : locales2Test) {96resLoc = ResourceBundle.getBundle(97"sun.util.resources.LocaleNames", locale);9899sdfLoc = new SimpleDateFormat("zzzz", locale);100sdfLocShort = new SimpleDateFormat("z", locale);101102for (String timezone : timezones) {103if (isTZIgnored(timezone)) {104continue;105}106warnings.append(testTZ(timezone, locale));107}108109countryMap = getList(resLoc, true);110111for (String country : countries) {112String[] result = testEntry(country,113countryMapEn,114countryMap,115locale,116"ERROR: {0} country name for country code: {1} not found!\n",117"WARNING: {0} country name for country code: {1} not localized!\n"118);119if (warnings.indexOf(result[0]) == -1) {120warnings.append(result[0]);121}122if (errors.indexOf(result[1]) == -1) {123errors.append(result[1]);124}125}126127languageMap = getList(resLoc, false);128for (String language : languages) {129String[] result = testEntry(language,130languageMapEn,131languageMap,132locale,133"ERROR: {0} language name for language code: {1} not found!\n",134"WARNING: {0} language name for language code: {1} not localized!\n");135if (warnings.indexOf(result[0]) == -1) {136warnings.append(result[0]);137}138if (errors.indexOf(result[1]) == -1) {139errors.append(result[1]);140}141}142}143144StringBuffer message = new StringBuffer("");145if (!"".equals(errors.toString())) {146message.append("Test failed! ");147message.append("ERROR: some keys are missing! ");148}149150if ("".equals(message.toString())) {151System.out.println("\nTest passed");152System.out.println(warnings.toString());153} else {154System.out.println("\nTest failed!");155System.out.println(errors.toString());156System.out.println(warnings.toString());157throw new Exception("\n" + message);158}159} finally {160// restore the reserved locale161Locale.setDefault(reservedLocale);162}163}164165/**166* Compares the english timezone name and timezone name in specified locale167* @param timeZoneName - name of the timezone to compare168* @param locale - locale to test against english169* @return empty string when passed, descriptive error message in other cases170*/171private static String testTZ(String timeZoneName, Locale locale) {172StringBuffer timeZoneResult = new StringBuffer("");173TimeZone tz = TimeZone.getTimeZone(timeZoneName);174sdfEn.setTimeZone(tz);175sdfEnShort.setTimeZone(tz);176sdfLoc.setTimeZone(tz);177sdfLocShort.setTimeZone(tz);178179String en, enShort, loc, locShort;180en = sdfEn.format(date);181enShort = sdfEnShort.format(date);182loc = sdfLoc.format(date);183locShort = sdfLocShort.format(date);184185String displayLanguage = locale.getDisplayLanguage();186String displayCountry = locale.getDisplayCountry();187188if (loc.equals(en)) {189timeZoneResult.append("[");190timeZoneResult.append(displayLanguage);191if (!"".equals(displayCountry)) {192timeZoneResult.append(" ");193timeZoneResult.append(displayCountry);194}195timeZoneResult.append("] timezone \"");196timeZoneResult.append(timeZoneName);197timeZoneResult.append("\" long name \"" + en);198timeZoneResult.append("\" not localized!\n");199}200201if (!locShort.equals(enShort)) {202timeZoneResult.append("[");203timeZoneResult.append(displayLanguage);204if (!"".equals(displayCountry)) {205timeZoneResult.append(" ");206timeZoneResult.append(displayCountry);207}208timeZoneResult.append("] timezone \"");209timeZoneResult.append(timeZoneName);210timeZoneResult.append("\" short name \"" + enShort);211timeZoneResult.append("\" is localized \"");212timeZoneResult.append(locShort);213timeZoneResult.append("\"!\n");214}215return timeZoneResult.toString();216}217218/**219* Verifies whether the name for ISOCode is localized.220* @param ISOCode - ISO country/language code for country/language name221* to test222* @param entriesEn - array of english country/language names223* @param entriesLoc - array of localized country/language names for224* specified locale225* @param locale - locale to test against english226* @param notFoundMessage - message in form ready for MessageFormat,227* {0} will be human readable language name, {1} will be ISOCode.228* @param notLocalizedMessage - message in for ready for MessageFormat,229* same formatting like for notFountMessage230* @return array of two empty strings when passed, descriptive error message231* in other cases, [0] - warnings for not localized, [1] - errors for232* missing keys.233*/234private static String[] testEntry(String ISOCode,235Map<String, String> entriesEn,236Map<String, String> entriesLoc,237Locale locale,238String notFoundMessage,239String notLocalizedMessage) {240String nameEn = null;241String nameLoc = null;242243for (String key: entriesEn.keySet()) {244if (ISOCode.equalsIgnoreCase(key)) {245nameEn = entriesEn.get(key);246break;247}248}249250for (String key: entriesLoc.keySet()) {251if (ISOCode.equalsIgnoreCase(key)) {252nameLoc = entriesLoc.get(key);253break;254}255}256257if (nameEn == null) {258// We should not get here but test is a MUST have259return new String[] {"", MessageFormat.format(notFoundMessage,260new String[] {"English", ISOCode})};261}262263if (nameLoc == null) {264return new String[] {"", MessageFormat.format(notFoundMessage,265new String[] {locale.getDisplayName(), ISOCode})};266}267268if (nameEn.equals(nameLoc)) {269return new String[] {MessageFormat.format(notLocalizedMessage,270new String[] {locale.getDisplayName(), ISOCode}), ""};271}272273return new String[] {"", ""};274}275276private static boolean isTZIgnored(String TZName) {277if (TZName.startsWith("Etc/GMT") ||278TZName.indexOf("Riyadh8") != -1 ||279TZName.equals("GMT0") ||280TZName.equals("MET")281) {282return true;283}284return false;285}286287private static Map<String, String> getList(288ResourceBundle rs, Boolean getCountryList) {289char beginChar = 'a';290char endChar = 'z';291if (getCountryList) {292beginChar = 'A';293endChar = 'Z';294}295296Map<String, String> hm = new HashMap<String, String>();297Enumeration<String> keys = rs.getKeys();298while (keys.hasMoreElements()) {299String s = keys.nextElement();300if (s.length() == 2 &&301s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {302hm.put(s, rs.getString(s));303}304}305return hm;306}307}308309310