File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\scheduler\CronTabEventualityTest.java.html

1 package hudson.scheduler;
2
3 import antlr.ANTLRException;
4 import static org.junit.Assert.*;
5
6 import org.junit.Test;
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;
11
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;
17
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")});
27 return parameters;
28 }
29
30 private Calendar createLimit(Calendar start, int field, int amount){
31 Calendar limit = (Calendar)start.clone();
32 limit.add(field, amount);
33 return limit;
34 }
35
36 private String name;
37 private Hash hash;
38
39 public CronTabEventualityTest(String name, Hash hash) {
40 this.name = name;
41 this.hash = hash;
42 }
43
44 @Test(timeout = 1000)
45 @Bug(12388)
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);
50 }
51
52 @Test(timeout = 1000)
53 @Bug(12388)
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);
58 }
59
60 @Test(timeout = 1000)
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);
65 }
66
67 @Test(timeout = 1000)
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);
72 }
73
74 @Test(timeout = 1000)
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);
79 }
80
81 @Test(timeout = 1000)
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);
86 }
87
88 @Test(timeout = 1000)
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);
93 }
94
95 @Test(timeout = 1000)
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);
100 }
101
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);
110 }
111
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());
120 fail(msg);
121 }
122 }
123 }
124