KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > scheduling > RepeatJobTest


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

4 package org.oddjob.scheduling;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.text.ParseException JavaDoc;
9 import java.util.Date JavaDoc;
10
11 import junit.framework.TestCase;
12
13 import org.apache.commons.beanutils.PropertyUtils;
14 import org.apache.log4j.Logger;
15 import org.oddjob.Helper;
16 import org.oddjob.Oddjob;
17 import org.oddjob.framework.RunnableWrapper;
18 import org.oddjob.images.IconEvent;
19 import org.oddjob.images.IconHelper;
20 import org.oddjob.images.IconListener;
21 import org.oddjob.jobs.DummyStateJob;
22 import org.oddjob.schedules.Schedule;
23 import org.oddjob.schedules.schedules.CountSchedule;
24 import org.oddjob.schedules.schedules.IntervalSchedule;
25 import org.oddjob.schedules.schedules.TimeSchedule;
26 import org.oddjob.state.JobState;
27 import org.oddjob.util.Clock;
28 import org.oddjob.util.DateHelper;
29
30 /**
31  *
32  * @author Rob Gordon.
33  */

34 public class RepeatJobTest extends TestCase {
35
36     private static final Logger logger = Logger.getLogger(RepeatJobTest.class);
37     volatile boolean stop;
38     String JavaDoc iconId;
39     RepeatJob job;
40     
41     public void setUp() {
42         logger.debug("---------------- " + getName()
43                 + " ----------------");
44         stop = false;
45         iconId = "none";
46         job = new RepeatJob();
47         job.setName("Test Repeat");
48     }
49     
50     public void testInterval()
51     throws InterruptedException JavaDoc, ParseException JavaDoc {
52         TimeSchedule schedule = new TimeSchedule();
53         schedule.setOn("12:00");
54         
55         ManualClock clock = new ManualClock();
56         clock.setDate("25-dec-03 11:59:58");
57         
58         job.addIconListener(new MyIconListener());
59         job.setSchedule(schedule);
60         job.setClock(clock);
61         CheckJob checkJob = new CheckJob();
62         job.addComponentChild(RunnableWrapper.wrapperFor(checkJob));
63         job.init();
64         
65         Thread JavaDoc t = new Thread JavaDoc(job);
66         logger.debug("> Starting Job execution thread.");
67         t.start();
68         // wait for repeat job to sleep
69
while (!iconId.equals(IconHelper.SLEEPING)) {
70             Thread.sleep(100);
71         }
72         // first and only repeat.
73
clock.setDate("25-dec-03 12:00:01");
74         // wait for repeat job to sleep
75
while (!stop) {
76             Thread.sleep(100);
77         }
78         job.stop();
79         t.join();
80
81         assertTrue("Test job should have run.", checkJob.hasRun);
82     }
83     
84     public void testExceptionWithRetry()
85     throws InterruptedException JavaDoc, ParseException JavaDoc {
86         job.addIconListener(new MyIconListener());
87
88         // set schedule
89
TimeSchedule schedule = new TimeSchedule();
90         schedule.setOn("12:00");
91         
92         Clock clock = new Clock() {
93             int i;
94             Date JavaDoc[] dates = new Date JavaDoc[] {
95                     DateHelper.parseDateTime("25-dec-03 11:59:58"),
96                     DateHelper.parseDateTime("25-dec-03 11:59:58"),
97                     DateHelper.parseDateTime("25-dec-03 12:00:01"),
98                     DateHelper.parseDateTime("25-dec-03 12:00:01"),
99                     DateHelper.parseDateTime("25-dec-03 12:00:03"),
100                     DateHelper.parseDateTime("25-dec-03 12:00:03"),
101                     DateHelper.parseDateTime("25-dec-03 12:00:10"),
102                     DateHelper.parseDateTime("25-dec-03 12:00:10"),
103             };
104             public Date JavaDoc getDate() {
105                 return dates[i++];
106             }
107         };
108         
109         job.setSchedule(schedule);
110         job.setRetry(retrySchedule());
111         job.setClock(clock);
112         
113         DummyStateJob child = new DummyStateJob();
114         child.setDesired("not complete");
115         job.addComponentChild(child);
116         CheckJob checkJob = new CheckJob();
117         job.addComponentException(checkJob);
118         job.init();
119
120         Thread JavaDoc t = new Thread JavaDoc(job);
121         logger.debug("> Starting Job execution thread.");
122         
123         t.start();
124         while (!stop) {
125             Thread.sleep(100);
126         }
127         job.stop();
128         t.join();
129         assertTrue("Test job should have run.", checkJob.hasRun);
130     }
131
132     public void testNoSchedules()
133     throws InterruptedException JavaDoc, ParseException JavaDoc {
134         
135         CheckJob checkJob = new CheckJob();
136         job.addComponentChild(RunnableWrapper.wrapperFor(checkJob));
137         job.addComponentException(checkJob);
138         job.init();
139         
140         Thread JavaDoc t = new Thread JavaDoc(job);
141         logger.debug("> Starting Job execution thread.");
142         t.start();
143         while (!stop) {
144             Thread.sleep(100);
145         }
146         job.stop();
147         t.join();
148         assertTrue("Test job should have run.", checkJob.hasRun);
149     }
150     
151     public void testNoSchedulesException()
152     throws InterruptedException JavaDoc, ParseException JavaDoc {
153         
154         DummyStateJob child = new DummyStateJob();
155         child.setDesired("not complete");
156         job.addComponentChild(child);
157         CheckJob checkJob = new CheckJob();
158         job.addComponentException(checkJob);
159         job.init();
160         
161         Thread JavaDoc t = new Thread JavaDoc(job);
162         logger.debug("> Starting Job execution thread.");
163         t.start();
164         
165         // job running will signal stop
166
while (!stop) {
167             Thread.sleep(100);
168         }
169         job.stop();
170         t.join();
171         assertTrue("Test job should have run.", checkJob.hasRun);
172     }
173     
174     public void testInOddjob() {
175         String JavaDoc config
176         = "<oddjob><repeat id='whatever'><child>" +
177                 "<stop job='${whatever}'/>"
178             + "</child></repeat></oddjob>";
179         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
180         Oddjob oj = new Oddjob();
181         oj.setInput(is);
182         oj.run();
183         assertEquals("OJ complete", JobState.COMPLETE, Helper.getJobState(oj));
184     }
185     
186     private Schedule retrySchedule() throws ParseException JavaDoc {
187         IntervalSchedule interval = new IntervalSchedule();
188         interval.setInterval("00:00:02");
189         
190         CountSchedule count = new CountSchedule();
191         count.setCount("1");
192         count.addValueSchedule(interval);
193         return count;
194     }
195     
196     class CheckJob implements Runnable JavaDoc {
197
198         boolean hasRun = false;
199         
200         public void run() {
201             hasRun = true;
202             stop = true;
203         }
204     }
205     
206     class MyIconListener implements IconListener {
207         public void iconEvent(IconEvent e) {
208             iconId = e.getIconId();
209         }
210     }
211         
212     public static class Counter implements Runnable JavaDoc {
213         int count;
214         public void run() { count++; }
215         public int getCount() {
216             return count;
217         }
218     }
219     
220     // simple count test.
221
public void testSimpleCount() {
222         CountSchedule count = new CountSchedule();
223         count.setCount("10");
224         
225         Counter counter = new Counter();
226         
227         RepeatJob rj = new RepeatJob();
228         rj.setSchedule(count);
229         rj.addComponentChild(RunnableWrapper.wrapperFor(counter));
230         rj.init();
231         
232         rj.run();
233         
234         assertEquals(10, counter.count);
235     }
236
237     // the same simple count from oddjob;
238
public void testSimpleCountOJ() throws Exception JavaDoc {
239         String JavaDoc xml = "<oddjob><repeat>" +
240                 "<schedule><count count='10'/></schedule>" +
241                 "<child><c id='c' class='" + Counter.class.getName() + "'/></child>" +
242                 "</repeat></oddjob>";
243
244         Oddjob oj = new Oddjob();
245         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
246         oj.run();
247         
248         Object JavaDoc c = oj.lookup("c");
249         assertEquals(new Integer JavaDoc(10), PropertyUtils.getProperty(c, "count"));
250     }
251
252     public static class FailCounter implements Runnable JavaDoc {
253         int count;
254         public void run() {
255             count++;
256             throw new RuntimeException JavaDoc("fail");
257         }
258         public int getCount() {
259             return count;
260         }
261     }
262     
263     // 2 normal and 2 * 3 retry = count of 8.
264
public void testSimpleFailOJ() throws Exception JavaDoc {
265         String JavaDoc xml = "<oddjob><repeat>" +
266                 "<schedule><count count='2'/></schedule>" +
267                 "<retry><count count='3'/></retry>" +
268                 "<child><c id='c' class='" + FailCounter.class.getName() + "'/></child>" +
269                 "<exception><c id='r' class='" + Counter.class.getName() + "'/></exception>" +
270                 "</repeat></oddjob>";
271
272         Oddjob oj = new Oddjob();
273         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
274         oj.run();
275         
276         Object JavaDoc fc = oj.lookup("c");
277         assertEquals(new Integer JavaDoc(8), PropertyUtils.getProperty(fc , "count"));
278         Object JavaDoc r = oj.lookup("r");
279         assertEquals(new Integer JavaDoc(2), PropertyUtils.getProperty(r , "count"));
280     }
281
282 }
283
284
285     
Popular Tags