Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/resources/TimeZone/Bug4640234.java
38853 views
1
/*
2
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* 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
28
* presence of country name for english and selected locales for all
29
* ISO country codes.
30
* The test program also displays which timezone, country and
31
* language names 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
41
import java.text.MessageFormat;
42
import java.text.SimpleDateFormat;
43
44
import java.util.Date;
45
import java.util.Locale;
46
import java.util.Enumeration;
47
import java.util.HashMap;
48
import java.util.Map;
49
import java.util.ResourceBundle;
50
import java.util.TimeZone;
51
52
53
public 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 reservedLocale = Locale.getDefault();
77
try {
78
Locale.setDefault(Locale.ENGLISH);
79
80
StringBuffer errors = new StringBuffer("");
81
StringBuffer warnings = new StringBuffer("");
82
83
String[] timezones = TimeZone.getAvailableIDs();
84
String[] countries = locEn.getISOCountries();
85
String[] languages = locEn.getISOLanguages();
86
87
ResourceBundle resEn = ResourceBundle.getBundle(
88
"sun.util.resources.LocaleNames", locEn);
89
Map<String, String> countryMapEn = getList(resEn, true);
90
Map<String, String> languageMapEn = getList(resEn, false);
91
92
ResourceBundle resLoc;
93
Map<String, String> countryMap;
94
Map<String, String> languageMap;
95
96
for (Locale locale : locales2Test) {
97
resLoc = ResourceBundle.getBundle(
98
"sun.util.resources.LocaleNames", locale);
99
100
sdfLoc = new SimpleDateFormat("zzzz", locale);
101
sdfLocShort = new SimpleDateFormat("z", locale);
102
103
for (String timezone : timezones) {
104
if (isTZIgnored(timezone)) {
105
continue;
106
}
107
warnings.append(testTZ(timezone, locale));
108
}
109
110
countryMap = getList(resLoc, true);
111
112
for (String country : countries) {
113
String[] result = testEntry(country,
114
countryMapEn,
115
countryMap,
116
locale,
117
"ERROR: {0} country name for country code: {1} not found!\n",
118
"WARNING: {0} country name for country code: {1} not localized!\n"
119
);
120
if (warnings.indexOf(result[0]) == -1) {
121
warnings.append(result[0]);
122
}
123
if (errors.indexOf(result[1]) == -1) {
124
errors.append(result[1]);
125
}
126
}
127
128
languageMap = getList(resLoc, false);
129
for (String language : languages) {
130
String[] result = testEntry(language,
131
languageMapEn,
132
languageMap,
133
locale,
134
"ERROR: {0} language name for language code: {1} not found!\n",
135
"WARNING: {0} language name for language code: {1} not localized!\n");
136
if (warnings.indexOf(result[0]) == -1) {
137
warnings.append(result[0]);
138
}
139
if (errors.indexOf(result[1]) == -1) {
140
errors.append(result[1]);
141
}
142
}
143
}
144
145
StringBuffer message = new StringBuffer("");
146
if (!"".equals(errors.toString())) {
147
message.append("Test failed! ");
148
message.append("ERROR: some keys are missing! ");
149
}
150
151
if ("".equals(message.toString())) {
152
System.out.println("\nTest passed");
153
System.out.println(warnings.toString());
154
} else {
155
System.out.println("\nTest failed!");
156
System.out.println(errors.toString());
157
System.out.println(warnings.toString());
158
throw new Exception("\n" + message);
159
}
160
} finally {
161
// restore the reserved locale
162
Locale.setDefault(reservedLocale);
163
}
164
}
165
166
/**
167
* Compares the english timezone name and timezone name in specified locale
168
* @param timeZoneName - name of the timezone to compare
169
* @param locale - locale to test against english
170
* @return empty string when passed, descriptive error message in other cases
171
*/
172
private static String testTZ(String timeZoneName, Locale locale) {
173
StringBuffer timeZoneResult = new StringBuffer("");
174
TimeZone tz = TimeZone.getTimeZone(timeZoneName);
175
sdfEn.setTimeZone(tz);
176
sdfEnShort.setTimeZone(tz);
177
sdfLoc.setTimeZone(tz);
178
sdfLocShort.setTimeZone(tz);
179
180
String en, enShort, loc, locShort;
181
en = sdfEn.format(date);
182
enShort = sdfEnShort.format(date);
183
loc = sdfLoc.format(date);
184
locShort = sdfLocShort.format(date);
185
186
String displayLanguage = locale.getDisplayLanguage();
187
String displayCountry = locale.getDisplayCountry();
188
189
if (loc.equals(en)) {
190
timeZoneResult.append("[");
191
timeZoneResult.append(displayLanguage);
192
if (!"".equals(displayCountry)) {
193
timeZoneResult.append(" ");
194
timeZoneResult.append(displayCountry);
195
}
196
timeZoneResult.append("] timezone \"");
197
timeZoneResult.append(timeZoneName);
198
timeZoneResult.append("\" long name \"" + en);
199
timeZoneResult.append("\" not localized!\n");
200
}
201
202
if (!locShort.equals(enShort)) {
203
timeZoneResult.append("[");
204
timeZoneResult.append(displayLanguage);
205
if (!"".equals(displayCountry)) {
206
timeZoneResult.append(" ");
207
timeZoneResult.append(displayCountry);
208
}
209
timeZoneResult.append("] timezone \"");
210
timeZoneResult.append(timeZoneName);
211
timeZoneResult.append("\" short name \"" + enShort);
212
timeZoneResult.append("\" is localized \"");
213
timeZoneResult.append(locShort);
214
timeZoneResult.append("\"!\n");
215
}
216
return timeZoneResult.toString();
217
}
218
219
/**
220
* Verifies whether the name for ISOCode is localized.
221
* @param ISOCode - ISO country/language code for country/language name
222
* to test
223
* @param entriesEn - array of english country/language names
224
* @param entriesLoc - array of localized country/language names for
225
* specified locale
226
* @param locale - locale to test against english
227
* @param notFoundMessage - message in form ready for MessageFormat,
228
* {0} will be human readable language name, {1} will be ISOCode.
229
* @param notLocalizedMessage - message in for ready for MessageFormat,
230
* same formatting like for notFountMessage
231
* @return array of two empty strings when passed, descriptive error message
232
* in other cases, [0] - warnings for not localized, [1] - errors for
233
* missing keys.
234
*/
235
private static String[] testEntry(String ISOCode,
236
Map<String, String> entriesEn,
237
Map<String, String> entriesLoc,
238
Locale locale,
239
String notFoundMessage,
240
String notLocalizedMessage) {
241
String nameEn = null;
242
String nameLoc = null;
243
244
for (String key: entriesEn.keySet()) {
245
if (ISOCode.equalsIgnoreCase(key)) {
246
nameEn = entriesEn.get(key);
247
break;
248
}
249
}
250
251
for (String key: entriesLoc.keySet()) {
252
if (ISOCode.equalsIgnoreCase(key)) {
253
nameLoc = entriesLoc.get(key);
254
break;
255
}
256
}
257
258
if (nameEn == null) {
259
// We should not get here but test is a MUST have
260
return new String[] {"", MessageFormat.format(notFoundMessage,
261
new String[] {"English", ISOCode})};
262
}
263
264
if (nameLoc == null) {
265
return new String[] {"", MessageFormat.format(notFoundMessage,
266
new String[] {locale.getDisplayName(), ISOCode})};
267
}
268
269
if (nameEn.equals(nameLoc)) {
270
return new String[] {MessageFormat.format(notLocalizedMessage,
271
new String[] {locale.getDisplayName(), ISOCode}), ""};
272
}
273
274
return new String[] {"", ""};
275
}
276
277
private static boolean isTZIgnored(String TZName) {
278
if (TZName.startsWith("Etc/GMT") ||
279
TZName.indexOf("Riyadh8") != -1 ||
280
TZName.equals("GMT0") ||
281
TZName.equals("MET")
282
) {
283
return true;
284
}
285
return false;
286
}
287
288
private static Map<String, String> getList(
289
ResourceBundle rs, Boolean getCountryList) {
290
char beginChar = 'a';
291
char endChar = 'z';
292
if (getCountryList) {
293
beginChar = 'A';
294
endChar = 'Z';
295
}
296
297
Map<String, String> hm = new HashMap<String, String>();
298
Enumeration<String> keys = rs.getKeys();
299
while (keys.hasMoreElements()) {
300
String s = keys.nextElement();
301
if (s.length() == 2 &&
302
s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {
303
hm.put(s, rs.getString(s));
304
}
305
}
306
return hm;
307
}
308
}
309
310