KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > ScheduleCalculatorTest


1 /*
2  * Copyright 2004, Rob Gordon.
3  */

4 package org.oddjob.schedules;
5
6 import java.text.DateFormat JavaDoc;
7 import java.text.ParseException JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import org.apache.log4j.Logger;
16 import org.oddjob.schedules.schedules.CountSchedule;
17 import org.oddjob.schedules.schedules.DateSchedule;
18 import org.oddjob.schedules.schedules.IntervalSchedule;
19 import org.oddjob.schedules.schedules.TimeSchedule;
20 import org.oddjob.scheduling.ManualClock;
21 import org.oddjob.util.DateHelper;
22
23 /**
24  *
25  * @author Rob Gordon.
26  */

27 public class ScheduleCalculatorTest extends TestCase {
28
29     private static final Logger logger = Logger.getLogger(ScheduleCalculatorTest.class);
30
31     DateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm");
32
33     TimeSchedule schedule;
34     Schedule retrySchedule;
35     
36     String JavaDoc scheduleDate;
37     String JavaDoc nextDue;
38     Interval lastComplete;
39     boolean failed;
40     boolean retry;
41     
42     class SL implements ScheduleListener {
43         /* (non-Javadoc)
44          * @see org.oddjob.treesched.ScheduleListener#initialised(java.util.Date)
45          */

46         public void initialised(Date JavaDoc scheduleDate) {
47             ScheduleCalculatorTest.this.scheduleDate =
48                 format.format(scheduleDate);
49             ScheduleCalculatorTest.this.nextDue =
50                 format.format(scheduleDate);
51             ScheduleCalculatorTest.this.retry = false;
52             ScheduleCalculatorTest.this.failed = false;
53         }
54         
55         /* (non-Javadoc)
56          * @see org.oddjob.treesched.ScheduleListener#complete(java.util.Date, java.util.Date)
57          */

58         public void complete(Date JavaDoc scheduleDate, Interval lastComplete) {
59             if (scheduleDate == null) {
60                 ScheduleCalculatorTest.this.scheduleDate = null;
61                 ScheduleCalculatorTest.this.nextDue = null;
62             }
63             else {
64                 ScheduleCalculatorTest.this.scheduleDate =
65                     format.format(scheduleDate);
66                 ScheduleCalculatorTest.this.nextDue =
67                     format.format(scheduleDate);
68             }
69             if (lastComplete == null) {
70                 ScheduleCalculatorTest.this.lastComplete = null;
71             }
72             else {
73                 ScheduleCalculatorTest.this.lastComplete =
74                     lastComplete;
75             }
76             ScheduleCalculatorTest.this.retry = false;
77             ScheduleCalculatorTest.this.failed = false;
78         }
79         
80         /* (non-Javadoc)
81          * @see org.oddjob.treesched.ScheduleListener#retry(java.util.Date, java.util.Date)
82          */

83         public void retry(Date JavaDoc scheduleDate, Date JavaDoc retryDate) {
84             ScheduleCalculatorTest.this.scheduleDate =
85                 format.format(scheduleDate);
86             ScheduleCalculatorTest.this.nextDue =
87                 format.format(retryDate);
88             ScheduleCalculatorTest.this.retry = true;
89             ScheduleCalculatorTest.this.failed = false;
90         }
91         
92         /* (non-Javadoc)
93          * @see org.oddjob.treesched.ScheduleListener#failed(java.util.Date)
94          */

95         public void failed(Date JavaDoc scheduleDate) {
96             ScheduleCalculatorTest.this.scheduleDate =
97                 format.format(scheduleDate);
98             ScheduleCalculatorTest.this.nextDue =
99                 format.format(scheduleDate);
100             ScheduleCalculatorTest.this.retry = false;
101             ScheduleCalculatorTest.this.failed = true;
102         }
103     }
104     
105     public void setUp() throws ParseException JavaDoc {
106         logger.debug("------------- " + getName() + " -------------");
107
108         // set schedule
109
schedule = new TimeSchedule();
110         schedule.setFrom("12:00");
111         schedule.setTo("14:00");
112
113         IntervalSchedule interval= new IntervalSchedule();
114         interval.setInterval("00:10");
115         CountSchedule count = new CountSchedule();
116         count.setCount("2");
117         count.setChildSchedule(interval);
118         retrySchedule = count;
119         
120     }
121     
122     public void testInitialiseBeforeDue() throws Exception JavaDoc {
123         ManualClock clock = new ManualClock();
124         clock.setDate("25-Dec-03 11:00");
125
126         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
127         schedCalc.addScheduleListener(new SL());
128         schedCalc.initialise();
129         
130         assertEquals("scheduleDate", "25-Dec-03 12:00", scheduleDate);
131         assertEquals("nextDue", "25-Dec-03 12:00", nextDue);
132         assertNull("lastComplete", lastComplete);
133         assertFalse("retry", retry);
134         assertFalse("failed", failed);
135     }
136     
137     public void testInitialiseDuringDue() throws Exception JavaDoc {
138         ManualClock clock = new ManualClock();
139         clock.setDate("25-Dec-03 12:10");
140
141         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
142         schedCalc.addScheduleListener(new SL());
143         schedCalc.initialise();
144         
145         assertEquals("scheduleDate", "25-Dec-03 12:00", scheduleDate);
146         assertEquals("nextDue", "25-Dec-03 12:00", nextDue);
147         assertNull("lastComplete", lastComplete);
148         assertFalse("retry", retry);
149         assertFalse("failed", failed);
150         
151     }
152
153     public void testInitialiseAfterDue() throws Exception JavaDoc {
154         ManualClock clock = new ManualClock();
155         clock.setDate("25-Dec-03 14:10");
156
157         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
158         schedCalc.addScheduleListener(new SL());
159         schedCalc.initialise();
160         
161         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
162         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
163         assertNull("lastComplete", lastComplete);
164         assertFalse("retry", retry);
165         assertFalse("failed", failed);
166         
167     }
168     
169     public void testComplete() throws Exception JavaDoc {
170         ManualClock clock = new ManualClock();
171         clock.setDate("25-Dec-03 11:00");
172
173         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
174         schedCalc.addScheduleListener(new SL());
175         schedCalc.initialise();
176         schedCalc.calculateComplete();
177         
178         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
179         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
180         assertEquals("lastComplete",
181                 DateHelper.parseDateTime("25-Dec-03 12:00"),
182                 lastComplete.getFromDate());
183         assertFalse("retry", retry);
184         assertFalse("failed", failed);
185         
186     }
187     
188     public void testCopmleteComplete() throws Exception JavaDoc {
189         ManualClock clock = new ManualClock();
190         clock.setDate("25-Dec-03 11:00");
191
192         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
193         schedCalc.addScheduleListener(new SL());
194         schedCalc.initialise();
195         schedCalc.calculateComplete();
196         schedCalc.calculateComplete();
197         
198         assertEquals("scheduleDate", "27-Dec-03 12:00", scheduleDate);
199         assertEquals("nextDue", "27-Dec-03 12:00", nextDue);
200         assertEquals("lastComplete",
201                 DateHelper.parseDateTime("26-Dec-03 12:00"),
202                 lastComplete.getFromDate());
203         assertFalse("retry", retry);
204         assertFalse("failed", failed);
205         
206     }
207     
208     public void testFailNoException() throws Exception JavaDoc {
209         ManualClock clock = new ManualClock();
210         clock.setDate("25-Dec-03 11:00");
211
212         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
213         schedCalc.addScheduleListener(new SL());
214         schedCalc.initialise();
215         schedCalc.calculateRetry();
216         
217         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
218         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
219         assertNull("lastComplete", lastComplete);
220         assertFalse("retry", retry);
221         assertTrue("failed", failed);
222         
223     }
224     
225     public void testCompleteFailNoException() throws Exception JavaDoc {
226         ManualClock clock = new ManualClock();
227         clock.setDate("25-Dec-03 11:00");
228
229         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
230         schedCalc.addScheduleListener(new SL());
231         schedCalc.initialise();
232         schedCalc.calculateComplete();
233         schedCalc.calculateRetry();
234         
235         assertEquals("scheduleDate", "27-Dec-03 12:00", scheduleDate);
236         assertEquals("nextDue", "27-Dec-03 12:00", nextDue);
237         assertEquals("lastComplete",
238                 DateHelper.parseDateTime("25-Dec-03 12:00"),
239                 lastComplete.getFromDate());
240         assertFalse("retry", retry);
241         assertTrue("failed", failed);
242     }
243     
244     public void testExceptionScheduleOnce() throws ParseException JavaDoc {
245         ManualClock clock = new ManualClock();
246         clock.setDate("25-Dec-03 11:00");
247                 
248         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule, retrySchedule);
249         schedCalc.addScheduleListener(new SL());
250
251         schedCalc.initialise();
252                 
253         clock.setDate("25-Dec-03 12:00");
254         schedCalc.calculateRetry();
255         
256         assertEquals("scheduleDate", "25-Dec-03 12:00", scheduleDate);
257         assertEquals("nextDue", "25-Dec-03 12:10", nextDue);
258         assertNull("lastComplete", lastComplete);
259         assertTrue("retry", retry);
260         assertFalse("failed", failed);
261     }
262     
263     public void testExceptionScheduleTwice() throws ParseException JavaDoc {
264         ManualClock clock = new ManualClock();
265         clock.setDate("25-Dec-03 11:00");
266         
267         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule, retrySchedule);
268         schedCalc.addScheduleListener(new SL());
269
270         schedCalc.initialise();
271         
272         
273         clock.setDate("25-Dec-03 12:00");
274         schedCalc.calculateRetry();
275         schedCalc.calculateRetry();
276         
277         assertEquals("scheduleDate", "25-Dec-03 12:00", scheduleDate);
278         assertEquals("nextDue", "25-Dec-03 12:20", nextDue);
279         assertNull("lastComplete", lastComplete);
280         assertTrue("retry", retry);
281         assertFalse("failed", failed);
282     }
283
284     // with a retry count of 2 this fails twice and then switches back
285
// to the normal schedule.
286
public void testExceptionScheduleThrice() throws ParseException JavaDoc {
287         ManualClock clock = new ManualClock();
288         clock.setDate("25-Dec-03 11:00");
289         
290         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule, retrySchedule);
291         schedCalc.addScheduleListener(new SL());
292
293         schedCalc.initialise();
294         
295         clock.setDate("25-Dec-03 12:00");
296         schedCalc.calculateRetry();
297         schedCalc.calculateRetry();
298         schedCalc.calculateRetry();
299         
300         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
301         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
302         assertNull("lastComplete", lastComplete);
303         assertFalse("retry", retry);
304         assertTrue("failed", failed);
305     
306     }
307     
308     // with a retry count of 2 this fails twice and then switches back
309
// to the normal schedule, then back to the retry.
310
public void testExceptionScheduleFourth() throws ParseException JavaDoc {
311         ManualClock clock = new ManualClock();
312         clock.setDate("25-Dec-03 11:00");
313         
314         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule, retrySchedule);
315         schedCalc.addScheduleListener(new SL());
316
317         schedCalc.initialise();
318         
319         
320         schedCalc.calculateRetry();
321         schedCalc.calculateRetry();
322         schedCalc.calculateRetry();
323         clock.setDate("26-Dec-03 12:00");
324         schedCalc.calculateRetry();
325         
326         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
327         assertEquals("nextDue", "26-Dec-03 12:10", nextDue);
328         assertNull("lastComplete", lastComplete);
329         assertTrue("retry", retry);
330         assertFalse("failed", failed);
331     
332     }
333     
334     /**
335      * Test what happens when a schedule completes
336      * all together.
337      */

