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

1 package hudson.scheduler;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.text.DateFormat;
6 import java.util.ArrayList;
7 import java.util.Calendar;
8 import java.util.Collection;
9 import java.util.GregorianCalendar;
10 import java.util.Locale;
11
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.junit.runners.Parameterized;
15 import org.junit.runners.Parameterized.Parameters;
16 import org.jvnet.hudson.test.Bug;
17 import org.jvnet.hudson.test.For;
18 import org.jvnet.hudson.test.Url;
19
20 /**
21 * A collection of unit tests focused around crontabs restricted to particular
22 * days of the week. This flexes across all the locales in the system to check
23 * the correctness of the {@link CronTab} class, more specifically the
24 * {@link CronTab#floor(Calendar)} and {@link CronTab#ceil(Calendar)} methods.
25 */
26 @For(CronTab.class)
27 @RunWith(Parameterized.class)
28 public class CronTabDayOfWeekLocaleTest {
29 @Parameters
30 public static Collection<Object[]> parameters() {
31 final Locale[] locales = Locale.getAvailableLocales();
32 final Collection<Object[]> parameters = new ArrayList<Object[]>();
33 for (final Locale locale : locales) {
34 final Calendar cal = Calendar.getInstance(locale);
35 if (GregorianCalendar.class.equals(cal.getClass())) {
36 parameters.add(new Object[] { locale });
37 }
38 }
39 return parameters;
40 }
41
42 private final Locale locale;
43
44 public CronTabDayOfWeekLocaleTest(Locale locale) {
45 this.locale = locale;
46 }
47
48 /**
49 * This unit test is an slight adaptation of the unit test found in
50 * HUDSON-8656.
51 */
52 @Test
53 @Url("http://issues.hudson-ci.org/browse/HUDSON-8656")
54 public void hudson8658() throws Exception {
55 final Calendar cal = Calendar.getInstance(locale);
56 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
57 final String cronStr = "0 23 * * 1-5"; // execute on weekdays @23:00
58
59 final CronTab cron = new CronTab(cronStr);
60 final Calendar next = cron.ceil(cal);
61
62 final Calendar expectedDate = Calendar.getInstance();
63 // Expected next: Monday, Jan 17th 2011, 23:00
64 expectedDate.set(2011, 0, 17, 23, 0, 0);
65 compare(expectedDate, next);
66 }
67
68 @Test
69 public void isSundayAndNextRunIsMonday() throws Exception {
70 final Calendar cal = Calendar.getInstance(locale);
71 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
72 final String cronStr = "0 0 * * 1"; // Mondays @00:00
73
74 final CronTab cron = new CronTab(cronStr);
75 final Calendar actual = cron.ceil(cal);
76
77 final Calendar expected = Calendar.getInstance();
78 // Expected next: Monday, Jan 17th 2011, 00:00
79 expected.set(2011, 0, 17, 0, 0, 0);
80 compare(expected, actual);
81 }
82
83 @Test
84 public void isSundayAndPreviousRunIsMonday() throws Exception {
85 final Calendar cal = Calendar.getInstance(locale);
86 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
87 final String cronStr = "0 0 * * 1"; // Mondays @00:00
88
89 final CronTab cron = new CronTab(cronStr);
90 final Calendar actual = cron.floor(cal);
91
92 final Calendar expected = Calendar.getInstance();
93 // Expected next: Monday, Jan 10th 2011, 00:00
94 expected.set(2011, 0, 10, 0, 0, 0);
95 compare(expected, actual);
96 }
97
98 @Test
99 public void isSundayAndNextRunIsTuesday() throws Exception {
100 final Calendar cal = Calendar.getInstance(locale);
101 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
102 final String cronStr = "0 0 * * 2"; // Tuesdays @00:00
103
104 final CronTab cron = new CronTab(cronStr);
105 final Calendar actual = cron.ceil(cal);
106
107 final Calendar expected = Calendar.getInstance();
108 // Expected next: Tuesday, Jan 18th 2011, 00:00
109 expected.set(2011, 0, 18, 0, 0, 0);
110 compare(expected, actual);
111 }
112
113 @Test
114 public void isSundayAndPreviousRunIsTuesday() throws Exception {
115 final Calendar cal = Calendar.getInstance(locale);
116 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
117 final String cronStr = "0 0 * * 2"; // Tuesdays @00:00
118
119 final CronTab cron = new CronTab(cronStr);
120 final Calendar actual = cron.floor(cal);
121
122 final Calendar expected = Calendar.getInstance();
123 // Expected next: Tuesday, Jan 11th 2011, 00:00
124 expected.set(2011, 0, 11, 0, 0, 0);
125 compare(expected, actual);
126 }
127
128 @Test
129 public void isSundayAndNextRunIsWednesday() throws Exception {
130 final Calendar cal = Calendar.getInstance(locale);
131 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
132 final String cronStr = "0 0 * * 3"; // Wednesdays @00:00
133
134 final CronTab cron = new CronTab(cronStr);
135 final Calendar actual = cron.ceil(cal);
136
137 final Calendar expected = Calendar.getInstance();
138 // Expected next: Wednesday, Jan 19th 2011, 00:00
139 expected.set(2011, 0, 19, 0, 0, 0);
140 compare(expected, actual);
141 }
142
143 @Test
144 public void isSundayAndPreviousRunIsWednesday() throws Exception {
145 final Calendar cal = Calendar.getInstance(locale);
146 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
147 final String cronStr = "0 0 * * 3"; // Wednesdays @00:00
148
149 final CronTab cron = new CronTab(cronStr);
150 final Calendar actual = cron.floor(cal);
151
152 final Calendar expected = Calendar.getInstance();
153 // Expected next: Wednesday, Jan 12th 2011, 00:00
154 expected.set(2011, 0, 12, 0, 0, 0);
155 compare(expected, actual);
156 }
157
158 @Test
159 public void isSundayAndNextRunIsThursday() throws Exception {
160 final Calendar cal = Calendar.getInstance(locale);
161 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
162 final String cronStr = "0 0 * * 4"; // Thursdays @00:00
163
164 final CronTab cron = new CronTab(cronStr);
165 final Calendar actual = cron.ceil(cal);
166
167 final Calendar expected = Calendar.getInstance();
168 // Expected next: Thursday, Jan 20th 2011, 00:00
169 expected.set(2011, 0, 20, 0, 0, 0);
170 compare(expected, actual);
171 }
172
173 @Test
174 public void isSundayAndPreviousRunIsThursday() throws Exception {
175 final Calendar cal = Calendar.getInstance(locale);
176 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
177 final String cronStr = "0 0 * * 4"; // Thursdays @00:00
178
179 final CronTab cron = new CronTab(cronStr);
180 final Calendar actual = cron.floor(cal);
181
182 final Calendar expected = Calendar.getInstance();
183 // Expected next: Thursday, Jan 13th 2011, 00:00
184 expected.set(2011, 0, 13, 0, 0, 0);
185 compare(expected, actual);
186 }
187
188 @Test
189 public void isSundayAndNextRunIsFriday() throws Exception {
190 final Calendar cal = Calendar.getInstance(locale);
191 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
192 final String cronStr = "0 0 * * 5"; // Fridays @00:00
193
194 final CronTab cron = new CronTab(cronStr);
195 final Calendar actual = cron.ceil(cal);
196
197 final Calendar expected = Calendar.getInstance();
198 // Expected next: Friday, Jan 21th 2011, 00:00
199 expected.set(2011, 0, 21, 0, 0, 0);
200 compare(expected, actual);
201 }
202
203 @Test
204 public void isSundayAndPreviousRunIsFriday() throws Exception {
205 final Calendar cal = Calendar.getInstance(locale);
206 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
207 final String cronStr = "0 0 * * 5"; // Fridays @00:00
208
209 final CronTab cron = new CronTab(cronStr);
210 final Calendar actual = cron.floor(cal);
211
212 final Calendar expected = Calendar.getInstance();
213 // Expected next: Friday, Jan 14th 2011, 00:00
214 expected.set(2011, 0, 14, 0, 0, 0);
215 compare(expected, actual);
216 }
217
218 @Test
219 public void isSundayAndNextRunIsSaturday() throws Exception {
220 final Calendar cal = Calendar.getInstance(locale);
221 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
222 final String cronStr = "0 0 * * 6"; // Saturdays @00:00
223
224 final CronTab cron = new CronTab(cronStr);
225 final Calendar actual = cron.ceil(cal);
226
227 final Calendar expected = Calendar.getInstance();
228 // Expected next: Saturday, Jan 22th 2011, 00:00
229 expected.set(2011, 0, 22, 0, 0, 0);
230 compare(expected, actual);
231 }
232
233 @Test
234 public void isSundayAndPreviousRunIsSaturday() throws Exception {
235 final Calendar cal = Calendar.getInstance(locale);
236 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
237 final String cronStr = "0 0 * * 6"; // Saturdays @00:00
238
239 final CronTab cron = new CronTab(cronStr);
240 final Calendar actual = cron.floor(cal);
241
242 final Calendar expected = Calendar.getInstance();
243 // Expected next: Saturday, Jan 15th 2011, 00:00
244 expected.set(2011, 0, 15, 0, 0, 0);
245 compare(expected, actual);
246 }
247
248 @Test
249 public void isSundayAndNextRunIsNextSunday() throws Exception {
250 final Calendar cal = Calendar.getInstance(locale);
251 cal.set(2011, 0, 16, 1, 0, 0); // Sunday, Jan 16th 2011, 01:00
252 final String cronStr = "0 0 * * 0"; // Sundays @00:00
253
254 final CronTab cron = new CronTab(cronStr);
255 final Calendar actual = cron.ceil(cal);
256
257 final Calendar expected = Calendar.getInstance();
258 // Expected next: Sunday, Jan 22th 2011, 00:00
259 expected.set(2011, 0, 23, 0, 0, 0);
260 compare(expected, actual);
261 }
262
263 @Test
264 public void isSundayAndPreviousRunIsPreviousSunday() throws Exception {
265 final Calendar cal = Calendar.getInstance(locale);
266 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
267 final String cronStr = "0 1 * * 0"; // Sundays @01:00
268
269 final CronTab cron = new CronTab(cronStr);
270 final Calendar actual = cron.floor(cal);
271
272 final Calendar expected = Calendar.getInstance();
273 // Expected next: Sunday, Jan 9th 2011, 01:00
274 expected.set(2011, 0, 9, 1, 0, 0);
275 compare(expected, actual);
276 }
277
278 @Test
279 @Bug(12357)
280 public void isSundayAndNextRunIsNextSunday7() throws Exception {
281 final Calendar cal = Calendar.getInstance(locale);
282 cal.set(2011, 0, 16, 1, 0, 0); // Sunday, Jan 16th 2011, 01:00
283 final String cronStr = "0 0 * * 7"; // Sundays(7 not 0) @00:00
284
285 final CronTab cron = new CronTab(cronStr);
286 final Calendar actual = cron.ceil(cal);
287
288 final Calendar expected = Calendar.getInstance();
289 // Expected next: Sunday, Jan 22th 2011, 00:00
290 expected.set(2011, 0, 23, 0, 0, 0);
291 compare(expected, actual);
292 }
293
294 @Test
295 public void isSundayAndPreviousRunIsPreviousSunday7() throws Exception {
296 final Calendar cal = Calendar.getInstance(locale);
297 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
298 final String cronStr = "0 1 * * 7"; // Sundays(7 not 0) @01:00
299
300 final CronTab cron = new CronTab(cronStr);
301 final Calendar actual = cron.floor(cal);
302
303 final Calendar expected = Calendar.getInstance();
304 // Expected next: Sunday, Jan 9th 2011, 01:00
305 expected.set(2011, 0, 9, 1, 0, 0);
306 compare(expected, actual);
307 }
308
309 @Test
310 public void isSaturdayAndNextRunIsSundayAsterisk() throws Exception {
311 final Calendar cal = Calendar.getInstance(locale);
312 cal.set(2011, 0, 15, 1, 0, 0); // Saturday, Jan 15th 2011, 01:00
313 final String cronStr = "0 0 * * *"; // Everyday @00:00
314
315 final CronTab cron = new CronTab(cronStr);
316 final Calendar actual = cron.ceil(cal);
317
318 final Calendar expected = Calendar.getInstance();
319 // Expected next: Sunday, Jan 16th 2011, 00:00
320 expected.set(2011, 0, 16, 0, 0, 0);
321 compare(expected, actual);
322 }
323
324 @Test
325 public void isSundayAndPreviousRunIsSaturdayAsterisk() throws Exception {
326 final Calendar cal = Calendar.getInstance(locale);
327 cal.set(2011, 0, 16, 0, 0, 0); // Sunday, Jan 16th 2011, 00:00
328 final String cronStr = "0 23 * * *"; // Everyday @23:00
329
330 final CronTab cron = new CronTab(cronStr);
331 final Calendar actual = cron.floor(cal);
332
333 final Calendar expected = Calendar.getInstance();
334 // Expected next: Saturday, Jan 15th 2011, 23:00
335 expected.set(2011, 0, 15, 23, 0, 0);
336 compare(expected, actual);
337 }
338
339 private void compare(final Calendar expected, final Calendar actual) {
340 final DateFormat f = DateFormat.getDateTimeInstance();
341 final String msg = "Locale: " + locale + " FirstDayOfWeek: " + actual.getFirstDayOfWeek() + " Expected: "
342 + f.format(expected.getTime()) + " Actual: " + f.format(actual.getTime());
343 assertEquals(msg, expected.get(Calendar.YEAR), actual.get(Calendar.YEAR));
344 assertEquals(msg, expected.get(Calendar.MONTH), actual.get(Calendar.MONTH));
345 assertEquals(msg, expected.get(Calendar.DAY_OF_MONTH), actual.get(Calendar.DAY_OF_MONTH));
346 assertEquals(msg, expected.get(Calendar.HOUR), actual.get(Calendar.HOUR));
347 assertEquals(msg, expected.get(Calendar.MINUTE), actual.get(Calendar.MINUTE));
348 }
349 }
350