KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > runner > engine > RunEngine


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2003 Vladimir Ritz Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.runner.engine;
56
57 import java.lang.reflect.Method JavaDoc;
58 import java.lang.reflect.Modifier JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.Vector JavaDoc;
61
62 import junit.framework.AssertionFailedError;
63 import junit.framework.Test;
64 import junit.framework.TestCase;
65 import junit.framework.TestSuite;
66 import junitx.framework.TestListener;
67 import junitx.framework.*;
68 import junitx.framework.TestResult14;
69
70 /**
71  *
72  * @version $Revision: 1.8 $, $Date: 2003/05/08 02:51:16 $
73  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir Ritz Bossicard</a>
74  */

75 public class RunEngine implements RunContext {
76
77     public static final String JavaDoc SUITE_METHODNAME = "suite";
78
79     junit.framework.TestResult m_testresult;
80
81     private RunMonitor m_monitor;
82
83     private List JavaDoc m_runListeners = new Vector JavaDoc();
84
85     public RunEngine(RunMonitor monitor) {
86         m_monitor = monitor;
87     }
88
89     public void addRunListener(RunListener listener) {
90         listener.setRunContext(this);
91         m_runListeners.add(listener);
92     }
93
94     public void removeRunListener(RunListener listener) {
95         if (m_runListeners.contains(listener)) {
96             m_runListeners.remove(listener);
97         }
98     }
99
100     public boolean isRunning() {
101         return m_testresult != null;
102     }
103
104     public void stop() {
105         if (m_testresult != null) {
106             m_testresult.stop();
107         }
108     }
109
110     public Test getTest(String JavaDoc classname) throws Exception JavaDoc {
111         if (classname.length() <= 0) {
112             throw new IllegalArgumentException JavaDoc("Unable to load test with empty name");
113         }
114         Class JavaDoc testClass = Class.forName(classname);
115         Method JavaDoc suiteMethod = null;
116         try {
117             suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class JavaDoc[0]);
118         } catch (Exception JavaDoc e) {
119             // try to extract a test suite automatically
120
return new TestSuite(testClass);
121         }
122
123         if (!Modifier.isStatic(suiteMethod.getModifiers())) {
124             return warning("Method 'suite' must be static (class " + classname + ")");
125         } else if (!Modifier.isPublic(suiteMethod.getModifiers())) {
126             return warning("Method 'suite' must be public (class " + classname + ")");
127         } else if (!Test.class.isAssignableFrom(suiteMethod.getReturnType())) {
128             return warning("Method 'suite' must return a Test (class " + classname + ")");
129         } else if (suiteMethod.getParameterTypes().length != 0) {
130             return warning("Method 'suite' must have no argument (class " + classname + ")");
131         }
132
133         return (Test) suiteMethod.invoke(null, new Class JavaDoc[0]);
134     }
135
136     private boolean runningJDK14() {
137         try {
138             Class.forName("java.lang.AssertionError");
139             return true;
140         } catch (Exception JavaDoc e) {
141             return false;
142         }
143     }
144
145     public junit.framework.TestResult runTest(Test test) {
146         if (runningJDK14()) {
147             m_testresult = new TestResult14(m_monitor);
148         } else {
149             m_testresult = new TestResult(m_monitor);
150         }
151
152         /* The idea is to hide the TestResult from the listeners and only
153          * communicate via the RunListener interface. */

154         m_testresult.addListener(new TestListener() {
155
156             public synchronized void startTest(Test test) {
157                 fireTestStarted(test, m_testresult);
158             }
159
160             public synchronized void endTest(Test test) {
161                 fireTestSuccess(test, m_testresult);
162             }
163
164             public synchronized void addIgnore(final Test test) {
165                 fireTestIgnored(test);
166             }
167
168             public synchronized void addError(final Test test, final Throwable JavaDoc t) {
169                 fireTestError(test, t);
170             }
171
172             public synchronized void addFailure(final Test test, final AssertionFailedError t) {
173                 fireTestFailure(test, t);
174             }
175
176             public synchronized void addFailure(final Test test, final Throwable JavaDoc t) {
177                 fireTestFailure(test, t);
178             }
179         });
180
181         long startTime = System.currentTimeMillis();
182         fireRunStarted(test, startTime);
183         test.run(m_testresult);
184         fireRunEnded(test, m_testresult, System.currentTimeMillis() - startTime);
185
186         return m_testresult;
187     }
188
189     protected synchronized void fireRunStarted(final Test test, long time) {
190         for (int ii = 0; ii < m_runListeners.size(); ii++) {
191             RunListener listener = (RunListener) m_runListeners.get(ii);
192             listener.runStarted(test, time);
193         }
194     }
195
196     protected synchronized void fireRunStopped(final Test test, long duration) {
197         for (int ii = 0; ii < m_runListeners.size(); ii++) {
198             RunListener listener = (RunListener) m_runListeners.get(ii);
199             listener.runStopped(test, duration);
200         }
201     }
202
203     protected synchronized void fireRunEnded(final Test test, final junit.framework.TestResult result, long duration) {
204         for (int ii = 0; ii < m_runListeners.size(); ii++) {
205             RunListener listener = (RunListener) m_runListeners.get(ii);
206             listener.runEnded(test, result, duration);
207         }
208     }
209
210     protected synchronized void fireTestIgnored(final Test test) {
211         for (int ii = 0; ii < m_runListeners.size(); ii++) {
212             RunListener listener = (RunListener) m_runListeners.get(ii);
213             listener.testIgnored(test);
214         }
215     }
216
217     protected synchronized void fireTestStarted(final Test test, final junit.framework.TestResult result) {
218         for (int ii = 0; ii < m_runListeners.size(); ii++) {
219             RunListener listener = (RunListener) m_runListeners.get(ii);
220             listener.testStarted(test, result);
221         }
222     }
223
224     protected synchronized void fireTestFailure(final Test test, final Throwable JavaDoc t) {
225         for (int ii = 0; ii < m_runListeners.size(); ii++) {
226             RunListener listener = (RunListener) m_runListeners.get(ii);
227             listener.testFailure(test, m_testresult, t);
228         }
229     }
230
231     protected synchronized void fireTestError(final Test test, final Throwable JavaDoc t) {
232         for (int ii = 0; ii < m_runListeners.size(); ii++) {
233             RunListener listener = (RunListener) m_runListeners.get(ii);
234             listener.testError(test, m_testresult, t);
235         }
236     }
237
238     protected synchronized void fireTestSuccess(final Test test, final junit.framework.TestResult result) {
239         for (int ii = 0; ii < m_runListeners.size(); ii++) {
240             RunListener listener = (RunListener) m_runListeners.get(ii);
241             listener.testSuccess(test, result);
242         }
243     }
244
245     /**
246      * Returns a test which will fail and log a warning message.
247      */

248     private static Test warning(final String JavaDoc message) {
249         return new TestCase("warning") {
250             protected void runTest() {
251                 fail(message);
252             }
253         };
254     }
255
256 }
257
Popular Tags