1 package jester; 2 3 import java.io.*; 4 import java.lang.reflect.Method ; 5 6 import junit.framework.*; 7 8 24 public class TestRunnerImpl implements TestListener { 25 static String FAILED = "FAILED"; 26 static String PASSED = "PASSED"; 27 static long ONE_HOUR = 1 * 60 * 60 * 1000; 28 static long TEN_SECONDS = 10 * 1000; 29 static long MINIMUM_TEST_TIMEOUT = TEN_SECONDS; 30 static long TEST_TIMEOUT_IF_NONE_SET = ONE_HOUR; 31 32 public TestRunnerImpl() { 33 } 34 35 public synchronized void addError(Test test, Throwable t) { 36 print(FAILED); 37 System.exit(0); 38 } 39 40 public synchronized void addFailure(Test test, AssertionFailedError t) { 41 print(FAILED); 42 System.exit(0); 43 } 44 47 protected TestResult createTestResult() { 48 return new TestResult(); 49 } 50 51 public void endTest(Test test) { 52 } 53 54 57 public static void main(String args[]) { 58 final long startTime = System.currentTimeMillis(); 59 boolean timeoutFileNeedsWriting = false; 60 long timeout = 0; 61 try { 62 timeout = Math.max(readTestsTimeout() * 2, MINIMUM_TEST_TIMEOUT); 63 } catch (IOException ex) { 64 timeoutFileNeedsWriting = true; 65 timeout = TEST_TIMEOUT_IF_NONE_SET; 66 } 67 final long testsTimeout = timeout; 68 final long delayBeforeCheckingIfTestsTakingTooLong = 1000; 69 Runnable timeoutChecker = new Runnable () { 70 public void run() { 71 while (true) { 72 if (System.currentTimeMillis() > startTime + testsTimeout) { 73 System.exit(0); 74 } 75 try { 76 Thread.sleep(delayBeforeCheckingIfTestsTakingTooLong); 77 } catch (InterruptedException ex) { 78 } 79 } 80 } 81 }; 82 new Thread (timeoutChecker).start(); 83 TestRunnerImpl aTestRunner = new TestRunnerImpl(); 84 aTestRunner.start(args); 85 if (timeoutFileNeedsWriting) { 86 writeTestsTimeout(System.currentTimeMillis() - startTime); 87 } 88 System.exit(0); 89 } 90 93 public synchronized void print(TestResult result) { 94 print(result.wasSuccessful() ? PASSED : FAILED); 96 } 97 98 102 protected void start(String args[]) { 103 try { 104 Class testClass = getTestCaseClass(args); 105 Test suite = getTestSuite(testClass); 106 doRun(suite); 107 } catch (Exception e) { 108 println("Could not create and run test suite"); 109 System.exit(-1); 110 } 111 } 112 113 private Test getTestSuite(Class testClass) { 114 try { 115 Method suiteMethod = testClass.getMethod("suite", new Class [0]); 116 try { 117 return (Test) suiteMethod.invoke(null, new Class [0]); 118 } catch (Exception e) { 120 println("Could not invoke the suite() method"); 121 System.exit(-1); 122 return null; } 124 } catch (Exception e) { 125 return new TestSuite(testClass); 127 } 128 } 129 130 private Class getTestCaseClass(String [] args){ 131 String testCaseName = getTestCaseName(args); 132 133 try { 134 return Class.forName(testCaseName); 135 } catch (Exception e) { 136 println("Suite class \"" + testCaseName + "\" not found"); 137 System.exit(-1); 138 return null; } 140 } 141 142 private String getTestCaseName(String [] args) { 143 String testCaseName = ""; 144 145 for (int i = 0; i < args.length; i++) { 146 if (args[i].equals("-v")) 147 println("JUnit 3.2 by Kent Beck and Erich Gamma"); 148 else 149 testCaseName = args[i]; 150 } 151 152 if (testCaseName.equals("")) { 153 println("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"); 154 System.exit(-1); 155 } 156 157 return testCaseName; 158 } 159 160 public synchronized void startTest(Test test) { 161 print("."); 162 } 163 164 private void print(String string) { 165 System.out.print(string); 166 } 167 168 private void println(String string) { 169 System.out.println(string); 170 } 171 172 private void println() { 173 System.out.println(); 174 } 175 176 protected void doRun(Test suite) { 177 TestResult result = createTestResult(); 178 result.addListener(this); 179 long startTime = System.currentTimeMillis(); 180 suite.run(result); 181 long endTime = System.currentTimeMillis(); 182 long runTime = endTime - startTime; 183 println(); 184 println("Time: " + runTime); 185 print(result); 186 187 println(); 188 } 189 190 private static long readTestsTimeout() throws IOException { 191 FileReader timeoutFile = new FileReader(TestTester.TIMEOUT_FILENAME); 192 BufferedReader in = new BufferedReader(timeoutFile); 193 String firstLine = in.readLine(); 194 timeoutFile.close(); 195 return Long.parseLong(firstLine); 196 } 197 198 private static void writeTestsTimeout(long timeToRunTests) { 199 try { 200 FileWriter timeoutFile = new FileWriter(TestTester.TIMEOUT_FILENAME); 201 timeoutFile.write(Long.toString(timeToRunTests)); 202 timeoutFile.close(); 203 } catch (IOException notMuchYouCanDo) { 204 } 205 } 206 } | Popular Tags |