KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > structural > StatefulChildHelperTest


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

4 package org.oddjob.structural;
5
6 import junit.framework.TestCase;
7
8 import org.oddjob.framework.RunnableWrapper;
9 import org.oddjob.jobs.DummyStateJob;
10 import org.oddjob.state.JobState;
11 import org.oddjob.state.JobStateEvent;
12 import org.oddjob.state.JobStateListener;
13
14 /**
15  *
16  */

17 public class StatefulChildHelperTest extends TestCase
18 implements JobStateListener {
19
20     class GoodRunnable implements Runnable JavaDoc {
21         public void run() {
22             
23         }
24     }
25     
26     class BadRunnable implements Runnable JavaDoc {
27         public void run() {
28             throw new RuntimeException JavaDoc("Exception!!");
29         }
30     }
31     
32     JobState state;
33     
34     public void jobStateChange(JobStateEvent event) {
35         state = event.getJobState();
36     }
37     
38     // test with lots of children
39
public void test1() {
40         DummyStateJob j1 = new DummyStateJob();
41         j1.setDesired("complete");
42         Object JavaDoc j2 = new Object JavaDoc();
43         Object JavaDoc j3 = RunnableWrapper.wrapperFor(new GoodRunnable());
44         Object JavaDoc j4 = RunnableWrapper.wrapperFor(new BadRunnable());
45         DummyStateJob j5 = new DummyStateJob();
46         j5.setDesired("not complete");
47     
48         StatefulChildHelper h = new StatefulChildHelper(this);
49         h.addJobStateListener(this);
50         
51         assertEquals(JobState.READY, state);
52         
53         h.addChild(j1);
54         h.addChild(j2);
55         h.addChild(j3);
56         h.addChild(j4);
57         h.addChild(j5);
58         h.initialise();
59         
60         assertEquals(JobState.READY, state);
61         
62         j1.run();
63
64         assertEquals(JobState.READY, state);
65         
66         // j3 wrapper
67
((Runnable JavaDoc) h.getChildAt(2)).run();
68
69         assertEquals(JobState.READY, state);
70         
71         // j4 wrapper
72
((Runnable JavaDoc) h.getChildAt(3)).run();
73         
74         assertEquals(JobState.EXCEPTION, state);
75         
76         h.removeChildAt(3);
77         
78         j5.run();
79
80         assertEquals(JobState.NOT_COMPLETE, state);
81         
82         h.removeChild(j5);
83
84         j1.hardReset();
85         j1.run();
86         
87         assertEquals(JobState.COMPLETE, state);
88         
89         h.replaceChild(j3, j5);
90         
91         j5.hardReset();
92         j5.run();
93         
94         assertEquals(JobState.NOT_COMPLETE, state);
95         
96         h.softResetChildren();
97         
98         assertEquals(JobState.READY, state);
99     }
100
101     // If a job just contains a folder like object
102
// it should be complete.
103
public void testLikeFolder() {
104         Object JavaDoc j1 = new Object JavaDoc();
105         
106         StatefulChildHelper h = new StatefulChildHelper(this);
107         h.addJobStateListener(this);
108         h.addChild(j1);
109         h.initialise();
110         
111         h.refreshState();
112         assertEquals(JobState.COMPLETE, state);
113     }
114
115     
116     // one runnable one not
117
public void testTwo() {
118         Object JavaDoc j1 = new Object JavaDoc();
119         Object JavaDoc j2 = RunnableWrapper.wrapperFor(new GoodRunnable());
120         
121         StatefulChildHelper h = new StatefulChildHelper(this);
122         h.addJobStateListener(this);
123         h.addChild(j1);
124         h.addChild(j2);
125         h.initialise();
126         
127         assertEquals(JobState.READY, state);
128         
129         ((Runnable JavaDoc)h.getChildAt(1)).run();
130
131         assertEquals(JobState.COMPLETE, state);
132     }
133     
134     
135     public void testEmpty() {
136         StatefulChildHelper h = new StatefulChildHelper(this);
137         h.initialise();
138         h.addJobStateListener(this);
139         
140         assertEquals(JobState.READY, state);
141     }
142 }
143
Popular Tags