1 54 55 package junitx.runner.engine; 56 57 import java.lang.reflect.Method ; 58 import java.lang.reflect.Modifier ; 59 import java.util.List ; 60 import java.util.Vector ; 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 75 public class RunEngine implements RunContext { 76 77 public static final String SUITE_METHODNAME = "suite"; 78 79 junit.framework.TestResult m_testresult; 80 81 private RunMonitor m_monitor; 82 83 private List m_runListeners = new Vector (); 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 classname) throws Exception { 111 if (classname.length() <= 0) { 112 throw new IllegalArgumentException ("Unable to load test with empty name"); 113 } 114 Class testClass = Class.forName(classname); 115 Method suiteMethod = null; 116 try { 117 suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class [0]); 118 } catch (Exception e) { 119 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 [0]); 134 } 135 136 private boolean runningJDK14() { 137 try { 138 Class.forName("java.lang.AssertionError"); 139 return true; 140 } catch (Exception 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 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 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 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 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 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 248 private static Test warning(final String message) { 249 return new TestCase("warning") { 250 protected void runTest() { 251 fail(message); 252 } 253 }; 254 } 255 256 } 257 | Popular Tags |