KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > structural > SequentialJobTest


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.jobs.structural;
5
6 import junit.framework.TestCase;
7
8 import org.apache.commons.beanutils.DynaBean;
9 import org.oddjob.Resetable;
10 import org.oddjob.Stateful;
11 import org.oddjob.framework.RunnableWrapper;
12 import org.oddjob.jobs.DummyStateJob;
13 import org.oddjob.jobs.EchoJob;
14 import org.oddjob.jobs.job.TriggerJob;
15 import org.oddjob.state.JobState;
16 import org.oddjob.state.JobStateEvent;
17 import org.oddjob.state.JobStateListener;
18
19 /**
20  *
21  */

22 public class SequentialJobTest extends TestCase {
23
24     // an empty sequence must be ready. This is to agree with oddjob
25
// which must also be ready when reset and empty.
26
// this is really a bug in StatefulChildHelper. An empty sequence should
27
// be ready until run and then be complete. I think.
28
public void testEmpty() {
29         SequentialJob j = new SequentialJob();
30         j.init();
31         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
32         j.run();
33         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
34     }
35     
36     // a sequence of just objects will always be complete when it runs
37
public void testObject() {
38         SequentialJob j = new SequentialJob();
39         j.addComponent((new Object JavaDoc()));
40         j.addComponent((new Object JavaDoc()));
41         j.init();
42         
43         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
44         j.run();
45         assertEquals(JobState.COMPLETE, j.lastJobStateEvent().getJobState());
46         
47     }
48     
49     public void testTriggers() {
50         Object JavaDoc j1 = RunnableWrapper.wrapperFor(new EchoJob());
51         ((DynaBean) j1).set("text", "one");
52         TriggerJob t1 = new TriggerJob();
53         t1.setOn((Stateful) j1);
54         t1.run();
55         
56         Object JavaDoc j2 = RunnableWrapper.wrapperFor(new EchoJob());
57         ((DynaBean) j2).set("text", "two");
58         TriggerJob t2 = new TriggerJob();
59         t2.setOn((Stateful) j2);
60         t2.run();
61         
62         SequentialJob j = new SequentialJob();
63         j.addComponent(t1);
64         j.addComponent(t2);
65         j.init();
66         
67         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
68         
69         j.run();
70         
71         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
72         
73         ((Runnable JavaDoc) j1).run();
74         
75         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
76         
77         ((Runnable JavaDoc) j2).run();
78         
79         assertEquals(JobState.COMPLETE, j.lastJobStateEvent().getJobState());
80         
81         ((Resetable) j2).hardReset();
82
83         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
84     }
85     
86     public void testRunnable() {
87         Object JavaDoc j1 = RunnableWrapper.wrapperFor(new EchoJob());
88         ((DynaBean) j1).set("text", "one");
89         
90         Object JavaDoc j2 = RunnableWrapper.wrapperFor(new EchoJob());
91         ((DynaBean) j2).set("text", "two");
92         
93         SequentialJob j = new SequentialJob();
94         j.addComponent(j1);
95         j.addComponent(j2);
96         j.init();
97
98         
99         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
100         j.run();
101         
102         assertEquals(JobState.COMPLETE, j.lastJobStateEvent().getJobState());
103         
104         ((Resetable) j2).hardReset();
105
106         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
107     }
108     
109     /**
110      * Test a mixture of Objects and jobs.
111      */

112     public void testMixture() {
113         Object JavaDoc j1 = RunnableWrapper.wrapperFor(new EchoJob());
114         ((DynaBean) j1).set("text", "one");
115         
116         Object JavaDoc j2 = RunnableWrapper.wrapperFor(new EchoJob());
117         ((DynaBean) j2).set("text", "two");
118         TriggerJob t2 = new TriggerJob();
119         t2.setOn((Stateful) j2);
120         t2.run();
121         
122         SequentialJob j = new SequentialJob();
123         j.addComponent(j1);
124         j.addComponent(t2);
125         j.addComponent(new Object JavaDoc());
126         j.init();
127
128         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
129         j.run();
130         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
131         
132         ((Runnable JavaDoc) j2).run();
133         
134         assertEquals(JobState.COMPLETE, j.lastJobStateEvent().getJobState());
135         
136         j.hardReset();
137
138         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
139     }
140     
141     public void testNotComplete() {
142         DummyStateJob j1 = new DummyStateJob();
143         j1.setDesired("complete");
144         
145         DummyStateJob j2 = new DummyStateJob();
146         j2.setDesired("not complete");
147         
148         SequentialJob j = new SequentialJob();
149         j.addComponent(j1);
150         j.addComponent(j2);
151         j.init();
152
153         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
154         j.run();
155         
156         assertEquals(JobState.NOT_COMPLETE, j.lastJobStateEvent().getJobState());
157         
158         j.hardReset();
159
160         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
161         
162     }
163     
164     public void testException() {
165         DummyStateJob j1 = new DummyStateJob();
166         j1.setDesired("complete");
167         
168         DummyStateJob j2 = new DummyStateJob();
169         j2.setDesired("exception");
170         
171         SequentialJob j = new SequentialJob();
172         j.addComponent(j1);
173         j.addComponent(j2);
174         j.init();
175
176         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
177         j.run();
178         
179         assertEquals(JobState.EXCEPTION, j.lastJobStateEvent().getJobState());
180         
181         j.hardReset();
182
183         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
184         
185     }
186     
187     public void testDestroyed() {
188         DummyStateJob j1 = new DummyStateJob();
189         j1.setDesired("complete");
190         
191         
192         SequentialJob j = new SequentialJob();
193         j.addComponent(j1);
194         j.init();
195
196         assertEquals(JobState.READY, j.lastJobStateEvent().getJobState());
197         j.run();
198         
199         assertEquals(JobState.COMPLETE, j.lastJobStateEvent().getJobState());
200
201         class L implements JobStateListener {
202             JobState s;
203             public void jobStateChange(JobStateEvent event) {
204                 s = event.getJobState();
205             }
206         }
207         L l = new L();
208         j.addJobStateListener(l);
209         
210         // not sure this is the desired behaviour
211
// the last event sent is ready
212
j.destroy();
213
214         assertEquals(JobState.READY, l.s);
215     }
216 }
217
Popular Tags