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/Calendar/NonLenientTest.java
47182 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 4147269 4266783 4726030
27
* @summary Make sure that validation is adequate in non-lenient mode.
28
* @library /java/text/testlib
29
*/
30
31
import java.util.*;
32
33
import static java.util.Calendar.*;
34
35
public class NonLenientTest extends IntlTest {
36
37
public static void main(String[] args) throws Exception {
38
Locale reservedLocale = Locale.getDefault();
39
TimeZone reservedTimeZone = TimeZone.getDefault();
40
try {
41
Locale.setDefault(Locale.US);
42
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
43
new NonLenientTest().run(args);
44
} finally {
45
// restore the reserved locale and time zone
46
Locale.setDefault(reservedLocale);
47
TimeZone.setDefault(reservedTimeZone);
48
}
49
}
50
51
public void TestValidationInNonLenient() {
52
Koyomi cal = getNonLenient();
53
54
// 2003 isn't a leap year.
55
cal.set(2003, FEBRUARY, 29);
56
validate(cal, "2003/2/29");
57
58
// October has only 31 days.
59
cal.set(2003, OCTOBER, 32);
60
validate(cal, "2003/10/32");
61
62
// 2003/10/31 is Friday.
63
cal.set(2003, OCTOBER, 31);
64
cal.set(DAY_OF_WEEK, SUNDAY);
65
validate(cal, "2003/10/31 SUNDAY");
66
67
// 2003/10/31 is the 304th day of the year.
68
cal.clear();
69
cal.set(DAY_OF_YEAR, 1);
70
cal.set(2003, OCTOBER, 31);
71
validate(cal, "2003/10/31 DAY_OF_YEAR=1");
72
73
// 2003/10 isn't the 1st week of the year.
74
cal.clear();
75
cal.set(YEAR, 2003);
76
cal.set(WEEK_OF_YEAR, 1);
77
cal.set(MONTH, OCTOBER);
78
validate(cal, "2003/10 WEEK_OF_YEAR=1");
79
80
// The 1st week of 2003 doesn't have Monday.
81
cal.clear();
82
cal.set(YEAR, 2003);
83
cal.set(WEEK_OF_YEAR, 1);
84
cal.set(DAY_OF_WEEK, MONDAY);
85
validate(cal, "2003 WEEK_OF_YEAR=1 MONDAY.");
86
87
// 2003 has 52 weeks.
88
cal.clear();
89
cal.set(YEAR, 2003);
90
cal.set(WEEK_OF_YEAR, 53);
91
cal.set(DAY_OF_WEEK, WEDNESDAY);
92
validate(cal, "2003 WEEK_OF_YEAR=53");
93
94
/*
95
* These test cases assume incompatible behavior in Tiger as
96
* the result of the validation bug fixes. However, it looks
97
* like we have to allow applications to set ZONE_OFFSET and
98
* DST_OFFSET values to modify the time zone offsets given by
99
* a TimeZone. The definition of non-leniency for time zone
100
* offsets is somewhat vague. (See 6231602)
101
*
102
* The following test cases are now disabled.
103
104
// America/Los_Angeles is GMT-08:00
105
cal.clear();
106
cal.set(2003, OCTOBER, 31);
107
cal.set(ZONE_OFFSET, 0);
108
validate(cal, "ZONE_OFFSET=0:00 in America/Los_Angeles");
109
110
// 2003/10/31 shouldn't be in DST.
111
cal.clear();
112
cal.set(2003, OCTOBER, 31);
113
cal.set(DST_OFFSET, 60*60*1000);
114
validate(cal, "2003/10/31 DST_OFFSET=1:00 in America/Los_Angeles");
115
116
*/
117
}
118
119
/**
120
* 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient
121
*/
122
public void Test4266783() {
123
Koyomi cal = getNonLenient();
124
// 2003/1 has up to 5 weeks.
125
cal.set(YEAR, 2003);
126
cal.set(MONTH, JANUARY);
127
cal.set(WEEK_OF_MONTH, 6);
128
cal.set(DAY_OF_WEEK, SUNDAY);
129
validate(cal, "6th Sunday in Jan 2003");
130
}
131
132
133
/**
134
* 4726030: GregorianCalendar doesn't check invalid dates in non-lenient
135
*/
136
public void Test4726030() {
137
Koyomi cal = getNonLenient();
138
// Default year is 1970 in GregorianCalendar which isn't a leap year.
139
cal.set(MONTH, FEBRUARY);
140
cal.set(DAY_OF_MONTH, 29);
141
validate(cal, "2/29 in the default year 1970");
142
}
143
144
/**
145
* 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false
146
*/
147
public void Test4147269() {
148
Koyomi calendar = getNonLenient();
149
Date date = (new GregorianCalendar(1996,0,3)).getTime();
150
151
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
152
calendar.setTime(date);
153
int max = calendar.getActualMaximum(field);
154
int value = max+1;
155
calendar.set(field, value);
156
try {
157
calendar.computeTime(); // call method under test
158
errln("Test failed with field " + calendar.getFieldName(field)
159
+ "\n\tdate before: " + date
160
+ "\n\tdate after: " + calendar.getTime()
161
+ "\n\tvalue: " + value + " (max = " + max +")");
162
} catch (IllegalArgumentException e) {
163
}
164
}
165
166
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
167
calendar.setTime(date);
168
int min = calendar.getActualMinimum(field);
169
int value = min-1;
170
calendar.set(field, value);
171
try {
172
calendar.computeTime(); // call method under test
173
errln("Test failed with field " + calendar.getFieldName(field)
174
+ "\n\tdate before: " + date
175
+ "\n\tdate after: " + calendar.getTime()
176
+ "\n\tvalue: " + value + " (min = " + min +")");
177
} catch (IllegalArgumentException e) {
178
}
179
}
180
}
181
182
void validate(Koyomi cal, String desc) {
183
int[] originalFields = cal.getFields();
184
int setFields = cal.getSetStateFields();
185
186
try {
187
cal.complete();
188
errln(desc + " should throw IllegalArgumentException in non-lenient.");
189
} catch (IllegalArgumentException e) {
190
}
191
192
// The code below will be executed with the -nothrow option
193
194
// In non-lenient, calendar field values that have beeb set by
195
// user shouldn't be modified.
196
int[] afterFields = cal.getFields();
197
for (int i = 0; i < Calendar.FIELD_COUNT; i++) {
198
if (cal.isSet(i) && originalFields[i] != afterFields[i]) {
199
errln(" complete() modified fields[" + cal.getFieldName(i) + "] got "
200
+ afterFields[i] + ", expected " + originalFields[i]);
201
}
202
}
203
// In non-lenient, set state of fields shouldn't be modified.
204
int afterSetFields = cal.getSetStateFields();
205
if (setFields != afterSetFields) {
206
errln(" complate() modified set states: before 0x" + toHex(setFields)
207
+ ", after 0x"+ toHex(afterSetFields));
208
}
209
}
210
211
static Koyomi getNonLenient() {
212
Koyomi cal = new Koyomi();
213
cal.clear();
214
cal.setLenient(false);
215
return cal;
216
}
217
218
static String toHex(int x) {
219
return Integer.toHexString(x);
220
}
221
}
222
223