Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/ResourceBundle/Control/DefaultControlTest.java
41155 views
1
/*
2
* Copyright (c) 2007, 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
/*
24
* @test
25
* @bug 5102289 6278334 8261179
26
* @summary Test the default Control implementation. The expiration
27
* functionality of newBundle, getTimeToLive, and needsReload is
28
* tested by ExpirationTest.sh. The factory methods are tested
29
* separately.
30
* @build TestResourceRB
31
* @build NonResourceBundle
32
* @run main DefaultControlTest
33
*/
34
35
import java.util.*;
36
import static java.util.ResourceBundle.Control.*;
37
38
public class DefaultControlTest {
39
// The ResourceBundle.Control instance
40
static final ResourceBundle.Control CONTROL
41
= ResourceBundle.Control.getControl(FORMAT_DEFAULT);
42
43
static final ResourceBundle BUNDLE = new ResourceBundle() {
44
public Enumeration<String> getKeys() { return null; }
45
protected Object handleGetObject(String key) { return null; }
46
};
47
48
static final String CLAZZ = FORMAT_CLASS.get(0);
49
50
static final String PROPERTIES = FORMAT_PROPERTIES.get(0);
51
52
static final ClassLoader LOADER = DefaultControlTest.class.getClassLoader();
53
54
// Full arguments for NPE testing
55
static final Object[] FULLARGS = { "any",
56
Locale.US,
57
FORMAT_PROPERTIES.get(0),
58
LOADER,
59
BUNDLE };
60
61
static int errors;
62
63
public static void main(String[] args) {
64
checkConstants();
65
66
// Test getFormats(String)
67
testGetFormats();
68
69
// Test getCandidateLocales(String, Locale)
70
testGetCandidateLocales();
71
72
// Test getFallbackLocale(String, Locale)
73
testGetFallbackLocale();
74
75
// Test newBundle(String, Locale, String, ClassLoader, boolean)
76
testNewBundle();
77
78
// Test toBundleName(String, Locale)
79
testToBundleName();
80
81
// Test getTimeToLive(String, Locale)
82
testGetTimeToLive();
83
84
// Test needsReload(String, Locale, String, ClassLoader,
85
// ResourceBundle, long)
86
testNeedsReload();
87
88
// Test toResourceName(String, String)
89
testToResourceName();
90
91
if (errors > 0) {
92
throw new RuntimeException("FAILED: " + errors + " error(s)");
93
}
94
}
95
96
private static void checkConstants() {
97
// Check FORMAT_*
98
if (!CONTROL.FORMAT_DEFAULT.equals(Arrays.asList("java.class",
99
"java.properties"))) {
100
error("Wrong Control.FORMAT_DEFAULT");
101
}
102
checkImmutableList(CONTROL.FORMAT_DEFAULT);
103
if (!CONTROL.FORMAT_CLASS.equals(Arrays.asList("java.class"))) {
104
error("Wrong Control.FORMAT_CLASS");
105
}
106
checkImmutableList(CONTROL.FORMAT_CLASS);
107
if (!CONTROL.FORMAT_PROPERTIES.equals(Arrays.asList("java.properties"))) {
108
error("Wrong Control.FORMAT_PROPERTIES");
109
}
110
checkImmutableList(CONTROL.FORMAT_PROPERTIES);
111
112
// Check TTL_*
113
if (CONTROL.TTL_DONT_CACHE != -1) {
114
error("Wrong Control.TTL_DONT_CACHE: %d%n", CONTROL.TTL_DONT_CACHE);
115
}
116
if (CONTROL.TTL_NO_EXPIRATION_CONTROL != -2) {
117
error("Wrong Control.TTL_NO_EXPIRATION_CONTROL: %d%n",
118
CONTROL.TTL_NO_EXPIRATION_CONTROL);
119
}
120
}
121
122
private static void checkImmutableList(List<String> list) {
123
try {
124
list.add("hello");
125
error("%s is mutable%n", list);
126
} catch (UnsupportedOperationException e) {
127
}
128
}
129
130
private static void testGetFormats() {
131
List<String> list = CONTROL.getFormats("foo");
132
if (list != CONTROL.FORMAT_DEFAULT) {
133
error("getFormats returned " + list);
134
}
135
try {
136
list = CONTROL.getFormats(null);
137
error("getFormats doesn't throw NPE.");
138
} catch (NullPointerException e) {
139
}
140
}
141
142
private static void testGetCandidateLocales() {
143
Map<Locale, Locale[]> candidateData = new HashMap<Locale, Locale[]>();
144
candidateData.put(new Locale("ja", "JP", "YOK"), new Locale[] {
145
new Locale("ja", "JP", "YOK"),
146
new Locale("ja", "JP"),
147
new Locale("ja"),
148
Locale.ROOT });
149
candidateData.put(new Locale("ja", "JP"), new Locale[] {
150
new Locale("ja", "JP"),
151
new Locale("ja"),
152
Locale.ROOT });
153
candidateData.put(new Locale("ja"), new Locale[] {
154
new Locale("ja"),
155
Locale.ROOT });
156
157
candidateData.put(new Locale("ja", "", "YOK"), new Locale[] {
158
new Locale("ja", "", "YOK"),
159
new Locale("ja"),
160
Locale.ROOT });
161
candidateData.put(new Locale("", "JP", "YOK"), new Locale[] {
162
new Locale("", "JP", "YOK"),
163
new Locale("", "JP"),
164
Locale.ROOT });
165
candidateData.put(new Locale("", "", "YOK"), new Locale[] {
166
new Locale("", "", "YOK"),
167
Locale.ROOT });
168
candidateData.put(new Locale("", "JP"), new Locale[] {
169
new Locale("", "JP"),
170
Locale.ROOT });
171
candidateData.put(Locale.ROOT, new Locale[] {
172
Locale.ROOT });
173
174
// Norwegian Bokmal
175
candidateData.put(Locale.forLanguageTag("nb-NO-POSIX"), new Locale[] {
176
Locale.forLanguageTag("nb-NO-POSIX"),
177
Locale.forLanguageTag("no-NO-POSIX"),
178
Locale.forLanguageTag("nb-NO"),
179
Locale.forLanguageTag("no-NO"),
180
Locale.forLanguageTag("nb"),
181
Locale.forLanguageTag("no"),
182
Locale.ROOT});
183
candidateData.put(Locale.forLanguageTag("no-NO-POSIX"), new Locale[] {
184
Locale.forLanguageTag("no-NO-POSIX"),
185
Locale.forLanguageTag("nb-NO-POSIX"),
186
Locale.forLanguageTag("no-NO"),
187
Locale.forLanguageTag("nb-NO"),
188
Locale.forLanguageTag("no"),
189
Locale.forLanguageTag("nb"),
190
Locale.ROOT});
191
192
193
for (Locale locale : candidateData.keySet()) {
194
List<Locale> candidates = CONTROL.getCandidateLocales("any", locale);
195
List<Locale> expected = Arrays.asList(candidateData.get(locale));
196
if (!candidates.equals(expected)) {
197
error("Wrong candidates for %s: got %s, expected %s%n",
198
toString(locale), candidates, expected);
199
}
200
}
201
final int NARGS = 2;
202
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
203
Object[] data = getNpeArgs(NARGS, mask);
204
try {
205
List<Locale> candidates = CONTROL.getCandidateLocales((String) data[0],
206
(Locale) data[1]);
207
error("getCandidateLocales(%s, %s) doesn't throw NPE.%n",
208
data[0], toString((Locale)data[1]));
209
} catch (NullPointerException e) {
210
}
211
}
212
}
213
214
private static void testGetFallbackLocale() {
215
Locale current = Locale.getDefault();
216
Locale.setDefault(Locale.ITALY);
217
try {
218
Locale loc = CONTROL.getFallbackLocale("any", Locale.FRANCE);
219
if (loc != Locale.ITALY) {
220
error("getFallbackLocale: got %s, expected %s%n",
221
toString(loc), toString(Locale.ITALY));
222
}
223
loc = CONTROL.getFallbackLocale("any", Locale.ITALY);
224
if (loc != null) {
225
error("getFallbackLocale: got %s, expected null%n", toString(loc));
226
}
227
} finally {
228
Locale.setDefault(current);
229
}
230
231
final int NARGS = 2;
232
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
233
Object[] data = getNpeArgs(NARGS, mask);
234
try {
235
Locale loc = CONTROL.getFallbackLocale((String) data[0], (Locale) data[1]);
236
error("getFallbackLocale(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
237
} catch (NullPointerException e) {
238
}
239
}
240
}
241
242
private static void testNewBundle() {
243
int testNo = 0;
244
ResourceBundle rb = null;
245
try {
246
testNo = 1;
247
rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,
248
PROPERTIES, LOADER, false);
249
String s = rb.getString("data");
250
if (!s.equals("Japan")) {
251
error("newBundle: #%d got %s, expected Japan%n", testNo, s);
252
}
253
254
testNo = 2;
255
rb = CONTROL.newBundle("TestResourceRB", Locale.ROOT,
256
CLAZZ, LOADER, false);
257
s = rb.getString("type");
258
if (!s.equals(CLAZZ)) {
259
error("newBundle: #%d got %s, expected %s%n", testNo, s, CLAZZ);
260
}
261
} catch (Throwable e) {
262
error("newBundle: #%d threw %s%n", testNo, e);
263
e.printStackTrace();
264
}
265
266
// Test exceptions
267
268
try {
269
// MalformedDataRB contains an invalid Unicode notation which
270
// causes to throw an IllegalArgumentException.
271
rb = CONTROL.newBundle("MalformedDataRB", Locale.ENGLISH,
272
PROPERTIES, LOADER, false);
273
error("newBundle: doesn't throw IllegalArgumentException with malformed data.");
274
} catch (IllegalArgumentException iae) {
275
} catch (Exception e) {
276
error("newBundle: threw %s%n", e);
277
}
278
279
try {
280
rb = CONTROL.newBundle("StressOut", Locale.JAPANESE,
281
"foo.bar", LOADER, false);
282
error("newBundle: doesn't throw IllegalArgumentException with invalid format.");
283
} catch (IllegalArgumentException iae) {
284
} catch (Exception e) {
285
error("newBundle: threw %s%n", e);
286
}
287
288
try {
289
rb = CONTROL.newBundle("NonResourceBundle", Locale.ROOT,
290
"java.class", LOADER, false);
291
error("newBundle: doesn't throw ClassCastException with a non-ResourceBundle subclass.");
292
} catch (ClassCastException cce) {
293
} catch (Exception e) {
294
error("newBundle: threw %s%n", e);
295
}
296
297
// NPE test
298
final int NARGS = 4;
299
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
300
Object[] data = getNpeArgs(NARGS, mask);
301
Locale loc = (Locale) data[1];
302
try {
303
rb = CONTROL.newBundle((String) data[0], loc,
304
(String) data[2], (ClassLoader) data[3],
305
false);
306
error("newBundle(%s, %s, %s, %s, false) doesn't throw NPE.%n",
307
data[0], toString(loc), data[2], data[3]);
308
} catch (NullPointerException npe) {
309
} catch (Exception e) {
310
error("newBundle(%s, %s, %s, %s, false) threw %s.%n",
311
data[0], toString(loc), data[2], data[3], e);
312
}
313
}
314
}
315
316
private static void testGetTimeToLive() {
317
long ttl = CONTROL.getTimeToLive("any", Locale.US);
318
if (ttl != CONTROL.TTL_NO_EXPIRATION_CONTROL) {
319
error("getTimeToLive: got %d, expected %d%n", ttl,
320
CONTROL.TTL_NO_EXPIRATION_CONTROL);
321
}
322
final int NARGS = 2;
323
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
324
Object[] data = getNpeArgs(NARGS, mask);
325
try {
326
ttl = CONTROL.getTimeToLive((String) data[0], (Locale) data[1]);
327
error("getTimeToLive(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
328
} catch (NullPointerException e) {
329
}
330
}
331
}
332
333
// The functionality of needsReload is tested by
334
// ExpirationTest.sh. Only parameter checking is tested here.
335
private static void testNeedsReload() {
336
long loadTime = System.currentTimeMillis();
337
338
// NPE test
339
final int NARGS = 5;
340
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
341
Object[] data = getNpeArgs(NARGS, mask);
342
Locale loc = (Locale) data[1];
343
try {
344
boolean b = CONTROL.needsReload((String) data[0], loc,
345
(String) data[2], (ClassLoader) data[3],
346
(ResourceBundle) data[4], loadTime);
347
error("needsReload(%s, %s, %s, %s, %s, loadTime) doesn't throw NPE.%n",
348
data[0], toString(loc), data[2], data[3], data[4]);
349
} catch (NullPointerException e) {
350
}
351
}
352
}
353
354
private static void testToBundleName() {
355
final String name = "J2SE";
356
Map<Locale, String> bundleNames = new HashMap<Locale, String>();
357
bundleNames.put(new Locale("ja", "JP", "YOK"),
358
name + "_" + "ja" + "_" + "JP" + "_" + "YOK");
359
bundleNames.put(new Locale("ja", "JP"),
360
name + "_" + "ja" + "_" + "JP");
361
bundleNames.put(new Locale("ja"),
362
name + "_" + "ja");
363
bundleNames.put(new Locale("ja", "", "YOK"),
364
name + "_" + "ja" + "_" + "" + "_" + "YOK");
365
bundleNames.put(new Locale("", "JP", "YOK"),
366
name + "_" + "" + "_" + "JP" + "_" + "YOK");
367
bundleNames.put(new Locale("", "", "YOK"),
368
name + "_" + "" + "_" + "" + "_" + "YOK");
369
bundleNames.put(new Locale("", "JP"),
370
name + "_" + "" + "_" + "JP");
371
bundleNames.put(Locale.ROOT,
372
name);
373
374
for (Locale locale : bundleNames.keySet()) {
375
String bn = CONTROL.toBundleName(name, locale);
376
String expected = bundleNames.get(locale);
377
if (!bn.equals(expected)) {
378
error("toBundleName: got %s, expected %s%n", bn, expected);
379
}
380
}
381
382
final int NARGS = 2;
383
for (int mask = 0; mask < (1 << NARGS)-1; mask++) {
384
Object[] data = getNpeArgs(NARGS, mask);
385
try {
386
String s = CONTROL.toBundleName((String) data[0], (Locale) data[1]);
387
error("toBundleName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
388
} catch (NullPointerException e) {
389
}
390
}
391
}
392
393
private static void testToResourceName() {
394
String[][] names = {
395
// bundleName, suffix, expected result
396
{ "com.sun.J2SE", "xml", "com/sun/J2SE.xml" },
397
{ ".J2SE", "xml", "/J2SE.xml" },
398
{ "J2SE", "xml", "J2SE.xml" },
399
{ "com/sun/J2SE", "xml", "com/sun/J2SE.xml" },
400
{ "com.sun.J2SE", "", "com/sun/J2SE." },
401
{ ".", "", "/." },
402
{ "", "", "." },
403
404
// data for NPE tests
405
{ null, "any", null },
406
{ "any", null, null },
407
{ null, null, null },
408
};
409
410
for (String[] data : names) {
411
String result = null;
412
boolean npeThrown = false;
413
try {
414
result = CONTROL.toResourceName(data[0], data[1]);
415
} catch (NullPointerException npe) {
416
npeThrown = true;
417
}
418
String expected = data[2];
419
if (expected != null) {
420
if (!result.equals(expected)) {
421
error("toResourceName: got %s, expected %s%n", result, data[2]);
422
}
423
} else {
424
if (!npeThrown) {
425
error("toResourceName(%s, %s) doesn't throw NPE.%n", data[0], data[1]);
426
}
427
}
428
}
429
}
430
431
/**
432
* Produces permutations argument data that contains at least one
433
* null.
434
*/
435
private static Object[] getNpeArgs(int length, int mask) {
436
Object[] data = new Object[length];
437
for (int i = 0; i < length; i++) {
438
if ((mask & (1 << i)) == 0) {
439
data[i] = null;
440
} else {
441
data[i] = FULLARGS[i];
442
}
443
}
444
return data;
445
}
446
447
private static String toString(Locale loc) {
448
if (loc == null)
449
return "null";
450
return "\"" + loc.getLanguage() + "_" + loc.getCountry() + "_" + loc.getVariant() + "\"";
451
}
452
453
private static void error(String msg) {
454
System.out.println(msg);
455
errors++;
456
}
457
458
private static void error(String fmt, Object... args) {
459
System.out.printf(fmt, args);
460
errors++;
461
}
462
}
463
464