~ubuntu-branches/ubuntu/trusty/jenkins/trusty

« back to all changes in this revision

Viewing changes to core/src/test/java/hudson/scheduler/CronTabEventualityTest.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-13 12:35:19 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20130813123519-tizgfxcr70trl7r0
Tags: 1.509.2+dfsg-1
* New upstream release (Closes: #706725):
  - d/control: Update versioned BD's:
    * jenkins-executable-war >= 1.28.
    * jenkins-instance-identity >= 1.3.
    * libjenkins-remoting-java >= 2.23.
    * libjenkins-winstone-java >= 0.9.10-jenkins-44.
    * libstapler-java >= 1.207.
    * libjenkins-json-java >= 2.4-jenkins-1.
    * libstapler-adjunct-timeline-java >= 1.4.
    * libstapler-adjunct-codemirror-java >= 1.2.
    * libmaven-hpi-plugin-java >= 1.93.
    * libjenkins-xstream-java >= 1.4.4-jenkins-3.
  - d/maven.rules: Map to older version of animal-sniffer-maven-plugin.
  - Add patch for compatibility with guava >= 0.14.
  - Add patch to exclude asm4 dependency via jnr-posix.
  - Fixes the following security vulnerabilities:
    CVE-2013-2034, CVE-2013-2033, CVE-2013-2034, CVE-2013-1808
* d/patches/*: Switch to using git patch-queue for managing patches.
* De-duplicate jars between libjenkins-java and jenkins-external-job-monitor
  (Closes: #701163):
  - d/control: Add dependency between jenkins-external-job-monitor ->
    libjenkins-java.
  - d/rules: 
    Drop installation of jenkins-core in jenkins-external-job-monitor.
  - d/jenkins-external-job-monitor.{links,install}: Link to jenkins-core
    in /usr/share/java instead of included version.
* Wait longer for jenkins to stop during restarts (Closes: #704848):
  - d/jenkins.init: Re-sync init script from upstream codebase.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
}
 
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
}