1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy;
8
9 import junit.framework.TestCase;
10
11
12 /***
13 * DOCUMENT ME!
14 *
15 * @version $Revision$
16 * @author $author$
17 */
18 public class TestIndyThread extends TestCase {
19 private volatile boolean runFired;
20 private volatile boolean beforeRunFired;
21 private volatile boolean afterRunFired;
22 private volatile boolean cleanupFired;
23 private volatile boolean beforeExecuteFired;
24 private volatile boolean afterExecuteFired;
25
26 /***
27 * Creates a new TestIndyThread object.
28 *
29 * @param s DOCUMENT ME!
30 */
31 public TestIndyThread(String s) {
32 super(s);
33 }
34
35 /***
36 * DOCUMENT ME!
37 */
38 protected void setUp() {
39 runFired = false;
40 beforeRunFired = false;
41 afterRunFired = false;
42 cleanupFired = false;
43 beforeExecuteFired = false;
44 afterExecuteFired = false;
45 }
46
47 /***
48 * DOCUMENT ME!
49 */
50 protected void tearDown() {
51 }
52
53 /***
54 * DOCUMENT ME!
55 */
56 public void testStart() {
57 IndyThread testStartThread = new TestRunThread();
58 testStartThread.start();
59
60 try {
61 testStartThread.join();
62 }
63 catch (InterruptedException ex) {
64 }
65
66
67 //All methods should have fired at least once
68 assertEquals("Check run() called", true, runFired);
69 assertEquals("Check beforeRun() called", true, beforeRunFired);
70 assertEquals("Check afterRun() called", true, afterRunFired);
71 assertEquals("Check cleanup() called", true, cleanupFired);
72 assertEquals("Check afterExecute() called", true, afterExecuteFired);
73 assertEquals("Check beforeExecute() called", true, beforeExecuteFired);
74 }
75
76 /***
77 * DOCUMENT ME!
78 */
79 public void testExceptionBeforeExecute() {
80 IndyThread thread = new TestRunThread() {
81 protected void beforeExecute() {
82 throw new RuntimeException("Exception from Before Execute");
83 }
84 };
85
86 thread.start();
87
88 try {
89 thread.join();
90 }
91 catch (InterruptedException ex) {
92 }
93
94
95 //After execute SHOULD fire
96 assertTrue("Check afterExecute() called", afterExecuteFired);
97
98
99 //Nothing else should
100 assertTrue("Check run() NOT Called", !runFired);
101 assertTrue("Check beforeRun() NOT called", !beforeRunFired);
102 assertTrue("Check afterRun() NOT called", !afterRunFired);
103 assertTrue("Check cleanup() NOT called", !cleanupFired);
104 }
105
106 /***
107 * DOCUMENT ME!
108 */
109 public void testExceptionBeforeRun() {
110 IndyThread thread = new TestRunThread() {
111 protected void beforeRun() {
112 throw new RuntimeException("Exception from Before Run");
113 }
114 };
115
116 thread.start();
117
118 try {
119 thread.join();
120 }
121 catch (InterruptedException ex) {
122 }
123
124
125 //These SHOULD fire
126 assertTrue("Check afterExecute() called", afterExecuteFired);
127 assertTrue("Check cleanup() called", cleanupFired);
128
129
130 //Nothing else should
131 assertTrue("Check run() NOT Called", !runFired);
132 assertTrue("Check afterRun() NOT called", !afterRunFired);
133 }
134
135 /***
136 * DOCUMENT ME!
137 */
138 public void testExceptionRun() {
139 IndyThread thread = new TestRunThread() {
140 public void run() {
141 super.run();
142 throw new RuntimeException("Exception from Run");
143 }
144 };
145
146 thread.start();
147
148 try {
149 thread.join();
150 }
151 catch (InterruptedException ex) {
152 }
153
154
155 //All methods SHOULD fire
156 assertTrue("Check afterExecute() called", afterExecuteFired);
157 assertTrue("Check cleanup() called", cleanupFired);
158 assertTrue("Check run() called", runFired);
159 assertTrue("Check beforeRun() called", beforeRunFired);
160 assertTrue("Check afterRun() called", afterRunFired);
161 }
162
163 /***
164 * DOCUMENT ME!
165 */
166 public void testExceptionCleanup() {
167 IndyThread thread = new TestRunThread() {
168 protected void cleanup() {
169 super.cleanup();
170 throw new RuntimeException("Exception from Cleanup");
171 }
172 };
173
174 thread.start();
175
176 try {
177 thread.join();
178 }
179 catch (InterruptedException ex) {
180 }
181
182
183 //All methods SHOULD fire
184 assertTrue("Check afterExecute() called", afterExecuteFired);
185 assertTrue("Check cleanup() called", cleanupFired);
186 assertTrue("Check run() called", runFired);
187 assertTrue("Check beforeRun() called", beforeRunFired);
188 assertTrue("Check afterRun() called", afterRunFired);
189 }
190
191 /***
192 * DOCUMENT ME!
193 */
194 public void testPriority() {
195 IndyThread thread = new TestRunThread() {
196 public void run() {
197 runFired = true;
198 }
199 };
200
201 thread.start();
202
203 thread.setPriority(IndyThread.Priority.MIN);
204 assertEquals("Thread priority min", IndyThread.Priority.MIN,
205 thread.getPriority());
206
207 thread.setPriority(IndyThread.Priority.NORMAL);
208 assertEquals("Thread priority min", IndyThread.Priority.NORMAL,
209 thread.getPriority());
210
211 thread.setPriority(IndyThread.Priority.MAX);
212 assertEquals("Thread priority min", IndyThread.Priority.MAX,
213 thread.getPriority());
214
215 thread.stop();
216
217 try {
218 thread.join();
219 }
220 catch (InterruptedException ex) {
221 }
222 }
223
224 private class TestRunThread extends IndyThread {
225 public void run() {
226 System.err.println("Run()");
227 runFired = true;
228 stop();
229 }
230
231 protected void afterRun() {
232 System.err.println("afterRun()");
233 afterRunFired = true;
234 }
235
236 protected void beforeRun() {
237 System.err.println("beforeRun()");
238 beforeRunFired = true;
239 }
240
241 protected void cleanup() {
242 System.err.println("cleanup()");
243 cleanupFired = true;
244 }
245
246 protected void afterExecute() {
247 System.err.println("afterExecute()");
248 afterExecuteFired = true;
249 }
250
251 protected void beforeExecute() {
252 System.err.println("beforeExecute()");
253 beforeExecuteFired = true;
254 }
255 }
256 }
This page was automatically generated by Maven