aboutsummaryrefslogtreecommitdiff
path: root/test/sun/util/resources/TimeZone/Bug4640234.java
blob: a8766ea1224fc25d6cbab2c07ca8ded76d589741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 4640234 4946057 4938151 4873691 5023181
 * @summary Verifies the translation of time zone names, this test will catch
 *          presence of country name for english and selected locales for all
 *          ISO country codes.
 *          The test program also displays which timezone, country and
 *          language names are not translated
 */


/*
 * 4946057 Localization for ISO country & language data.
 * 4938151 Time zones not translated
 * 4873691 Changes in TimeZone mapping
 */

import java.text.MessageFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TimeZone;


public class Bug4640234  {
    static SimpleDateFormat sdfEn = new SimpleDateFormat("zzzz", Locale.US);
    static SimpleDateFormat sdfEnShort = new SimpleDateFormat("z", Locale.US);
    static Locale locEn = Locale.ENGLISH;

    static SimpleDateFormat sdfLoc;
    static SimpleDateFormat sdfLocShort;
    static Date date = new Date();

    // Define supported locales
    static Locale[] locales2Test = new Locale[] {
        new Locale("de"),
        new Locale("es"),
        new Locale("fr"),
        new Locale("it"),
        new Locale("ja"),
        new Locale("ko"),
        new Locale("sv"),
        new Locale("zh", "CN"),
        new Locale("zh", "TW")
    };

    public static void main(String[] args) throws Exception {
        Locale reservedLocale = Locale.getDefault();
        try {
            Locale.setDefault(Locale.ENGLISH);

            StringBuffer errors = new StringBuffer("");
            StringBuffer warnings = new StringBuffer("");

            String[] timezones = TimeZone.getAvailableIDs();
            String[] countries = locEn.getISOCountries();
            String[] languages = locEn.getISOLanguages();

            ResourceBundle resEn = ResourceBundle.getBundle(
                    "sun.util.resources.LocaleNames", locEn);
            Map<String, String> countryMapEn = getList(resEn, true);
            Map<String, String> languageMapEn = getList(resEn, false);

            ResourceBundle resLoc;
            Map<String, String> countryMap;
            Map<String, String> languageMap;

            for (Locale locale : locales2Test) {
                resLoc = ResourceBundle.getBundle(
                    "sun.util.resources.LocaleNames", locale);

                sdfLoc = new SimpleDateFormat("zzzz", locale);
                sdfLocShort = new SimpleDateFormat("z", locale);

                for (String timezone : timezones) {
                    if (isTZIgnored(timezone)) {
                        continue;
                    }
                    warnings.append(testTZ(timezone, locale));
                }

                countryMap = getList(resLoc, true);

                for (String country : countries) {
                    String[] result = testEntry(country,
                        countryMapEn,
                        countryMap,
                        locale,
                        "ERROR: {0} country name for country code: {1} not found!\n",
                        "WARNING: {0} country name for country code: {1} not localized!\n"
                    );
                    if (warnings.indexOf(result[0]) == -1) {
                        warnings.append(result[0]);
                    }
                    if (errors.indexOf(result[1]) == -1) {
                        errors.append(result[1]);
                    }
                }

                languageMap = getList(resLoc, false);
                for (String language : languages) {
                    String[] result = testEntry(language,
                        languageMapEn,
                        languageMap,
                        locale,
                        "ERROR: {0} language name for language code: {1} not found!\n",
                        "WARNING: {0} language name for language code: {1} not localized!\n");
                    if (warnings.indexOf(result[0]) == -1) {
                        warnings.append(result[0]);
                    }
                    if (errors.indexOf(result[1]) == -1) {
                        errors.append(result[1]);
                    }
                }
            }

            StringBuffer message = new StringBuffer("");
            if (!"".equals(errors.toString())) {
                message.append("Test failed! ");
                message.append("ERROR: some keys are missing! ");
            }

            if ("".equals(message.toString())) {
                System.out.println("\nTest passed");
                System.out.println(warnings.toString());
            } else {
                System.out.println("\nTest failed!");
                System.out.println(errors.toString());
                System.out.println(warnings.toString());
                throw new Exception("\n" + message);
            }
        } finally {
            // restore the reserved locale
            Locale.setDefault(reservedLocale);
        }
    }

