1 2 3 package org.quilt.runner; 4 5 import java.io.ByteArrayOutputStream ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.IOException ; 9 import java.io.OutputStream ; 10 import java.io.PrintStream ; 11 12 import java.lang.reflect.Method ; 13 import java.util.Enumeration ; 14 import java.util.Properties ; 15 import java.util.Vector ; 16 17 import org.apache.tools.ant.AntClassLoader; 18 import org.apache.tools.ant.BuildException; 19 import org.apache.tools.ant.Project; 20 21 import junit.framework.*; 22 23 import org.quilt.cl.*; 24 import org.quilt.framework.QuiltTest; 25 import org.quilt.reports.*; 26 27 36 37 public class BaseTestRunner extends Runner { 38 39 40 private QuiltTest qt; 41 42 43 private Test suite = null; 44 45 46 private Exception exception = null; 47 48 49 private int retCode = SUCCESS; 50 51 52 private TestResult res; 53 54 56 57 private Vector formatters = new Vector (); 58 59 60 62 63 65 69 71 72 private PrintStream systemError; 73 74 75 private PrintStream systemOut; 76 77 79 86 public BaseTestRunner (QuiltTest test) { 87 this (test, null); 88 } 89 99 public BaseTestRunner(QuiltTest test, ClassLoader loader) { 100 qt = test; 101 102 try { 103 Class testClass = null; 104 if (loader == null) { 105 testClass = Class.forName(qt.getName()); 106 } else { 107 testClass = loader.loadClass(qt.getName()); 108 if (!(loader instanceof QuiltClassLoader)) { 109 AntClassLoader.initializeClass(testClass); 111 } 112 } 113 114 Method suiteMethod = null; 115 try { 116 suiteMethod = testClass.getMethod("suite", new Class [0]); 118 } catch (Exception e) { 119 } 121 if (suiteMethod != null){ 122 suite = (Test) suiteMethod.invoke(null, new Class [0]); 125 } else { 126 suite = new TestSuite(testClass); 129 } 130 131 } catch (Exception e) { 132 retCode = ERRORS; 133 exception = e; 134 } 135 } 136 149 183 public void run() { 184 res = new TestResult(); 185 res.addListener(this); 186 for (int i = 0; i < formatters.size(); i++) { 187 res.addListener((TestListener) formatters.elementAt(i)); 188 } 189 190 long start = System.currentTimeMillis(); 191 192 fireStartTestSuite(); 193 if (exception != null) { for (int i = 0; i < formatters.size(); i++) { 195 ((TestListener) formatters.elementAt(i)).addError(null, 196 exception); 197 } 198 qt.setCounts(1, 0, 1); 199 qt.setRunTime(0); 200 } else { 201 202 203 ByteArrayOutputStream errStrm = new ByteArrayOutputStream (); 204 systemError = new PrintStream (errStrm); 205 206 ByteArrayOutputStream outStrm = new ByteArrayOutputStream (); 207 systemOut = new PrintStream (outStrm); 208 209 PrintStream savedOut = null; 210 PrintStream savedErr = null; 211 212 if ( qt.getFork() ) { 213 savedOut = System.out; 214 savedErr = System.err; 215 if (!qt.getShowOutput()) { 216 System.setOut(systemOut); 217 System.setErr(systemError); 218 } else { 219 System.setOut(new PrintStream ( 220 new TeeOutputStream( 221 new OutputStream [] {savedOut, 222 systemOut} 223 ) 224 ) 225 ); 226 System.setErr(new PrintStream ( 227 new TeeOutputStream( 228 new OutputStream [] {savedErr, 229 systemError} 230 ) 231 ) 232 ); 233 } 234 } 235 236 237 try { 238 suite.run(res); 239 } finally { 240 if (savedOut != null) { 241 System.setOut(savedOut); 242 } 243 if (savedErr != null) { 244 System.setErr(savedErr); 245 } 246 247 systemError.close(); 248 systemError = null; 249 systemOut.close(); 250 systemOut = null; 251 sendOutAndErr(new String (outStrm.toByteArray()), 252 new String (errStrm.toByteArray())); 253 254 qt.setCounts(res.runCount(), res.failureCount(), 255 res.errorCount()); 256 qt.setRunTime(System.currentTimeMillis() - start); 257 } 258 } 259 fireEndTestSuite(); 260 261 if (retCode != SUCCESS || res.errorCount() != 0) { 262 retCode = ERRORS; 263 } else if (res.failureCount() != 0) { 264 retCode = FAILURES; 265 } 266 } 267 268 273 public int getRetCode() { 274 return retCode; 275 } 276 277 283 284 public void startTest(Test t) {} 285 286 287 public void endTest(Test test) {} 288 289 290 public void addFailure(Test test, Throwable t) { 291 if (qt.getHaltOnFailure()) { 292 res.stop(); 293 } 294 } 295 296 297 public void addFailure(Test test, AssertionFailedError t) { 298 addFailure(test, (Throwable ) t); 299 } 300 301 302 public void addError(Test test, Throwable t) { 303 if (qt.getHaltOnError()) { 304 res.stop(); 305 } 306 } 307 308 309 public void handleOutput(String line) { 310 if (systemOut != null) { 311 systemOut.println(line); 312 } 313 } 314 315 316 public void handleErrorOutput(String line) { 317 if (systemError != null) { 318 systemError.println(line); 319 } 320 } 321 322 public void handleFlush(String line) { 323 if (systemOut != null) { 324 systemOut.print(line); 325 } 326 } 327 328 public void handleErrorFlush(String line) { 329 if (systemError != null) { 330 systemError.print(line); 331 } 332 } 333 337 private void sendOutAndErr(String out, String err) { 338 for (int i = 0; i < formatters.size(); i++) { 339 Formatter formatter = 340 ((Formatter) formatters.elementAt(i)); 341 342 formatter.setSystemOutput(out); 343 formatter.setSystemError(err); 344 } 345 } 346 347 348 private void fireStartTestSuite() { 349 for (int i = 0; i < formatters.size(); i++) { 350 ((Formatter) formatters.elementAt(i)) 351 .startTestSuite(qt); 352 ((Formatter) formatters.elementAt(i)) 355 .setFiltertrace(qt.getFiltertrace()); 356 } 357 } 358 359 private void fireEndTestSuite() { 360 for (int i = 0; i < formatters.size(); i++) { 361 ((Formatter) formatters.elementAt(i)) 362 .endTestSuite(qt); 363 } 364 } 365 366 367 public void addFormatter(Formatter f) { 368 formatters.addElement(f); 369 } 370 371 private class TeeOutputStream extends OutputStream { 373 374 private OutputStream [] outs; 375 376 private TeeOutputStream(OutputStream [] outs) { 377 this.outs = outs; 378 } 379 380 public void write(int b) throws IOException { 381 for (int i = 0; i < outs.length; i++) { 382 outs[i].write(b); 383 } 384 } 385 } 386 } 387 | Popular Tags |