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

« back to all changes in this revision

Viewing changes to src/app/com/clarkware/junitperf/TestFactory.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 junit.framework.Test;
4
 
import junit.framework.TestCase;
5
 
import junit.framework.TestResult;
6
 
import junit.framework.TestSuite;
7
 
 
8
 
/**
9
 
 * The <code>TestFactory</code> class creates thread-local 
10
 
 * <code>TestSuite</code> instances.
11
 
 * <p>
12
 
 * This factory class should be used in cases when a stateful test
13
 
 * is intended to be decorated by a <code>LoadTest</code>.  A stateful
14
 
 * test is defined as any test that defines test-specific state in
15
 
 * its <code>setUp()</code> method.
16
 
 * </p>
17
 
 * <p>
18
 
 * Use of the <code>TestFactory</code> ensures that each thread spawned
19
 
 * by a <code>LoadTest</code> contains its own <code>TestSuite</code>
20
 
 * instance containing all tests defined in the specified 
21
 
 * <code>TestCase</code> class.
22
 
 * </p>
23
 
 * <p>
24
 
 * A typical usage scenario is as follows:
25
 
 * <blockquote>
26
 
 * <pre>
27
 
 * Test factory = new TestFactory(YourTestCase.class);
28
 
 * LoadTest test = new LoadTest(factory, numberOfUsers, ...);
29
 
 * ...
30
 
 * </pre>
31
 
 * </blockquote>
32
 
 * </p>
33
 
 * <p>
34
 
 * Of course, static variables cannot be protected externally, so tests
35
 
 * intended to be run in a multi-threaded environment should ensure
36
 
 * that the use of static variables is thread-safe.
37
 
 * </p>
38
 
 * <p>
39
 
 * This class is dependent on Java 2.  For earlier platforms a 
40
 
 * local cache implementation should be changed to use, for example, 
41
 
 * a HashMap to track thread-local information.
42
 
 * </p>
43
 
 * @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
44
 
 * @author <a href="http://www.clarkware.com">Clarkware Consulting, Inc.</a>
45
 
 * @author Ervin Varga
46
 
 *
47
 
 * @see junit.framework.Test
48
 
 * @see com.clarkware.junitperf.LoadTest
49
 
 */
50
 
 
51
 
public class TestFactory implements Test {
52
 
    
53
 
        protected final Class _testClass;
54
 
        private TestSuite _suite; 
55
 
        private final TestCache _testCache;
56
 
    
57
 
        /**
58
 
         * Constructs a <code>TestFactory</code> instance.
59
 
         *
60
 
         * @param testClass The <code>TestCase</code> class to load test.
61
 
         */
62
 
        public TestFactory(Class testClass) {
63
 
 
64
 
                if (!(TestCase.class.isAssignableFrom(testClass))) {
65
 
                        throw new IllegalArgumentException("TestFactory must " +
66
 
                                "be constructed with a TestCase class.");
67
 
                }
68
 
 
69
 
                _testClass = testClass;
70
 
                _testCache = new TestCache();
71
 
        }
72
 
 
73
 
        /**
74
 
         * Runs an instance of the <code>Test</code> class and 
75
 
         * collects its result in the specified <code>TestResult</code>. 
76
 
         * <p>
77
 
         * Each invocation of this method triggers the creation of a 
78
 
         * new <code>Test</code> class instance as specified in the
79
 
         * construction of this <code>TestFactory</code>.
80
 
         *
81
 
         * @param result Test result.
82
 
         */
83
 
        public void run(TestResult result) {
84
 
                getTest().run(result);        
85
 
        }
86
 
    
87
 
        /**
88
 
         * Returns the number of tests in this test.
89
 
         *
90
 
         * @return Number of tests.
91
 
         */
92
 
        public int countTestCases() {
93
 
                return getTestSuite().countTestCases();
94
 
        }
95
 
 
96
 
        /**
97
 
         * Returns the test description.
98
 
         *
99
 
         * @return Description.
100
 
         */
101
 
        public String toString() {
102
 
                return "TestFactory: " + getTestSuite().toString();
103
 
        }
104
 
        
105
 
        protected Test getTest() {
106
 
                return _testCache.getTest();
107
 
        }
108
 
        
109
 
        protected TestSuite getTestSuite() {
110
 
                if (_suite == null) {
111
 
                        _suite = makeTestSuite();
112
 
                }
113
 
                
114
 
                return _suite;
115
 
        }
116
 
        
117
 
        protected TestSuite makeTestSuite() {
118
 
                return new TestSuite(_testClass);
119
 
        }
120
 
 
121
 
        /*
122
 
         * The <code>TestCache</code> class provides thread-local
123
 
         * instances of a <code>TestSuite</code> class containing
124
 
         * tests defined in the <code>TestCase</code> class
125
 
         * specified in the <code>TestFactory</code>.
126
 
     */ 
127
 
        private final class TestCache {
128
 
 
129
 
                private final ThreadLocal _localCache = new ThreadLocal() {
130
 
 
131
 
                        protected Object initialValue() {
132
 
                                return makeTestSuite();
133
 
                        }
134
 
                };
135
 
        
136
 
                /*
137
 
                 * Returns the <code>Test</code> instance local to the 
138
 
                 * calling thread.
139
 
                 *
140
 
                 * @return Thread-local <code>Test</code> instance.
141
 
                 */
142
 
                Test getTest() {
143
 
                        return (Test)_localCache.get();
144
 
                }
145
 
        }
146
 
}