Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Calendar/SupplementalJapaneseEraTest.java
41149 views
1
/*
2
* Copyright (c) 2014, 2018, 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
import java.text.SimpleDateFormat;
25
import java.time.chrono.JapaneseChronology;
26
import java.time.chrono.JapaneseDate;
27
import java.time.chrono.JapaneseEra;
28
import java.time.format.DateTimeFormatter;
29
import java.time.format.TextStyle;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import static java.util.GregorianCalendar.*;
34
import java.util.Locale;
35
36
/*
37
* Usage:
38
* java -Djdk.calendar.japanese.supplemental.era=... SupplementalJapaneseEraTest <flag>
39
* -t executes tests with a valid property value
40
* -b <eraname>
41
* executes tests with an invalid property value
42
*/
43
44
public class SupplementalJapaneseEraTest {
45
private static final Locale WAREKI_LOCALE = Locale.forLanguageTag("ja-JP-u-ca-japanese");
46
private static final String SUP_ERA_NAME = "SupEra";
47
private static final String SUP_ERA_ABBR = "S.E.";
48
private static int errors = 0;
49
50
public static void main(String[] args) {
51
// args[0] is a flag.
52
switch (args[0]) {
53
case "-t":
54
// test with a valid property value
55
testProperty();
56
break;
57
58
case "-b":
59
// test with an invalid property value
60
// args[1] is the current era name.
61
testValidation(args[1].replace("\r", "")); // remove any CR for Cygwin
62
break;
63
}
64
if (errors != 0) {
65
throw new RuntimeException("test failed");
66
}
67
}
68
69
private static void testProperty() {
70
Calendar jcal = new Calendar.Builder()
71
.setCalendarType("japanese")
72
.setFields(ERA, 6, YEAR, 1, DAY_OF_YEAR, 1)
73
.build();
74
Date firstDayOfEra = jcal.getTime();
75
jcal.set(ERA, jcal.get(ERA) - 1); // previous era
76
jcal.set(YEAR, 1);
77
jcal.set(DAY_OF_YEAR, 1);
78
Calendar cal = new GregorianCalendar();
79
cal.setTimeInMillis(jcal.getTimeInMillis());
80
cal.add(YEAR, 199);
81
int year = cal.get(YEAR);
82
83
SimpleDateFormat sdf;
84
String expected, got;
85
86
// test long era name
87
sdf = new SimpleDateFormat("GGGG y-MM-dd", WAREKI_LOCALE);
88
got = sdf.format(firstDayOfEra);
89
expected = SUP_ERA_NAME + " 1-02-11";
90
if (!expected.equals(got)) {
91
System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
92
errors++;
93
}
94
95
// test era abbreviation
96
sdf = new SimpleDateFormat("G y-MM-dd", WAREKI_LOCALE);
97
got = sdf.format(firstDayOfEra);
98
expected = SUP_ERA_ABBR + " 1-02-11";
99
if (!expected.equals(got)) {
100
System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
101
errors++;
102
}
103
104
// confirm the gregorian year
105
sdf = new SimpleDateFormat("y", Locale.US);
106
int y = Integer.parseInt(sdf.format(firstDayOfEra));
107
if (y != year) {
108
System.err.printf("Gregorian year: got=%d, expected=%d%n", y, year);
109
errors++;
110
}
111
112
// test java.time.chrono.JapaneseEra
113
JapaneseDate jdate = JapaneseDate.of(year, 2, 11);
114
got = jdate.toString();
115
expected = "Japanese " + SUP_ERA_NAME + " 1-02-11";
116
if (!expected.equals(got)) {
117
System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected);
118
errors++;
119
}
120
JapaneseEra jera = jdate.getEra();
121
got = jera.getDisplayName(TextStyle.FULL, Locale.US);
122
if (!SUP_ERA_NAME.equals(got)) {
123
System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);
124
errors++;
125
}
126
got = jera.getDisplayName(TextStyle.SHORT, Locale.US);
127
if (!SUP_ERA_NAME.equals(got)) {
128
System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_NAME);
129
errors++;
130
}
131
got = jera.getDisplayName(TextStyle.NARROW, Locale.US);
132
if (!SUP_ERA_ABBR.equals(got)) {
133
System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);
134
errors++;
135
}
136
got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US);
137
if (!SUP_ERA_ABBR.equals(got)) {
138
System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, SUP_ERA_ABBR);
139
errors++;
140
}
141
142
// test full/short/narrow names with java.time.format
143
got = DateTimeFormatter
144
.ofPattern("GGGG G GGGGG")
145
.withLocale(Locale.US)
146
.withChronology(JapaneseChronology.INSTANCE)
147
.format(jdate);
148
expected = SUP_ERA_NAME + " " + SUP_ERA_NAME + " " + SUP_ERA_ABBR;
149
if (!expected.equals(got)) {
150
System.err.printf("java.time formatter full/short/narrow names: got=\"%s\", expected=\"%s\"%n", got, expected);
151
errors++;
152
}
153
}
154
155
private static void testValidation(String eraName) {
156
Calendar jcal = new Calendar.Builder()
157
.setCalendarType("japanese")
158
.setFields(YEAR, 1, DAY_OF_YEAR, 1)
159
.build();
160
if (!jcal.getDisplayName(ERA, LONG, Locale.US).equals(eraName)) {
161
errors++;
162
String prop = System.getProperty("jdk.calendar.japanese.supplemental.era");
163
System.err.println("Era changed with invalid property: " + prop);
164
}
165
}
166
}
167
168