1 package junit.textui; 2 3 4 import java.io.PrintStream ; 5 6 import junit.framework.Test; 7 import junit.framework.TestCase; 8 import junit.framework.TestResult; 9 import junit.framework.TestSuite; 10 import junit.runner.BaseTestRunner; 11 import junit.runner.Version; 12 13 30 public class TestRunner extends BaseTestRunner { 31 private ResultPrinter fPrinter; 32 33 public static final int SUCCESS_EXIT= 0; 34 public static final int FAILURE_EXIT= 1; 35 public static final int EXCEPTION_EXIT= 2; 36 37 40 public TestRunner() { 41 this(System.out); 42 } 43 44 47 public TestRunner(PrintStream writer) { 48 this(new ResultPrinter(writer)); 49 } 50 51 54 public TestRunner(ResultPrinter printer) { 55 fPrinter= printer; 56 } 57 58 61 static public void run(Class <? extends TestCase> testClass) { 62 run(new TestSuite(testClass)); 63 } 64 65 75 static public TestResult run(Test test) { 76 TestRunner runner= new TestRunner(); 77 return runner.doRun(test); 78 } 79 80 84 static public void runAndWait(Test suite) { 85 TestRunner aTestRunner= new TestRunner(); 86 aTestRunner.doRun(suite, true); 87 } 88 89 @Override 90 public void testFailed(int status, Test test, Throwable t) { 91 } 92 93 @Override 94 public void testStarted(String testName) { 95 } 96 97 @Override 98 public void testEnded(String testName) { 99 } 100 101 104 protected TestResult createTestResult() { 105 return new TestResult(); 106 } 107 108 public TestResult doRun(Test test) { 109 return doRun(test, false); 110 } 111 112 public TestResult doRun(Test suite, boolean wait) { 113 TestResult result= createTestResult(); 114 result.addListener(fPrinter); 115 long startTime= System.currentTimeMillis(); 116 suite.run(result); 117 long endTime= System.currentTimeMillis(); 118 long runTime= endTime-startTime; 119 fPrinter.print(result, runTime); 120 121 pause(wait); 122 return result; 123 } 124 125 protected void pause(boolean wait) { 126 if (!wait) return; 127 fPrinter.printWaitPrompt(); 128 try { 129 System.in.read(); 130 } 131 catch(Exception e) { 132 } 133 } 134 135 public static void main(String args[]) { 136 TestRunner aTestRunner= new TestRunner(); 137 try { 138 TestResult r= aTestRunner.start(args); 139 if (!r.wasSuccessful()) 140 System.exit(FAILURE_EXIT); 141 System.exit(SUCCESS_EXIT); 142 } catch(Exception e) { 143 System.err.println(e.getMessage()); 144 System.exit(EXCEPTION_EXIT); 145 } 146 } 147 148 152 public TestResult start(String args[]) throws Exception { 153 String testCase= ""; 154 String method= ""; 155 boolean wait= false; 156 157 for (int i= 0; i < args.length; i++) { 158 if (args[i].equals("-wait")) 159 wait= true; 160 else if (args[i].equals("-c")) 161 testCase= extractClassName(args[++i]); 162 else if (args[i].equals("-m")) { 163 String arg= args[++i]; 164 int lastIndex= arg.lastIndexOf('.'); 165 testCase= arg.substring(0, lastIndex); 166 method= arg.substring(lastIndex + 1); 167 } else if (args[i].equals("-v")) 168 System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma"); 169 else 170 testCase= args[i]; 171 } 172 173 if (testCase.equals("")) 174 throw new Exception ("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"); 175 176 try { 177 if (!method.equals("")) 178 return runSingleMethod(testCase, method, wait); 179 Test suite= getTest(testCase); 180 return doRun(suite, wait); 181 } catch (Exception e) { 182 throw new Exception ("Could not create and run test suite: " + e); 183 } 184 } 185 186 protected TestResult runSingleMethod(String testCase, String method, boolean wait) throws Exception { 187 Class <? extends TestCase> testClass= loadSuiteClass(testCase); 188 Test test= TestSuite.createTest(testClass, method); 189 return doRun(test, wait); 190 } 191 192 @Override 193 protected void runFailed(String message) { 194 System.err.println(message); 195 System.exit(FAILURE_EXIT); 196 } 197 198 public void setPrinter(ResultPrinter printer) { 199 fPrinter= printer; 200 } 201 202 203 } | Popular Tags |