Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/LocaleProviders.java
38813 views
1
/*
2
* Copyright (c) 2012, 2013, 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.util.*;
26
import java.util.spi.*;
27
import sun.util.locale.provider.LocaleProviderAdapter;
28
29
public class LocaleProviders {
30
31
public static void main(String[] args) {
32
String methodName = args[0];
33
34
switch (methodName) {
35
case "getPlatformLocale":
36
if (args[1].equals("format")) {
37
getPlatformLocale(Locale.Category.FORMAT);
38
} else {
39
getPlatformLocale(Locale.Category.DISPLAY);
40
}
41
break;
42
43
case "adapterTest":
44
adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
45
break;
46
47
case "bug7198834Test":
48
bug7198834Test();
49
break;
50
51
case "tzNameTest":
52
tzNameTest(args[1]);
53
break;
54
55
case "bug8001440Test":
56
bug8001440Test();
57
break;
58
59
case "bug8010666Test":
60
bug8010666Test();
61
break;
62
63
case "bug8013086Test":
64
bug8013086Test(args[1], args[2]);
65
break;
66
67
case "bug8013903Test":
68
bug8013903Test();
69
break;
70
71
default:
72
throw new RuntimeException("Test method '"+methodName+"' not found.");
73
}
74
}
75
76
static void getPlatformLocale(Locale.Category cat) {
77
Locale defloc = Locale.getDefault(cat);
78
System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
79
}
80
81
static void adapterTest(String expected, String lang, String ctry) {
82
Locale testLocale = new Locale(lang, ctry);
83
LocaleProviderAdapter ldaExpected =
84
LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
85
if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
86
System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
87
return;
88
}
89
String preference = System.getProperty("java.locale.providers", "");
90
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
91
LocaleProviderAdapter.Type type = lda.getAdapterType();
92
System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
93
if (!type.toString().equals(expected)) {
94
throw new RuntimeException("Returned locale data adapter is not correct.");
95
}
96
}
97
98
static void bug7198834Test() {
99
LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
100
LocaleProviderAdapter.Type type = lda.getAdapterType();
101
if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {
102
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
103
String date = df.format(new Date());
104
if (date.charAt(date.length()-1) == ' ') {
105
throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
106
}
107
} else {
108
System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
109
}
110
}
111
112
static void tzNameTest(String id) {
113
TimeZone tz = TimeZone.getTimeZone(id);
114
String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
115
if (tzName.startsWith("GMT")) {
116
throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
117
}
118
}
119
120
static void bug8001440Test() {
121
Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
122
NumberFormat nf = NumberFormat.getInstance(locale);
123
String nu = nf.format(1234560);
124
}
125
126
// This test assumes Windows localized language/country display names.
127
static void bug8010666Test() {
128
if (System.getProperty("os.name").startsWith("Windows")) {
129
NumberFormat nf = NumberFormat.getInstance(Locale.US);
130
try {
131
double ver = nf.parse(System.getProperty("os.version"))
132
.doubleValue();
133
System.out.printf("Windows version: %.1f\n", ver);
134
if (ver >= 6.0) {
135
LocaleProviderAdapter lda =
136
LocaleProviderAdapter.getAdapter(
137
LocaleNameProvider.class, Locale.ENGLISH);
138
LocaleProviderAdapter.Type type = lda.getAdapterType();
139
if (type == LocaleProviderAdapter.Type.HOST) {
140
LocaleNameProvider lnp = lda.getLocaleNameProvider();
141
Locale mkmk = Locale.forLanguageTag("mk-MK");
142
String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
143
String hostResult =
144
lnp.getDisplayLanguage(mkmk.getLanguage(),
145
Locale.ENGLISH);
146
System.out.printf(" Display language name for" +
147
" (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
148
hostResult, result);
149
if (result == null ||
150
hostResult != null &&
151
!result.equals(hostResult)) {
152
throw new RuntimeException("Display language name" +
153
" mismatch for \"mk\". Returned name was" +
154
" \"" + result + "\", result(HOST): \"" +
155
hostResult + "\"");
156
}
157
result = Locale.US.getDisplayLanguage(Locale.ENGLISH);
158
hostResult =
159
lnp.getDisplayLanguage(Locale.US.getLanguage(),
160
Locale.ENGLISH);
161
System.out.printf(" Display language name for" +
162
" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
163
hostResult, result);
164
if (result == null ||
165
hostResult != null &&
166
!result.equals(hostResult)) {
167
throw new RuntimeException("Display language name" +
168
" mismatch for \"en\". Returned name was" +
169
" \"" + result + "\", result(HOST): \"" +
170
hostResult + "\"");
171
}
172
if (ver >= 6.1) {
173
result = Locale.US.getDisplayCountry(Locale.ENGLISH);
174
hostResult = lnp.getDisplayCountry(
175
Locale.US.getCountry(), Locale.ENGLISH);
176
System.out.printf(" Display country name for" +
177
" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
178
hostResult, result);
179
if (result == null ||
180
hostResult != null &&
181
!result.equals(hostResult)) {
182
throw new RuntimeException("Display country name" +
183
" mismatch for \"US\". Returned name was" +
184
" \"" + result + "\", result(HOST): \"" +
185
hostResult + "\"");
186
}
187
}
188
} else {
189
throw new RuntimeException("Windows Host" +
190
" LocaleProviderAdapter was not selected for" +
191
" English locale.");
192
}
193
}
194
} catch (ParseException pe) {
195
throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
196
}
197
}
198
}
199
200
static void bug8013086Test(String lang, String ctry) {
201
try {
202
// Throws a NullPointerException if the test fails.
203
System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
204
} catch (ParseException pe) {
205
// ParseException is fine in this test, as it's not "UTC"
206
}
207
}
208
209
static void bug8013903Test() {
210
if (System.getProperty("os.name").startsWith("Windows")) {
211
Date sampleDate = new Date(0x10000000000L);
212
String fallbackResult = "Heisei 16.Nov.03 (Wed) AM 11:53:47";
213
String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
214
Locale l = new Locale("ja", "JP", "JP");
215
SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
216
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
217
String result = sdf.format(sampleDate);
218
System.out.println(result);
219
if (LocaleProviderAdapter.getAdapterPreference()
220
.contains(LocaleProviderAdapter.Type.JRE)) {
221
if (!jreResult.equals(result)) {
222
throw new RuntimeException("Format failed. result: \"" +
223
result + "\", expected: \"" + jreResult);
224
}
225
} else {
226
// should be FALLBACK, as Windows HOST does not return
227
// display names
228
if (!fallbackResult.equals(result)) {
229
throw new RuntimeException("Format failed. result: \"" +
230
result + "\", expected: \"" + fallbackResult);
231
}
232
}
233
}
234
}
235
}
236
237