01: package com.clarkware.junitperf;
02:
03: import junit.framework.Test;
04: import junit.framework.TestResult;
05:
06: /**
07: * The <code>ThreadedTest</code> is a test decorator that
08: * runs a test in a separate thread.
09: *
10: * @author <b>Mike Clark</b>
11: * @author Clarkware Consulting, Inc.
12: */
13:
14: public class ThreadedTest implements Test {
15:
16: private final Test test;
17: private final ThreadGroup group;
18: private final ThreadBarrier barrier;
19:
20: /**
21: * Constructs a <code>ThreadedTest</code> to decorate the
22: * specified test using the same thread group as the
23: * current thread.
24: *
25: * @param test Test to decorate.
26: */
27: public ThreadedTest(Test test) {
28: this (test, null, new ThreadBarrier(1));
29: }
30:
31: /**
32: * Constructs a <code>ThreadedTest</code> to decorate the
33: * specified test using the specified thread group and
34: * thread barrier.
35: *
36: * @param test Test to decorate.
37: * @param group Thread group.
38: * @param barrier Thread barrier.
39: */
40: public ThreadedTest(Test test, ThreadGroup group,
41: ThreadBarrier barrier) {
42: this .test = test;
43: this .group = group;
44: this .barrier = barrier;
45: }
46:
47: /**
48: * Returns the number of test cases in this threaded test.
49: *
50: * @return Number of test cases.
51: */
52: public int countTestCases() {
53: return test.countTestCases();
54: }
55:
56: /**
57: * Runs this test.
58: *
59: * @param result Test result.
60: */
61: public void run(TestResult result) {
62: Thread t = new Thread(group, new TestRunner(result));
63: t.start();
64: }
65:
66: class TestRunner implements Runnable {
67:
68: private TestResult result;
69:
70: public TestRunner(TestResult result) {
71: this .result = result;
72: }
73:
74: public void run() {
75: test.run(result);
76: barrier.onCompletion(Thread.currentThread());
77: }
78: }
79:
80: /**
81: * Returns the test description.
82: *
83: * @return Description.
84: */
85: public String toString() {
86: return "ThreadedTest: " + test.toString();
87: }
88: }
|