001: package com.clarkware.junitperf;
002:
003: import java.io.*;
004: import java.lang.reflect.*;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009:
010: /**
011: * The <code>TestMethodFactory</code> class is a <code>TestFactory</code>
012: * that creates thread-local <code>TestSuite</code> instances containing
013: * a specific test method of a <code>TestCase</code>.
014: * <p>
015: * A typical usage scenario is as follows:
016: * <blockquote>
017: * <pre>
018: * Test factory = new TestMethodFactory(YourTestCase.class, "testSomething");
019: * LoadTest test = new LoadTest(factory, numberOfUsers, ...);
020: * ...
021: * </pre>
022: * </blockquote>
023: * </p>
024: *
025: * @author <b>Mike Clark</b>
026: * @author Clarkware Consulting, Inc.
027: *
028: * @see com.clarkware.junitperf.TestFactory
029: * @see com.clarkware.junitperf.LoadTest
030: */
031:
032: public class TestMethodFactory extends TestFactory {
033:
034: private final String testMethodName;
035:
036: /**
037: * Constructs a <code>TestMethodFactory</code> instance.
038: *
039: * @param testClass The <code>TestCase</code> class to load test.
040: * @param testMethodName The name of the test method to load test.
041: */
042: public TestMethodFactory(Class testClass, String testMethodName) {
043: super (testClass);
044: this .testMethodName = testMethodName;
045: }
046:
047: protected TestSuite makeTestSuite() {
048:
049: TestSuite suite = new TestSuite();
050:
051: Constructor constructor = null;
052:
053: try {
054:
055: constructor = getConstructor(testClass);
056:
057: } catch (NoSuchMethodException e) {
058: suite
059: .addTest(warning("Class "
060: + testClass.getName()
061: + " has no public constructor TestCase(String name)"));
062: return suite;
063: }
064:
065: if (!Modifier.isPublic(testClass.getModifiers())) {
066: suite.addTest(warning("Class " + testClass.getName()
067: + " is not public"));
068: return suite;
069: }
070:
071: addTestMethod(suite, constructor, testMethodName);
072:
073: if (suite.testCount() == 0) {
074: suite.addTest(warning("No tests found in "
075: + testClass.getName()));
076: }
077:
078: return suite;
079: }
080:
081: private void addTestMethod(TestSuite suite,
082: Constructor constructor, String methodName) {
083:
084: Object[] args = new Object[] { methodName };
085:
086: try {
087:
088: suite.addTest((Test) constructor.newInstance(args));
089:
090: } catch (InstantiationException ie) {
091: suite.addTest(warning("Cannot instantiate test case: "
092: + methodName + " (" + toString(ie) + ")"));
093: } catch (InvocationTargetException ite) {
094: suite.addTest(warning("Exception in constructor: "
095: + methodName + " ("
096: + toString(ite.getTargetException()) + ")"));
097: } catch (IllegalAccessException iae) {
098: suite.addTest(warning("Cannot access test case: "
099: + methodName + " (" + toString(iae) + ")"));
100: }
101: }
102:
103: private Constructor getConstructor(Class theClass)
104: throws NoSuchMethodException {
105:
106: Class[] args = { String.class };
107: return theClass.getConstructor(args);
108: }
109:
110: private Test warning(final String message) {
111: return new TestCase("warning") {
112: protected void runTest() {
113: fail(message);
114: }
115: };
116: }
117:
118: private String toString(Throwable t) {
119: StringWriter stringWriter = new StringWriter();
120: PrintWriter writer = new PrintWriter(stringWriter);
121: t.printStackTrace(writer);
122: return stringWriter.toString();
123: }
124: }
|