1. package junit.textui;
  2. import java.io.PrintStream;
  3. import junit.framework.*;
  4. import junit.runner.*;
  5. /**
  6. * A command line based tool to run tests.
  7. * <pre>
  8. * java junit.textui.TestRunner [-wait] TestCaseClass
  9. * </pre>
  10. * TestRunner expects the name of a TestCase class as argument.
  11. * If this class defines a static <code>suite</code> method it
  12. * will be invoked and the returned test is run. Otherwise all
  13. * the methods starting with "test" having no arguments are run.
  14. * <p>
  15. * When the wait command line argument is given TestRunner
  16. * waits until the users types RETURN.
  17. * <p>
  18. * TestRunner prints a trace as the tests are executed followed by a
  19. * summary at the end.
  20. */
  21. public class TestRunner extends BaseTestRunner {
  22. private ResultPrinter fPrinter;
  23. public static final int SUCCESS_EXIT= 0;
  24. public static final int FAILURE_EXIT= 1;
  25. public static final int EXCEPTION_EXIT= 2;
  26. /**
  27. * Constructs a TestRunner.
  28. */
  29. public TestRunner() {
  30. this(System.out);
  31. }
  32. /**
  33. * Constructs a TestRunner using the given stream for all the output
  34. */
  35. public TestRunner(PrintStream writer) {
  36. this(new ResultPrinter(writer));
  37. }
  38. /**
  39. * Constructs a TestRunner using the given ResultPrinter all the output
  40. */
  41. public TestRunner(ResultPrinter printer) {
  42. fPrinter= printer;
  43. }
  44. /**
  45. * Runs a suite extracted from a TestCase subclass.
  46. */
  47. static public void run(Class testClass) {
  48. run(new TestSuite(testClass));
  49. }
  50. /**
  51. * Runs a single test and collects its results.
  52. * This method can be used to start a test run
  53. * from your program.
  54. * <pre>
  55. * public static void main (String[] args) {
  56. * test.textui.TestRunner.run(suite());
  57. * }
  58. * </pre>
  59. */
  60. static public TestResult run(Test test) {
  61. TestRunner runner= new TestRunner();
  62. return runner.doRun(test);
  63. }
  64. /**
  65. * Runs a single test and waits until the user
  66. * types RETURN.
  67. */
  68. static public void runAndWait(Test suite) {
  69. TestRunner aTestRunner= new TestRunner();
  70. aTestRunner.doRun(suite, true);
  71. }
  72. /**
  73. * Always use the StandardTestSuiteLoader. Overridden from
  74. * BaseTestRunner.
  75. */
  76. public TestSuiteLoader getLoader() {
  77. return new StandardTestSuiteLoader();
  78. }
  79. public void testFailed(int status, Test test, Throwable t) {
  80. }
  81. public void testStarted(String testName) {
  82. }
  83. public void testEnded(String testName) {
  84. }
  85. /**
  86. * Creates the TestResult to be used for the test run.
  87. */
  88. protected TestResult createTestResult() {
  89. return new TestResult();
  90. }
  91. public TestResult doRun(Test test) {
  92. return doRun(test, false);
  93. }
  94. public TestResult doRun(Test suite, boolean wait) {
  95. TestResult result= createTestResult();
  96. result.addListener(fPrinter);
  97. long startTime= System.currentTimeMillis();
  98. suite.run(result);
  99. long endTime= System.currentTimeMillis();
  100. long runTime= endTime-startTime;
  101. fPrinter.print(result, runTime);
  102. pause(wait);
  103. return result;
  104. }
  105. protected void pause(boolean wait) {
  106. if (!wait) return;
  107. fPrinter.printWaitPrompt();
  108. try {
  109. System.in.read();
  110. }
  111. catch(Exception e) {
  112. }
  113. }
  114. public static void main(String args[]) {
  115. TestRunner aTestRunner= new TestRunner();
  116. try {
  117. TestResult r= aTestRunner.start(args);
  118. if (!r.wasSuccessful())
  119. System.exit(FAILURE_EXIT);
  120. System.exit(SUCCESS_EXIT);
  121. } catch(Exception e) {
  122. System.err.println(e.getMessage());
  123. System.exit(EXCEPTION_EXIT);
  124. }
  125. }
  126. /**
  127. * Starts a test run. Analyzes the command line arguments
  128. * and runs the given test suite.
  129. */
  130. protected TestResult start(String args[]) throws Exception {
  131. String testCase= "";
  132. boolean wait= false;
  133. for (int i= 0; i < args.length; i++) {
  134. if (args[i].equals("-wait"))
  135. wait= true;
  136. else if (args[i].equals("-c"))
  137. testCase= extractClassName(args[++i]);
  138. else if (args[i].equals("-v"))
  139. System.err.println("JUnit "+Version.id()+" by Kent Beck and Erich Gamma");
  140. else
  141. testCase= args[i];
  142. }
  143. if (testCase.equals(""))
  144. throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
  145. try {
  146. Test suite= getTest(testCase);
  147. return doRun(suite, wait);
  148. }
  149. catch(Exception e) {
  150. throw new Exception("Could not create and run test suite: "+e);
  151. }
  152. }
  153. protected void runFailed(String message) {
  154. System.err.println(message);
  155. System.exit(FAILURE_EXIT);
  156. }
  157. public void setPrinter(ResultPrinter printer) {
  158. fPrinter= printer;
  159. }
  160. }