Skip to content

Commit 355811a

Browse files
author
Justin Lu
committedSep 28, 2023
8316559: Refactor some util/Calendar tests to JUnit
Reviewed-by: naoto, lancea
1 parent ecb5e8a commit 355811a

10 files changed

+554
-474
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,191 +24,200 @@
2424
/*
2525
* @test
2626
* @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
2830
*/
2931

3032
import java.util.Calendar;
3133
import java.util.GregorianCalendar;
3234
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;
3456

3557
public class BuddhistCalendarTest {
3658

3759
private static final Locale THAI_LOCALE = Locale.of("th", "TH");
3860

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");
44147
}
45148

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.
48152
*/
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();
128166
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);
137168
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().
174178
*/
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() {
176201
Calendar cal = getBuddhistCalendar();
177202
int year = cal.get(YEAR);
178203
String s = cal.toString();
179204
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");
184206
}
185207

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() {
190211
Calendar cal = getBuddhistCalendar();
191212
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));
201214
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");
206216
}
207217

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() {
212221
Calendar bc = getBuddhistCalendar();
213222
// Specify THAI_LOCALE to get the same params for WEEK
214223
// calculations (6904680).
@@ -219,25 +228,17 @@ static void testLeastMax() {
219228
}
220229
int bn = bc.getLeastMaximum(f);
221230
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));
227232
}
228233
}
229234

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);
235238
}
236239

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);
242243
}
243244
}

‎test/jdk/java/util/Calendar/Bug4302966.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,18 +26,22 @@
2626
* @bug 4302966 8176841
2727
* @modules jdk.localedata
2828
* @summary In Czech Republic first day of week is Monday not Sunday
29+
* @run junit Bug4302966
2930
*/
3031

3132
import java.util.Calendar;
3233
import java.util.Locale;
3334

