1
package com.clarkware.junitperf;
4
import java.lang.reflect.*;
6
import junit.framework.Test;
7
import junit.framework.TestCase;
8
import junit.framework.TestSuite;
11
* The <code>TestMethodFactory</code> class is a <code>TestFactory</code>
12
* that creates thread-local <code>TestSuite</code> instances containing
13
* a specific test method of a <code>TestCase</code>.
15
* A typical usage scenario is as follows:
18
* Test factory = new TestMethodFactory(YourTestCase.class, "testSomething");
19
* LoadTest test = new LoadTest(factory, numberOfUsers, ...);
25
* @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
26
* @author <a href="http://www.clarkware.com">Clarkware Consulting, Inc.</a>
28
* @see com.clarkware.junitperf.TestFactory
29
* @see com.clarkware.junitperf.LoadTest
32
public class TestMethodFactory extends TestFactory {
34
private final String _testMethodName;
37
* Constructs a <code>TestMethodFactory</code> instance.
39
* @param testClass The <code>TestCase</code> class to load test.
40
* @param testMethodName The name of the test method to load test.
42
public TestMethodFactory(Class testClass, String testMethodName) {
44
_testMethodName = testMethodName;
47
protected TestSuite makeTestSuite() {
49
TestSuite suite = new TestSuite();
51
Constructor constructor = null;
55
constructor = getConstructor(_testClass);
57
} catch (NoSuchMethodException e) {
58
suite.addTest(warning("Class " + _testClass.getName() +
59
" has no public constructor TestCase(String name)"));
63
if (!Modifier.isPublic(_testClass.getModifiers())) {
64
suite.addTest(warning("Class " + _testClass.getName() +
69
addTestMethod(suite, constructor, _testMethodName);
71
if (suite.testCount() == 0) {
72
suite.addTest(warning("No tests found in " + _testClass.getName()));
79
addTestMethod(TestSuite suite, Constructor constructor, String methodName) {
81
Object[] args = new Object[] { methodName };
85
suite.addTest((Test)constructor.newInstance(args));
87
} catch (InstantiationException ie) {
88
suite.addTest(warning("Cannot instantiate test case: " +
89
methodName + " (" + toString(ie) + ")"));
90
} catch (InvocationTargetException ite) {
91
suite.addTest(warning("Exception in constructor: " +
92
methodName + " (" + toString(ite.getTargetException()) + ")"));
93
} catch (IllegalAccessException iae) {
94
suite.addTest(warning("Cannot access test case: " +
95
methodName + " (" + toString(iae) + ")"));
99
private Constructor getConstructor(Class theClass)
100
throws NoSuchMethodException {
102
Class[] args = { String.class };
103
return theClass.getConstructor(args);
106
private Test warning(final String message) {
107
return new TestCase("warning") {
108
protected void runTest() {
114
private String toString(Throwable t) {
115
StringWriter stringWriter = new StringWriter();
116
PrintWriter writer = new PrintWriter(stringWriter);
117
t.printStackTrace(writer);
118
return stringWriter.toString();