diff --git a/core/src/main/java/hudson/scheduler/CronTab.java b/core/src/main/java/hudson/scheduler/CronTab.java index 39de30a737..fc372f7682 100644 --- a/core/src/main/java/hudson/scheduler/CronTab.java +++ b/core/src/main/java/hudson/scheduler/CronTab.java @@ -218,18 +218,23 @@ public final class CronTab { private static final CalendarField DAY_OF_WEEK = new CalendarField("dow", Calendar.DAY_OF_WEEK, 1,-1, true, HOUR) { long bits(CronTab c) { return c.dayOfWeek; } void rollUp(Calendar cal, int i) { - cal.add(Calendar.DAY_OF_WEEK,7); + cal.add(Calendar.DAY_OF_WEEK, 7 * i); } @Override void setTo(Calendar c, int i) { int v = i-offset; + int was = c.get(field); c.set(field,v); - if (v= firstDayOfWeek) { // in crontab, the first DoW is always Sunday, but in Java, it can be Monday or in theory arbitrary other days. // When first DoW is 1/2 Monday, calendar points to 1/2 Monday, setting the DoW to Sunday makes // the calendar moves forward to 1/8 Sunday, instead of 1/1 Sunday. So we need to compensate that effect here. addTo(c,-7); + } else if (was < firstDayOfWeek && firstDayOfWeek <= v) { + // If we wrap the other way around, we need to adjust in the opposite direction of above. + addTo(c, 7); } } }; diff --git a/core/src/test/java/hudson/scheduler/CronTabDayOfWeekLocaleTest.java b/core/src/test/java/hudson/scheduler/CronTabDayOfWeekLocaleTest.java new file mode 100644 index 0000000000..22159ed949 --- /dev/null +++ b/core/src/test/java/hudson/scheduler/CronTabDayOfWeekLocaleTest.java @@ -0,0 +1,287 @@ +package hudson.scheduler; + +import static org.junit.Assert.assertEquals; + +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collection; +import java.util.GregorianCalendar; +import java.util.Locale; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.jvnet.hudson.test.For; +import org.jvnet.hudson.test.Url; + +/** + * A collection of unit tests focused around crontabs restricted to particular + * days of the week. This flexes across all the locales in the system to check + * the correctness of the {@link CronTab} class, more specifically the + * {@link CronTab#floor(Calendar)} and {@link CronTab#ceil(Calendar)} methods. + */ +@For(CronTab.class) +@RunWith(Parameterized.class) +public class CronTabDayOfWeekLocaleTest { + @Parameters + public static Collection parameters() { + final Locale[] locales = Locale.getAvailableLocales(); + final Collection parameters = new ArrayList(); + for (final Locale locale : locales) { + final Calendar cal = Calendar.getInstance(locale); + if (GregorianCalendar.class.equals(cal.getClass())) { + parameters.add(new Object[] { locale }); + } + } + return parameters; + } + + private final Locale locale; + + public CronTabDayOfWeekLocaleTest(Locale locale) { + this.locale = locale; + } + + /** + * This unit test is an slight adaptation of the unit test found in + * HUDSON-8656. + */ + @Test + @Url("http://issues.hudson-ci.org/browse/HUDSON-8656") + public void hudson8658() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 23 * * 1-5"; // execute on weekdays @23:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar next = cron.ceil(cal); + + final Calendar expectedDate = Calendar.getInstance(); + // Expected next: Monday, Jan 17th 2011, 23:00 + expectedDate.set(2011, 0, 17, 23, 0, 0); + compare(expectedDate, next); + } + + @Test + public void isSundayAndNextRunIsMonday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 1"; // Mondays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Monday, Jan 17th 2011, 00:00 + expected.set(2011, 0, 17, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsMonday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 1"; // Mondays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Monday, Jan 10th 2011, 00:00 + expected.set(2011, 0, 10, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsTuesday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 2"; // Tuesdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Tuesday, Jan 18th 2011, 00:00 + expected.set(2011, 0, 18, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsTuesday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 2"; // Tuesdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Tuesday, Jan 11th 2011, 00:00 + expected.set(2011, 0, 11, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsWednesday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 3"; // Wednesdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Wednesday, Jan 19th 2011, 00:00 + expected.set(2011, 0, 19, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsWednesday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 3"; // Wednesdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Wednesday, Jan 12th 2011, 00:00 + expected.set(2011, 0, 12, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsThursday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 4"; // Thursdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Thursday, Jan 20th 2011, 00:00 + expected.set(2011, 0, 20, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsThursday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 4"; // Thursdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Thursday, Jan 13th 2011, 00:00 + expected.set(2011, 0, 13, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsFriday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 5"; // Fridays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Friday, Jan 21th 2011, 00:00 + expected.set(2011, 0, 21, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsFriday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 5"; // Fridays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Friday, Jan 14th 2011, 00:00 + expected.set(2011, 0, 14, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsSaturday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 6"; // Saturdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Saturday, Jan 22th 2011, 00:00 + expected.set(2011, 0, 22, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndPreviousRunIsSaturday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 0 * * 6"; // Saturdays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Saturday, Jan 15th 2011, 00:00 + expected.set(2011, 0, 15, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsNextSunday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 1, 0, 0); // Sunday, Jan 16th 2011, 01:00 + final String cronStr = "0 0 * * 0"; // Sundays @00:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.ceil(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Sunday, Jan 22th 2011, 00:00 + expected.set(2011, 0, 23, 0, 0, 0); + compare(expected, actual); + } + + @Test + public void isSundayAndNextRunIsPreviousSunday() throws Exception { + final Calendar cal = Calendar.getInstance(locale); + cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00 + final String cronStr = "0 1 * * 0"; // Sundays @01:00 + + final CronTab cron = new CronTab(cronStr); + final Calendar actual = cron.floor(cal); + + final Calendar expected = Calendar.getInstance(); + // Expected next: Sunday, Jan 9th 2011, 01:00 + expected.set(2011, 0, 9, 1, 0, 0); + compare(expected, actual); + } + + private void compare(final Calendar expected, final Calendar actual) { + final DateFormat f = DateFormat.getDateTimeInstance(); + final String msg = "Locale: " + locale + " FirstDayOfWeek: " + actual.getFirstDayOfWeek() + " Expected: " + + f.format(expected.getTime()) + " Actual: " + f.format(actual.getTime()); + assertEquals(msg, expected.get(Calendar.YEAR), actual.get(Calendar.YEAR)); + assertEquals(msg, expected.get(Calendar.MONTH), actual.get(Calendar.MONTH)); + assertEquals(msg, expected.get(Calendar.DAY_OF_MONTH), actual.get(Calendar.DAY_OF_MONTH)); + assertEquals(msg, expected.get(Calendar.HOUR), actual.get(Calendar.HOUR)); + assertEquals(msg, expected.get(Calendar.MINUTE), actual.get(Calendar.MINUTE)); + } +}