Path: blob/master/test/jdk/sun/util/resources/TimeZone/Bug4640234.java
41154 views
/*1* Copyright (c) 2007, 2015, 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* @modules java.base/sun.util.resources32*/333435/*36* 4946057 Localization for ISO country & language data.37* 4938151 Time zones not translated38* 4873691 Changes in TimeZone mapping39*/4041import java.text.MessageFormat;42import java.text.SimpleDateFormat;4344import java.util.Date;45import java.util.Locale;46import java.util.Enumeration;47import java.util.HashMap;48import java.util.Map;49import java.util.ResourceBundle;50import java.util.TimeZone;5152import sun.util.resources.LocaleData;5354public class Bug4640234 {55static SimpleDateFormat sdfEn = new SimpleDateFormat("zzzz", Locale.US);56static SimpleDateFormat sdfEnShort = new SimpleDateFormat("z", Locale.US);57static Locale locEn = Locale.ENGLISH;5859static SimpleDateFormat sdfLoc;60static SimpleDateFormat sdfLocShort;61static Date date = new Date();6263// Define supported locales64static Locale[] locales2Test = new Locale[] {65new Locale("de"),66new Locale("es"),67new Locale("fr"),68new Locale("it"),69new Locale("ja"),70new Locale("ko"),71new Locale("sv"),72new Locale("zh", "CN"),73new Locale("zh", "TW")74};7576public static void main(String[] args) throws Exception {77Locale reservedLocale = Locale.getDefault();78try {79Locale.setDefault(Locale.ENGLISH);8081StringBuffer errors = new StringBuffer("");82StringBuffer warnings = new StringBuffer("");8384String[] timezones = TimeZone.getAvailableIDs();85String[] countries = locEn.getISOCountries();86String[] languages = locEn.getISOLanguages();8788ResourceBundle resEn = LocaleData.getBundle("sun.util.resources.LocaleNames",89locEn);90Map<String, String> countryMapEn = getList(resEn, true);91Map<String, String> languageMapEn = getList(resEn, false);9293ResourceBundle resLoc;94Map<String, String> countryMap;95Map<String, String> languageMap;9697for (Locale locale : locales2Test) {98resLoc = LocaleData.getBundle("sun.util.resources.LocaleNames",99locale);100101sdfLoc = new SimpleDateFormat("zzzz", locale);102sdfLocShort = new SimpleDateFormat("z", locale);103104for (String timezone : timezones) {105if (isTZIgnored(timezone)) {106continue;107}108warnings.append(testTZ(timezone, locale));109}110111countryMap = getList(resLoc, true);112113for (String country : countries) {114String[] result = testEntry(country,115countryMapEn,116countryMap,117locale,118"ERROR: {0} country name for country code: {1} not found!\n",119"WARNING: {0} country name for country code: {1} not localized!\n"120);121if (warnings.indexOf(result[0]) == -1) {122warnings.append(result[0]);123}124if (errors.indexOf(result[1]) == -1) {125errors.append(result[1]);126}127}128129languageMap = getList(resLoc, false);130for (String language : languages) {131String[] result = testEntry(language,132languageMapEn,133languageMap,134locale,135"ERROR: {0} language name for language code: {1} not found!\n",136"WARNING: {0} language name for language code: {1} not localized!\n");137if (warnings.indexOf(result[0]) == -1) {138warnings.append(result[0]);139}140if (errors.indexOf(result[1]) == -1) {141errors.append(result[1]);142}143}144}145146StringBuffer message = new StringBuffer("");147if (!"".equals(errors.toString())) {148message.append("Test failed! ");149message.append("ERROR: some keys are missing! ");150}151152if ("".equals(message.toString())) {153System.out.println("\nTest passed");154System.out.println(warnings.toString());155} else {156System.out.println("\nTest failed!");157System.out.println(errors.toString());158System.out.println(warnings.toString());159throw new Exception("\n" + message);160}161} finally {162// restore the reserved locale163Locale.setDefault(reservedLocale);164}165}166167/**168* Compares the english timezone name and timezone name in specified locale169* @param timeZoneName - name of the timezone to compare170* @param locale - locale to test against english171* @return empty string when passed, descriptive error message in other cases172*/173private static String testTZ(String timeZoneName, Locale locale) {174StringBuffer timeZoneResult = new StringBuffer("");175TimeZone tz = TimeZone.getTimeZone(timeZoneName);176sdfEn.setTimeZone(tz);177sdfEnShort.setTimeZone(tz);178sdfLoc.setTimeZone(tz);179sdfLocShort.setTimeZone(tz);180181String en, enShort, loc, locShort;182en = sdfEn.format(date);183enShort = sdfEnShort.format(date);184loc = sdfLoc.format(date);185locShort = sdfLocShort.format(date);186187String displayLanguage = locale.getDisplayLanguage();188String displayCountry = locale.getDisplayCountry();189190if (loc.equals(en)) {191timeZoneResult.append("[");192timeZoneResult.append(displayLanguage);193if (!"".equals(displayCountry)) {194timeZoneResult.append(" ");195timeZoneResult.append(displayCountry);196}197timeZoneResult.append("] timezone \"");198timeZoneResult.append(timeZoneName);199timeZoneResult.append("\" long name \"" + en);200timeZoneResult.append("\" not localized!\n");201}202203if (!locShort.equals(enShort)) {204timeZoneResult.append("[");205timeZoneResult.append(displayLanguage);206if (!"".equals(displayCountry)) {207timeZoneResult.append(" ");208timeZoneResult.append(displayCountry);209}210timeZoneResult.append("] timezone \"");211timeZoneResult.append(timeZoneName);212timeZoneResult.append("\" short name \"" + enShort);213timeZoneResult.append("\" is localized \"");214timeZoneResult.append(locShort);215timeZoneResult.append("\"!\n");216}217return timeZoneResult.toString();218}219220/**221* Verifies whether the name for ISOCode is localized.222* @param ISOCode - ISO country/language code for country/language name223* to test224* @param entriesEn - array of english country/language names225* @param entriesLoc - array of localized country/language names for226* specified locale227* @param locale - locale to test against english228* @param notFoundMessage - message in form ready for MessageFormat,229* {0} will be human readable language name, {1} will be ISOCode.230* @param notLocalizedMessage - message in for ready for MessageFormat,231* same formatting like for notFountMessage232* @return array of two empty strings when passed, descriptive error message233* in other cases, [0] - warnings for not localized, [1] - errors for234* missing keys.235*/236private static String[] testEntry(String ISOCode,237Map<String, String> entriesEn,238Map<String, String> entriesLoc,239Locale locale,240String notFoundMessage,241String notLocalizedMessage) {242String nameEn = null;243String nameLoc = null;244245for (String key: entriesEn.keySet()) {246if (ISOCode.equalsIgnoreCase(key)) {247nameEn = entriesEn.get(key);248break;249}250}251252for (String key: entriesLoc.keySet()) {253if (ISOCode.equalsIgnoreCase(key)) {254nameLoc = entriesLoc.get(key);255break;256}257}258259if (nameEn == null) {260// We should not get here but test is a MUST have261return new String[] {"",262MessageFormat.format(notFoundMessage, "English", ISOCode)};263}264265if (nameLoc == null) {266return new String[] {"",267MessageFormat.format(notFoundMessage,268locale.getDisplayName(), ISOCode)};269}270271if (nameEn.equals(nameLoc)) {272return new String[] {MessageFormat.format(notLocalizedMessage,273locale.getDisplayName(), ISOCode),274""};275}276277return new String[] {"", ""};278}279280private static boolean isTZIgnored(String TZName) {281if (TZName.startsWith("Etc/GMT") ||282TZName.indexOf("Riyadh8") != -1 ||283TZName.equals("GMT0") ||284TZName.equals("MET")285) {286return true;287}288return false;289}290291private static Map<String, String> getList(292ResourceBundle rs, Boolean getCountryList) {293char beginChar = 'a';294char endChar = 'z';295if (getCountryList) {296beginChar = 'A';297endChar = 'Z';298}299300Map<String, String> hm = new HashMap<String, String>();301Enumeration<String> keys = rs.getKeys();302while (keys.hasMoreElements()) {303String s = keys.nextElement();304if (s.length() == 2 &&305s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {306hm.put(s, rs.getString(s));307}308}309return hm;310}311}312313314