KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > ScheduleTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol;
38
39 import java.util.Calendar JavaDoc;
40 import java.util.Date JavaDoc;
41 import java.util.SimpleTimeZone JavaDoc;
42 import java.util.TimeZone JavaDoc;
43
44 import junit.framework.TestCase;
45 import net.sourceforge.cruisecontrol.builders.MockBuilder;
46
47 import org.jdom.Element;
48
49 public class ScheduleTest extends TestCase {
50     private Schedule schedule;
51     private TimeZone JavaDoc defaultTimeZone;
52
53     private static final long ONE_MINUTE = Schedule.ONE_MINUTE;
54     private static final long ONE_HOUR = 60 * ONE_MINUTE;
55     private static final long TWELVE_HOURS = 12 * ONE_HOUR;
56     private static final long ONE_DAY = Schedule.ONE_DAY;
57     private static final TimeZone JavaDoc LOS_ANGELES;
58
59     private static final Calendar JavaDoc FRIDAY;
60     private static final Calendar JavaDoc THURSDAY;
61
62     private static final Date JavaDoc THURSDAY_1001;
63     private static final Date JavaDoc THURSDAY_1101;
64     private static final Date JavaDoc THURSDAY_1201;
65     private static final Date JavaDoc THURSDAY_2301;
66     private static final Date JavaDoc FRIDAY_0000;
67
68     private static final MockBuilder NOON_BUILDER;
69     private static final MockBuilder MIDNIGHT_BUILDER;
70     private static final MockBuilder MULTIPLE_5;
71     private static final MockBuilder MULTIPLE_1;
72     
73     private static final PauseBuilder PAUSE_11_13;
74     private static final PauseBuilder PAUSE_11_13_FRIDAY;
75
76     static {
77         LOS_ANGELES = createLosAngelesTimeZone();
78         
79         NOON_BUILDER = new MockBuilder();
80         NOON_BUILDER.setTime("1200");
81         NOON_BUILDER.setBuildLogXML(new Element("builder1"));
82
83         MIDNIGHT_BUILDER = new MockBuilder();
84         MIDNIGHT_BUILDER.setTime("0000");
85         MIDNIGHT_BUILDER.setBuildLogXML(new Element("builder1"));
86
87         MULTIPLE_5 = new MockBuilder();
88         MULTIPLE_5.setMultiple(5);
89         MULTIPLE_5.setBuildLogXML(new Element("builder2"));
90
91         MULTIPLE_1 = new MockBuilder();
92         MULTIPLE_1.setMultiple(1);
93         MULTIPLE_1.setBuildLogXML(new Element("builder3"));
94
95         PAUSE_11_13 = new PauseBuilder();
96         PAUSE_11_13.setStartTime(1100);
97         PAUSE_11_13.setEndTime(1300);
98
99         PAUSE_11_13_FRIDAY = new PauseBuilder();
100         PAUSE_11_13_FRIDAY.setStartTime(1100);
101         PAUSE_11_13_FRIDAY.setEndTime(1300);
102         PAUSE_11_13_FRIDAY.setDay("friday");
103         
104         THURSDAY = Calendar.getInstance();
105         THURSDAY.set(2001, Calendar.NOVEMBER, 22);
106         FRIDAY = Calendar.getInstance();
107         FRIDAY.set(2001, Calendar.NOVEMBER, 23);
108
109         THURSDAY_1001 = getDate(THURSDAY, 10, 01);
110         THURSDAY_1101 = getDate(THURSDAY, 11, 01);
111         THURSDAY_1201 = getDate(THURSDAY, 12, 01);
112         THURSDAY_2301 = getDate(THURSDAY, 23, 01);
113         FRIDAY_0000 = getDate(FRIDAY, 0, 0);
114     }
115
116     protected void setUp() {
117         schedule = new Schedule();
118
119         schedule.add(NOON_BUILDER);
120         schedule.add(MIDNIGHT_BUILDER);
121         schedule.add(MULTIPLE_5);
122         schedule.add(MULTIPLE_1);
123         
124         defaultTimeZone = TimeZone.getDefault();
125         TimeZone.setDefault(LOS_ANGELES);
126     }
127
128     protected void tearDown() throws Exception JavaDoc {
129         schedule = null;
130         TimeZone.setDefault(defaultTimeZone);
131     }
132     
133     private static TimeZone JavaDoc createLosAngelesTimeZone() {
134         // taken from SimpleTimeZone javadoc:
135

136         // Base GMT offset: -8:00
137
// DST starts: at 2:00am in standard time
138
// on the first Sunday in April
139
// DST ends: at 2:00am in daylight time
140
// on the last Sunday in October
141
// Save: 1 hour
142
return new SimpleTimeZone JavaDoc(-28800000,
143                        "America/Los_Angeles",
144                        Calendar.APRIL, 1, -Calendar.SUNDAY,
145                        7200000,
146                        Calendar.OCTOBER, -1, Calendar.SUNDAY,
147                        7200000,
148                        3600000);
149     }
150
151     /**
152      * @param calendar Calendar with date set
153      * @param hour
154      * @param min
155      * @return Date with date and time set
156      */

157     private static Date JavaDoc getDate(Calendar JavaDoc calendar, int hour, int min) {
158         Calendar JavaDoc cal = (Calendar JavaDoc) calendar.clone();
159         cal.setTimeZone(LOS_ANGELES);
160         cal.set(Calendar.HOUR_OF_DAY, hour);
161         cal.set(Calendar.MINUTE, min);
162         return cal.getTime();
163     }
164
165     public void testSelectBuilder() throws CruiseControlException {
166         Builder buildIsMultipleOfOne = schedule.selectBuilder(13, THURSDAY_1001, THURSDAY_1101);
167         assertEquals(MULTIPLE_1, buildIsMultipleOfOne);
168
169         Builder buildIsMultipleOfFive = schedule.selectBuilder(10, THURSDAY_1001, THURSDAY_1101);
170         assertEquals(MULTIPLE_5, buildIsMultipleOfFive);
171
172         Builder middayTimeBuild = schedule.selectBuilder(13, THURSDAY_1001, THURSDAY_1201);
173         assertEquals(NOON_BUILDER, middayTimeBuild);
174
175         Builder midnightTimeBuild = schedule.selectBuilder(13, THURSDAY_1001, FRIDAY_0000);
176         assertEquals(MIDNIGHT_BUILDER, midnightTimeBuild);
177
178         Calendar JavaDoc wednesday = Calendar.getInstance();
179         wednesday.set(2001, Calendar.NOVEMBER, 21);
180         Date JavaDoc noonWednesday = getDate(wednesday, 12, 00);
181
182         Builder timeBuildAcrossDays = schedule.selectBuilder(11, noonWednesday, FRIDAY_0000);
183         assertEquals(MIDNIGHT_BUILDER, timeBuildAcrossDays);
184
185         Schedule timeBasedSchedule = new Schedule();
186         timeBasedSchedule.add(NOON_BUILDER);
187         timeBasedSchedule.add(MIDNIGHT_BUILDER);
188
189         Builder nextTimeBuilder = timeBasedSchedule.selectBuilder(3, THURSDAY_1001, THURSDAY_1101);
190         assertEquals(NOON_BUILDER, nextTimeBuilder);
191
192         try {
193             Schedule badSchedule = new Schedule();
194             badSchedule.selectBuilder(1, THURSDAY_1001, THURSDAY_1101);
195             fail("should fail with no builders");
196         } catch (CruiseControlException expected) {
197         }
198     }
199
200     public void testSelectBuilder_MultipleBuildersWithDaySet() throws CruiseControlException {
201         Builder thursdayBuilder = new MockBuilder();
202         thursdayBuilder.setDay("thursday");
203         Schedule scheduledByDay = new Schedule();
204         scheduledByDay.add(thursdayBuilder);
205         assertEquals(thursdayBuilder, scheduledByDay.selectBuilder(1, THURSDAY_1001, THURSDAY_1101));
206         assertEquals(thursdayBuilder, scheduledByDay.selectBuilder(1, THURSDAY_1001, FRIDAY_0000));
207
208         Builder fridayBuilder = new MockBuilder();
209         fridayBuilder.setDay("friday");
210         scheduledByDay.add(fridayBuilder);
211         assertEquals(thursdayBuilder, scheduledByDay.selectBuilder(1, THURSDAY_1001, THURSDAY_1101));
212         assertEquals(fridayBuilder, scheduledByDay.selectBuilder(1, THURSDAY_1001, FRIDAY_0000));
213     }
214
215     public void testSelectBuilder_ForcedBuildAcrossTimeChangeBoundary() throws CruiseControlException {
216         Builder thursdayBuilder = new MockBuilder();
217
218         thursdayBuilder.setTime("1120");
219         thursdayBuilder.setDay("thursday");
220         Schedule scheduledByDay = new Schedule();
221         scheduledByDay.add(thursdayBuilder);
222
223         Calendar JavaDoc oct272005 = Calendar.getInstance();
224         oct272005.set(2005, Calendar.OCTOBER, 27);
225
226         // mimic a JMX trigger
227
Date JavaDoc last = getDate(oct272005, 11, 20);
228         Date JavaDoc now = getDate(oct272005, 11, 25);
229         assertEquals(thursdayBuilder, scheduledByDay.selectBuilder(123, last, now));
230     }
231     
232     public void testIsPaused() {
233         PauseBuilder pauseBuilder = new PauseBuilder();
234         pauseBuilder.setStartTime(2300);
235         pauseBuilder.setEndTime(2359);
236         schedule.add(pauseBuilder);
237
238         assertEquals(schedule.isPaused(THURSDAY_2301), true);
239         assertEquals(schedule.isPaused(THURSDAY_1101), false);
240     }
241
242     public void testGetTimeToNextBuild() {
243         long fiveSeconds = 5 * 1000;
244         long elevenHours = 11 * ONE_HOUR;
245         long oneHourFiftyNineMinutes = (2 * ONE_HOUR) - ONE_MINUTE;
246
247         PauseBuilder pause = new PauseBuilder();
248         pause.setStartTime(2300);
249         pause.setEndTime(2359);
250         schedule.add(pause);
251
252         assertEquals(
253             "next time build > build interval",
254             fiveSeconds,
255             schedule.getTimeToNextBuild(THURSDAY_1001, fiveSeconds));
256
257         assertEquals(
258             "next time build < build interval",
259             oneHourFiftyNineMinutes,
260             schedule.getTimeToNextBuild(THURSDAY_1001, elevenHours));
261
262         assertEquals(
263             "next time build is tomorrow",
264             TWELVE_HOURS - ONE_MINUTE,
265             schedule.getTimeToNextBuild(THURSDAY_1201, ONE_DAY * 2));
266
267         assertEquals(
268             "wait till after pause",
269             TWELVE_HOURS - ONE_MINUTE,
270             schedule.getTimeToNextBuild(THURSDAY_1201, elevenHours));
271
272         assertEquals(
273             "wait till after pause we're in",
274             ONE_HOUR - ONE_MINUTE,
275             schedule.getTimeToNextBuild(THURSDAY_2301, fiveSeconds));
276
277         pause = new PauseBuilder();
278         pause.setStartTime(0000);
279         pause.setEndTime(100);
280         schedule.add(pause);
281
282         assertEquals(
283             "wait till after pause on next day",
284             2 * ONE_HOUR,
285             schedule.getTimeToNextBuild(THURSDAY_2301, ONE_HOUR));
286
287         assertEquals(
288             "two back-to-back pauses",
289             2 * ONE_HOUR,
290             schedule.getTimeToNextBuild(THURSDAY_2301, fiveSeconds));
291
292         pause = new PauseBuilder();
293         pause.setStartTime(0000);
294         pause.setEndTime(2359);
295         pause.setDay("friday");
296         schedule.add(pause);
297
298         assertEquals(
299             "chained pauses with day specific pause",
300             ONE_DAY + (2 * ONE_HOUR),
301             schedule.getTimeToNextBuild(THURSDAY_2301, ONE_HOUR));
302     }
303     
304     public void testGetTimeToNextBuild_MultipleBuilderWithDaySet() {
305         Schedule intervalThursdaysSchedule = new Schedule();
306         Builder intervalThursdays = new MockBuilder();
307         intervalThursdays.setDay("thursday");
308         intervalThursdaysSchedule.add(intervalThursdays);
309         assertEquals(ONE_MINUTE, intervalThursdaysSchedule.getTimeToNextBuild(THURSDAY_2301, ONE_MINUTE));
310         assertEquals(6 * ONE_DAY + ONE_HOUR - ONE_MINUTE,
311                 intervalThursdaysSchedule.getTimeToNextBuild(THURSDAY_2301, ONE_HOUR));
312         assertEquals(6 * ONE_DAY + ONE_HOUR - ONE_MINUTE,
313                 intervalThursdaysSchedule.getTimeToNextBuild(THURSDAY_2301, ONE_DAY));
314     }
315     
316     public void testGetTimeToNextBuild_ShouldUseIntervalWhenThereAreNoBuilders() {
317         Schedule badSchedule = new Schedule();
318
319         assertEquals(
320             ONE_DAY,
321             badSchedule.getTimeToNextBuild(THURSDAY_1001, ONE_DAY));
322     }
323     
324     public void testGetTimeToNextBuild_ShouldNotExceedMaximumInterval() {
325         Schedule badSchedule = new Schedule();
326         PauseBuilder alwaysPaused = new PauseBuilder();
327         alwaysPaused.setStartTime(0000);
328         alwaysPaused.setEndTime(2359);
329         badSchedule.add(alwaysPaused);
330
331         assertEquals(
332             Schedule.MAX_INTERVAL_MILLISECONDS,
333             badSchedule.getTimeToNextBuild(THURSDAY_1001, ONE_DAY));
334         
335         badSchedule = new Schedule();
336         badSchedule.add(NOON_BUILDER);
337         badSchedule.add(PAUSE_11_13);
338         
339         assertEquals(
340             Schedule.MAX_INTERVAL_MILLISECONDS,
341             badSchedule.getTimeToNextBuild(THURSDAY_1101, ONE_MINUTE));
342     }
343     
344     public void testGetTimeToNextBuild_DailyBuild() {
345         Schedule dailyBuildSchedule = new Schedule();
346         dailyBuildSchedule.add(NOON_BUILDER);
347
348         assertEquals(
349             "ignore interval when only time builds",
350             ONE_HOUR - ONE_MINUTE,
351             dailyBuildSchedule.getTimeToNextBuild(THURSDAY_1101, ONE_MINUTE));
352             
353         PauseBuilder pause = new PauseBuilder();
354         pause.setStartTime(0000);
355         pause.setEndTime(2359);
356         pause.setDay("friday");
357         dailyBuildSchedule.add(pause);
358
359         assertEquals(
360             "pause w/only time builder",
361             (ONE_DAY * 2) - ONE_MINUTE,
362             dailyBuildSchedule.getTimeToNextBuild(THURSDAY_1201, ONE_MINUTE));
363     }
364
365     public void testGetTimeToNextBuild_WeeklyBuild() {
366         schedule = new Schedule();
367         Builder weeklyBuilder = new MockBuilder();
368         weeklyBuilder.setTime("0100");
369         weeklyBuilder.setDay("Sunday");
370         schedule.add(weeklyBuilder);
371
372         assertEquals((ONE_DAY * 2) + ONE_HOUR,
373                 schedule.getTimeToNextBuild(FRIDAY_0000, ONE_MINUTE));
374     }
375
376     public void testGetTimeToNextBuild_WeeklyBuildAcrossDaylightSavingsTimeBoundary() {
377         Builder thursdayBuilder = new MockBuilder();
378
379         thursdayBuilder.setTime("1120");
380         thursdayBuilder.setDay("thursday");
381         Schedule scheduledByDay = new Schedule();
382         scheduledByDay.add(thursdayBuilder);
383
384         Calendar JavaDoc oct272005 = Calendar.getInstance();
385         oct272005.set(2005, Calendar.OCTOBER, 27);
386
387         Date JavaDoc now = getDate(oct272005, 11, 25);
388         long dstEndingAdjustment = ONE_HOUR;
389         assertEquals(((ONE_DAY * 6) + (ONE_HOUR * 23) + (ONE_MINUTE * 55) + dstEndingAdjustment),
390                         scheduledByDay.getTimeToNextBuild(now, ONE_MINUTE));
391     }
392
393     public void testGetTimeToNextBuild_MonthlyBuild() {
394         schedule = new Schedule();
395         Builder monthlyBuilder = new MockBuilder() {
396             private long targetTime = FRIDAY_0000.getTime() + (30 * ONE_DAY);
397             public boolean isValidDay(Date JavaDoc now) {
398                 return targetTime <= now.getTime() ? true : false;
399             }
400         };
401         monthlyBuilder.setTime("0000");
402         schedule.add(monthlyBuilder);
403
404         assertEquals(ONE_DAY * 30,
405                 schedule.getTimeToNextBuild(FRIDAY_0000, ONE_MINUTE));
406
407     }
408     
409     public void testInterval() {
410         assertEquals("default interval", 300 * 1000, schedule.getInterval());
411         schedule.setInterval(500);
412         assertEquals(500 * 1000, schedule.getInterval());
413     }
414
415     public void testValidate() throws CruiseControlException {
416         schedule = new Schedule();
417
418         try {
419             schedule.validate();
420             fail("validate should throw exception if it is configured with no builders");
421         } catch (CruiseControlException e) {
422         }
423
424         schedule.add(MULTIPLE_1);
425         schedule.validate();
426
427         long oneYearInSeconds = 60 * 60 * 24 * 365;
428         schedule.setInterval(oneYearInSeconds);
429         schedule.validate();
430
431         schedule.setInterval(oneYearInSeconds + 1);
432         try {
433             schedule.validate();
434             fail("maximum allowed interval should be " + oneYearInSeconds);
435         } catch (CruiseControlException e) {
436         }
437     }
438     
439     public void testValidateShouldFailIfAllTimedBuildersInPauses() throws CruiseControlException {
440         schedule = new Schedule();
441
442         schedule.add(PAUSE_11_13_FRIDAY);
443         schedule.add(NOON_BUILDER);
444         schedule.validate();
445         
446         schedule.add(PAUSE_11_13);
447         try {
448             schedule.validate();
449             fail();
450         } catch (CruiseControlException expected) {
451         }
452        
453         schedule.add(MIDNIGHT_BUILDER);
454         schedule.validate();
455     }
456
457 }
458
Popular Tags