1
16
17 import org.as2lib.app.exec.AbstractProcess;
18 import org.as2lib.app.exec.Processor;
19 import org.as2lib.app.exec.StepByStepProcess;
20 import org.as2lib.data.holder.Queue;
21 import org.as2lib.data.holder.queue.LinearQueue;
22 import org.as2lib.test.unit.ExecutionInfo;
23 import org.as2lib.test.unit.TestCase;
24 import org.as2lib.test.unit.TestCaseMethodInfo;
25 import org.as2lib.test.unit.TestCaseResult;
26 import org.as2lib.test.unit.TestRunner;
27 import org.as2lib.test.unit.TestResult;
28 import org.as2lib.test.unit.info.InstantiationError;
29 import org.as2lib.test.unit.info.ExecutionError;
30 import org.as2lib.test.unit.info.SetUpError;
31 import org.as2lib.test.unit.info.TearDownError;
32 import org.as2lib.util.ClassUtil;
33 import org.as2lib.util.StopWatch;
34 import org.as2lib.util.StringUtil;
35
36
51 class org.as2lib.test.unit.TestCaseRunner
52 extends AbstractProcess
53 implements TestRunner, StepByStepProcess {
54
55
56 private var STATE_NOT_STARTED:Number = 1;
57
58
59 private var STATE_TEST_CREATED:Number = 2;
60
61
62 private var STATE_SET_UP_FINISHED:Number = 3;
63
64
65 private var STATE_EXECUTION_FINISHED:Number = 4;
66
67
68 private var STATE_TEAR_DOWN_FINISHED:Number = 5;
69
70
71 private var testResult:TestCaseResult;
72
73
74 private var openTestCaseMethods:Queue;
75
76
81 private var methodInfo:TestCaseMethodInfo;
82
83
88 private var methodState:Number;
89
90
91 private var testCaseInstance:TestCase;
92
93
94 private var sW:StopWatch;
95
96
101 function TestCaseRunner(testCase:TestCase) {
102 testResult = new TestCaseResult(testCase);
103 methodState = STATE_NOT_STARTED;
104 openTestCaseMethods = new LinearQueue(testResult.getMethodInfos());
105 }
106
107
118 public function getTestResult(Void):TestResult {
119 return testResult;
120 }
121
122
131 public function getCurrentTestCase(Void):TestCaseResult {
132 return testResult;
133 }
134
135
145 public function getCurrentTestCaseMethodInfo(Void):TestCaseMethodInfo {
146 return methodInfo;
147 }
148
149
154 public function addInfo(info:ExecutionInfo):Void {
155 methodInfo.addInfo(info);
156 }
157
158
161 private function run(Void):Void {
162 working = true;
163
164 Processor.getInstance().addStepByStepProcess(this);
165 }
166
167
172 public function nextStep(Void):Void {
173 if (openTestCaseMethods.isEmpty()) {
174 finish();
175 } else {
176 if (methodState == STATE_NOT_STARTED) {
177 methodInfo = openTestCaseMethods.dequeue();
178 sW = methodInfo.getStopWatch();
179 sendUpdateEvent();
180 }
181 while (processMethod());
182 }
183 }
184
185
188 public function getPercentage(Void):Number {
189 return (100-(100/testResult.getMethodInfos().length*openTestCaseMethods.size()));
190 }
191
192
200 private function processMethod(Void):Boolean {
201
202 switch (methodState) {
203 case STATE_NOT_STARTED:
204
205
206 methodState = STATE_TEST_CREATED;
207
208 try {
209 testCaseInstance = ClassUtil.createInstance(
210 testResult.getTestCase()["__constructor__"]
211 );
212 } catch(e) {
213 fatal(new InstantiationError("IMPORTANT: Testcase threw "
214 + "an error by instanciation.\n"
215 + StringUtil.addSpaceIndent(e.toString(), 2), this, arguments));
216 }
217 break;
218
219 case STATE_TEST_CREATED:
220
221
222 methodState = STATE_SET_UP_FINISHED;
223
224 testCaseInstance.getTestRunner();
225
226
227 if (!methodInfo.hasErrors()) {
228 try {
229 testCaseInstance.setUp();
230 } catch (e) {
231 fatal(new SetUpError("IMPORTANT: Error occured during"
232 + " set up(Testcase wasn't executed):\n"+StringUtil.addSpaceIndent(e.toString(), 2), testCaseInstance, arguments));
233 }
234 }
235 break;
236
237 case STATE_SET_UP_FINISHED:
238
239
240 methodState = STATE_EXECUTION_FINISHED;
241
242 if (!methodInfo.hasErrors()) {
243
244 sW.start();
245 try {
246 methodInfo.getMethodInfo().invoke(testCaseInstance, null);
247 } catch (e) {
248 fatal(new ExecutionError("Unexpected exception thrown"
249 + " during execution:\n"
250 + StringUtil.addSpaceIndent(e.toString(), 2),
251 testCaseInstance, arguments));
252 }
253 }
254 break;
255
256 case STATE_EXECUTION_FINISHED:
257
258
259 methodState = STATE_TEAR_DOWN_FINISHED;
260 if (sW.hasStarted()) {
261 sW.stop();
262 }
263
264 if (!methodInfo.hasErrors()) {
265 try {
266 testCaseInstance.tearDown();
267 } catch(e) {
268 fatal(new TearDownError("IMPORTANT: Error occured during"
269 + " tear down:\n"+StringUtil.addSpaceIndent(e.toString(), 2),
270 testCaseInstance, arguments));
271 }
272 }
273 break;
274
275 case STATE_TEAR_DOWN_FINISHED:
276 methodState = STATE_NOT_STARTED;
277 methodInfo.setExecuted(true);
278 return false;
279
280 }
281
282 return true;
283 }
284
285
293 private function fatal(error:ExecutionInfo):Void {
294 addInfo(error);
295 methodState = STATE_TEAR_DOWN_FINISHED;
296 }
297 }