1
package hudson.scheduler;
3
import antlr.ANTLRException;
4
import static org.junit.Assert.*;
7
import org.junit.runner.RunWith;
8
import org.junit.runners.Parameterized;
9
import org.jvnet.hudson.test.Bug;
10
import org.jvnet.hudson.test.For;
12
import java.text.DateFormat;
13
import java.util.ArrayList;
14
import java.util.Calendar;
15
import java.util.Collection;
16
import java.util.GregorianCalendar;
18
@RunWith(Parameterized.class)
19
@For({CronTab.class, Hash.class})
20
public class CronTabEventualityTest {
21
@Parameterized.Parameters
22
public static Collection<Object[]> parameters() {
23
Collection<Object[]> parameters = new ArrayList<Object[]>();
24
parameters.add(new Object[]{"zero", Hash.zero()});
25
parameters.add(new Object[]{"seed1", Hash.from("seed1")});
26
parameters.add(new Object[]{"seed2", Hash.from("seed2")});
30
private Calendar createLimit(Calendar start, int field, int amount){
31
Calendar limit = (Calendar)start.clone();
32
limit.add(field, amount);
39
public CronTabEventualityTest(String name, Hash hash) {
46
public void testYearlyWillBeEventuallyTriggeredWithinOneYear() throws ANTLRException {
47
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
48
Calendar limit = createLimit(start, Calendar.YEAR, 1);
49
checkEventuality(start, "@yearly", limit);
54
public void testAnnuallyWillBeEventuallyTriggeredWithinOneYear() throws ANTLRException {
55
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
56
Calendar limit = createLimit(start, Calendar.YEAR, 1);
57
checkEventuality(start, "@annually", limit);
61
public void testMonthlyWillBeEventuallyTriggeredWithinOneMonth() throws ANTLRException {
62
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
63
Calendar limit = createLimit(start, Calendar.MONTH, 1);
64
checkEventuality(start, "@monthly", limit);
68
public void testWeeklyWillBeEventuallyTriggeredWithinOneWeek() throws ANTLRException {
69
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
70
Calendar limit = createLimit(start, Calendar.WEEK_OF_YEAR, 1);
71
checkEventuality(start, "@weekly", limit);
75
public void testDailyWillBeEventuallyTriggeredWithinOneDay() throws ANTLRException {
76
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
77
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 1);
78
checkEventuality(start, "@daily", limit);
82
public void testMidnightWillBeEventuallyTriggeredWithinOneDay() throws ANTLRException {
83
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
84
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 1);
85
checkEventuality(start, "@midnight", limit);
89
public void testHourlyWillBeEventuallyTriggeredWithinOneHour() throws ANTLRException {
90
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
91
Calendar limit = createLimit(start, Calendar.HOUR, 1);
92
checkEventuality(start, "@hourly", limit);
96
public void testFirstDayOfMonthWillBeEventuallyTriggeredWithinOneMonth() throws ANTLRException {
97
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
98
Calendar limit = createLimit(start, Calendar.MONTH, 1);
99
checkEventuality(start, "H H 1 * *", limit);
102
@Test(timeout = 1000)
103
public void testFirstSundayOfMonthWillBeEventuallyTriggeredWithinOneMonthAndOneWeek() throws ANTLRException {
104
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
105
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 31+7);
106
// If both day of month and day of week are specified:
107
// UNIX: triggered when either matches
108
// Jenkins: triggered when both match
109
checkEventuality(start, "H H 1-7 * 0", limit);
112
private void checkEventuality(Calendar start, String crontabFormat, Calendar limit) throws ANTLRException {
113
CronTab cron = new CronTab(crontabFormat, hash);
114
Calendar next = cron.ceil(start);
115
if(next.after(limit)) {
116
DateFormat f = DateFormat.getDateTimeInstance();
117
String msg = "Name: " + name
118
+ " Limit: " + f.format(limit.getTime())
119
+ " Next: " + f.format(next.getTime());
1
package hudson.scheduler;
3
import antlr.ANTLRException;
4
import static org.junit.Assert.*;
7
import org.junit.runner.RunWith;
8
import org.junit.runners.Parameterized;
9
import org.jvnet.hudson.test.Bug;
10
import org.jvnet.hudson.test.For;
12
import java.text.DateFormat;
13
import java.util.ArrayList;
14
import java.util.Calendar;
15
import java.util.Collection;
16
import java.util.GregorianCalendar;
18
@RunWith(Parameterized.class)
19
@For({CronTab.class, Hash.class})
20
public class CronTabEventualityTest {
21
@Parameterized.Parameters
22
public static Collection<Object[]> parameters() {
23
Collection<Object[]> parameters = new ArrayList<Object[]>();
24
parameters.add(new Object[]{"zero", Hash.zero()});
25
parameters.add(new Object[]{"seed1", Hash.from("seed1")});
26
parameters.add(new Object[]{"seed2", Hash.from("seed2")});
30
private Calendar createLimit(Calendar start, int field, int amount){
31
Calendar limit = (Calendar)start.clone();
32
limit.add(field, amount);
39
public CronTabEventualityTest(String name, Hash hash) {
46
public void testYearlyWillBeEventuallyTriggeredWithinOneYear() throws ANTLRException {
47
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
48
Calendar limit = createLimit(start, Calendar.YEAR, 1);
49
checkEventuality(start, "@yearly", limit);
54
public void testAnnuallyWillBeEventuallyTriggeredWithinOneYear() throws ANTLRException {
55
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
56
Calendar limit = createLimit(start, Calendar.YEAR, 1);
57
checkEventuality(start, "@annually", limit);
61
public void testMonthlyWillBeEventuallyTriggeredWithinOneMonth() throws ANTLRException {
62
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
63
Calendar limit = createLimit(start, Calendar.MONTH, 1);
64
checkEventuality(start, "@monthly", limit);
68
public void testWeeklyWillBeEventuallyTriggeredWithinOneWeek() throws ANTLRException {
69
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
70
Calendar limit = createLimit(start, Calendar.WEEK_OF_YEAR, 1);
71
checkEventuality(start, "@weekly", limit);
75
public void testDailyWillBeEventuallyTriggeredWithinOneDay() throws ANTLRException {
76
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
77
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 1);
78
checkEventuality(start, "@daily", limit);
82
public void testMidnightWillBeEventuallyTriggeredWithinOneDay() throws ANTLRException {
83
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
84
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 1);
85
checkEventuality(start, "@midnight", limit);
89
public void testHourlyWillBeEventuallyTriggeredWithinOneHour() throws ANTLRException {
90
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
91
Calendar limit = createLimit(start, Calendar.HOUR, 1);
92
checkEventuality(start, "@hourly", limit);
96
public void testFirstDayOfMonthWillBeEventuallyTriggeredWithinOneMonth() throws ANTLRException {
97
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
98
Calendar limit = createLimit(start, Calendar.MONTH, 1);
99
checkEventuality(start, "H H 1 * *", limit);
102
@Test(timeout = 1000)
103
public void testFirstSundayOfMonthWillBeEventuallyTriggeredWithinOneMonthAndOneWeek() throws ANTLRException {
104
Calendar start = new GregorianCalendar(2012, 0, 11, 22, 33); // Jan 11th 2012 22:33
105
Calendar limit = createLimit(start, Calendar.DAY_OF_MONTH, 31+7);
106
// If both day of month and day of week are specified:
107
// UNIX: triggered when either matches
108
// Jenkins: triggered when both match
109
checkEventuality(start, "H H 1-7 * 0", limit);
112
private void checkEventuality(Calendar start, String crontabFormat, Calendar limit) throws ANTLRException {
113
CronTab cron = new CronTab(crontabFormat, hash);
114
Calendar next = cron.ceil(start);
115
if(next.after(limit)) {
116
DateFormat f = DateFormat.getDateTimeInstance();
117
String msg = "Name: " + name
118
+ " Limit: " + f.format(limit.getTime())
119
+ " Next: " + f.format(next.getTime());