提交 f8515e96 编写于 作者: K Kohsuke Kawaguchi

Merge branch 'HUDSON-8656' of https://github.com/nairb774/jenkins

......@@ -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<c.getFirstDayOfWeek()) {
final int firstDayOfWeek = c.getFirstDayOfWeek();
if (v < firstDayOfWeek && was >= 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);
}
}
};
......
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<Object[]> parameters() {
final Locale[] locales = Locale.getAvailableLocales();
final Collection<Object[]> parameters = new ArrayList<Object[]>();
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));
}
}
......@@ -27,8 +27,11 @@ import antlr.ANTLRException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import junit.framework.TestCase;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Url;
import static java.util.Calendar.MONDAY;
......@@ -69,6 +72,48 @@ public class CronTabTest extends TestCase {
compare(new GregorianCalendar(2010,7,1,0,0),x.ceil(c));
}
/**
* Verifies that HUDSON-8656 never crops up again.
*/
@Url("http://issues.hudson-ci.org/browse/HUDSON-8656")
public void testCeil4() throws ANTLRException {
final Calendar cal = Calendar.getInstance(new Locale("de", "de"));
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();
expectedDate.set(2011, 0, 17, 23, 0, 0); // Expected next: Monday, Jan 17th 2011, 23:00
assertEquals(expectedDate.get(Calendar.HOUR), next.get(Calendar.HOUR));
assertEquals(expectedDate.get(Calendar.MINUTE), next.get(Calendar.MINUTE));
assertEquals(expectedDate.get(Calendar.YEAR), next.get(Calendar.YEAR));
assertEquals(expectedDate.get(Calendar.MONTH), next.get(Calendar.MONTH));
assertEquals(expectedDate.get(Calendar.DAY_OF_MONTH), next.get(Calendar.DAY_OF_MONTH)); // FAILS: is Monday, Jan 10th, 23:00
}
/**
* Verifies that HUDSON-8656 never crops up again.
*/
@Url("http://issues.hudson-ci.org/browse/HUDSON-8656")
public void testCeil5() throws ANTLRException {
final Calendar cal = Calendar.getInstance(new Locale("de", "at"));
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();
expectedDate.set(2011, 0, 17, 23, 0, 0); // Expected next: Monday, Jan 17th 2011, 23:00
assertEquals(expectedDate.get(Calendar.HOUR), next.get(Calendar.HOUR));
assertEquals(expectedDate.get(Calendar.MINUTE), next.get(Calendar.MINUTE));
assertEquals(expectedDate.get(Calendar.YEAR), next.get(Calendar.YEAR));
assertEquals(expectedDate.get(Calendar.MONTH), next.get(Calendar.MONTH));
assertEquals(expectedDate.get(Calendar.DAY_OF_MONTH), next.get(Calendar.DAY_OF_MONTH)); // FAILS: is Monday, Jan 10th, 23:00
}
public void testFloor1() throws Exception {
CronTab x = new CronTab("30 * * * *");
Calendar c = new GregorianCalendar(2000,2,1,1,40);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册