blob: 360d360f80087838cea2fb0dede370c4d5661d41 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4640234 4946057 4938151 4873691 5023181
27 * @summary Verifies the translation of time zone names, this test will catch presence
28 * of country name for english and selected locales for all ISO country
29 * codes.
30 * The test program also displays which timezone, country and language names
31 * are not translated
32 */
33
34
35/*
36 * 4946057 Localization for ISO country & language data.
37 * 4938151 Time zones not translated
38 * 4873691 Changes in TimeZone mapping
39 */
40
41import java.text.MessageFormat;
42import java.text.SimpleDateFormat;
43
44import 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;
51
52
53public class Bug4640234 {
54 static SimpleDateFormat sdfEn = new SimpleDateFormat("zzzz", Locale.US);
55 static SimpleDateFormat sdfEnShort = new SimpleDateFormat("z", Locale.US);
56 static Locale locEn = Locale.ENGLISH;
57
58 static SimpleDateFormat sdfLoc;
59 static SimpleDateFormat sdfLocShort;
60 static Date date = new Date();
61
62 // Define supported locales
63 static Locale[] locales2Test = new Locale[] {
64 new Locale("de"),
65 new Locale("es"),
66 new Locale("fr"),
67 new Locale("it"),
68 new Locale("ja"),
69 new Locale("ko"),
70 new Locale("sv"),
71 new Locale("zh", "CN"),
72 new Locale("zh", "TW")
73 };
74
75 public static void main(String[] args) throws Exception {
76 Locale.setDefault(Locale.ENGLISH);
77
78 StringBuffer errors = new StringBuffer("");
79 StringBuffer warnings = new StringBuffer("");
80
81 String[] timezones = TimeZone.getAvailableIDs();
82 String[] countries = locEn.getISOCountries();
83 String[] languages = locEn.getISOLanguages();
84
85 ResourceBundle resEn = ResourceBundle.getBundle("sun.util.resources.LocaleNames", locEn);
86 Map<String, String> countryMapEn = getList(resEn, true);
87 Map<String, String> languageMapEn = getList(resEn, false);
88
89 ResourceBundle resLoc;
90 Map<String, String> countryMap;
91 Map<String, String> languageMap;
92
93 for (Locale locale : locales2Test) {
94 resLoc = ResourceBundle.getBundle("sun.util.resources.LocaleNames", locale);
95
96 sdfLoc = new SimpleDateFormat("zzzz", locale);
97 sdfLocShort = new SimpleDateFormat("z", locale);
98
99 for (String timezone : timezones) {
100 if (isTZIgnored(timezone)) {
101 continue;
102 }
103 warnings.append(testTZ(timezone, locale));
104 }
105
106 countryMap = getList(resLoc, true);
107
108 for (String country : countries) {
109 String[] result = testEntry(country,
110 countryMapEn,
111 countryMap,
112 locale,
113 "ERROR: {0} country name for country code: {1} not found!\n",
114 "WARNING: {0} country name for country code: {1} not localized!\n"
115 );
116 if (warnings.indexOf(result[0]) == -1) {
117 warnings.append(result[0]);
118 }
119 if (errors.indexOf(result[1]) == -1) {
120 errors.append(result[1]);
121 }
122 }
123
124 languageMap = getList(resLoc, false);
125 for (String language : languages) {
126 String[] result = testEntry(language,
127 languageMapEn,
128 languageMap,
129 locale,
130 "ERROR: {0} language name for language code: {1} not found!\n",
131 "WARNING: {0} language name for language code: {1} not localized!\n");
132 if (warnings.indexOf(result[0]) == -1) {
133 warnings.append(result[0]);
134 }
135 if (errors.indexOf(result[1]) == -1) {
136 errors.append(result[1]);
137 }
138 }
139 }
140
141 StringBuffer message = new StringBuffer("");
142 if (!"".equals(errors.toString())) {
143 message.append("Test failed! ");
144 message.append("ERROR: some keys are missing! ");
145 }
146
147 if ("".equals(message.toString())) {
148 System.out.println("\nTest passed");
149 System.out.println(warnings.toString());
150 } else {
151 System.out.println("\nTest failed!");
152 System.out.println(errors.toString());
153 System.out.println(warnings.toString());
154 throw new Exception("\n" + message);
155 }
156 }
157
158 /**
159 * Compares the english timezone name and timezone name in specified locale
160 * @param timeZoneName - name of the timezone to compare
161 * @param locale - locale to test against english
162 * @return empty string when passed, descriptive error message in other cases
163 */
164 private static String testTZ(String timeZoneName, Locale locale) {
165 StringBuffer timeZoneResult = new StringBuffer("");
166 TimeZone tz = TimeZone.getTimeZone(timeZoneName);
167 sdfEn.setTimeZone(tz);
168 sdfEnShort.setTimeZone(tz);
169 sdfLoc.setTimeZone(tz);
170 sdfLocShort.setTimeZone(tz);
171
172 String en, enShort, loc, locShort;
173 en = sdfEn.format(date);
174 enShort = sdfEnShort.format(date);
175 loc = sdfLoc.format(date);
176 locShort = sdfLocShort.format(date);
177
178 String displayLanguage = locale.getDisplayLanguage();
179 String displayCountry = locale.getDisplayCountry();
180
181 if (loc.equals(en)) {
182 timeZoneResult.append("[");
183 timeZoneResult.append(displayLanguage);
184 if (!"".equals(displayCountry)) {
185 timeZoneResult.append(" ");
186 timeZoneResult.append(displayCountry);
187 }
188 timeZoneResult.append("] timezone \"");
189 timeZoneResult.append(timeZoneName);
190 timeZoneResult.append("\" long name \"" + en);
191 timeZoneResult.append("\" not localized!\n");
192 }
193
194 if (!locShort.equals(enShort)) {
195 timeZoneResult.append("[");
196 timeZoneResult.append(displayLanguage);
197 if (!"".equals(displayCountry)) {
198 timeZoneResult.append(" ");
199 timeZoneResult.append(displayCountry);
200 }
201 timeZoneResult.append("] timezone \"");
202 timeZoneResult.append(timeZoneName);
203 timeZoneResult.append("\" short name \"" + enShort);
204 timeZoneResult.append("\" is localized \"");
205 timeZoneResult.append(locShort);
206 timeZoneResult.append("\"!\n");
207 }
208 return timeZoneResult.toString();
209 }
210
211 /**
212 * Verifies whether the name for ISOCode is localized.
213 * @param ISOCode - ISO country/language code for country/language name to test
214 * @param entriesEn - array of english country/language names
215 * @param entriesLoc - array of localized country/language names for specified locale
216 * @param locale - locale to test against english
217 * @param notFoundMessage - message in form ready for MessageFormat, {0} will be human readable
218 * language name, {1} will be ISOCode.
219 * @param notLocalizedMessage - message in for ready for MessageFormat, same formatting like
220 * for notFountMessage
221 * @return array of two empty strings when passed, descriptive error message in
222 * other cases, [0] - warnings for not localized, [1] - errors for missing keys.
223 */
224 private static String[] testEntry(String ISOCode,
225 Map<String, String> entriesEn,
226 Map<String, String> entriesLoc,
227 Locale locale,
228 String notFoundMessage,
229 String notLocalizedMessage) {
230 String nameEn = null;
231 String nameLoc = null;
232
233 for (String key: entriesEn.keySet()) {
234 if (ISOCode.equalsIgnoreCase(key)) {
235 nameEn = entriesEn.get(key);
236 break;
237 }
238 }
239
240 for (String key: entriesLoc.keySet()) {
241 if (ISOCode.equalsIgnoreCase(key)) {
242 nameLoc = entriesLoc.get(key);
243 break;
244 }
245 }
246
247 if (nameEn == null) {
248 // We should not get here but test is a MUST have
249 return new String[] {"", MessageFormat.format(notFoundMessage,
250 new String[] {"English", ISOCode})};
251 }
252
253 if (nameLoc == null) {
254 return new String[] {"", MessageFormat.format(notFoundMessage,
255 new String[] {locale.getDisplayName(), ISOCode})};
256 }
257
258 if (nameEn.equals(nameLoc)) {
259 return new String[] {MessageFormat.format(notLocalizedMessage,
260 new String[] {locale.getDisplayName(), ISOCode}), ""};
261 }
262
263 return new String[] {"", ""};
264 }
265
266 private static boolean isTZIgnored(String TZName) {
267 if (TZName.startsWith("Etc/GMT") ||
268 TZName.indexOf("Riyadh8") != -1 ||
269 TZName.equals("GMT0") ||
270 TZName.equals("MET")
271 ) {
272 return true;
273 }
274 return false;
275 }
276
277 private static Map<String, String> getList(ResourceBundle rs, Boolean getCountryList) {
278 char beginChar = 'a';
279 char endChar = 'z';
280 if (getCountryList) {
281 beginChar = 'A';
282 endChar = 'Z';
283 }
284
285 Map<String, String> hm = new HashMap<String, String>();
286 Enumeration<String> keys = rs.getKeys();
287 while (keys.hasMoreElements()) {
288 String s = keys.nextElement();
289 if (s.length() == 2 &&
290 s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {
291 hm.put(s, rs.getString(s));
292 }
293 }
294 return hm;
295 }
296}