338     public void testCompletes()
339     throws InterruptedException JavaDoc, ParseException JavaDoc {
340         ManualClock clock = new ManualClock();
341         clock.setDate("25-Dec-03 11:00");
342         
343         // set schedule
344
DateSchedule dateSchedule = new DateSchedule();
345         dateSchedule.setOn("25-Dec-03");
346         TimeSchedule timeSchedule = new TimeSchedule();
347         timeSchedule.setOn("12:00");
348         dateSchedule.addValueSchedule(timeSchedule);
349         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, dateSchedule);
350         schedCalc.addScheduleListener(new SL());
351
352         schedCalc.initialise();
353         
354         schedCalc.calculateComplete();
355         
356         assertNull("scheduleDate", scheduleDate);
357         assertNull("nextDue", nextDue);
358         assertEquals("lastComplete",
359                 DateHelper.parseDateTime("25-Dec-03 12:00"),
360                 lastComplete.getFromDate());
361         assertFalse("retry", retry);
362         assertFalse("failed", failed);
363         
364     }
365         
366     /**
367      * Test as if states been persited by setting lastComplete.
368      */

369     public void testPersitence()
370     throws Exception JavaDoc {
371         ManualClock clock = new ManualClock();
372         clock.setDate("25-Dec-03 11:00");
373
374         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule);
375         schedCalc.addScheduleListener(new SL());
376
377         Map JavaDoc m = new HashMap JavaDoc();
378         schedCalc.initialise(null, m);
379
380         schedCalc.calculateComplete();
381
382         schedCalc = new ScheduleCalculator(clock, schedule);
383         schedCalc.initialise(lastComplete, m);
384         
385         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
386         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
387         assertFalse("retry", retry);
388         assertFalse("failed", failed);
389     }
390
391     public void testPersistence2() throws ParseException JavaDoc {
392         ManualClock clock = new ManualClock();
393         clock.setDate("25-Dec-03 11:00");
394         
395         ScheduleList scheduleList = new ScheduleList();
396         
397         CountSchedule countSchedule = new CountSchedule();
398         countSchedule.setCount("1");
399         DateSchedule dateSchedule = new DateSchedule();
400         dateSchedule.setOn("25-dec-2020");
401         
402         scheduleList.addValueSchedule(countSchedule);
403         scheduleList.addValueSchedule(dateSchedule);
404         
405         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, scheduleList);
406         schedCalc.addScheduleListener(new SL());
407         
408         HashMap JavaDoc map = new HashMap JavaDoc();
409         
410         schedCalc.initialise(null, map);
411                 
412         schedCalc = new ScheduleCalculator(clock, scheduleList);
413         schedCalc.addScheduleListener(new SL());
414         
415         schedCalc.initialise(
416                 new Interval(format.parse("25-Dec-03 12:00"), format.parse("25-Dec-03 12:10")),
417                 map);
418         
419         assertEquals("25-Dec-20 00:00", nextDue);
420         
421     }
422     
423     // test the retry is limited by the main schedule
424
public void testLimitedRetry() throws ParseException JavaDoc {
425         ManualClock clock = new ManualClock();
426         clock.setDate("25-Dec-03 13:55");
427         
428         ScheduleCalculator schedCalc = new ScheduleCalculator(clock, schedule, retrySchedule);
429         schedCalc.addScheduleListener(new SL());
430
431         schedCalc.initialise();
432                 
433         schedCalc.calculateRetry();
434         schedCalc.calculateRetry();
435         
436         assertEquals("scheduleDate", "26-Dec-03 12:00", scheduleDate);
437         assertEquals("nextDue", "26-Dec-03 12:00", nextDue);
438         assertNull("lastComplete", lastComplete);
439         assertFalse("retry", retry);
440         assertTrue("failed", failed);
441     
442     }
443 }
444
Popular Tags