Skip to content

Commit d6b7f9b

Browse files
serhiysachkovMahendra Chhipa
authored andcommitted
8331851: Add specific regression leap year tests for Calendar.roll()
Reviewed-by: naoto
1 parent b92bd67 commit d6b7f9b

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright (c) 2024, 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 8331851
27+
* @summary confirm that Calendar.roll() works correctly with leap year calculations
28+
* @run junit CalendarLeapYearRollTest
29+
*/
30+
31+
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.params.ParameterizedTest;
33+
import org.junit.jupiter.params.provider.Arguments;
34+
import org.junit.jupiter.params.provider.MethodSource;
35+
36+
import java.util.Calendar;
37+
import java.util.GregorianCalendar;
38+
import java.util.Locale;
39+
import java.util.stream.Stream;
40+
41+
import static java.util.Calendar.APRIL;
42+
import static java.util.Calendar.DATE;
43+
import static java.util.Calendar.DAY_OF_MONTH;
44+
import static java.util.Calendar.DAY_OF_WEEK;
45+
import static java.util.Calendar.DAY_OF_WEEK_IN_MONTH;
46+
import static java.util.Calendar.DAY_OF_YEAR;
47+
import static java.util.Calendar.FEBRUARY;
48+
import static java.util.Calendar.LONG;
49+
import static java.util.Calendar.MARCH;
50+
import static java.util.Calendar.MONTH;
51+
import static java.util.Calendar.WEEK_OF_YEAR;
52+
import static java.util.Calendar.YEAR;
53+
import static org.junit.jupiter.api.Assertions.assertEquals;
54+
55+
public class CalendarLeapYearRollTest {
56+
57+
/**
58+
* 8331851 Calendar roll for leap year
59+
*/
60+
@ParameterizedTest
61+
@MethodSource("calendarRollSource")
62+
public void testRollLeapYear(String testName, int calendarDate, int calendarMonth, int calendarYear,
63+
int value, int calendarField, int expectedDate, int expectedMonth,
64+
int expectedYear) {
65+
Calendar calendar = new GregorianCalendar(calendarYear, calendarMonth, calendarDate);
66+
calendar.roll(calendarField, value);
67+
assertEquals(expectedDate, calendar.get(DATE), testName
68+
+ " Expected " + expectedDate + " of " + expectedMonth + expectedYear + " but got " + calendar.getTime());
69+
assertEquals(expectedMonth, calendar.get(MONTH), testName
70+
+ " Expected " + expectedMonth + " but got " + calendar.getDisplayName(MONTH, LONG, Locale.getDefault()));
71+
assertEquals(expectedYear, calendar.get(YEAR), testName
72+
+ " Expected " + expectedYear + " but got " + calendar.get(YEAR));
73+
}
74+
75+
/**
76+
* 8331851 Calendar roll up/down for leap year
77+
*/
78+
@ParameterizedTest
79+
@MethodSource("calendarRollUpDownSource")
80+
public void testRollUpDownLeapYear(String testName, int calendarDate, int calendarMonth, int calendarYear,
81+
int firstValue, int secondValue, int calendarField, int expectedDate,
82+
int expectedMonth, int expectedYear) {
83+
Calendar calendar = new GregorianCalendar(calendarYear, calendarMonth, calendarDate);
84+
calendar.roll(calendarField, firstValue);
85+
calendar.roll(calendarField, secondValue);
86+
assertEquals(expectedDate, calendar.get(DATE), testName
87+
+ " Expected " + expectedDate + " of " + expectedMonth + expectedYear + " but got " + calendar.getTime());
88+
assertEquals(expectedMonth, calendar.get(MONTH), testName
89+
+ " Expected " + expectedMonth + " but got " + calendar.getDisplayName(MONTH, LONG, Locale.getDefault()));
90+
assertEquals(expectedYear, calendar.get(YEAR), testName
91+
+ " Expected " + expectedYear + " but got " + calendar.get(YEAR));
92+
}
93+
94+
/**
95+
* 8331851 Calendar roll boolean for leap year
96+
*/
97+
@ParameterizedTest
98+
@MethodSource("calendarBooleanRollSource")
99+
public void testBooleanRollLeapYear(String testName, int calendarDate, int calendarMonth, int calendarYear,
100+
boolean value, int calendarField, int expectedDate,
101+
int expectedMonth, int expectedYear) {
102+
Calendar calendar = new GregorianCalendar(calendarYear, calendarMonth, calendarDate);
103+
calendar.roll(calendarField, value);
104+
assertEquals(expectedDate, calendar.get(DATE), testName
105+
+ " Expected " + expectedDate + " of " + expectedMonth + expectedYear + " but got " + calendar.getTime());
106+
assertEquals(expectedMonth, calendar.get(MONTH), testName
107+
+ " Expected " + expectedMonth + " but got " + calendar.getDisplayName(MONTH, LONG, Locale.getDefault()));
108+
assertEquals(expectedYear, calendar.get(YEAR), testName
109+
+ " Expected " + expectedYear + " but got " + calendar.get(YEAR));
110+
}
111+
112+
/**
113+
* 8331851 Calendar month and year roll for leap/non-leap year
114+
*/
115+
@Test
116+
public void testMonthYearRollUpDownNonLeapYear() {
117+
Calendar calendar = new GregorianCalendar(2024, FEBRUARY, 29);
118+
calendar.roll(MONTH, 1);
119+
calendar.roll(YEAR, -1);
120+
calendar.roll(MONTH, -1);
121+
assertEquals(28, calendar.get(DATE),
122+
"testMonthYearRollUpDownNonLeapYear Expected 28th of February 2024 but got " + calendar.getTime());
123+
assertEquals(FEBRUARY, calendar.get(MONTH),
124+
"testMonthYearRollUpDownNonLeapYear Expected February but got " + calendar.getDisplayName(MONTH, LONG, Locale.getDefault()));
125+
assertEquals(2023, calendar.get(YEAR),
126+
"testMonthYearRollUpDownNonLeapYear Expected 2023 but got " + calendar.get(YEAR));
127+
}
128+
129+
private static Stream<Arguments> calendarRollUpDownSource() {
130+
return Stream.of(
131+
Arguments.of("testMonthRollDownUpLeapYearReversed", 31, MARCH, 2024, -1, 1, MONTH, 29, MARCH, 2024),
132+
Arguments.of("testMonthRollUpDownLeapYearReversed", 29, FEBRUARY, 2024, 1, -1, MONTH, 29, FEBRUARY, 2024),
133+
Arguments.of("testYearRollUpDownLeapYear", 29, FEBRUARY, 2024, 1, -1, YEAR, 1, MARCH, 2024),
134+
Arguments.of("testFourYearRollUpDownLeapYear", 29, FEBRUARY, 2024, 4, -4, YEAR, 29, FEBRUARY, 2024),
135+
Arguments.of("testDayOfYearRollUpDownLeapYear", 29, FEBRUARY, 2024, 365, -365, DAY_OF_YEAR, 29, FEBRUARY, 2024),
136+
Arguments.of("testDateRollUpDownLeapYear", 29, FEBRUARY, 2024, 365, -365, DATE, 29, FEBRUARY, 2024),
137+
Arguments.of("testWeekOfYearRollUpDownLeapYear", 29, FEBRUARY, 2024, 52, -52, WEEK_OF_YEAR, 29, FEBRUARY, 2024),
138+
Arguments.of("testDayOfMonthRollUpDownLeapYear", 29, FEBRUARY, 2024, 31, -31, DAY_OF_MONTH, 29, FEBRUARY, 2024),
139+
Arguments.of("testDayOfWeekInMonthRollUpDownLeapYear", 29, FEBRUARY, 2024, 6, -6, DAY_OF_WEEK_IN_MONTH, 29, FEBRUARY, 2024),
140+
Arguments.of("testDayOfWeekRollUpDownLeapYear", 29, FEBRUARY, 2024, 6, -6, DAY_OF_WEEK, 29, FEBRUARY, 2024)
141+
);
142+
}
143+
144+
private static Stream<Arguments> calendarRollSource() {
145+
return Stream.of(
146+
Arguments.of("testMonthRollUpLeapYear", 29, FEBRUARY, 2024, 1, MONTH, 29, MARCH, 2024),
147+
Arguments.of("testOneMonthRollDownLeapYear", 31, MARCH, 2024, -1, MONTH, 29, FEBRUARY, 2024),
148+
Arguments.of("testTwoMonthDownEndOfMonthLeapYear", 30, APRIL, 2024, -2, MONTH, 29, FEBRUARY, 2024),
149+
Arguments.of("testTwoMonthDownSameDateLeapYear", 29, APRIL, 2024, -2, MONTH, 29, FEBRUARY, 2024),
150+
Arguments.of("testFourYearRollUpLeapYear", 29, FEBRUARY, 2024, 4, YEAR, 29, FEBRUARY, 2028),
151+
Arguments.of("testTwelveMonthRollDownLeapYear", 29, FEBRUARY, 2024, 12, MONTH, 29, FEBRUARY, 2024),
152+
Arguments.of("testYearRollUpLeapYear", 29, FEBRUARY, 2024, 1, YEAR, 1, MARCH, 2025),
153+
Arguments.of("testYearRollDownLeapYear", 29, FEBRUARY, 2024, -1, YEAR, 1, MARCH, 2023),
154+
Arguments.of("testDayOfYearRollDownLeapYear", 29, FEBRUARY, 2024, -1, DAY_OF_YEAR, 28, FEBRUARY, 2024),
155+
Arguments.of("testDayOfYearRollUpLeapYear", 29, FEBRUARY, 2024, 1, DAY_OF_YEAR, 1, MARCH, 2024),
156+
Arguments.of("testDateRollDownLeapYear", 29, FEBRUARY, 2024, -1, DATE, 28, FEBRUARY, 2024),
157+
Arguments.of("testDateRollUpLeapYear", 29, FEBRUARY, 2024, 1, DATE, 1, FEBRUARY, 2024),
158+
Arguments.of("testWeekOfYearRollUpLeapYear", 29, FEBRUARY, 2024, 1, WEEK_OF_YEAR, 7, MARCH, 2024),
159+
Arguments.of("testWeekOfYearRollDownLeapYear", 29, FEBRUARY, 2024, -1, WEEK_OF_YEAR, 22, FEBRUARY, 2024),
160+
Arguments.of("testDayOfMonthRollUpLeapYear", 29, FEBRUARY, 2024, 1, DAY_OF_MONTH, 1, FEBRUARY, 2024),
161+
Arguments.of("testDayOfMonthRollDownLeapYear", 29, FEBRUARY, 2024, -1, DAY_OF_MONTH, 28, FEBRUARY, 2024),
162+
Arguments.of("testDayOfWeekInMonthRollUpLeapYear", 29, FEBRUARY, 2024, 1, DAY_OF_WEEK_IN_MONTH, 1, FEBRUARY, 2024),
163+
Arguments.of("testDayOfWeekInMonthRollDownLeapYear", 29, FEBRUARY, 2024, -1, DAY_OF_WEEK_IN_MONTH, 22, FEBRUARY, 2024),
164+
Arguments.of("testDayOfWeekRollUpLeapYear", 29, FEBRUARY, 2024, 1, DAY_OF_WEEK, 1, MARCH, 2024),
165+
Arguments.of("testDayOfWeekRollDownLeapYear", 29, FEBRUARY, 2024, -1, DAY_OF_WEEK, 28, FEBRUARY, 2024)
166+
);
167+
}
168+
169+
private static Stream<Arguments> calendarBooleanRollSource() {
170+
return Stream.of(
171+
Arguments.of("testBooleanMonthRollDownLeapYear", 31, MARCH, 2024, false, MONTH, 29, FEBRUARY, 2024),
172+
Arguments.of("testBooleanMonthRollUpLeapYear", 29, FEBRUARY, 2024, true, MONTH, 29, MARCH, 2024),
173+
Arguments.of("testBooleanYearRollUpLeapYear", 29, FEBRUARY, 2024, true, YEAR, 1, MARCH, 2025),
174+
Arguments.of("testBooleanYearRollDownLeapYear", 29, FEBRUARY, 2024, false, YEAR, 1, MARCH, 2023),
175+
Arguments.of("testBooleanDayOfYearRollDownLeapYear", 29, FEBRUARY, 2024, false, DAY_OF_YEAR, 28, FEBRUARY, 2024),
176+
Arguments.of("testBooleanDayOfYearRollUpLeapYear", 29, FEBRUARY, 2024, true, DAY_OF_YEAR, 1, MARCH, 2024),
177+
Arguments.of("testBooleanDateRollDownLeapYear", 29, FEBRUARY, 2024, false, DATE, 28, FEBRUARY, 2024),
178+
Arguments.of("testBooleanDateRollUpLeapYear", 29, FEBRUARY, 2024, true, DATE, 1, FEBRUARY, 2024),
179+
Arguments.of("testBooleanWeekOfYearRollUpLeapYear", 29, FEBRUARY, 2024, true, WEEK_OF_YEAR, 7, MARCH, 2024),
180+
Arguments.of("testBooleanWeekOfYearRollDownLeapYear", 29, FEBRUARY, 2024, false, WEEK_OF_YEAR, 22, FEBRUARY, 2024),
181+
Arguments.of("testBooleanDayOfMonthRollUpLeapYear", 29, FEBRUARY, 2024, true, DAY_OF_MONTH, 1, FEBRUARY, 2024),
182+
Arguments.of("testBooleanDayOfMonthRollDownLeapYear", 29, FEBRUARY, 2024, false, DAY_OF_MONTH, 28, FEBRUARY, 2024),
183+
Arguments.of("testBooleanDayOfWeekInMonthRollUpLeapYear", 29, FEBRUARY, 2024, true, DAY_OF_WEEK_IN_MONTH, 1, FEBRUARY, 2024),
184+
Arguments.of("testBooleanDayOfWeekInMonthRollDownLeapYear", 29, FEBRUARY, 2024, false, DAY_OF_WEEK_IN_MONTH, 22, FEBRUARY, 2024),
185+
Arguments.of("testBooleanDayOfWeekRollUpLeapYear", 29, FEBRUARY, 2024, true, DAY_OF_WEEK, 1, MARCH, 2024),
186+
Arguments.of("testBooleanDayOfWeekRollDownLeapYear", 29, FEBRUARY, 2024, false, DAY_OF_WEEK, 28, FEBRUARY, 2024)
187+
);
188+
}
189+
190+
}

0 commit comments

Comments
 (0)