|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 8225641 |
| 27 | + * @summary Test the behavior of GregorianCalendar.roll(WEEK_OF_YEAR) |
| 28 | + * when the last week is rolled into the first week of the same year |
| 29 | + * @run junit RollFromLastToFirstWeek |
| 30 | + */ |
| 31 | + |
| 32 | + |
| 33 | +import java.util.*; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import static java.util.Calendar.*; |
| 36 | +import static org.junit.jupiter.api.Assertions.fail; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.MethodSource; |
| 39 | +import org.junit.jupiter.params.provider.Arguments; |
| 40 | + |
| 41 | +/** |
| 42 | + * Test to validate the behavior of GregorianCalendar.roll(WEEK_OF_YEAR, +1) |
| 43 | + * when rolling from the last week of a year into the first week of the same year. |
| 44 | + * This only test the implementation of the Gregorian Calendar roll. |
| 45 | + * |
| 46 | + * Rolling from the last week of a year into the first week of the same year |
| 47 | + * could cause a WEEK_OF_YEAR with a non-existent DAY_OF_WEEK combination. |
| 48 | + * The associated fix ensures that a final check is made, so that the first |
| 49 | + * week is incremented to prevent this. |
| 50 | + */ |
| 51 | +public class RollFromLastToFirstWeek { |
| 52 | + private static final Builder GREGORIAN_BUILDER = new Builder() |
| 53 | + .setCalendarType("gregory"); |
| 54 | + |
| 55 | + @ParameterizedTest |
| 56 | + @MethodSource("rollUpCalProvider") |
| 57 | + public void rollUpTest(Calendar calendar, String[] validDates){ |
| 58 | + if (calendar instanceof GregorianCalendar) { |
| 59 | + testRoll(calendar, validDates); |
| 60 | + } else { |
| 61 | + fail(String.format("Calendar is not Gregorian: %s", calendar)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void testRoll(Calendar calendar, String[] validDates) { |
| 66 | + String originalDate = longDateString(calendar); |
| 67 | + calendar.roll(Calendar.WEEK_OF_YEAR, 1); |
| 68 | + String rolledDate = longDateString(calendar); |
| 69 | + if (!Arrays.asList(validDates).contains(rolledDate)) { |
| 70 | + fail(String.format(""" |
| 71 | + {$$$ Failed: Rolled: "%s" by 1 week, where the first day of the week |
| 72 | + is: %s with a minimum week length of: %s and was expecting one of: "%s", but got: "%s"}, |
| 73 | + """, originalDate, calendar.getFirstDayOfWeek(), |
| 74 | + calendar.getMinimalDaysInFirstWeek(), Arrays.toString(validDates), rolledDate)); |
| 75 | + } else { |
| 76 | + System.out.printf(""" |
| 77 | + {$$$ Passed: Rolled: "%s" by 1 week where the first day of the week |
| 78 | + is: %s with a minimum week length of: %s and successfully got: "%s"}, |
| 79 | + """, originalDate, calendar.getFirstDayOfWeek(), |
| 80 | + calendar.getMinimalDaysInFirstWeek(), rolledDate); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // This implicitly tests the Iso8601 calendar as |
| 85 | + // MinWeek = 4 and FirstDayOfWeek = Monday is included in the provider |
| 86 | + private static Stream<Arguments> rollUpCalProvider() { |
| 87 | + ArrayList<Arguments> calList = new ArrayList<Arguments>(); |
| 88 | + // Week 1, Week 2 are all potential dates to roll into |
| 89 | + // Depends on first day of week / min days in week |
| 90 | + String[][] validDates = { |
| 91 | + {"Wednesday, 2 January 2019", "Wednesday, 9 January 2019"}, |
| 92 | + {"Thursday, 3 January 2019" , "Thursday, 10 January 2019"}, |
| 93 | + {"Friday, 4 January 2019" , "Friday, 11 January 2019"}, |
| 94 | + {"Saturday, 5 January 2019" , "Saturday, 12 January 2019"}, |
| 95 | + {"Sunday, 6 January 2019" , "Sunday, 13 January 2019"}, |
| 96 | + {"Monday, 7 January 2019" , "Monday, 14 January 2019"}, |
| 97 | + {"Tuesday, 1 January 2019" , "Tuesday, 8 January 2019"} |
| 98 | + }; |
| 99 | + int date = 0; |
| 100 | + // Test all days at the end of the year that roll into week 1 |
| 101 | + for (int dayOfMonth = 25; dayOfMonth <= 31; dayOfMonth++) { |
| 102 | + for (int weekLength = 1; weekLength <= 7; weekLength++) { |
| 103 | + // Sunday .. Monday -> Saturday |
| 104 | + for (int firstDay = SUNDAY; firstDay <= SATURDAY; firstDay++) { |
| 105 | + calList.add(Arguments.of(buildCalendar(firstDay, weekLength, |
| 106 | + dayOfMonth, DECEMBER, 2019), validDates[date])); |
| 107 | + } |
| 108 | + } |
| 109 | + date++; |
| 110 | + } |
| 111 | + return calList.stream(); |
| 112 | + } |
| 113 | + |
| 114 | + private static Calendar buildCalendar(int firstDayOfWeek, |
| 115 | + int minimumWeekLength, int dayOfMonth, |
| 116 | + int month, int year) { |
| 117 | + return GREGORIAN_BUILDER |
| 118 | + .setWeekDefinition(firstDayOfWeek, minimumWeekLength) |
| 119 | + .setDate(year, month, dayOfMonth) |
| 120 | + .build(); |
| 121 | + } |
| 122 | + |
| 123 | + private static String longDateString(Calendar calendar) { |
| 124 | + return String.format("%s, %s %s %s", |
| 125 | + calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH), |
| 126 | + calendar.get(Calendar.DAY_OF_MONTH), |
| 127 | + calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH), |
| 128 | + calendar.get(YEAR)); |
| 129 | + } |
| 130 | +} |
0 commit comments