Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Locale/LocaleProviders.java
41149 views
1
/*
2
* Copyright (c) 2012, 2021, 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
import java.text.*;
24
import java.text.spi.*;
25
import java.time.LocalDate;
26
import java.time.ZoneId;
27
import java.time.ZonedDateTime;
28
import java.time.format.DateTimeFormatter;
29
import java.time.format.FormatStyle;
30
import java.time.temporal.WeekFields;
31
import java.util.*;
32
import java.util.concurrent.CopyOnWriteArrayList;
33
import java.util.logging.Level;
34
import java.util.logging.LogRecord;
35
import java.util.logging.StreamHandler;
36
import java.util.spi.*;
37
import java.util.stream.IntStream;
38
import sun.util.locale.provider.LocaleProviderAdapter;
39
40
import static java.util.logging.LogManager.*;
41
42
public class LocaleProviders {
43
44
private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
45
private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");
46
47
public static void main(String[] args) {
48
String methodName = args[0];
49
50
switch (methodName) {
51
case "getPlatformLocale":
52
if (args[1].equals("format")) {
53
getPlatformLocale(Locale.Category.FORMAT);
54
} else {
55
getPlatformLocale(Locale.Category.DISPLAY);
56
}
57
break;
58
59
case "adapterTest":
60
adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
61
break;
62
63
case "bug7198834Test":
64
bug7198834Test();
65
break;
66
67
case "tzNameTest":
68
tzNameTest(args[1]);
69
break;
70
71
case "bug8001440Test":
72
bug8001440Test();
73
break;
74
75
case "bug8010666Test":
76
bug8010666Test();
77
break;
78
79
case "bug8013086Test":
80
bug8013086Test(args[1], args[2]);
81
break;
82
83
case "bug8013903Test":
84
bug8013903Test();
85
break;
86
87
case "bug8027289Test":
88
bug8027289Test(args[1]);
89
break;
90
91
case "bug8220227Test":
92
bug8220227Test();
93
break;
94
95
case "bug8228465Test":
96
bug8228465Test();
97
break;
98
99
case "bug8232871Test":
100
bug8232871Test();
101
break;
102
103
case "bug8232860Test":
104
bug8232860Test();
105
break;
106
107
case "bug8245241Test":
108
bug8245241Test(args[1]);
109
break;
110
111
case "bug8248695Test":
112
bug8248695Test();
113
break;
114
115
case "bug8257964Test":
116
bug8257964Test();
117
break;
118
119
default:
120
throw new RuntimeException("Test method '"+methodName+"' not found.");
121
}
122
}
123
124
static void getPlatformLocale(Locale.Category cat) {
125
Locale defloc = Locale.getDefault(cat);
126
System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
127
}
128
129
static void adapterTest(String expected, String lang, String ctry) {
130
Locale testLocale = new Locale(lang, ctry);
131
LocaleProviderAdapter ldaExpected =
132
LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
133
if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
134
System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
135
return;
136
}
137
String preference = System.getProperty("java.locale.providers", "");
138
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
139
LocaleProviderAdapter.Type type = lda.getAdapterType();
140
System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
141
if (!type.toString().equals(expected)) {
142
throw new RuntimeException("Returned locale data adapter is not correct.");
143
}
144
}
145
146
static void bug7198834Test() {
147
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
148
LocaleProviderAdapter.Type type = lda.getAdapterType();
149
if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
150
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
151
String date = df.format(new Date());
152
if (date.charAt(date.length()-1) == ' ') {
153
throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
154
}
155
} else {
156
System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
157
}
158
}
159
160
static void tzNameTest(String id) {
161
TimeZone tz = TimeZone.getTimeZone(id);
162
String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
163
if (tzName.startsWith("GMT")) {
164
throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
165
}
166
}
167
168
static void bug8001440Test() {
169
Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
170
NumberFormat nf = NumberFormat.getInstance(locale);
171
String nu = nf.format(1234560);
172
}
173
174
// This test assumes Windows localized language/country display names.
175
static void bug8010666Test() {
176
if (IS_WINDOWS) {
177
NumberFormat nf = NumberFormat.getInstance(Locale.US);
178
try {
179
double ver = nf.parse(System.getProperty("os.version"))
180
.doubleValue();
181
System.out.printf("Windows version: %.1f\n", ver);
182
if (ver >= 6.0) {
183
LocaleProviderAdapter lda =
184
LocaleProviderAdapter.getAdapter(
185
LocaleNameProvider.class, Locale.ENGLISH);
186
LocaleProviderAdapter.Type type = lda.getAdapterType();
187
if (type == LocaleProviderAdapter.Type.HOST) {
188
LocaleNameProvider lnp = lda.getLocaleNameProvider();
189
Locale mkmk = Locale.forLanguageTag("mk-MK");
190
String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
191
String hostResult =
192
lnp.getDisplayLanguage(mkmk.getLanguage(),
193
Locale.ENGLISH);
194
System.out.printf(" Display language name for" +
195
" (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
196
hostResult, result);
197
if (result == null ||
198
hostResult != null &&
199
!result.equals(hostResult)) {
200
throw new RuntimeException("Display language name" +
201
" mismatch for \"mk\". Returned name was" +
202
" \"" + result + "\", result(HOST): \"" +
203
hostResult + "\"");
204
}
205
result = Locale.US.getDisplayLanguage(Locale.ENGLISH);
206
hostResult =
207
lnp.getDisplayLanguage(Locale.US.getLanguage(),
208
Locale.ENGLISH);
209
System.out.printf(" Display language name for" +
210
" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
211
hostResult, result);
212
if (result == null ||
213
hostResult != null &&
214
!result.equals(hostResult)) {
215
throw new RuntimeException("Display language name" +
216
" mismatch for \"en\". Returned name was" +
217
" \"" + result + "\", result(HOST): \"" +
218
hostResult + "\"");
219
}
220
if (ver >= 6.1) {
221
result = Locale.US.getDisplayCountry(Locale.ENGLISH);
222
hostResult = lnp.getDisplayCountry(
223
Locale.US.getCountry(), Locale.ENGLISH);
224
System.out.printf(" Display country name for" +
225
" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
226
hostResult, result);
227
if (result == null ||
228
hostResult != null &&
229
!result.equals(hostResult)) {
230
throw new RuntimeException("Display country name" +
231
" mismatch for \"US\". Returned name was" +
232
" \"" + result + "\", result(HOST): \"" +
233
hostResult + "\"");
234
}
235
}
236
} else {
237
throw new RuntimeException("Windows Host" +
238
" LocaleProviderAdapter was not selected for" +
239
" English locale.");
240
}
241
}
242
} catch (ParseException pe) {
243
throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
244
}
245
}
246
}
247
248
static void bug8013086Test(String lang, String ctry) {
249
try {
250
// Throws a NullPointerException if the test fails.
251
System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
252
} catch (ParseException pe) {
253
// ParseException is fine in this test, as it's not "UTC"
254
}
255
}
256
257
static void bug8013903Test() {
258
if (IS_WINDOWS) {
259
Date sampleDate = new Date(0x10000000000L);
260
String expected = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
261
Locale l = new Locale("ja", "JP", "JP");
262
SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
263
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
264
String result = sdf.format(sampleDate);
265
System.out.println(result);
266
if (LocaleProviderAdapter.getAdapterPreference()
267
.contains(LocaleProviderAdapter.Type.JRE)) {
268
if (!expected.equals(result)) {
269
throw new RuntimeException("Format failed. result: \"" +
270
result + "\", expected: \"" + expected);
271
}
272
} else {
273
// Windows display names. Subject to change if Windows changes its format.
274
if (!expected.equals(result)) {
275
throw new RuntimeException("Format failed. result: \"" +
276
result + "\", expected: \"" + expected);
277
}
278
}
279
}
280
}
281
282
static void bug8027289Test(String expectedCodePoint) {
283
if (IS_WINDOWS) {
284
char[] expectedSymbol = Character.toChars(Integer.valueOf(expectedCodePoint, 16));
285
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
286
char formatted = nf.format(7000).charAt(0);
287
System.out.println("returned: " + formatted + ", expected: " + expectedSymbol[0]);
288
if (formatted != expectedSymbol[0]) {
289
throw new RuntimeException(
290
"Unexpected Chinese currency symbol. returned: "
291
+ formatted + ", expected: " + expectedSymbol[0]);
292
}
293
}
294
}
295
296
static void bug8220227Test() {
297
if (IS_WINDOWS) {
298
Locale l = new Locale("xx","XX");
299
String country = l.getDisplayCountry();
300
if (country.endsWith("(XX)")) {
301
throw new RuntimeException(
302
"Unexpected Region name: " + country);
303
}
304
}
305
}
306
307
static void bug8228465Test() {
308
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US);
309
LocaleProviderAdapter.Type type = lda.getAdapterType();
310
if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
311
var names = new GregorianCalendar()
312
.getDisplayNames(Calendar.ERA, Calendar.SHORT_FORMAT, Locale.US);
313
if (!names.keySet().contains("AD") ||
314
names.get("AD").intValue() != 1) {
315
throw new RuntimeException(
316
"Short Era name for 'AD' is missing or incorrect");
317
} else {
318
System.out.println("bug8228465Test succeeded.");
319
}
320
}
321
}
322
323
static void bug8232871Test() {
324
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US);
325
LocaleProviderAdapter.Type type = lda.getAdapterType();
326
var lang = Locale.getDefault().getLanguage();
327
var cal = Calendar.getInstance();
328
var calType = cal.getCalendarType();
329
var expected = "\u4ee4\u548c1\u5e745\u67081\u65e5 \u6c34\u66dc\u65e5 \u5348\u524d0:00:00 \u30a2\u30e1\u30ea\u30ab\u592a\u5e73\u6d0b\u590f\u6642\u9593";
330
331
if (type == LocaleProviderAdapter.Type.HOST &&
332
IS_MAC &&
333
lang.equals("ja") &&
334
calType.equals("japanese")) {
335
cal.set(1, 4, 1, 0, 0, 0);
336
cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
337
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
338
Locale.JAPAN);
339
df.setCalendar(cal);
340
var result = df.format(cal.getTime());
341
if (result.equals(expected)) {
342
System.out.println("bug8232871Test succeeded.");
343
} else {
344
throw new RuntimeException(
345
"Japanese calendar names mismatch. result: " +
346
result +
347
", expected: " +
348
expected);
349
}
350
} else {
351
System.out.println("Test ignored. Either :-\n" +
352
"OS is not macOS, or\n" +
353
"provider is not HOST: " + type + ", or\n" +
354
"Language is not Japanese: " + lang + ", or\n" +
355
"native calendar is not JapaneseCalendar: " + calType);
356
}
357
}
358
359
static void bug8232860Test() {
360
var inputList = List.of(123, 123.4);
361
var nfExpectedList = List.of("123", "123.4");
362
var ifExpectedList = List.of("123", "123");
363
364
var defLoc = Locale.getDefault(Locale.Category.FORMAT);
365
var type = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US)
366
.getAdapterType();
367
if (defLoc.equals(Locale.US) &&
368
type == LocaleProviderAdapter.Type.HOST &&
369
(IS_WINDOWS || IS_MAC)) {
370
final var numf = NumberFormat.getNumberInstance(Locale.US);
371
final var intf = NumberFormat.getIntegerInstance(Locale.US);
372
373
IntStream.range(0, inputList.size())
374
.forEach(i -> {
375
var input = inputList.get(i);
376
var nfExpected = nfExpectedList.get(i);
377
var result = numf.format(input);
378
if (!result.equals(nfExpected)) {
379
throw new RuntimeException("Incorrect number format. " +
380
"input: " + input + ", expected: " +
381
nfExpected + ", result: " + result);
382
}
383
384
var ifExpected = ifExpectedList.get(i);
385
result = intf.format(input);
386
if (!result.equals(ifExpected)) {
387
throw new RuntimeException("Incorrect integer format. " +
388
"input: " + input + ", expected: " +
389
ifExpected + ", result: " + result);
390
}
391
});
392
System.out.println("bug8232860Test succeeded.");
393
} else {
394
System.out.println("Test ignored. Either :-\n" +
395
"Default format locale is not Locale.US: " + defLoc + ", or\n" +
396
"OS is neither macOS/Windows, or\n" +
397
"provider is not HOST: " + type);
398
}
399
}
400
401
static void bug8245241Test(String expected) {
402
// this will ensure LocaleProviderAdapter initialization
403
DateFormat.getDateInstance();
404
LogConfig.handler.flush();
405
406
if (LogConfig.logRecordList.stream()
407
.noneMatch(r -> r.getLevel() == Level.INFO &&
408
r.getMessage().equals(expected))) {
409
throw new RuntimeException("Expected log was not emitted.");
410
}
411
}
412
413
// Set the root logger on loading the logging class
414
public static class LogConfig {
415
final static CopyOnWriteArrayList<LogRecord> logRecordList = new CopyOnWriteArrayList<>();
416
final static StreamHandler handler = new StreamHandler() {
417
@Override
418
public void publish(LogRecord record) {
419
logRecordList.add(record);
420
System.out.println("LogRecord: " + record.getMessage());
421
}
422
};
423
static {
424
getLogManager().getLogger("").addHandler(handler);
425
}
426
}
427
428
static void bug8248695Test() {
429
Locale l = Locale.getDefault(Locale.Category.FORMAT);
430
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, l);
431
LocaleProviderAdapter.Type type = lda.getAdapterType();
432
if (type == LocaleProviderAdapter.Type.HOST) {
433
System.out.println("Locale: " + l);
434
var ld = LocalDate.now();
435
var zdt = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
436
var df = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(l);
437
var tf = DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL).withLocale(l);
438
var dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(l);
439
440
// Checks there's no "unsupported temporal field" exception thrown, such as HourOfDay
441
System.out.println(df.format(ld));
442
System.out.println(tf.format(zdt));
443
444
// Checks there's no "Too many pattern letters: aa" exception thrown, if
445
// underlying OS provides the "am/pm" pattern.
446
System.out.println(dtf.format(zdt));
447
}
448
}
449
450
// Run only if the platform locale is en-GB
451
static void bug8257964Test() {
452
var defLoc = Locale.getDefault(Locale.Category.FORMAT);
453
var type = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.UK)
454
.getAdapterType();
455
if (defLoc.equals(Locale.UK) &&
456
type == LocaleProviderAdapter.Type.HOST &&
457
(IS_WINDOWS || IS_MAC)) {
458
Calendar instance = Calendar.getInstance(Locale.UK);
459
int result = instance.getMinimalDaysInFirstWeek();
460
if (result != 4) {
461
throw new RuntimeException("MinimalDaysInFirstWeek for Locale.UK is incorrect. " +
462
"returned: " + result);
463
}
464
465
LocalDate date = LocalDate.of(2020,12,31);
466
result = date.get(WeekFields.of(Locale.UK).weekOfWeekBasedYear());
467
if (result != 53) {
468
throw new RuntimeException("weekNumber is incorrect. " +
469
"returned: " + result);
470
}
471
System.out.println("bug8257964Test succeeded.");
472
} else {
473
System.out.println("Test ignored. Either :-\n" +
474
"Default format locale is not Locale.UK: " + defLoc + ", or\n" +
475
"OS is neither macOS/Windows, or\n" +
476
"provider is not HOST: " + type);
477
}
478
}
479
}
480
481