0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4640234 4946057 4938151 4873691 5023181
0N/A * @summary Verifies the translation of time zone names, this test will catch presence
0N/A * of country name for english and selected locales for all ISO country
0N/A * codes.
0N/A * The test program also displays which timezone, country and language names
0N/A * are not translated
0N/A */
0N/A
0N/A
0N/A/*
0N/A * 4946057 Localization for ISO country & language data.
0N/A * 4938151 Time zones not translated
0N/A * 4873691 Changes in TimeZone mapping
0N/A */
0N/A
0N/Aimport java.text.MessageFormat;
0N/Aimport java.text.SimpleDateFormat;
0N/A
0N/Aimport java.util.Date;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.TimeZone;
0N/A
0N/A
0N/Apublic class Bug4640234 {
0N/A static SimpleDateFormat sdfEn = new SimpleDateFormat("zzzz", Locale.US);
0N/A static SimpleDateFormat sdfEnShort = new SimpleDateFormat("z", Locale.US);
0N/A static Locale locEn = Locale.ENGLISH;
0N/A
0N/A static SimpleDateFormat sdfLoc;
0N/A static SimpleDateFormat sdfLocShort;
0N/A static Date date = new Date();
0N/A
0N/A // Define supported locales
0N/A static Locale[] locales2Test = new Locale[] {
0N/A new Locale("de"),
0N/A new Locale("es"),
0N/A new Locale("fr"),
0N/A new Locale("it"),
0N/A new Locale("ja"),
0N/A new Locale("ko"),
0N/A new Locale("sv"),
0N/A new Locale("zh", "CN"),
0N/A new Locale("zh", "TW")
0N/A };
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A Locale.setDefault(Locale.ENGLISH);
0N/A
0N/A StringBuffer errors = new StringBuffer("");
0N/A StringBuffer warnings = new StringBuffer("");
0N/A
0N/A String[] timezones = TimeZone.getAvailableIDs();
0N/A String[] countries = locEn.getISOCountries();
0N/A String[] languages = locEn.getISOLanguages();
0N/A
0N/A ResourceBundle resEn = ResourceBundle.getBundle("sun.util.resources.LocaleNames", locEn);
0N/A Map<String, String> countryMapEn = getList(resEn, true);
0N/A Map<String, String> languageMapEn = getList(resEn, false);
0N/A
0N/A ResourceBundle resLoc;
0N/A Map<String, String> countryMap;
0N/A Map<String, String> languageMap;
0N/A
0N/A for (Locale locale : locales2Test) {
0N/A resLoc = ResourceBundle.getBundle("sun.util.resources.LocaleNames", locale);
0N/A
0N/A sdfLoc = new SimpleDateFormat("zzzz", locale);
0N/A sdfLocShort = new SimpleDateFormat("z", locale);
0N/A
0N/A for (String timezone : timezones) {
0N/A if (isTZIgnored(timezone)) {
0N/A continue;
0N/A }
0N/A warnings.append(testTZ(timezone, locale));
0N/A }
0N/A
0N/A countryMap = getList(resLoc, true);
0N/A
0N/A for (String country : countries) {
0N/A String[] result = testEntry(country,
0N/A countryMapEn,
0N/A countryMap,
0N/A locale,
0N/A "ERROR: {0} country name for country code: {1} not found!\n",
0N/A "WARNING: {0} country name for country code: {1} not localized!\n"
0N/A );
0N/A if (warnings.indexOf(result[0]) == -1) {
0N/A warnings.append(result[0]);
0N/A }
0N/A if (errors.indexOf(result[1]) == -1) {
0N/A errors.append(result[1]);
0N/A }
0N/A }
0N/A
0N/A languageMap = getList(resLoc, false);
0N/A for (String language : languages) {
0N/A String[] result = testEntry(language,
0N/A languageMapEn,
0N/A languageMap,
0N/A locale,
0N/A "ERROR: {0} language name for language code: {1} not found!\n",
0N/A "WARNING: {0} language name for language code: {1} not localized!\n");
0N/A if (warnings.indexOf(result[0]) == -1) {
0N/A warnings.append(result[0]);
0N/A }
0N/A if (errors.indexOf(result[1]) == -1) {
0N/A errors.append(result[1]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A StringBuffer message = new StringBuffer("");
0N/A if (!"".equals(errors.toString())) {
0N/A message.append("Test failed! ");
0N/A message.append("ERROR: some keys are missing! ");
0N/A }
0N/A
0N/A if ("".equals(message.toString())) {
0N/A System.out.println("\nTest passed");
0N/A System.out.println(warnings.toString());
0N/A } else {
0N/A System.out.println("\nTest failed!");
0N/A System.out.println(errors.toString());
0N/A System.out.println(warnings.toString());
0N/A throw new Exception("\n" + message);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Compares the english timezone name and timezone name in specified locale
0N/A * @param timeZoneName - name of the timezone to compare
0N/A * @param locale - locale to test against english
0N/A * @return empty string when passed, descriptive error message in other cases
0N/A */
0N/A private static String testTZ(String timeZoneName, Locale locale) {
0N/A StringBuffer timeZoneResult = new StringBuffer("");
0N/A TimeZone tz = TimeZone.getTimeZone(timeZoneName);
0N/A sdfEn.setTimeZone(tz);
0N/A sdfEnShort.setTimeZone(tz);
0N/A sdfLoc.setTimeZone(tz);
0N/A sdfLocShort.setTimeZone(tz);
0N/A
0N/A String en, enShort, loc, locShort;
0N/A en = sdfEn.format(date);
0N/A enShort = sdfEnShort.format(date);
0N/A loc = sdfLoc.format(date);
0N/A locShort = sdfLocShort.format(date);
0N/A
0N/A String displayLanguage = locale.getDisplayLanguage();
0N/A String displayCountry = locale.getDisplayCountry();
0N/A
0N/A if (loc.equals(en)) {
0N/A timeZoneResult.append("[");
0N/A timeZoneResult.append(displayLanguage);
0N/A if (!"".equals(displayCountry)) {
0N/A timeZoneResult.append(" ");
0N/A timeZoneResult.append(displayCountry);
0N/A }
0N/A timeZoneResult.append("] timezone \"");
0N/A timeZoneResult.append(timeZoneName);
0N/A timeZoneResult.append("\" long name \"" + en);
0N/A timeZoneResult.append("\" not localized!\n");
0N/A }
0N/A
0N/A if (!locShort.equals(enShort)) {
0N/A timeZoneResult.append("[");
0N/A timeZoneResult.append(displayLanguage);
0N/A if (!"".equals(displayCountry)) {
0N/A timeZoneResult.append(" ");
0N/A timeZoneResult.append(displayCountry);
0N/A }
0N/A timeZoneResult.append("] timezone \"");
0N/A timeZoneResult.append(timeZoneName);
0N/A timeZoneResult.append("\" short name \"" + enShort);
0N/A timeZoneResult.append("\" is localized \"");
0N/A timeZoneResult.append(locShort);
0N/A timeZoneResult.append("\"!\n");
0N/A }
0N/A return timeZoneResult.toString();
0N/A }
0N/A
0N/A /**
0N/A * Verifies whether the name for ISOCode is localized.
0N/A * @param ISOCode - ISO country/language code for country/language name to test
0N/A * @param entriesEn - array of english country/language names
0N/A * @param entriesLoc - array of localized country/language names for specified locale
0N/A * @param locale - locale to test against english
0N/A * @param notFoundMessage - message in form ready for MessageFormat, {0} will be human readable
0N/A * language name, {1} will be ISOCode.
0N/A * @param notLocalizedMessage - message in for ready for MessageFormat, same formatting like
0N/A * for notFountMessage
0N/A * @return array of two empty strings when passed, descriptive error message in
0N/A * other cases, [0] - warnings for not localized, [1] - errors for missing keys.
0N/A */
0N/A private static String[] testEntry(String ISOCode,
0N/A Map<String, String> entriesEn,
0N/A Map<String, String> entriesLoc,
0N/A Locale locale,
0N/A String notFoundMessage,
0N/A String notLocalizedMessage) {
0N/A String nameEn = null;
0N/A String nameLoc = null;
0N/A
0N/A for (String key: entriesEn.keySet()) {
0N/A if (ISOCode.equalsIgnoreCase(key)) {
0N/A nameEn = entriesEn.get(key);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A for (String key: entriesLoc.keySet()) {
0N/A if (ISOCode.equalsIgnoreCase(key)) {
0N/A nameLoc = entriesLoc.get(key);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (nameEn == null) {
0N/A // We should not get here but test is a MUST have
0N/A return new String[] {"", MessageFormat.format(notFoundMessage,
0N/A new String[] {"English", ISOCode})};
0N/A }
0N/A
0N/A if (nameLoc == null) {
0N/A return new String[] {"", MessageFormat.format(notFoundMessage,
0N/A new String[] {locale.getDisplayName(), ISOCode})};
0N/A }
0N/A
0N/A if (nameEn.equals(nameLoc)) {
0N/A return new String[] {MessageFormat.format(notLocalizedMessage,
0N/A new String[] {locale.getDisplayName(), ISOCode}), ""};
0N/A }
0N/A
0N/A return new String[] {"", ""};
0N/A }
0N/A
0N/A private static boolean isTZIgnored(String TZName) {
0N/A if (TZName.startsWith("Etc/GMT") ||
0N/A TZName.indexOf("Riyadh8") != -1 ||
0N/A TZName.equals("GMT0") ||
0N/A TZName.equals("MET")
0N/A ) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A private static Map<String, String> getList(ResourceBundle rs, Boolean getCountryList) {
0N/A char beginChar = 'a';
0N/A char endChar = 'z';
0N/A if (getCountryList) {
0N/A beginChar = 'A';
0N/A endChar = 'Z';
0N/A }
0N/A
0N/A Map<String, String> hm = new HashMap<String, String>();
0N/A Enumeration<String> keys = rs.getKeys();
0N/A while (keys.hasMoreElements()) {
0N/A String s = keys.nextElement();
0N/A if (s.length() == 2 &&
0N/A s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {
0N/A hm.put(s, rs.getString(s));
0N/A }
0N/A }
0N/A return hm;
0N/A }
0N/A}