1 11 package org.eclipse.test; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.io.PrintStream ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 import junit.framework.AssertionFailedError; 27 import junit.framework.Test; 28 import junit.framework.TestListener; 29 import junit.framework.TestResult; 30 import junit.framework.TestSuite; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.taskdefs.optional.junit.FormatterElement; 34 import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter; 35 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest; 36 import org.eclipse.core.runtime.Platform; 37 38 43 public class EclipseTestRunner implements TestListener { 44 class TestFailedException extends Exception { 45 46 private static final long serialVersionUID = 6009335074727417445L; 47 48 TestFailedException(String message) { 49 super(message); 50 } 51 } 52 55 public static final int SUCCESS= 0; 56 59 public static final int FAILURES= 1; 60 63 public static final int ERRORS= 2; 64 65 private static final String SUITE_METHODNAME= "suite"; 66 69 private boolean debug= false; 70 73 private TestResult fTestResult; 74 77 private String fTestPluginName; 78 81 private Test fSuite; 82 85 private static Vector fgFromCmdLine= new Vector (); 86 89 private Vector formatters= new Vector (); 90 93 private boolean fHaltOnError= false; 94 97 private boolean fHaltOnFailure= false; 98 101 private JUnitTest fJunitTest; 102 105 private PrintStream fSystemError; 106 109 private PrintStream fSystemOut; 110 113 private Exception fException; 114 117 private int fRetCode= SUCCESS; 118 119 133 public static void main(String [] args) throws IOException { 134 System.exit(run(args)); 135 } 136 public static int run(String [] args) throws IOException { 137 String className= null; 138 String testPluginName= null; 139 140 boolean haltError = false; 141 boolean haltFail = false; 142 143 Properties props = new Properties (); 144 145 int startArgs= 0; 146 if (args.length > 0) { 147 if (!args[0].startsWith("-")) { 150 className= args[0]; 151 startArgs++; 152 } 153 } 154 for (int i= startArgs; i < args.length; i++) { 155 if (args[i].toLowerCase().equals("-classname")) { 156 if (i < args.length-1) 157 className= args[i+1]; 158 i++; 159 } else if (args[i].toLowerCase().equals("-testpluginname")) { 160 if (i < args.length-1) 161 testPluginName= args[i+1]; 162 i++; 163 } else if (args[i].startsWith("haltOnError=")) { 164 haltError= Project.toBoolean(args[i].substring(12)); 165 } else if (args[i].startsWith("haltOnFailure=")) { 166 haltFail = Project.toBoolean(args[i].substring(14)); 167 } else if (args[i].startsWith("formatter=")) { 168 try { 169 createAndStoreFormatter(args[i].substring(10)); 170 } catch (BuildException be) { 171 System.err.println(be.getMessage()); 172 return ERRORS; 173 } 174 } else if (args[i].startsWith("propsfile=")) { 175 FileInputStream in = new FileInputStream (args[i].substring(10)); 176 props.load(in); 177 in.close(); 178 } else if (args[i].equals("-testlistener")) { 179 System.err.println("The -testlistener option is no longer supported\nuse the formatter= option instead"); 180 return ERRORS; 181 } 182 } 183 184 if (className == null) 185 throw new IllegalArgumentException ("Test class name not specified"); 186 187 JUnitTest t= new JUnitTest(className); 188 189 Hashtable p= System.getProperties(); 191 for (Enumeration _enum = p.keys(); _enum.hasMoreElements(); ) { 192 Object key = _enum.nextElement(); 193 props.put(key, p.get(key)); 194 } 195 t.setProperties(props); 196 197 EclipseTestRunner runner= new EclipseTestRunner(t, testPluginName, haltError, haltFail); 198 transferFormatters(runner); 199 runner.run(); 200 return runner.getRetCode(); 201 } 202 203 206 public EclipseTestRunner(JUnitTest test, String testPluginName, boolean haltOnError, boolean haltOnFailure) { 207 fJunitTest= test; 208 fTestPluginName= testPluginName; 209 fHaltOnError= haltOnError; 210 fHaltOnFailure= haltOnFailure; 211 212 try { 213 fSuite= getTest(test.getName()); 214 } catch(Exception e) { 215 fRetCode = ERRORS; 216 fException = e; 217 } 218 } 219 220 223 protected Test getTest(String suiteClassName) throws TestFailedException { 224 if (suiteClassName.length() <= 0) { 225 clearStatus(); 226 return null; 227 } 228 Class testClass= null; 229 try { 230 testClass= loadSuiteClass(suiteClassName); 231 } catch (ClassNotFoundException e) { 232 String clazz= e.getMessage(); 233 if (clazz == null) 234 clazz= suiteClassName; 235 runFailed("Class not found \""+clazz+"\""); 236 return null; 237 } catch(Exception e) { 238 runFailed("Error: "+e.toString()); 239 return null; 240 } 241 Method suiteMethod= null; 242 try { 243 suiteMethod= testClass.getMethod(SUITE_METHODNAME, new Class [0]); 244 } catch(Exception e) { 245 clearStatus(); 247 return new TestSuite(testClass); 248 } 249 Test test= null; 250 try { 251 test= (Test)suiteMethod.invoke(null, new Class [0]); if (test == null) 253 return test; 254 } 255 catch (InvocationTargetException e) { 256 runFailed("Failed to invoke suite():" + e.getTargetException().toString()); 257 return null; 258 } 259 catch (IllegalAccessException e) { 260 runFailed("Failed to invoke suite():" + e.toString()); 261 return null; 262 } 263 clearStatus(); 264 return test; 265 } 266 267 protected void runFailed(String message) throws TestFailedException { 268 System.err.println(message); 269 throw new TestFailedException(message); 270 } 271 272 protected void clearStatus() { 273 } 274 275 279 protected Class loadSuiteClass(String suiteClassName) throws ClassNotFoundException { 280 if (fTestPluginName == null) 281 return Class.forName(suiteClassName); 282 Class loader= Platform.getBundle(fTestPluginName).loadClass(suiteClassName); 283 return loader; 284 } 285 286 public void run() { 287 289 fTestResult= new TestResult(); 290 fTestResult.addListener(this); 291 for (int i= 0; i < formatters.size(); i++) { 292 fTestResult.addListener((TestListener)formatters.elementAt(i)); 293 } 294 295 long start= System.currentTimeMillis(); 296 fireStartTestSuite(); 297 298 if (fException != null) { for (int i= 0; i < formatters.size(); i++) { 300 ((TestListener)formatters.elementAt(i)).addError(null, fException); 301 } 302 fJunitTest.setCounts(1, 0, 1); 303 fJunitTest.setRunTime(0); 304 } else { 305 ByteArrayOutputStream errStrm = new ByteArrayOutputStream (); 306 fSystemError= new PrintStream (errStrm); 307 308 ByteArrayOutputStream outStrm = new ByteArrayOutputStream (); 309 fSystemOut= new PrintStream (outStrm); 310 311 try { 312 fSuite.run(fTestResult); 314 } finally { 315 fSystemError.close(); 317 fSystemError= null; 318 fSystemOut.close(); 319 fSystemOut= null; 320 sendOutAndErr(new String (outStrm.toByteArray()), new String (errStrm.toByteArray())); 321 fJunitTest.setCounts(fTestResult.runCount(), fTestResult.failureCount(), fTestResult.errorCount()); 322 fJunitTest.setRunTime(System.currentTimeMillis() - start); 323 } 324 } 325 fireEndTestSuite(); 326 327 if (fRetCode != SUCCESS || fTestResult.errorCount() != 0) { 328 fRetCode = ERRORS; 329 } else if (fTestResult.failureCount() != 0) { 330 fRetCode = FAILURES; 331 } 332 333 } 335 336 341 public int getRetCode() { 342 return fRetCode; 343 } 344 345 348 public void startTest(Test t) {} 349 350 353 public void endTest(Test test) {} 354 355 358 public void addFailure(Test test, AssertionFailedError t) { 359 if (fHaltOnFailure) { 360 fTestResult.stop(); 361 } 362 } 363 364 367 public void addError(Test test, Throwable t) { 368 if (fHaltOnError) { 369 fTestResult.stop(); 370 } 371 } 372 373 private void fireStartTestSuite() { 374 for (int i= 0; i < formatters.size(); i++) { 375 ((JUnitResultFormatter)formatters.elementAt(i)).startTestSuite(fJunitTest); 376 } 377 } 378 379 private void fireEndTestSuite() { 380 for (int i= 0; i < formatters.size(); i++) { 381 ((JUnitResultFormatter)formatters.elementAt(i)).endTestSuite(fJunitTest); 382 } 383 } 384 385 public void addFormatter(JUnitResultFormatter f) { 386 formatters.addElement(f); 387 } 388 389 392 private static void createAndStoreFormatter(String line) throws BuildException { 393 FormatterElement fe = new FormatterElement(); 394 String formatterClassName= null; 395 File formatterFile= null; 396 397 int pos = line.indexOf(','); 398 if (pos == -1) { 399 formatterClassName= line; 400 } else { 401 formatterClassName= line.substring(0, pos); 402 formatterFile= new File (line.substring(pos + 1)); } 404 fgFromCmdLine.addElement(createFormatter(formatterClassName, formatterFile)); 405 } 406 407 private static void transferFormatters(EclipseTestRunner runner) { 408 for (int i= 0; i < fgFromCmdLine.size(); i++) { 409 runner.addFormatter((JUnitResultFormatter)fgFromCmdLine.elementAt(i)); 410 } 411 } 412 413 416 private static JUnitResultFormatter createFormatter(String classname, File outfile) throws BuildException { 417 OutputStream out= System.out; 418 419 if (classname == null) { 420 throw new BuildException("you must specify type or classname"); 421 } 422 Class f = null; 423 try { 424 f= EclipseTestRunner.class.getClassLoader().loadClass(classname); 425 } catch (ClassNotFoundException e) { 426 throw new BuildException(e); 427 } 428 429 Object o = null; 430 try { 431 o = f.newInstance(); 432 } catch (InstantiationException e) { 433 throw new BuildException(e); 434 } catch (IllegalAccessException e) { 435 throw new BuildException(e); 436 } 437 438 if (!(o instanceof JUnitResultFormatter)) { 439 throw new BuildException(classname+" is not a JUnitResultFormatter"); 440 } 441 442 JUnitResultFormatter r = (JUnitResultFormatter) o; 443 444 if (outfile != null) { 445 try { 446 out = new FileOutputStream (outfile); 447 } catch (java.io.IOException e) { 448 throw new BuildException(e); 449 } 450 } 451 r.setOutput(out); 452 return r; 453 } 454 455 private void sendOutAndErr(String out, String err) { 456 for (int i=0; i<formatters.size(); i++) { 457 JUnitResultFormatter formatter = 458 ((JUnitResultFormatter)formatters.elementAt(i)); 459 460 formatter.setSystemOutput(out); 461 formatter.setSystemError(err); 462 } 463 } 464 465 protected void handleOutput(String line) { 466 if (fSystemOut != null) { 467 fSystemOut.println(line); 468 } 469 } 470 471 protected void handleErrorOutput(String line) { 472 if (fSystemError != null) { 473 fSystemError.println(line); 474 } 475 } 476 } 477 | Popular Tags |