1
1
/*
2
- * Copyright (c) 2003, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
24
24
/*
25
25
* @test
26
26
* @bug 4817812 4847186 4956227 4956479
27
- * @summary Confirm that BuddhistCalendar's add(), roll() and toString() work correctly with Buddhist Era years.
27
+ * @summary Confirm that BuddhistCalendar's add(), roll(), set(), and toString()
28
+ * work correctly with Buddhist Era years.
29
+ * @run junit BuddhistCalendarTest
28
30
*/
29
31
30
32
import java .util .Calendar ;
31
33
import java .util .GregorianCalendar ;
32
34
import java .util .Locale ;
33
- import static java .util .Calendar .*;
35
+ import java .util .stream .Stream ;
36
+
37
+ import static java .util .Calendar .APRIL ;
38
+ import static java .util .Calendar .DATE ;
39
+ import static java .util .Calendar .DECEMBER ;
40
+ import static java .util .Calendar .ERA ;
41
+ import static java .util .Calendar .FEBRUARY ;
42
+ import static java .util .Calendar .JANUARY ;
43
+ import static java .util .Calendar .MAY ;
44
+ import static java .util .Calendar .MONTH ;
45
+ import static java .util .Calendar .WEEK_OF_YEAR ;
46
+ import static java .util .Calendar .YEAR ;
47
+
48
+
49
+ import org .junit .jupiter .api .Test ;
50
+ import org .junit .jupiter .params .ParameterizedTest ;
51
+ import org .junit .jupiter .params .provider .Arguments ;
52
+ import org .junit .jupiter .params .provider .MethodSource ;
53
+
54
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
55
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34
56
35
57
public class BuddhistCalendarTest {
36
58
37
59
private static final Locale THAI_LOCALE = Locale .of ("th" , "TH" );
38
60
39
- public static void main (String [] args ) {
40
- testAddRoll ();
41
- testToString ();
42
- testException ();
43
- testLeastMax ();
61
+ /*
62
+ * Test some add values for the BuddhistCalendar. This test compares the same field
63
+ * as the one added.
64
+ */
65
+ @ ParameterizedTest
66
+ @ MethodSource ("addDataProvider" )
67
+ public void buddhistAddTest (Calendar cal , int amount , int fieldToAdd ) {
68
+ int base = cal .get (YEAR );
69
+ cal .add (fieldToAdd , amount );
70
+ int yearAfterRoll = cal .get (YEAR );
71
+ assertEquals (yearAfterRoll , base +amount , String .format (
72
+ "Added: %s to field: %s" , amount , fieldToAdd ));
73
+ }
74
+
75
+ /*
76
+ * Given in the format: Calendar, amount to add, and field to add.
77
+ * Test adding of positive and negative year values.
78
+ */
79
+ private static Stream <Arguments > addDataProvider () {
80
+ return Stream .of (
81
+ Arguments .of (getBuddhistCalendar (), 1 , YEAR ),
82
+ Arguments .of (getBuddhistCalendar (), -3 , YEAR )
83
+ );
84
+ }
85
+
86
+ /*
87
+ * Test some add values for the BuddhistCalendar. Compare a bigger field
88
+ * (year) than the one added (month). Larger field should roll over.
89
+ */
90
+ @ ParameterizedTest
91
+ @ MethodSource ("alternateAddDataProvider" )
92
+ public void buddhistAlternateAddTest (Calendar cal , int amount , int fieldToAdd ) {
93
+ int base = cal .get (YEAR );
94
+ cal .add (fieldToAdd , amount );
95
+ int yearAfterRoll = cal .get (YEAR );
96
+ assertEquals (yearAfterRoll , (amount >0 ) ? (base +1 ): (base -1 ), String .format (
97
+ "Added: %s to field: %s" , amount , fieldToAdd ));
98
+ }
99
+
100
+ /*
101
+ * Given in the format: Calendar, amount to add, and field to add.
102
+ * Test adding of positive and negative month values.
103
+ */
104
+ private static Stream <Arguments > alternateAddDataProvider () {
105
+ return Stream .of (
106
+ Arguments .of (getBuddhistCalendarBuilder ().set (MONTH , DECEMBER ).build (), 2 , MONTH ),
107
+ Arguments .of (getBuddhistCalendarBuilder ().set (MONTH , FEBRUARY ).build (), -4 , MONTH )
108
+ );
109
+ }
110
+
111
+ /*
112
+ * Test some roll values for the BuddhistCalendar. Compare same field
113
+ * that was rolled, value should change.
114
+ */
115
+ @ ParameterizedTest
116
+ @ MethodSource ("rollProvider" )
117
+ public void buddhistRollTest (Calendar cal , int amount , int fieldToRoll ) {
118
+ int base = cal .get (YEAR );
119
+ cal .roll (fieldToRoll , amount );
120
+ int year = cal .get (YEAR );
121
+ assertEquals (year , base +amount , "Rolling field should change value" );
122
+ }
123
+
124
+ /*
125
+ * Given in the format: Calendar, amount to roll, and field to roll.
126
+ * Test rolling of positive and negative year values.
127
+ */
128
+ private static Stream <Arguments > rollProvider () {
129
+ return Stream .of (
130
+ Arguments .of (getBuddhistCalendar (), 2 , YEAR ),
131
+ Arguments .of (getBuddhistCalendar (), -4 , YEAR )
132
+ );
133
+ }
134
+
135
+ /*
136
+ * Set some calendar values and roll, however, measure a different
137
+ * field than the field that was rolled. Rolling should not change the
138
+ * larger field.
139
+ */
140
+ @ ParameterizedTest
141
+ @ MethodSource ("alternateRollProvider" )
142
+ public void buddhistAlternateRollTest (Calendar cal , int amount , int fieldToRoll ) {
143
+ int base = cal .get (YEAR );
144
+ cal .roll (fieldToRoll , amount );
145
+ int year = cal .get (YEAR );
146
+ assertEquals (year , base , "Rolling smaller field should not change bigger field" );
44
147
}
45
148
46
- /**
47
- * 4817812
149
+ /*
150
+ * Given in the format: Calendar, amount to roll, and field to roll.
151
+ * Test rolling of positive and negative week_of_year values.
48
152
*/
49
- static void testAddRoll () {
50
- Calendar cal ;
51
- int base , year ;
52
-
53
- /*
54
- * Test: BuddhistCalendar.add(YEAR)
55
- */
56
- cal = getBuddhistCalendar ();
57
- base = cal .get (YEAR );
58
- cal .add (YEAR , 1 );
59
- year = cal .get (YEAR );
60
- check (year , base +1 , "add(+YEAR)" );
61
-
62
- cal = getBuddhistCalendar ();
63
- base = cal .get (YEAR );
64
- cal .add (YEAR , -3 );
65
- year = cal .get (YEAR );
66
- check (year , base -3 , "add(-YEAR)" );
67
-
68
- /*
69
- * Test BuddhistCalendar.add(MONTH)
70
- */
71
- cal = getBuddhistCalendar ();
72
- base = cal .get (YEAR );
73
- cal .set (MONTH , DECEMBER );
74
- cal .add (MONTH , 2 );
75
- year = cal .get (YEAR );
76
- check (year , base +1 , "add(+MONTH)" );
77
-
78
- cal = getBuddhistCalendar ();
79
- base = cal .get (YEAR );
80
- cal .set (MONTH , FEBRUARY );
81
- cal .add (MONTH , -4 );
82
- year = cal .get (YEAR );
83
- check (year , base -1 , "add(-MONTH)" );
84
-
85
- /*
86
- * Test BuddhistCalendar.roll(YEAR)
87
- */
88
- cal = getBuddhistCalendar ();
89
- base = cal .get (YEAR );
90
- cal .roll (YEAR , 2 );
91
- year = cal .get (YEAR );
92
- check (year , base +2 , "roll(+YEAR)" );
93
-
94
- cal = getBuddhistCalendar ();
95
- base = cal .get (YEAR );
96
- cal .roll (YEAR , -4 );
97
- year = cal .get (YEAR );
98
- check (year , base -4 , "roll(-YEAR)" );
99
-
100
- /*
101
- * Test BuddhistCalendar.roll(WEEK_OF_YEAR)
102
- */
103
- cal = getBuddhistCalendar ();
104
- cal .set (YEAR , 2543 ); // A.D.2000
105
- cal .set (MONTH , DECEMBER );
106
- cal .set (DATE , 31 );
107
- base = cal .get (YEAR );
108
- check (base , 2543 , "roll(+WEEK_OF_YEAR)" );
109
- cal .roll (WEEK_OF_YEAR , 10 );
110
- year = cal .get (YEAR );
111
- check (year , base , "roll(+WEEK_OF_YEAR)" );
112
-
113
- cal = getBuddhistCalendar ();
114
- cal .set (YEAR , 2543 ); // A.D.2000
115
- cal .set (MONTH , JANUARY );
116
- cal .set (DATE , 1 );
117
- base = cal .get (YEAR );
118
- check (base , 2543 , "roll(+WEEK_OF_YEAR)" );
119
- cal .roll (WEEK_OF_YEAR , -10 );
120
- year = cal .get (YEAR );
121
- check (year , base , "roll(-WEEK_OF_YEAR)" );
122
-
123
- /*
124
- * Test Calendar.set(year, month, date)
125
- */
126
- cal = getBuddhistCalendar ();
127
- base = cal .get (YEAR );
153
+ private static Stream <Arguments > alternateRollProvider () {
154
+ return Stream .of (
155
+ Arguments .of (getBuddhistCalendarBuilder ().set (YEAR , 2543 )
156
+ .set (MONTH , DECEMBER ).set (DATE , 31 ).build (), 10 , WEEK_OF_YEAR ),
157
+ Arguments .of (getBuddhistCalendarBuilder ().set (YEAR , 2543 )
158
+ .set (MONTH , JANUARY ).set (DATE , 1 ).build (), -10 , WEEK_OF_YEAR )
159
+ );
160
+ }
161
+
162
+ // Test the overloaded set() methods. Check year value.
163
+ @ Test
164
+ public void buddhistSetTest () {
165
+ Calendar cal = getBuddhistCalendar ();
128
166
cal .set (3001 , APRIL , 10 );
129
- year = cal .get (YEAR );
130
- check (year , 3001 , "set(year, month, date)" );
131
-
132
- /*
133
- * Test Calendar.set(year, month, date, hour, minute)
134
- */
135
- cal = getBuddhistCalendar ();
136
- base = cal .get (YEAR );
167
+ assertEquals (cal .get (YEAR ), 3001 );
137
168
cal .set (3020 , MAY , 20 , 9 , 10 );
138
- year = cal .get (YEAR );
139
- check (year , 3020 , "set(year, month, date, hour, minute)" );
140
-
141
- /*
142
- * Test Calendar.set(year, month, date, hour, minute, second)
143
- */
144
- cal = getBuddhistCalendar ();
145
- base = cal .get (YEAR );
146
- cal .set (3120 , MAY , 20 , 9 , 10 , 52 );
147
- year = cal .get (YEAR );
148
- check (year , 3120 , "set(year, month, date, hour, minute, second)" );
149
-
150
- /*
151
- * Test BuddhistCalendar.getActualMaximum(YEAR);
152
- * set(YEAR)/get(YEAR) in this method doesn't affect the real
153
- * YEAR value because a clone is used with set()&get().
154
- */
155
- cal = getBuddhistCalendar ();
156
- base = cal .get (YEAR );
157
- int limit = cal .getActualMaximum (YEAR );
158
- year = cal .get (YEAR );
159
- check (year , base , "BuddhistCalendar.getActualMaximum(YEAR)" );
160
-
161
- /*
162
- * Test BuddhistCalendar.getActualMinimum(YEAR);
163
- * This doesn't call set(YEAR) nor get(YEAR), though.
164
- */
165
- cal = getBuddhistCalendar ();
166
- base = cal .get (YEAR );
167
- limit = cal .getActualMinimum (YEAR );
168
- year = cal .get (YEAR );
169
- check (year , base , "BuddhistCalendar.getActualMinimum(YEAR)" );
170
- }
171
-
172
- /**
173
- * 4847186: BuddhistCalendar: toString() returns Gregorian year
169
+ assertEquals (cal .get (YEAR ), 3020 );
170
+ cal .set (3120 , MAY , 20 , 9 , 10 , 52 );
171
+ assertEquals (cal .get (YEAR ), 3120 );
172
+ }
173
+
174
+ /*
175
+ * Test BuddhistCalendar.getActualMaximum(YEAR);
176
+ * set(YEAR)/get(YEAR) in this method doesn't affect the real
177
+ * YEAR value because a clone is used with set() and get().
174
178
*/
175
- static void testToString () {
179
+ @ Test
180
+ public void buddhistActualMaximumTest () {
181
+ Calendar cal = getBuddhistCalendar ();
182
+ int base = cal .get (YEAR );
183
+ int ignored = cal .getActualMaximum (YEAR );
184
+ int year = cal .get (YEAR );
185
+ assertEquals (year , base , "BuddhistCalendar.getActualMaximum(YEAR)" );
186
+ }
187
+
188
+ // Test BuddhistCalendar.getActualMinimum(YEAR), doesn't call set(YEAR) nor get(YEAR).
189
+ @ Test
190
+ public void buddhistActualMinimumTest () {
191
+ Calendar cal = getBuddhistCalendar ();
192
+ int base = cal .get (YEAR );
193
+ int ignored = cal .getActualMinimum (YEAR );
194
+ int year = cal .get (YEAR );
195
+ assertEquals (year , base , "BuddhistCalendar.getActualMinimum(YEAR)" );
196
+ }
197
+
198
+ // 4847186: BuddhistCalendar: toString() returns Gregorian year
199
+ @ Test
200
+ public void buddhistToStringTest () {
176
201
Calendar cal = getBuddhistCalendar ();
177
202
int year = cal .get (YEAR );
178
203
String s = cal .toString ();
179
204
String y = s .replaceAll (".+,YEAR=(\\ d+),.+" , "$1" );
180
- if (Integer .parseInt (y ) != year ) {
181
- throw new RuntimeException ("toString(): wrong year value: got " + y
182
- + ", expected " + year );
183
- }
205
+ assertEquals (year , Integer .parseInt (y ), "Wrong year value" );
184
206
}
185
207
186
- /**
187
- * 4956479: BuddhistCalendar methods may return wrong values after exception
188
- */
189
- static void testException () {
208
+ // 4956479: BuddhistCalendar methods may return wrong values after exception
209
+ @ Test
210
+ public void buddhistValuesAfterExceptionTest () {
190
211
Calendar cal = getBuddhistCalendar ();
191
212
int year = cal .get (YEAR );
192
- boolean exceptionOccurred = false ;
193
- try {
194
- cal .add (100 , +1 ); // cause exception
195
- } catch (Exception e ) {
196
- exceptionOccurred = true ;
197
- }
198
- if (!exceptionOccurred ) {
199
- throw new RuntimeException ("testException: test case failed: no exception thrown" );
200
- }
213
+ assertThrows (IllegalArgumentException .class , ()-> cal .add (100 , +1 ));
201
214
int year2 = cal .get (YEAR );
202
- if (year2 != year ) {
203
- throw new RuntimeException ("wrong year value after exception: got " + year2
204
- + ", expected " + year );
205
- }
215
+ assertEquals (year2 , year , "Wrong year value after exception thrown" );
206
216
}
207
217
208
- /**
209
- * 4956227: getLeastMaximum(WEEK_OF_MONTH) return diff. val. for Greg. and Buddhist Calendar
210
- */
211
- static void testLeastMax () {
218
+ // 4956227: getLeastMaximum(WEEK_OF_MONTH) return diff. val. for Greg. and Buddhist Calendar
219
+ @ Test
220
+ public void buddhistLeastMaximumTest () {
212
221
Calendar bc = getBuddhistCalendar ();
213
222
// Specify THAI_LOCALE to get the same params for WEEK
214
223
// calculations (6904680).
@@ -219,25 +228,17 @@ static void testLeastMax() {
219
228
}
220
229
int bn = bc .getLeastMaximum (f );
221
230
int gn = gc .getLeastMaximum (f );
222
- if (bn != gn ) {
223
- throw new RuntimeException ("inconsistent Least Max value for " + Koyomi .getFieldName (f )
224
- + ": Buddhist=" + bn
225
- + ": Gregorian=" + gn );
226
- }
231
+ assertEquals (bn , gn , "Inconsistent Least Max value for " + Koyomi .getFieldName (f ));
227
232
}
228
233
}
229
234
230
- /**
231
- * @return a BuddhistCalendar
232
- */
233
- static Calendar getBuddhistCalendar () {
234
- return Calendar .getInstance (THAI_LOCALE );
235
+ // Utility to get a new Buddhist Calendar Builder (to allow setting of other values)
236
+ private static Calendar .Builder getBuddhistCalendarBuilder () {
237
+ return new Calendar .Builder ().setLocale (THAI_LOCALE );
235
238
}
236
239
237
- static void check (int got , int expected , String s ) {
238
- if (got != expected ) {
239
- throw new RuntimeException ("Failed: " +
240
- s + ": got:" + got + ", expected:" + expected );
241
- }
240
+ // Utility to get a new Buddhist calendar
241
+ private static Calendar getBuddhistCalendar () {
242
+ return Calendar .getInstance (THAI_LOCALE );
242
243
}
243
244
}
0 commit comments