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/LocaleCategory.java
38813 views
1
/*
2
* Copyright (c) 2010, 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
import java.util.Locale;
24
25
public class LocaleCategory {
26
private static Locale base = null;
27
private static Locale disp = null;
28
private static Locale fmt = null;
29
private static String enc = null;
30
31
public static void main(String[] args) {
32
Locale reservedLocale = Locale.getDefault();
33
try {
34
Locale.Builder builder = new Locale.Builder();
35
36
base = builder.setLanguage(System.getProperty("user.language", ""))
37
.setScript(System.getProperty("user.script", ""))
38
.setRegion(System.getProperty("user.country", ""))
39
.setVariant(System.getProperty("user.variant", "")).build();
40
disp = builder.setLanguage(
41
System.getProperty("user.language.display",
42
Locale.getDefault().getLanguage()))
43
.setScript(System.getProperty("user.script.display",
44
Locale.getDefault().getScript()))
45
.setRegion(System.getProperty("user.country.display",
46
Locale.getDefault().getCountry()))
47
.setVariant(System.getProperty("user.variant.display",
48
Locale.getDefault().getVariant())).build();
49
fmt = builder.setLanguage(System.getProperty("user.language.format",
50
Locale.getDefault().getLanguage()))
51
.setScript(System.getProperty("user.script.format",
52
Locale.getDefault().getScript()))
53
.setRegion(System.getProperty("user.country.format",
54
Locale.getDefault().getCountry()))
55
.setVariant(System.getProperty("user.variant.format",
56
Locale.getDefault().getVariant())).build();
57
checkDefault();
58
testGetSetDefault();
59
testBug7079486();
60
} finally {
61
// restore the reserved locale
62
Locale.setDefault(reservedLocale);
63
}
64
}
65
66
static void checkDefault() {
67
if (!base.equals(Locale.getDefault()) ||
68
!disp.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
69
!fmt.equals(Locale.getDefault(Locale.Category.FORMAT))) {
70
throw new RuntimeException("Some of the return values from getDefault() do not agree with the locale derived from \"user.xxxx\" system properties");
71
}
72
}
73
74
static void testGetSetDefault() {
75
try {
76
Locale.setDefault(null, null);
77
throw new RuntimeException("setDefault(null, null) should throw a NullPointerException");
78
} catch (NullPointerException npe) {}
79
80
Locale.setDefault(Locale.CHINA);
81
if (!Locale.CHINA.equals(Locale.getDefault(Locale.Category.DISPLAY)) ||
82
!Locale.CHINA.equals(Locale.getDefault(Locale.Category.FORMAT))) {
83
throw new RuntimeException("setDefault() should set all default locales for all categories");
84
}
85
}
86
87
static void testBug7079486() {
88
Locale zh_Hans_CN = Locale.forLanguageTag("zh-Hans-CN");
89
90
// make sure JRE has zh_Hans_CN localized string
91
if (zh_Hans_CN.getDisplayScript(Locale.US).equals(zh_Hans_CN.getDisplayScript(zh_Hans_CN))) {
92
return;
93
}
94
95
Locale.setDefault(Locale.US);
96
String en_script = zh_Hans_CN.getDisplayScript();
97
98
Locale.setDefault(Locale.Category.DISPLAY, zh_Hans_CN);
99
String zh_script = zh_Hans_CN.getDisplayScript();
100
101
if (en_script.equals(zh_script)) {
102
throw new RuntimeException("Locale.getDisplayScript() (no args) does not honor default DISPLAY locale");
103
}
104
}
105
}
106
107
108