~ubuntu-branches/ubuntu/quantal/junitperf/quantal

« back to all changes in this revision

Viewing changes to src/app/com/clarkware/junitperf/TestMethodFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-11-06 12:10:29 UTC
  • mfrom: (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20061106121029-0f8e7pzmig6sh1x5
Tags: 1.9.1-5
* built with java-gcj-compat-dev.
* debian/rules: 
  + removed ant-launcher.jar
  + removed the test target (closes: #396419)
* Standards-Version: 3.7.2 (no change)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package com.clarkware.junitperf;
2
 
 
3
 
import java.io.*;
4
 
import java.lang.reflect.*;
5
 
 
6
 
import junit.framework.Test;
7
 
import junit.framework.TestCase;
8
 
import junit.framework.TestSuite;
9
 
 
10
 
/**
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>.
14
 
 * <p>
15
 
 * A typical usage scenario is as follows:
16
 
 * <blockquote>
17
 
 * <pre>
18
 
 * Test factory = new TestMethodFactory(YourTestCase.class, "testSomething");
19
 
 * LoadTest test = new LoadTest(factory, numberOfUsers, ...);
20
 
 * ...
21
 
 * </pre>
22
 
 * </blockquote>
23
 
 * </p>
24
 
 * 
25
 
 * @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
26
 
 * @author <a href="http://www.clarkware.com">Clarkware Consulting, Inc.</a>
27
 
 *
28
 
 * @see com.clarkware.junitperf.TestFactory
29
 
 * @see com.clarkware.junitperf.LoadTest
30
 
 */
31
 
 
32
 
public class TestMethodFactory extends TestFactory {
33
 
    
34
 
        private final String _testMethodName;
35
 
 
36
 
        /**
37
 
         * Constructs a <code>TestMethodFactory</code> instance.
38
 
         *
39
 
         * @param testClass The <code>TestCase</code> class to load test.
40
 
         * @param testMethodName The name of the test method to load test.
41
 
         */
42
 
        public TestMethodFactory(Class testClass, String testMethodName) {
43
 
                super(testClass);
44
 
                _testMethodName = testMethodName;
45
 
        }
46
 
        
47
 
        protected TestSuite makeTestSuite() {
48
 
                
49
 
                TestSuite suite = new TestSuite();
50
 
                
51
 
                Constructor constructor = null;
52
 
                
53
 
                try {
54
 
                
55
 
                        constructor = getConstructor(_testClass);
56
 
                
57
 
                } catch (NoSuchMethodException e) {
58
 
                        suite.addTest(warning("Class " + _testClass.getName() + 
59
 
                                " has no public constructor TestCase(String name)"));
60
 
                        return suite;
61
 
                }
62
 
 
63
 
                if (!Modifier.isPublic(_testClass.getModifiers())) {
64
 
                        suite.addTest(warning("Class " + _testClass.getName() + 
65
 
                                " is not public"));
66
 
                        return suite;
67
 
                }
68
 
                
69
 
                addTestMethod(suite, constructor, _testMethodName);
70
 
 
71
 
                if (suite.testCount() == 0) {
72
 
                        suite.addTest(warning("No tests found in " + _testClass.getName()));
73
 
                }
74
 
                
75
 
                return suite;
76
 
        }
77
 
        
78
 
        private void 
79
 
        addTestMethod(TestSuite suite, Constructor constructor, String methodName) {
80
 
 
81
 
                Object[] args = new Object[] { methodName };
82
 
                
83
 
                try {
84
 
                        
85
 
                        suite.addTest((Test)constructor.newInstance(args));
86
 
                        
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) + ")"));
96
 
                }
97
 
        }
98
 
        
99
 
        private Constructor getConstructor(Class theClass) 
100
 
                throws NoSuchMethodException {
101
 
                
102
 
                Class[] args = { String.class };
103
 
                return theClass.getConstructor(args);
104
 
        }
105
 
        
106
 
        private Test warning(final String message) {
107
 
                return new TestCase("warning") {
108
 
                        protected void runTest() {
109
 
                                fail(message);
110
 
                        }
111
 
                };
112
 
        }
113
 
        
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();
119
 
        }
120
 
}