35+
import org.junit.jupiter.api.Test;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
3439
public class Bug4302966 {
3540

36-
public static void main(String[] args) {
41+
// Specific day of week test for Czech locale
42+
public void czechDayOfWeekTest() {
3743
Calendar czechCalendar = Calendar.getInstance(Locale.of("cs", "CZ"));
3844
int firstDayOfWeek = czechCalendar.getFirstDayOfWeek();
39-
if (firstDayOfWeek != Calendar.MONDAY) {
40-
throw new RuntimeException();
41-
}
45+
assertEquals(firstDayOfWeek, Calendar.MONDAY);
4246
}
4347
}

‎test/jdk/java/util/Calendar/Bug4766302.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,16 +24,21 @@
2424
/*
2525
* @test
2626
* @bug 4766302
27-
* @summary Make sure that computeTime call doesn't reset the isTimeSet value.
27+
* @summary Make sure that calling computeTime doesn't reset the isTimeSet value.
28+
* @run junit Bug4766302
2829
*/
2930

3031
import java.util.GregorianCalendar;
3132

32-
@SuppressWarnings("serial")
33+
import org.junit.jupiter.api.Test;
34+
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
3337
public class Bug4766302 {
3438

39+
// Extend GregorianCalendar to check the protected value of isTimeSet
40+
@SuppressWarnings("serial")
3541
static class MyCalendar extends GregorianCalendar {
36-
3742
boolean isTimeStillSet() {
3843
return isTimeSet;
3944
}
@@ -43,11 +48,11 @@ protected void computeTime() {
4348
}
4449
}
4550

46-
public static void main(String[] args) {
51+
// Check the value of isTimeStillSet() after calling computeTime()
52+
@Test
53+
public void validateIsTimeSetTest() {
4754
MyCalendar cal = new MyCalendar();
4855
cal.computeTime();
49-
if (!cal.isTimeStillSet()) {
50-
throw new RuntimeException("computeTime() call reset isTimeSet.");
51-
}
56+
assertTrue(cal.isTimeStillSet(), "computeTime() call reset isTimeSet.");
5257
}
5358
}
+18-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,33 +24,30 @@
2424
/*
2525
* @test
2626
* @bug 4028518
27-
* @summary Make sure cloned GregorianCalendar is unchanged by modifying its original.
27+
* @summary Ensure cloned GregorianCalendar is unchanged when modifying its original.
28+
* @run junit bug4028518
2829
*/
2930

30-
import java.util.GregorianCalendar ;
31-
import static java.util.Calendar.*;
31+
import java.util.GregorianCalendar;
32+
33+
import static java.util.Calendar.DAY_OF_MONTH;
34+
35+
import org.junit.jupiter.api.Test;
36+
37+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3238

3339
public class bug4028518 {
3440

35-
public static void main(String[] args)
36-
{
41+
/*
42+
* Ensure modifying the original GregorianCalendar does not
43+
* modify the cloned one as well
44+
*/
45+
@Test
46+
public void clonedShouldNotChangeOriginalTest() {
3747
GregorianCalendar cal1 = new GregorianCalendar() ;
3848
GregorianCalendar cal2 = (GregorianCalendar) cal1.clone() ;
39-
40-
printdate(cal1, "cal1: ") ;
41-
printdate(cal2, "cal2 - cloned(): ") ;
4249
cal1.add(DAY_OF_MONTH, 1) ;
43-
printdate(cal1, "cal1 after adding 1 day: ") ;
44-
printdate(cal2, "cal2 should be unmodified: ") ;
45-
if (cal1.get(DAY_OF_MONTH) == cal2.get(DAY_OF_MONTH)) {
46-
throw new RuntimeException("cloned GregorianCalendar modified");
47-
}
48-
}
49-
50-
private static void printdate(GregorianCalendar cal, String string)
51-
{
52-
System.out.println(string + (cal.get(MONTH) + 1)
53-
+ "/" + cal.get(DAY_OF_MONTH)
54-
+ "/" + cal.get(YEAR)) ;
50+
assertNotEquals(cal1.get(DAY_OF_MONTH), cal2.get(DAY_OF_MONTH),
51+
"Cloned calendar should not have same value as original");
5552
}
5653
}

‎test/jdk/java/util/Calendar/bug4100311.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,24 +24,29 @@
2424
/*
2525
* @test
2626
* @bug 4100311
27-
* @summary Make sure set(DAY_OF_YEAR, 1) works.
27+
* @summary Ensure set(DAY_OF_YEAR, 1) works.
28+
* @run junit bug4100311
2829
*/
2930

3031
import java.util.Calendar;
3132
import java.util.GregorianCalendar;
3233
import java.util.Date;
3334

34-
public class bug4100311
35-
{
35+
import org.junit.jupiter.api.Test;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
39+
public class bug4100311 {
40+
41+
// GregorianCalendar should be able to date to january 1st properly
3642
@SuppressWarnings("deprecation")
37-
public static void main(String args[])
38-
{
43+
@Test
44+
public void dayOfYearIsOneTest() {
3945
GregorianCalendar cal = new GregorianCalendar();
4046
cal.set(Calendar.YEAR, 1997);
4147
cal.set(Calendar.DAY_OF_YEAR, 1);
42-
Date d = cal.getTime(); // Should be Jan 1
43-
if (d.getMonth() != 0 || d.getDate() != 1) {
44-
throw new RuntimeException("Date isn't Jan 1");
45-
}
48+
Date d = cal.getTime();
49+
assertEquals(0, d.getMonth(), "Date: "+d+" isn't January 1st");
50+
assertEquals(1, d.getDate(),"Date: "+d+" isn't January 1st");
4651
}
4752
}
+67-49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,70 +24,88 @@
2424
/*
2525
* @test
2626
* @bug 4243802
27-
* @summary confirm that Calendar.setTimeInMillis() and
28-
* getTimeInMillis() can be called from a user program. (They used to
29-
* be protected methods.)
30-
* @library /java/text/testlib
27+
* @summary confirm that Calendar.setTimeInMillis() and getTimeInMillis()
28+
* can be called from a user program. They used to be protected methods.
29+
* @run junit bug4243802
3130
*/
3231

33-
import java.util.*;
32+
import java.util.Calendar;
33+
import java.util.Locale;
34+
import java.util.TimeZone;
3435

35-
public class bug4243802 extends IntlTest {
36+
import org.junit.jupiter.api.AfterAll;
37+
import org.junit.jupiter.api.BeforeAll;
3638

37-
public static void main(String[] args) throws Exception {
38-
new bug4243802().run(args);
39+
import org.junit.jupiter.api.Test;
40+
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
43+
public class bug4243802 {
44+
45+
private static final TimeZone savedTz = TimeZone.getDefault();
46+
private static final Locale savedLocale = Locale.getDefault();
47+
48+
// Save JVM default Locale and TimeZone
49+
@BeforeAll
50+
static void initAll() {
51+
Locale.setDefault(Locale.US);
52+
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
53+
}
54+
55+
// Restore JVM default Locale and TimeZone
56+
@AfterAll
57+
static void tearDownAll() {
58+
Locale.setDefault(savedLocale);
59+
TimeZone.setDefault(savedTz);
3960
}
4061

41-
/**
42-
* 4243802: RFE: need way to set the date of a calendar without a Date object
62+
/*
63+
* Test getTimeInMillis() and setTimeInMillis(). Compare a Calendar
64+
* set with a traditional date to one set using setTimeInMillis(),
65+
* where both Calendars should be of equal times.
4366
*/
44-
public void Test4243802() {
45-
TimeZone saveZone = TimeZone.getDefault();
46-
Locale saveLocale = Locale.getDefault();
47-
try {
48-
Locale.setDefault(Locale.US);
49-
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
67+
@Test
68+
public void setCalendarWithoutDateTest() {
69+
Calendar cal1 = Calendar.getInstance();
70+
Calendar cal2 = Calendar.getInstance();
5071

51-
Calendar cal1 = Calendar.getInstance();
52-
Calendar cal2 = Calendar.getInstance();
72+
cal1.clear();
73+
cal2.clear();
5374

54-
cal1.clear();
55-
cal2.clear();
56-
cal1.set(2001, Calendar.JANUARY, 25, 1, 23, 45);
57-
cal2.setTimeInMillis(cal1.getTimeInMillis());
58-
if ((cal2.get(Calendar.YEAR) != 2001) ||
59-
(cal2.get(Calendar.MONTH) != Calendar.JANUARY) ||
60-
(cal2.get(Calendar.DAY_OF_MONTH) != 25) ||
61-
(cal2.get(Calendar.HOUR_OF_DAY) != 1) ||
62-
(cal2.get(Calendar.MINUTE) != 23) ||
63-
(cal2.get(Calendar.SECOND) != 45) ||
64-
(cal2.get(Calendar.MILLISECOND) != 0)) {
65-
errln("Failed: expected 1/25/2001 1:23:45.000" +
66-
", got " + (cal2.get(Calendar.MONTH)+1) + "/" +
67-
cal2.get(Calendar.DAY_OF_MONTH) +"/" +
68-
cal2.get(Calendar.YEAR) + " " +
69-
cal2.get(Calendar.HOUR_OF_DAY) + ":" +
70-
cal2.get(Calendar.MINUTE) + ":" +
71-
cal2.get(Calendar.SECOND) + "." +
72-
toMillis(cal2.get(Calendar.MILLISECOND)));
73-
}
74-
logln("Passed.");
75-
}
76-
finally {
77-
Locale.setDefault(saveLocale);
78-
TimeZone.setDefault(saveZone);
79-
}
75+
cal1.set(2001, Calendar.JANUARY, 25, 1, 23, 45);
76+
// Build the second calendar using the getTimeInMillis of the first
77+
cal2.setTimeInMillis(cal1.getTimeInMillis());
78+
79+
assertEquals(2001, cal2.get(Calendar.YEAR), getErrMsg(cal1));
80+
assertEquals(Calendar.JANUARY, cal2.get(Calendar.MONTH), getErrMsg(cal1));
81+
assertEquals(25, cal2.get(Calendar.DAY_OF_MONTH), getErrMsg(cal1));
82+
assertEquals(1, cal2.get(Calendar.HOUR_OF_DAY), getErrMsg(cal1));
83+
assertEquals(23, cal2.get(Calendar.MINUTE), getErrMsg(cal1));
84+
assertEquals(45, cal2.get(Calendar.SECOND), getErrMsg(cal1));
85+
assertEquals(0, cal2.get(Calendar.MILLISECOND), getErrMsg(cal1));
86+
}
87+
88+
// Utility to build a long error message
89+
private static String getErrMsg(Calendar cal) {
90+
return "Failed: expected 1/25/2001 1:23:45.000" +
91+
", got " + (cal.get(Calendar.MONTH)+1) + "/" +
92+
cal.get(Calendar.DAY_OF_MONTH) +"/" +
93+
cal.get(Calendar.YEAR) + " " +
94+
cal.get(Calendar.HOUR_OF_DAY) + ":" +
95+
cal.get(Calendar.MINUTE) + ":" +
96+
cal.get(Calendar.SECOND) + "." +
97+
toMillis(cal.get(Calendar.MILLISECOND));
8098
}
8199

82-
private String toMillis(int m) {
83-
StringBuffer sb = new StringBuffer();
100+
// Utility to convert value to format of expected milisecond value
101+
private static String toMillis(int m) {
102+
StringBuilder sb = new StringBuilder();
84103
if (m < 100) {
85104
sb.append('0');
86105
}
87106
if (m < 10) {
88107
sb.append('0');
89108
}
90-
sb.append(m);
91-
return sb.toString();
109+
return sb.append(m).toString();
92110
}
93111
}
+48-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,49 +21,62 @@
2121
* questions.
2222
*/
2323

24-
import java.io.*;
25-
import java.util.*;
26-
import java.text.*;
27-
28-
/**
24+
/*
2925
* @test
3026
* @bug 4316678
31-
* @summary test that Calendar's Serializasion works correctly.
32-
* @library /java/text/testlib
27+
* @summary test that Calendar's Serialization works correctly.
28+
* @run junit bug4316678
3329
*/
34-
public class bug4316678 extends IntlTest {
3530

36-
public static void main(String[] args) throws Exception {
37-
new bug4316678().run(args);
38-
}
31+
import java.io.FileInputStream;
32+
import java.io.FileOutputStream;
33+
import java.io.IOException;
34+
import java.io.ObjectInputStream;
35+
import java.io.ObjectOutputStream;
36+
import java.util.Calendar;
37+
import java.util.GregorianCalendar;
38+
import java.util.TimeZone;
3939

40-
public void Test4316678() throws Exception {
41-
GregorianCalendar gc1;
42-
GregorianCalendar gc2;
43-
TimeZone saveZone = TimeZone.getDefault();
40+
import org.junit.jupiter.api.AfterAll;
41+
import org.junit.jupiter.api.BeforeAll;
42+
import org.junit.jupiter.api.Test;
43+
44+
import static org.junit.jupiter.api.Assertions.assertEquals;
45+
46+
public class bug4316678 {
4447

45-
try {
46-
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
48+
private static final String serializedData = "bug4316678.ser";
49+
private static final TimeZone savedTz = TimeZone.getDefault();
4750

48-
gc1 = new GregorianCalendar(2000, Calendar.OCTOBER, 10);
49-
try (ObjectOutputStream out
50-
= new ObjectOutputStream(new FileOutputStream("bug4316678.ser"))) {
51-
out.writeObject(gc1);
52-
}
51+
// Save JVM default Locale and TimeZone
52+
@BeforeAll
53+
static void initAll() {
54+
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
55+
}
56+
57+
// Restore JVM default Locale and TimeZone
58+
@AfterAll
59+
static void tearDownAll() {
60+
TimeZone.setDefault(savedTz);
61+
}
5362

54-
try (ObjectInputStream in
55-
= new ObjectInputStream(new FileInputStream("bug4316678.ser"))) {
56-
gc2 = (GregorianCalendar)in.readObject();
57-
}
63+
// Test that a serialized GregorianCalendar has the expected values
64+
@Test
65+
public void serializationTest() throws IOException, ClassNotFoundException {
66+
GregorianCalendar gc1 = new GregorianCalendar(2000, Calendar.OCTOBER, 10);
67+
GregorianCalendar gc2;
68+
try (ObjectOutputStream out
69+
= new ObjectOutputStream(new FileOutputStream(serializedData))) {
70+
out.writeObject(gc1);
71+
}
5872

59-
gc1.set(Calendar.DATE, 16);
60-
gc2.set(Calendar.DATE, 16);
61-
if (!gc1.getTime().equals(gc2.getTime())) {
62-
errln("Invalid Time :" + gc2.getTime() +
63-
", expected :" + gc1.getTime());
64-
}
65-
} finally {
66-
TimeZone.setDefault(saveZone);
73+
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(serializedData))) {
74+
gc2 = (GregorianCalendar)in.readObject();
6775
}
76+
77+
gc1.set(Calendar.DATE, 16);
78+
gc2.set(Calendar.DATE, 16);
79+
assertEquals(gc2.getTime(), gc1.getTime(),
80+
"Times should be equal after serialization");
6881
}
6982
}
+93-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,21 +25,106 @@
2525
* @test
2626
* @bug 4372743
2727
* @summary test that checks transitions of ERA and YEAR which are caused by add(MONTH).
28-
* @library /java/text/testlib
28+
* @run junit bug4372743
2929
*/
3030

3131
import java.util.GregorianCalendar;
3232
import java.util.TimeZone;
33+
import java.util.stream.Stream;
3334

34-
import static java.util.GregorianCalendar.*;
35+
import static java.util.GregorianCalendar.AD;
36+
import static java.util.GregorianCalendar.APRIL;
37+
import static java.util.GregorianCalendar.AUGUST;
38+
import static java.util.GregorianCalendar.BC;
39+
import static java.util.GregorianCalendar.DECEMBER;
40+
import static java.util.GregorianCalendar.ERA;
41+
import static java.util.GregorianCalendar.FEBRUARY;
42+
import static java.util.GregorianCalendar.JANUARY;
43+
import static java.util.GregorianCalendar.JULY;
44+
import static java.util.GregorianCalendar.JUNE;
45+
import static java.util.GregorianCalendar.MARCH;
46+
import static java.util.GregorianCalendar.MAY;
47+
import static java.util.GregorianCalendar.MONTH;
48+
import static java.util.GregorianCalendar.NOVEMBER;
49+
import static java.util.GregorianCalendar.OCTOBER;
50+
import static java.util.GregorianCalendar.SEPTEMBER;
51+
import static java.util.GregorianCalendar.YEAR;
3552

36-
public class bug4372743 extends IntlTest {
53+
import org.junit.jupiter.api.AfterAll;
54+
import org.junit.jupiter.api.BeforeAll;
3755

38-
public static void main(String[] args) throws Exception {
39-
new bug4372743().run(args);
56+
import org.junit.jupiter.params.ParameterizedTest;
57+
import org.junit.jupiter.params.provider.Arguments;
58+
import org.junit.jupiter.params.provider.MethodSource;
59+
60+
import static org.junit.jupiter.api.Assertions.assertEquals;
61+
62+
public class bug4372743 {
63+
64+
private static final TimeZone savedTz = TimeZone.getDefault();
65+
66+
// Save JVM default Locale and TimeZone
67+
@BeforeAll
68+
static void initAll() {
69+
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
70+
}
71+
72+
// Restore JVM default Locale and TimeZone
73+
@AfterAll
74+
static void tearDownAll() {
75+
TimeZone.setDefault(savedTz);
4076
}
4177

42-
private int[][] data = {
78+
/*
79+
* Set GregorianCalendar to (March 3, A.D. 2) and test adding
80+
* to the month field. Ensure that the added field is as expected.
81+
*/
82+
@ParameterizedTest
83+
@MethodSource("A_D_Values")
84+
public void A_D_Test(GregorianCalendar gc, int monthValue) {
85+
for (int i = 0; i < tableSize; i+=(-monthValue)) {
86+
check(gc, i);
87+
gc.add(MONTH, monthValue);
88+
}
89+
}
90+
91+
// Given in format: (A.D.) GregorianCalendar, amount to add
92+
private static Stream<Arguments> A_D_Values() {
93+
return Stream.of(
94+
Arguments.of(new GregorianCalendar(2, MARCH, 3), -1),
95+
Arguments.of(new GregorianCalendar(2, MARCH, 3), -7));
96+
}
97+
98+
/*
99+
* Set GregorianCalendar to (March 10, 2 B.C.) and test adding
100+
* to the month field. Ensure that the added field is as expected.
101+
*/
102+
@ParameterizedTest
103+
@MethodSource("B_C_Values")
104+
public void B_C_Test(GregorianCalendar gc, int monthValue) {
105+
gc.add(YEAR, -3);
106+
for (int i = tableSize - 1; i >= 0; i-=monthValue) {
107+
check(gc, i);
108+
gc.add(MONTH, monthValue);
109+
}
110+
}
111+
112+
// Given in format: (B.C.) GregorianCalendar, amount to add
113+
private static Stream<Arguments> B_C_Values() {
114+
return Stream.of(
115+
Arguments.of(new GregorianCalendar(2, OCTOBER, 10), 1),
116+
Arguments.of(new GregorianCalendar(2, OCTOBER, 10), 8));
117+
}
118+
119+
// Check golden data array with actual value
120+
private void check(GregorianCalendar gc, int index) {
121+
assertEquals(data[index][ERA], gc.get(ERA), "Invalid era");
122+
assertEquals(data[index][YEAR], gc.get(YEAR), "Invalid year");
123+
assertEquals(data[index][MONTH], gc.get(MONTH), "Invalid month");
124+
}
125+
126+
// Expected ERA, YEAR, and MONTH combinations
127+
private final int[][] data = {
43128
{AD, 2, MARCH},
44129
{AD, 2, FEBRUARY},
45130
{AD, 2, JANUARY},
@@ -70,61 +155,5 @@ public static void main(String[] args) throws Exception {
70155
{BC, 2, DECEMBER},
71156
{BC, 2, NOVEMBER},
72157
{BC, 2, OCTOBER}};
73-
private int tablesize = data.length;
74-
75-
private void check(GregorianCalendar gc, int index) {
76-
if (gc.get(ERA) != data[index][ERA]) {
77-
errln("Invalid era :" + gc.get(ERA)
78-
+ ", expected :" + data[index][ERA]);
79-
}
80-
if (gc.get(YEAR) != data[index][YEAR]) {
81-
errln("Invalid year :" + gc.get(YEAR)
82-
+ ", expected :" + data[index][YEAR]);
83-
}
84-
if (gc.get(MONTH) != data[index][MONTH]) {
85-
errln("Invalid month :" + gc.get(MONTH)
86-
+ ", expected :" + data[index][MONTH]);
87-
}
88-
}
89-
90-
public void Test4372743() {
91-
GregorianCalendar gc;
92-
TimeZone saveZone = TimeZone.getDefault();
93-
94-
try {
95-
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
96-
97-
/* Set March 3, A.D. 2 */
98-
gc = new GregorianCalendar(2, MARCH, 3);
99-
for (int i = 0; i < tablesize; i++) {
100-
check(gc, i);
101-
gc.add(MONTH, -1);
102-
}
103-
104-
/* Again, Set March 3, A.D. 2 */
105-
gc = new GregorianCalendar(2, MARCH, 3);
106-
for (int i = 0; i < tablesize; i += 7) {
107-
check(gc, i);
108-
gc.add(MONTH, -7);
109-
}
110-
111-
/* Set March 10, 2 B.C. */
112-
gc = new GregorianCalendar(2, OCTOBER, 10);
113-
gc.add(YEAR, -3);
114-
for (int i = tablesize - 1; i >= 0; i--) {
115-
check(gc, i);
116-
gc.add(MONTH, 1);
117-
}
118-
119-
/* Again, Set March 10, 2 B.C. */
120-
gc = new GregorianCalendar(2, OCTOBER, 10);
121-
gc.add(YEAR, -3);
122-
for (int i = tablesize - 1; i >= 0; i -= 8) {
123-
check(gc, i);
124-
gc.add(MONTH, 8);
125-
}
126-
} finally {
127-
TimeZone.setDefault(saveZone);
128-
}
129-
}
158+
private final int tableSize = data.length;
130159
}
+37-51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,73 +24,59 @@
2424
/*
2525
* @test
2626
* @bug 4401223
27-
* @summary Make sure that GregorianCalendar doesn't cause IllegalArgumentException at some special situations which are related to the Leap Year.
28-
* @library /java/text/testlib
27+
* @summary Make sure that GregorianCalendar doesn't cause
28+
* IllegalArgumentException at some special situations which are
29+
* related to the Leap Year.
30+
* @run junit bug4401223
2931
*/
3032

3133
import java.util.Date;
3234
import java.util.GregorianCalendar;
3335

34-
import static java.util.GregorianCalendar.*;
36+
import static java.util.GregorianCalendar.DATE;
37+
import static java.util.GregorianCalendar.DAY_OF_YEAR;
38+
import static java.util.GregorianCalendar.DECEMBER;
39+
import static java.util.GregorianCalendar.FEBRUARY;
40+
import static java.util.GregorianCalendar.MONTH;
41+
import static java.util.GregorianCalendar.YEAR;
3542

36-
public class bug4401223 extends IntlTest {
43+
import org.junit.jupiter.api.Test;
3744

38-
public void Test4401223a() {
39-
int status = 0;
40-
String s = null;
45+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
46+
import static org.junit.jupiter.api.Assertions.assertEquals;
4147

42-
try {
43-
@SuppressWarnings("deprecation")
44-
Date date = new Date(2000 - 1900, FEBRUARY, 29);
45-
GregorianCalendar gc = new GregorianCalendar();
48+
public class bug4401223 {
49+
50+
// Ensure IAE not thrown for date: 12-29-00
51+
@SuppressWarnings("deprecation")
52+
@Test
53+
public void checkExceptionTest() {
54+
Date date = new Date(2000 - 1900, FEBRUARY, 29);
55+
GregorianCalendar gc = new GregorianCalendar();
56+
assertDoesNotThrow(() -> {
4657
gc.setTime(date);
4758
gc.setLenient(false);
4859
gc.set(YEAR, 2001);
49-
s = "02/29/00 & set(YEAR,2001) = " + gc.getTime().toString();
50-
} catch (Exception ex) {
51-
status++;
52-
s = "Exception occurred for 2/29/00 & set(YEAR,2001): " + ex;
53-
}
54-
if (status > 0) {
55-
errln(s);
56-
} else {
57-
logln(s);
58-
}
60+
}, "Exception occurred for 2/29/00 & set(YEAR,2001)");
5961
}
6062

61-
public void Test4401223b() {
62-
int status = 0;
63-
String s = null;
64-
65-
try {
66-
@SuppressWarnings("deprecation")
67-
Date date = new Date(2000 - 1900, DECEMBER, 31);
68-
GregorianCalendar gc = new GregorianCalendar();
63+
// Ensure IAE not thrown for date: 12-31-00. Validate expected values.
64+
@SuppressWarnings("deprecation")
65+
@Test
66+
public void checkExceptionAndValuesTest() {
67+
Date date = new Date(2000 - 1900, DECEMBER, 31);
68+
GregorianCalendar gc = new GregorianCalendar();
69+
assertDoesNotThrow(() -> {
6970
gc.setTime(date);
7071
gc.setLenient(false);
7172
gc.set(YEAR, 2001);
73+
}, "Exception occurred for 12/31/00 & set(YEAR,2001)");
7274

73-
if (gc.get(YEAR) != 2001
74-
|| gc.get(MONTH) != DECEMBER
75-
|| gc.get(DATE) != 31
76-
|| gc.get(DAY_OF_YEAR) != 365) {
77-
status++;
78-
s = "Wrong Date : 12/31/00 & set(YEAR,2001) ---> " + gc.getTime().toString();
79-
} else {
80-
s = "12/31/00 & set(YEAR,2001) = " + gc.getTime().toString();
81-
}
82-
} catch (Exception ex) {
83-
status++;
84-
s = "Exception occurred for 12/31/00 & set(YEAR,2001) : " + ex;
85-
}
86-
if (status > 0) {
87-
errln(s);
88-
} else {
89-
logln(s);
90-
}
91-
}
75+
String errMsg = "Wrong date, got: " + gc.getTime();
9276

93-
public static void main(String[] args) throws Exception {
94-
new bug4401223().run(args);
77+
assertEquals(2001, gc.get(YEAR), errMsg);
78+
assertEquals(DECEMBER, gc.get(MONTH), errMsg);
79+
assertEquals(31, gc.get(DATE), errMsg);
80+
assertEquals(365, gc.get(DAY_OF_YEAR), errMsg);
9581
}
9682
}
+79-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,77 +24,99 @@
2424
/*
2525
* @test
2626
* @bug 4514831
27-
* @summary Confirm that GregorianCalendar.roll() works properly during transition from Daylight Saving Time to Standard Time.
27+
* @summary Confirm that GregorianCalendar.roll() works properly during
28+
* transition from Daylight Saving Time to Standard Time.
29+
* @run junit bug4514831
2830
*/
2931

3032
import java.util.GregorianCalendar;
3133
import java.util.Locale;
3234
import java.util.TimeZone;
3335

34-
import static java.util.GregorianCalendar.*;
36+
import static java.util.GregorianCalendar.DAY_OF_MONTH;
37+
import static java.util.GregorianCalendar.DAY_OF_WEEK;
38+
import static java.util.GregorianCalendar.DAY_OF_WEEK_IN_MONTH;
39+
import static java.util.GregorianCalendar.DAY_OF_YEAR;
40+
import static java.util.GregorianCalendar.OCTOBER;
41+
import static java.util.GregorianCalendar.THURSDAY;
3542

43+
import org.junit.jupiter.api.AfterAll;
44+
import org.junit.jupiter.api.BeforeAll;
45+
import org.junit.jupiter.api.Test;
3646

37-
public class bug4514831 {
38-
39-
public static void main(String[] args) {
40-
Locale savedLocale = Locale.getDefault();
41-
TimeZone savedTimeZone = TimeZone.getDefault();
42-
boolean err = false;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
4348

44-
String golden_data1 = "27-28 28-29 29-30 30-31 31-1 1-2 2-3 ";
45-
String golden_data2 = "27-28 28-29 29-30 30-31 31-25 25-26 26-27 ";
46-
String golden_data3 = "1-8 8-15 15-22 22-29 29-1 1-8 8-15 ";
49+
public class bug4514831 {
50+
// Data of 7 rolls in the form of a string for the respective field
51+
private static final String expectedDayOfYearData = "27-28 28-29 29-30 30-31 31-1 1-2 2-3 ";
52+
private static final String expectedDayOfWeekData = "27-28 28-29 29-30 30-31 31-25 25-26 26-27 ";
53+
private static final String expectedDayOfWeekInMonthData = "1-8 8-15 15-22 22-29 29-1 1-8 8-15 ";
54+
private static final TimeZone savedTz = TimeZone.getDefault();
55+
private static final Locale savedLocale = Locale.getDefault();
4756

48-
try {
49-
Locale.setDefault(Locale.US);
50-
TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
57+
// Save JVM default Locale and TimeZone
58+
@BeforeAll
59+
void initAll() {
60+
Locale.setDefault(Locale.US);
61+
TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
62+
}
5163

52-
String test_roll = "";
53-
GregorianCalendar c_roll = new GregorianCalendar(2001, OCTOBER, 27);
54-
for (int i = 0; i < 7; i++) {
55-
test_roll += c_roll.get(DAY_OF_MONTH) + "-";
56-
c_roll.roll(DAY_OF_YEAR, true);
57-
test_roll += c_roll.get(DAY_OF_MONTH) + " ";
58-
}
59-
if (!test_roll.equals(golden_data1)) {
60-
err = true;
61-
System.err.println("Wrong roll(DAY_OF_YEAR) transition: got "
62-
+ test_roll + "expected " + golden_data1);
63-
}
64+
// Restore JVM default Locale and TimeZone
65+
@AfterAll
66+
void tearDownAll() {
67+
Locale.setDefault(savedLocale);
68+
TimeZone.setDefault(savedTz);
69+
}
6470

65-
test_roll = "";
66-
c_roll = new GregorianCalendar(2001, OCTOBER, 27);
67-
c_roll.setFirstDayOfWeek(THURSDAY);
68-
for (int i = 0; i < 7; i++) {
69-
test_roll += c_roll.get(DAY_OF_MONTH) + "-";
70-
c_roll.roll(DAY_OF_WEEK, true);
71-
test_roll += c_roll.get(DAY_OF_MONTH) + " ";
72-
}
73-
if (!test_roll.equals(golden_data2)) {
74-
err = true;
75-
System.err.println("Wrong roll(DAY_OF_WEEK) transition: got "
76-
+ test_roll + "expected " + golden_data2);
77-
}
71+
/*
72+
* Test some roll values during transition (DAY_OF_YEAR field). Uses
73+
* the boolean roll method. Roll multiple times and attach the returned
74+
* values to a long string which is then compared to the expected data.
75+
*/
76+
public void rollDayOfYearTest() {
77+
StringBuilder actualRollData = new StringBuilder();
78+
GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 27);
79+
for (int i = 0; i < 7; i++) {
80+
actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
81+
cal.roll(DAY_OF_YEAR, true);
82+
actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
83+
}
84+
assertEquals(expectedDayOfYearData, actualRollData.toString(),
85+
"Wrong roll(DAY_OF_YEAR) transition");
86+
}
7887

79-
test_roll = "";
80-
c_roll = new GregorianCalendar(2001, OCTOBER, 1);
81-
for (int i = 0; i < 7; i++) {
82-
test_roll += c_roll.get(DAY_OF_MONTH) + "-";
83-
c_roll.roll(DAY_OF_WEEK_IN_MONTH, true);
84-
test_roll += c_roll.get(DAY_OF_MONTH) + " ";
85-
}
86-
if (!test_roll.equals(golden_data3)) {
87-
err = true;
88-
System.err.println("Wrong roll(DAY_OF_WEEK_IN_MONTH) transition: got "
89-
+ test_roll + "expected " + golden_data3);
90-
}
91-
} finally {
92-
Locale.setDefault(savedLocale);
93-
TimeZone.setDefault(savedTimeZone);
88+
/*
89+
* Test some roll values during transition (DAY_OF_WEEK field). Uses
90+
* the boolean roll method. Roll multiple times and attach the returned
91+
* values to a long string which is then compared to the expected data.
92+
*/
93+
public void rollDayOfWeekTest() {
94+
StringBuilder actualRollData = new StringBuilder();
95+
GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 27);
96+
cal.setFirstDayOfWeek(THURSDAY);
97+
for (int i = 0; i < 7; i++) {
98+
actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
99+
cal.roll(DAY_OF_WEEK, true);
100+
actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
94101
}
102+
assertEquals(expectedDayOfWeekData, actualRollData.toString(),
103+
"Wrong roll(DAY_OF_WEEK) transition");
104+
}
95105

96-
if (err) {
97-
throw new RuntimeException("Wrong roll() transition");
106+
/*
107+
* Test some roll values during transition (DAY_OF_WEEK_IN_MONTH field). Uses
108+
* the boolean roll method. Roll multiple times and attach the returned
109+
* values to a long string which is then compared to the expected data.
110+
*/
111+
public void rollDayOfWeekInMonthTest() {
112+
StringBuilder actualRollData = new StringBuilder();
113+
GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 1);
114+
for (int i = 0; i < 7; i++) {
115+
actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
116+
cal.roll(DAY_OF_WEEK_IN_MONTH, true);
117+
actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
98118
}
119+
assertEquals(expectedDayOfWeekInMonthData, actualRollData.toString(),
120+
"Wrong roll(DAY_OF_WEEK_IN_MONTH) transition");
99121
}
100122
}

0 commit comments

Comments
 (0)
Please sign in to comment.