8225641: Calendar.roll(int field) does not work correctly for WEEK_OF_YEAR

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2023-04-04 21:01:24 +00:00 committed by Naoto Sato
parent 3399fbf9fa
commit a324fa2639
2 changed files with 188 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1307,7 +1307,15 @@ public class GregorianCalendar extends Calendar {
woy = min;
}
}
set(field, getRolledValue(woy, amount, min, max));
int newWeekOfYear = getRolledValue(woy, amount, min, max);
// Final check to ensure that the first week has the
// current DAY_OF_WEEK. Only make a check for
// rolling up into week 1, as the existing checks
// sufficiently handle rolling down into week 1.
if (newWeekOfYear == 1 && isInvalidWeek1() && amount > 0) {
newWeekOfYear++;
}
set(field, newWeekOfYear);
return;
}
@ -2972,6 +2980,54 @@ public class GregorianCalendar extends Calendar {
return normalizedYear == cutoverYear;
}
/**
* {@return {@code true} if the first week of the current year is minimum
* and the {@code DAY_OF_WEEK} does not exist in that week}
*
* This method is used to check the validity of a {@code WEEK_OF_YEAR} and
* {@code DAY_OF_WEEK} combo when WEEK_OF_YEAR is rolled to a value of 1.
* This prevents other methods from calling complete() with an invalid combo.
*/
private boolean isInvalidWeek1() {
// Calculate the DAY_OF_WEEK for Jan 1 of the current YEAR
long jan1Fd = gcal.getFixedDate(internalGet(YEAR), 1, 1, null);
int jan1Dow = BaseCalendar.getDayOfWeekFromFixedDate(jan1Fd);
// Calculate how many days are in the first week
int daysInFirstWeek;
if (getFirstDayOfWeek() <= jan1Dow) {
// Add wrap around days
daysInFirstWeek = 7 - jan1Dow + getFirstDayOfWeek();
} else {
daysInFirstWeek = getFirstDayOfWeek() - jan1Dow;
}
// Calculate the end day of the first week
int endDow = getFirstDayOfWeek() - 1 == 0
? 7 : getFirstDayOfWeek() - 1;
// If the week is a valid minimum, check if the DAY_OF_WEEK does not exist
return daysInFirstWeek >= getMinimalDaysInFirstWeek() &&
!dayInMinWeek(internalGet(DAY_OF_WEEK), jan1Dow, endDow);
}
/**
* Given the first day and last day of a week, this method determines
* if the specified day exists in the minimum week.
* This method expects all parameters to be passed in as DAY_OF_WEEK values.
* For example, dayInMinWeek(4, 6, 3) returns false since Wednesday
* is not between the minimum week given by [Friday, Saturday,
* Sunday, Monday, Tuesday].
*/
private boolean dayInMinWeek (int day, int startDay, int endDay) {
if (endDay >= startDay) {
// dayInMinWeek(6, 3, 5), check that 6 is
// between 3 4 5
return (day >= startDay && day <= endDay);
} else {
// dayInMinWeek(4, 6, 3), check that 4 is
// between 6 7 1 2 3
return (day >= startDay || day <= endDay);
}
}
/**
* Returns the fixed date of the first day of the year (usually
* January 1) before the specified date.

View File

@ -0,0 +1,130 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8225641
* @summary Test the behavior of GregorianCalendar.roll(WEEK_OF_YEAR)
* when the last week is rolled into the first week of the same year
* @run junit RollFromLastToFirstWeek
*/
import java.util.*;
import java.util.stream.Stream;
import static java.util.Calendar.*;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
/**
* Test to validate the behavior of GregorianCalendar.roll(WEEK_OF_YEAR, +1)
* when rolling from the last week of a year into the first week of the same year.
* This only test the implementation of the Gregorian Calendar roll.
*
* Rolling from the last week of a year into the first week of the same year
* could cause a WEEK_OF_YEAR with a non-existent DAY_OF_WEEK combination.
* The associated fix ensures that a final check is made, so that the first
* week is incremented to prevent this.
*/
public class RollFromLastToFirstWeek {
private static final Builder GREGORIAN_BUILDER = new Builder()
.setCalendarType("gregory");
@ParameterizedTest
@MethodSource("rollUpCalProvider")
public void rollUpTest(Calendar calendar, String[] validDates){
if (calendar instanceof GregorianCalendar) {
testRoll(calendar, validDates);
} else {
fail(String.format("Calendar is not Gregorian: %s", calendar));
}
}
private void testRoll(Calendar calendar, String[] validDates) {
String originalDate = longDateString(calendar);
calendar.roll(Calendar.WEEK_OF_YEAR, 1);
String rolledDate = longDateString(calendar);
if (!Arrays.asList(validDates).contains(rolledDate)) {
fail(String.format("""
{$$$ Failed: Rolled: "%s" by 1 week, where the first day of the week
is: %s with a minimum week length of: %s and was expecting one of: "%s", but got: "%s"},
""", originalDate, calendar.getFirstDayOfWeek(),
calendar.getMinimalDaysInFirstWeek(), Arrays.toString(validDates), rolledDate));
} else {
System.out.printf("""
{$$$ Passed: Rolled: "%s" by 1 week where the first day of the week
is: %s with a minimum week length of: %s and successfully got: "%s"},
""", originalDate, calendar.getFirstDayOfWeek(),
calendar.getMinimalDaysInFirstWeek(), rolledDate);
}
}
// This implicitly tests the Iso8601 calendar as
// MinWeek = 4 and FirstDayOfWeek = Monday is included in the provider
private static Stream<Arguments> rollUpCalProvider() {
ArrayList<Arguments> calList = new ArrayList<Arguments>();
// Week 1, Week 2 are all potential dates to roll into
// Depends on first day of week / min days in week
String[][] validDates = {
{"Wednesday, 2 January 2019", "Wednesday, 9 January 2019"},
{"Thursday, 3 January 2019" , "Thursday, 10 January 2019"},
{"Friday, 4 January 2019" , "Friday, 11 January 2019"},
{"Saturday, 5 January 2019" , "Saturday, 12 January 2019"},
{"Sunday, 6 January 2019" , "Sunday, 13 January 2019"},
{"Monday, 7 January 2019" , "Monday, 14 January 2019"},
{"Tuesday, 1 January 2019" , "Tuesday, 8 January 2019"}
};
int date = 0;
// Test all days at the end of the year that roll into week 1
for (int dayOfMonth = 25; dayOfMonth <= 31; dayOfMonth++) {
for (int weekLength = 1; weekLength <= 7; weekLength++) {
// Sunday .. Monday -> Saturday
for (int firstDay = SUNDAY; firstDay <= SATURDAY; firstDay++) {
calList.add(Arguments.of(buildCalendar(firstDay, weekLength,
dayOfMonth, DECEMBER, 2019), validDates[date]));
}
}
date++;
}
return calList.stream();
}
private static Calendar buildCalendar(int firstDayOfWeek,
int minimumWeekLength, int dayOfMonth,
int month, int year) {
return GREGORIAN_BUILDER
.setWeekDefinition(firstDayOfWeek, minimumWeekLength)
.setDate(year, month, dayOfMonth)
.build();
}
private static String longDateString(Calendar calendar) {
return String.format("%s, %s %s %s",
calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH),
calendar.get(Calendar.DAY_OF_MONTH),
calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH),
calendar.get(YEAR));
}
}