1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TestRunner.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.ArrayList;
48 import java.util.Enumeration;
49
50 import junit.extensions.TestSetup;
51 import junit.framework.Test;
52 import junit.framework.TestResult;
53 import junit.framework.TestSuite;
54
55
56 /***
57 * This is the base class for test case runners.
58 *
59 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
60 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61 */
62 public abstract class TestRunner extends TestSetup implements JMSTest {
63
64 /***
65 * The context to test against
66 */
67 private TestContext _context = null;
68
69 /***
70 * The child context
71 */
72 private TestContext _child = null;
73
74 /***
75 * The test case filter, if any
76 */
77 private TestFilter _filter = null;
78
79 /***
80 * Construct an instance with the test case to run.
81 *
82 * @param test the test case to run.
83 */
84 public TestRunner(Test test) {
85 super(test);
86 }
87
88 /***
89 * Set the context to test against
90 *
91 * @param context the test context
92 */
93 public void setContext(TestContext context) {
94 _context = context;
95 }
96
97 /***
98 * Return the context to test against
99 *
100 * @return the test context
101 */
102 public TestContext getContext() {
103 return _context;
104 }
105
106 /***
107 * Set the test case filter
108 *
109 * @param filter the test case filter
110 */
111 public void setFilter(TestFilter filter) {
112 _filter = filter;
113 }
114
115 /***
116 * Return the test case filter
117 *
118 * @return the test case filter
119 */
120 public TestFilter getFilter() {
121 return _filter;
122 }
123
124 /***
125 * Returns if this test can share resources with other test cases.
126 * This implementation always returns <code>true</code>
127 *
128 * @return <code>true</code>
129 */
130 public boolean share() {
131 return true;
132 }
133
134 /***
135 * Runs the test case, by invoking {@link #runTest}. If {@link #getTest}
136 * returns a TestSuite, then {@link #runTest} will be invoked for each
137 * contained test.
138 *
139 * @param result the instance to collect results in
140 */
141 public void basicRun(TestResult result) {
142 Test[] tests = getTests();
143 for (int i = 0; i < tests.length && !result.shouldStop(); ++i) {
144 runTest(tests[i], result);
145 }
146 }
147
148 /***
149 * Run a test case
150 *
151 * @param test the test case to run
152 * @param result the instance to collect results in
153 */
154 protected abstract void runTest(Test test, TestResult result);
155
156 /***
157 * Sets the test context of child test cases
158 *
159 * @param context the child test case context
160 */
161 protected void setChildContext(TestContext context) {
162 _child = context;
163 }
164
165 /***
166 * Returns the test context of child test cases
167 *
168 * @return the child test case context
169 */
170 protected TestContext getChildContext() {
171 return _child;
172 }
173
174 /***
175 * Cleans up after the test case has been run
176 *
177 * @throws Exception for any error
178 */
179 protected void tearDown() throws Exception {
180 _context = null;
181 }
182
183 /***
184 * Helper method to check that a test case or suite implements the
185 * appropriate interface.
186 *
187 * @param type the interface that the test must implement
188 * @throws Exception if the test doesn't implement the required interface
189 */
190 protected void checkImplements(Class type) throws Exception {
191 Test[] tests = getTests();
192 for (int i = 0; i < tests.length; ++i) {
193 Test test = tests[i];
194 if (!type.isAssignableFrom(test.getClass())) {
195 throw new Exception("Test " + test + " doesn't implement "
196 + type.getName());
197 }
198 }
199 }
200
201 /***
202 * Returns the test(s) as an array. If the test is a {@link TestSuite},
203 * the contained tests will be extracted.
204 *
205 * @return an array of the contained tests
206 */
207 protected Test[] getTests() {
208 Test test = getTest();
209 ArrayList result = new ArrayList();
210
211 if (test instanceof TestSuite) {
212 Enumeration iter = ((TestSuite) test).tests();
213 while (iter.hasMoreElements()) {
214 result.add(iter.nextElement());
215 }
216 } else {
217 result.add(test);
218 }
219 return (Test[]) result.toArray(new Test[]{});
220 }
221
222 }
This page was automatically generated by Maven