001: package com.mockrunner.base;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.ArrayList;
005: import java.util.Enumeration;
006: import java.util.List;
007:
008: import junit.framework.Test;
009: import junit.framework.TestCase;
010: import junit.framework.TestResult;
011: import junit.framework.TestSuite;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: /**
017: * This TestSuite starts all test methods in
018: * a number of seperate threads. <b>Doesn't
019: * work properly yet. Do not use it :-)</b>
020: */
021: public class MultiThreadTestSuite extends TestSuite {
022: private final static Log log = LogFactory
023: .getLog(MultiThreadTestSuite.class);
024: private int numberThreads;
025: private boolean doClone;
026:
027: public MultiThreadTestSuite(Class theClass, String name) {
028: this (theClass, name, 5, true);
029: }
030:
031: public MultiThreadTestSuite(Class theClass) {
032: this (theClass, 5, true);
033: }
034:
035: public MultiThreadTestSuite(Class theClass, String name,
036: int numberThreads, boolean doClone) {
037: super (theClass, name);
038: this .numberThreads = numberThreads;
039: this .doClone = doClone;
040: }
041:
042: public MultiThreadTestSuite(Class theClass, int numberThreads,
043: boolean doClone) {
044: super (theClass);
045: this .numberThreads = numberThreads;
046: this .doClone = doClone;
047: }
048:
049: public void run(TestResult result) {
050: Enumeration tests = tests();
051: while (tests.hasMoreElements()) {
052: if (result.shouldStop())
053: return;
054: TestCase currentTest = (TestCase) tests.nextElement();
055: List threads = createThreadListForTest(currentTest, result);
056: runAllThreadsForTest(threads);
057: }
058: }
059:
060: private Test createNewTestInstanceBasedOn(TestCase test) {
061: TestCase newTest = null;
062: try {
063: Constructor constructor = getTestConstructor(test
064: .getClass());
065: if (constructor.getParameterTypes().length == 0) {
066: newTest = (TestCase) constructor
067: .newInstance(new Object[0]);
068: newTest.setName(test.getName());
069:
070: } else {
071: newTest = (TestCase) constructor
072: .newInstance(new Object[] { test.getName() });
073: }
074: } catch (Exception exc) {
075: log.error(exc.getMessage(), exc);
076: }
077: return newTest;
078: }
079:
080: private List createThreadListForTest(TestCase currentTest,
081: TestResult result) {
082: ArrayList threads = new ArrayList(numberThreads);
083: for (int ii = 0; ii < numberThreads; ii++) {
084: Test newTest = currentTest;
085: if (doClone)
086: newTest = createNewTestInstanceBasedOn(currentTest);
087: TestThread thread = new TestThread("TestThread " + ii,
088: newTest, result);
089: threads.add(thread);
090: }
091: return threads;
092: }
093:
094: private void runAllThreadsForTest(List threads) {
095: for (int ii = 0; ii < threads.size(); ii++) {
096: Thread thread = (Thread) threads.get(ii);
097: thread.start();
098: }
099: for (int ii = 0; ii < threads.size(); ii++) {
100: Thread thread = (Thread) threads.get(ii);
101: try {
102: thread.join();
103: } catch (InterruptedException exc) {
104: log.error("Interrupted", exc);
105: }
106: }
107: }
108:
109: private static class TestThread extends Thread {
110: private Test test;
111: private TestResult result;
112:
113: public TestThread(String name, Test test, TestResult result) {
114: super (name);
115: this .test = test;
116: this .result = result;
117: }
118:
119: public void run() {
120: test.run(result);
121: }
122: }
123: }
|