KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

4 package org.oddjob.schedules.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
11 import junit.framework.TestCase;
12
13 import org.oddjob.schedules.Interval;
14 import org.oddjob.schedules.Schedule;
15 import org.oddjob.schedules.ScheduleContext;
16
17 /**
18  *
19  * @author Rob Gordon.
20  */

21 public class OccurenceScheduleTest extends TestCase {
22
23     static DateFormat JavaDoc checkFormat = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss:SSS");
24     static DateFormat JavaDoc inputFormat = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm");
25     
26     public void testBasic() throws ParseException JavaDoc {
27         OccurrenceSchedule schedule = new OccurrenceSchedule();
28         schedule.setOccurrence("2");
29
30         MockSchedule child = new MockSchedule();
31         child.setResults(new Interval[] {
32                 new Interval(
33                         inputFormat.parse("10-feb-2004 10:00"),
34                         inputFormat.parse("10-feb-2004 14:00")),
35                 new Interval(
36                         inputFormat.parse("11-feb-2004 10:00"),
37                         inputFormat.parse("11-feb-2004 14:00"))
38             });
39         schedule.addValueSchedule(child);
40         Date JavaDoc now1 = inputFormat.parse("10-feb-2004 12:30");
41         Interval expected = new Interval(
42                         inputFormat.parse("11-feb-2004 10:00"),
43                         inputFormat.parse("11-feb-2004 14:00"));
44         Interval actual = schedule.nextDue(
45                 new ScheduleContext(now1));
46         assertTrue("[" + actual+ "] wrong.",
47         expected.equals(actual));
48     }
49     
50     public void testOutsideLimits() throws ParseException JavaDoc {
51         OccurrenceSchedule schedule = new OccurrenceSchedule();
52         schedule.setOccurrence("2");
53
54         MockSchedule child = new MockSchedule();
55         child.setResults(new Interval[] {
56                 new Interval(
57                         inputFormat.parse("10-feb-2004 10:00"),
58                         inputFormat.parse("10-feb-2004 14:00")),
59                 new Interval(
60                         inputFormat.parse("11-feb-2004 10:00"),
61                         inputFormat.parse("11-feb-2004 14:00"))
62             });
63         schedule.addValueSchedule(child);
64         Date JavaDoc now1 = inputFormat.parse("10-feb-2004 12:30");
65         Interval expected = null;
66         schedule.setLimits(new Interval(
67                 inputFormat.parse("10-feb-2004 09:00"),
68                 inputFormat.parse("10-feb-2004 15:00")));
69         Interval actual = schedule.nextDue(
70                 new ScheduleContext(now1));
71         assertTrue("[" + actual+ "] wrong.",
72                 actual == expected);
73     }
74     
75     private static class MockSchedule implements Schedule {
76         Interval[] results;
77         int next;
78         public void setResults(Interval[] results) {
79             this.results = results;
80             next = 0;
81         }
82         public void setLimits(Interval limits) {}
83         public Interval nextDue(ScheduleContext context) {
84             if (results == null) {
85                 return null;
86             }
87             if (next < results.length) {
88                 return results[next++];
89             }
90             return null;
91         }
92     }
93 }
94
Popular Tags