Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Calendar/GregorianCutoverTest.java
41149 views
1
/*
2
* Copyright (c) 2003, 2016, 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
/*
25
* @test
26
* @bug 4359204 4928615 4743587 4956232 6459836 6549953
27
* @library /java/text/testlib
28
* @build Koyomi
29
* @run main GregorianCutoverTest
30
* @summary Unit tests related to the Gregorian cutover support.
31
*/
32
33
import java.util.Date;
34
import java.util.Locale;
35
import java.util.TimeZone;
36
37
import static java.util.GregorianCalendar.*;
38
39
public class GregorianCutoverTest extends IntlTest {
40
41
public static void main(String[] args) throws Exception {
42
TimeZone tz = TimeZone.getDefault();
43
Locale lc = Locale.getDefault();
44
try {
45
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
46
Locale.setDefault(Locale.US);
47
48
new GregorianCutoverTest().run(args);
49
} finally {
50
TimeZone.setDefault(tz);
51
Locale.setDefault(lc);
52
}
53
}
54
55
/**
56
* 4359204: GregorianCalendar.get(cal.DAY_OF_YEAR) is inconsistent for year 1582
57
*/
58
public void Test4359204() {
59
Koyomi cal = new Koyomi();
60
61
cal.set(1582, JANUARY, 1);
62
checkContinuity(cal, DAY_OF_YEAR);
63
checkContinuity(cal, WEEK_OF_YEAR);
64
cal.set(1582, OCTOBER, 1);
65
checkContinuity(cal, WEEK_OF_MONTH);
66
67
// JCK tests the cutover date 1970-1-1 (Epoch)
68
cal.setGregorianChange(new Date(0));
69
cal.set(1969, JANUARY, 1);
70
checkContinuity(cal, DAY_OF_YEAR);
71
checkContinuity(cal, WEEK_OF_YEAR);
72
cal.set(1969, DECEMBER, 1);
73
checkContinuity(cal, WEEK_OF_MONTH);
74
cal.set(1970, JANUARY, 1);
75
checkContinuity(cal, DAY_OF_YEAR);
76
checkContinuity(cal, WEEK_OF_YEAR);
77
78
// Use large date (year >= 50000)
79
@SuppressWarnings("deprecation")
80
Date d = new Date(50000 - 1900, JANUARY, 20);
81
cal.setGregorianChange(d);
82
cal.set(49998, JANUARY, 1);
83
checkContinuity(cal, DAY_OF_YEAR);
84
checkContinuity(cal, WEEK_OF_YEAR);
85
cal.set(49999, JANUARY, 1);
86
checkContinuity(cal, DAY_OF_YEAR);
87
checkContinuity(cal, WEEK_OF_YEAR);
88
cal.set(50000, JANUARY, 20);
89
checkContinuity(cal, DAY_OF_YEAR);
90
checkContinuity(cal, WEEK_OF_YEAR);
91
92
// Handling of "overlapping" dates may still be incorrect as
93
// of 1.5. Also, there's no way to disambiguate "overlapping"
94
// dates.
95
// millis=-112033929600000: date=-1581-10-15T00:00:00.000Z
96
cal.setGregorianChange(new Date(-112033929600000L));
97
cal.set(ERA, AD);
98
cal.set(-1581, JANUARY, 1);
99
// The year should have 379 days.
100
checkContinuity(cal, DAY_OF_YEAR);
101
checkContinuity(cal, WEEK_OF_YEAR);
102
103
logln("Default cutover");
104
cal = new Koyomi();
105
cal.set(1582, OCTOBER, 1);
106
logln(" roll --DAY_OF_MONTH from 1582/10/01");
107
cal.roll(DAY_OF_MONTH, -1);
108
if (!cal.checkDate(1582, OCTOBER, 31)) {
109
errln(cal.getMessage());
110
}
111
logln(" roll DAY_OF_MONTH+10 from 1582/10/31");
112
cal.roll(DAY_OF_MONTH, +10);
113
if (!cal.checkDate(1582, OCTOBER, 20)) {
114
errln(cal.getMessage());
115
}
116
logln(" roll DAY_OF_MONTH-10 from 1582/10/20");
117
cal.roll(DAY_OF_MONTH, -10);
118
if (!cal.checkDate(1582, OCTOBER, 31)) {
119
errln(cal.getMessage());
120
}
121
logln(" roll back one day further");
122
cal.roll(DAY_OF_MONTH, +1);
123
if (!cal.checkDate(1582, OCTOBER, 1)) {
124
errln(cal.getMessage());
125
}
126
127
// should handle the gap between 1969/12/22 (Julian) to 1970/1/5 (Gregorian)
128
logln("Cutover date is 1970/1/5");
129
@SuppressWarnings("deprecation")
130
Date d1 = new Date(1970 - 1900, JANUARY, 5);
131
cal.setGregorianChange(d1);
132
cal.set(ERA, AD);
133
cal.set(YEAR, 1970);
134
logln(" Set DAY_OF_YEAR to the 28th day of 1970");
135
cal.set(DAY_OF_YEAR, 28);
136
if (!cal.checkDate(1970, FEBRUARY, 1)) {
137
errln(cal.getMessage());
138
}
139
if (!cal.checkFieldValue(WEEK_OF_YEAR, 5)) {
140
errln(cal.getMessage());
141
}
142
logln(" 1969/12/22 should be the 356th day of the year.");
143
cal.set(1969, DECEMBER, 22);
144
if (!cal.checkFieldValue(DAY_OF_YEAR, 356)) {
145
errln(cal.getMessage());
146
}
147
logln(" Set DAY_OF_YEAR to autual maximum.");
148
int actualMaxDayOfYear = cal.getActualMaximum(DAY_OF_YEAR);
149
if (actualMaxDayOfYear != 356) {
150
errln("actual maximum of DAY_OF_YEAR: got " + actualMaxDayOfYear + ", expected 356");
151
}
152
cal.set(DAY_OF_YEAR, actualMaxDayOfYear);
153
if (!cal.checkDate(1969, DECEMBER, 22)) {
154
errln(cal.getMessage());
155
}
156
cal.set(1969, DECEMBER, 22);
157
cal.roll(DAY_OF_YEAR, +1);
158
logln(" Set to 1969/12/22 and roll DAY_OF_YEAR++");
159
if (!cal.checkDate(1969, JANUARY, 1)) {
160
errln(cal.getMessage());
161
}
162
logln(" 1970/1/5 should be the first day of the year.");
163
cal.set(1970, JANUARY, 5);
164
if (!cal.checkFieldValue(DAY_OF_YEAR, 1)) {
165
errln(cal.getMessage());
166
}
167
logln(" roll --DAY_OF_MONTH from 1970/1/5");
168
cal.roll(DAY_OF_MONTH, -1);
169
if (!cal.checkDate(1970, JANUARY, 31)) {
170
errln(cal.getMessage());
171
}
172
logln(" roll back one day of month");
173
cal.roll(DAY_OF_MONTH, +1);
174
if (!cal.checkDate(1970, JANUARY, 5)) {
175
errln(cal.getMessage());
176
}
177
178
// Test "missing" dates in non-lenient.
179
cal = new Koyomi(); // new instance for the default cutover
180
cal.setLenient(false);
181
try {
182
// the next day of 1582/10/4 (Julian) is 1582/10/15 (Gregorian)
183
logln("1582/10/10 doesn't exit with the default cutover.");
184
cal.set(1582, OCTOBER, 10);
185
cal.getTime();
186
errln(" Didn't throw IllegalArgumentException in non-lenient.");
187
} catch (IllegalArgumentException e) {
188
}
189
}
190
191
private void checkContinuity(Koyomi cal, int field) {
192
cal.getTime();
193
logln(Koyomi.getFieldName(field) + " starting on " + cal.toDateString());
194
int max = cal.getActualMaximum(field);
195
for (int i = 1; i <= max; i++) {
196
logln(i + " " + cal.toDateString());
197
if (!cal.checkFieldValue(field, i)) {
198
errln(" " + cal.toDateString() + ":\t" + cal.getMessage());
199
}
200
cal.add(field, +1);
201
}
202
}
203
204
/**
205
* 4928615: GregorianCalendar returns wrong dates after setGregorianChange
206
*/
207
public void Test4928615() {
208
Koyomi cal = new Koyomi();
209
logln("Today is 2003/10/1 Gregorian.");
210
@SuppressWarnings("deprecation")
211
Date x = new Date(2003 - 1900, 10 - 1, 1);
212
cal.setTime(x);
213
214
logln(" Changing the cutover date to yesterday...");
215
cal.setGregorianChange(new Date(x.getTime() - (24 * 3600 * 1000)));
216
if (!cal.checkDate(2003, OCTOBER, 1)) {
217
errln(" " + cal.getMessage());
218
}
219
logln(" Changing the cutover date to tomorrow...");
220
cal.setGregorianChange(new Date(x.getTime() + (24 * 3600 * 1000)));
221
if (!cal.checkDate(2003, SEPTEMBER, 18)) {
222
errln(" " + cal.getMessage());
223
}
224
}
225
226
/**
227
* 4743587: GregorianCalendar.getLeastMaximum() returns wrong values
228
*/
229
public void Test4743587() {
230
Koyomi cal = new Koyomi();
231
Koyomi cal2 = (Koyomi) cal.clone();
232
logln("getLeastMaximum should handle cutover year.\n"
233
+ " default cutover date");
234
if (!cal.checkLeastMaximum(DAY_OF_YEAR, 365 - 10)) {
235
errln(" " + cal.getMessage());
236
}
237
if (!cal.checkLeastMaximum(WEEK_OF_YEAR, 52 - ((10 + 6) / 7))) {
238
errln(" " + cal.getMessage());
239
}
240
// Corrected for 4956232
241
if (!cal.checkLeastMaximum(DAY_OF_MONTH, 28)) {
242
errln(" " + cal.getMessage());
243
}
244
if (!cal.checkLeastMaximum(WEEK_OF_MONTH, 3)) {
245
errln(" " + cal.getMessage());
246
}
247
if (!cal.checkLeastMaximum(DAY_OF_WEEK_IN_MONTH, 3)) {
248
errln(" " + cal.getMessage());
249
}
250
// make sure that getLeastMaximum calls didn't affect the date
251
if (!cal.equals(cal2)) {
252
errln(" getLeastMaximum calls modified the object.");
253
}
254
if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 1)) {
255
errln(" " + cal.getMessage());
256
}
257
258
logln(" changing the date to 1582/10/20 for actual min/max tests");
259
cal.set(1582, OCTOBER, 20);
260
if (!cal.checkActualMinimum(DAY_OF_MONTH, 1)) {
261
errln(" " + cal.getMessage());
262
}
263
if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {
264
errln(" " + cal.getMessage());
265
}
266
267
cal = new Koyomi();
268
logln("Change the cutover date to 1970/1/5.");
269
@SuppressWarnings("deprecation")
270
Date d = new Date(1970 - 1900, 0, 5);
271
cal.setGregorianChange(d);
272
if (!cal.checkLeastMaximum(DAY_OF_YEAR, 356)) {
273
errln(" " + cal.getMessage());
274
}
275
if (!cal.checkLeastMaximum(DAY_OF_MONTH, 22)) {
276
errln(" " + cal.getMessage());
277
}
278
if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 5)) {
279
errln(" " + cal.getMessage());
280
}
281
cal.set(1970, JANUARY, 10);
282
if (!cal.checkActualMinimum(DAY_OF_MONTH, 5)) {
283
errln(" " + cal.getMessage());
284
}
285
if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {
286
errln(" " + cal.getMessage());
287
}
288
}
289
290
/**
291
* 6459836: (cal) GregorianCalendar set method provides wrong result
292
*/
293
public void Test6459836() {
294
int hour = 13865672;
295
Koyomi gc1 = new Koyomi();
296
gc1.clear();
297
gc1.set(1, JANUARY, 1, 0, 0, 0);
298
gc1.set(HOUR_OF_DAY, hour);
299
if (!gc1.checkDate(1582, OCTOBER, 4)) {
300
errln("test case 1: " + gc1.getMessage());
301
}
302
gc1.clear();
303
gc1.set(1, JANUARY, 1, 0, 0, 0);
304
gc1.set(HOUR_OF_DAY, hour + 24);
305
if (!gc1.checkDate(1582, OCTOBER, 15)) {
306
errln("test case 2: " + gc1.getMessage());
307
}
308
}
309
310
/**
311
* 6549953 (cal) WEEK_OF_YEAR and DAY_OF_YEAR calculation problems around Gregorian cutover
312
*/
313
public void Test6549953() {
314
Koyomi cal = new Koyomi();
315
316
cal.set(YEAR, 1582);
317
cal.set(WEEK_OF_YEAR, 42);
318
cal.set(DAY_OF_WEEK, FRIDAY);
319
cal.checkFieldValue(WEEK_OF_YEAR, 42);
320
cal.checkFieldValue(DAY_OF_WEEK, FRIDAY);
321
if (!cal.checkDate(1582, OCTOBER, 29)) {
322
errln(cal.getMessage());
323
}
324
cal.clear();
325
cal.set(1582, OCTOBER, 1);
326
cal.set(DAY_OF_YEAR, 292);
327
if (!cal.checkDate(1582, OCTOBER, 29)) {
328
errln(cal.getMessage());
329
}
330
}
331
}
332
333