1 package com.atlassian.scheduler.config;
2
3 import org.junit.Test;
4
5 import javax.annotation.Nullable;
6 import java.util.Date;
7 import java.util.TimeZone;
8
9 import static com.atlassian.scheduler.config.Schedule.Type.CRON_EXPRESSION;
10 import static com.atlassian.scheduler.config.Schedule.Type.INTERVAL;
11 import static com.atlassian.scheduler.config.Schedule.forCronExpression;
12 import static com.atlassian.scheduler.config.Schedule.forInterval;
13 import static com.atlassian.scheduler.config.Schedule.runOnce;
14 import static java.util.TimeZone.getTimeZone;
15 import static org.hamcrest.Matchers.is;
16 import static org.hamcrest.Matchers.notNullValue;
17 import static org.hamcrest.Matchers.nullValue;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertThat;
20
21
22
23
24 public class ScheduleTest {
25 private static final String AT_2AM = "0 0 2 * * *";
26 private static final String AT_4AM = "0 0 4 * * *";
27 private static final String BAD = "These aren't the cron expressions we are looking for.";
28
29 @Test
30 public void testRunOnceWithNull() throws Exception {
31 assertIntervalSchedule(runOnce(null), null, 0L);
32 }
33
34 @Test
35 public void testRunOnceWithDate() throws Exception {
36 final Date original = new Date();
37 final long originalTime = original.getTime();
38 final Schedule sched = runOnce(original);
39 original.setTime(42L);
40
41 final IntervalScheduleInfo info = simple(sched);
42 Date copy = info.getFirstRunTime();
43 assertNotNull(copy);
44 assertThat("Modifying the original should not pollute the stored copy", copy.getTime(), is(originalTime));
45 copy.setTime(42L);
46
47 copy = info.getFirstRunTime();
48 assertNotNull(copy);
49 assertThat("Modifying returned value should not pollute the original", copy.getTime(), is(originalTime));
50 }
51
52 @Test
53 public void testInterval() throws Exception {
54 final Date now = new Date();
55 assertIntervalSchedule(forInterval(0L, null), null, 0L);
56 assertIntervalSchedule(forInterval(42L, null), null, 42L);
57 assertIntervalSchedule(forInterval(0L, now), now, 0L);
58 assertIntervalSchedule(forInterval(42L, now), now, 42L);
59 }
60
61 @Test(expected = IllegalArgumentException.class)
62 public void testIntervalWithNegativeValue1() throws Exception {
63 forInterval(-42L, null);
64 }
65
66 @Test(expected = IllegalArgumentException.class)
67 public void testIntervalWithNegativeValue2() throws Exception {
68 forInterval(-42L, new Date());
69 }
70
71 @Test
72 public void testCronScheduleDefaultTimeZone() {
73 assertCronSchedule(forCronExpression(AT_2AM), AT_2AM, null);
74 assertCronSchedule(forCronExpression(AT_4AM, null), AT_4AM, null);
75 }
76
77
78 @Test
79 public void testCronScheduleMalformedCronExpression() {
80 assertCronSchedule(forCronExpression(BAD), BAD, null);
81 assertCronSchedule(forCronExpression(BAD, null), BAD, null);
82 }
83
84 @Test
85 public void testCronScheduleSpecificTimeZone() {
86 TimeZone zone = getTimeZone("America/Chicago");
87 if (zone.equals(TimeZone.getDefault())) {
88 zone = getTimeZone("Australia/Sydney");
89 }
90 assertCronSchedule(forCronExpression(AT_2AM, zone), AT_2AM, zone);
91 }
92
93 @SuppressWarnings("ConstantConditions")
94 @Test(expected = IllegalArgumentException.class)
95 public void testCronScheduleNull1() {
96 forCronExpression(null);
97 }
98
99 @SuppressWarnings("ConstantConditions")
100 @Test(expected = IllegalArgumentException.class)
101 public void testCronScheduleNull2() {
102 forCronExpression(null, TimeZone.getDefault());
103 }
104
105
106 private static void assertIntervalSchedule(Schedule sched, @Nullable Date firstRunTime, long intervalInMillis) {
107 assertSimpleSchedule(simple(sched), firstRunTime, intervalInMillis);
108 }
109
110 private static void assertSimpleSchedule(IntervalScheduleInfo info, @Nullable Date firstRunTime, long intervalInMillis) {
111 assertThat(info.getFirstRunTime(), is(firstRunTime));
112 assertThat(info.getIntervalInMillis(), is(intervalInMillis));
113 }
114
115 private static IntervalScheduleInfo simple(Schedule sched) {
116 assertThat(sched.getType(), is(INTERVAL));
117 assertThat(sched.getIntervalScheduleInfo(), notNullValue());
118 assertThat(sched.getCronScheduleInfo(), nullValue());
119 return sched.getIntervalScheduleInfo();
120 }
121
122
123 private static void assertCronSchedule(Schedule sched, String cronExpression, @Nullable TimeZone timeZone) {
124 assertCronSchedule(cron(sched), cronExpression, timeZone);
125 }
126
127 private static void assertCronSchedule(CronScheduleInfo info, String cronExpression, @Nullable TimeZone timeZone) {
128 assertThat(info.getCronExpression(), is(cronExpression));
129 assertThat(info.getTimeZone(), is(timeZone));
130 }
131
132 private static CronScheduleInfo cron(Schedule sched) {
133 assertThat(sched.getType(), is(CRON_EXPRESSION));
134 assertThat(sched.getIntervalScheduleInfo(), nullValue());
135 assertThat(sched.getCronScheduleInfo(), notNullValue());
136 return sched.getCronScheduleInfo();
137 }
138 }