    /**
    * Compares the english timezone name and timezone name in specified locale
    * @param timeZoneName - name of the timezone to compare
    * @param locale - locale to test against english
    * @return empty string when passed, descriptive error message in other cases
    */
    private static String testTZ(String timeZoneName, Locale locale) {
        StringBuffer timeZoneResult = new StringBuffer("");
        TimeZone tz = TimeZone.getTimeZone(timeZoneName);
        sdfEn.setTimeZone(tz);
        sdfEnShort.setTimeZone(tz);
        sdfLoc.setTimeZone(tz);
        sdfLocShort.setTimeZone(tz);

        String en, enShort, loc, locShort;
        en = sdfEn.format(date);
        enShort = sdfEnShort.format(date);
        loc = sdfLoc.format(date);
        locShort = sdfLocShort.format(date);

        String displayLanguage = locale.getDisplayLanguage();
        String displayCountry = locale.getDisplayCountry();

        if (loc.equals(en)) {
            timeZoneResult.append("[");
            timeZoneResult.append(displayLanguage);
            if (!"".equals(displayCountry)) {
                timeZoneResult.append(" ");
                timeZoneResult.append(displayCountry);
            }
            timeZoneResult.append("] timezone \"");
            timeZoneResult.append(timeZoneName);
            timeZoneResult.append("\" long name \"" + en);
            timeZoneResult.append("\" not localized!\n");
        }

        if (!locShort.equals(enShort)) {
            timeZoneResult.append("[");
            timeZoneResult.append(displayLanguage);
            if (!"".equals(displayCountry)) {
                timeZoneResult.append(" ");
                timeZoneResult.append(displayCountry);
            }
            timeZoneResult.append("] timezone \"");
            timeZoneResult.append(timeZoneName);
            timeZoneResult.append("\" short name \"" + enShort);
            timeZoneResult.append("\" is localized \"");
            timeZoneResult.append(locShort);
            timeZoneResult.append("\"!\n");
        }
        return timeZoneResult.toString();
    }

    /**
    * Verifies whether the name for ISOCode is localized.
    * @param ISOCode - ISO country/language code for country/language name
    *      to test
    * @param entriesEn - array of english country/language names
    * @param entriesLoc - array of localized country/language names for
    *      specified locale
    * @param locale - locale to test against english
    * @param notFoundMessage - message in form ready for MessageFormat,
    *      {0} will be human readable language name, {1} will be ISOCode.
    * @param notLocalizedMessage - message in for ready for MessageFormat,
    *      same formatting like for notFountMessage
    * @return array of two empty strings when passed, descriptive error message
    *      in other cases, [0] - warnings for not localized, [1] - errors for
    *      missing keys.
    */
    private static String[] testEntry(String ISOCode,
                Map<String, String> entriesEn,
                Map<String, String> entriesLoc,
                Locale locale,
                String notFoundMessage,
                String notLocalizedMessage) {
        String nameEn = null;
        String nameLoc = null;

        for (String key: entriesEn.keySet()) {
            if (ISOCode.equalsIgnoreCase(key)) {
                nameEn = entriesEn.get(key);
                break;
            }
        }

        for (String key: entriesLoc.keySet()) {
            if (ISOCode.equalsIgnoreCase(key)) {
                nameLoc = entriesLoc.get(key);
                break;
            }
        }

        if (nameEn == null) {
            // We should not get here but test is a MUST have
            return new String[] {"", MessageFormat.format(notFoundMessage,
                new String[] {"English", ISOCode})};
        }

        if (nameLoc == null) {
            return new String[] {"", MessageFormat.format(notFoundMessage,
                new String[] {locale.getDisplayName(), ISOCode})};
        }

        if (nameEn.equals(nameLoc)) {
            return new String[] {MessageFormat.format(notLocalizedMessage,
                new String[] {locale.getDisplayName(), ISOCode}), ""};
        }

        return new String[] {"", ""};
    }

    private static boolean isTZIgnored(String TZName) {
        if (TZName.startsWith("Etc/GMT") ||
                TZName.indexOf("Riyadh8") != -1 ||
                TZName.equals("GMT0") ||
                TZName.equals("MET")
                ) {
            return true;
        }
        return false;
    }

    private static Map<String, String> getList(
            ResourceBundle rs, Boolean getCountryList) {
        char beginChar = 'a';
        char endChar = 'z';
        if (getCountryList) {
            beginChar = 'A';
            endChar = 'Z';
        }

        Map<String, String> hm = new HashMap<String, String>();
        Enumeration<String> keys = rs.getKeys();
        while (keys.hasMoreElements()) {
            String s = keys.nextElement();
            if (s.length() == 2 &&
                s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {
                hm.put(s, rs.getString(s));
            }
        }
        return hm;
    }
}