1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.coretests; 18 19 import junit.framework.Test; 20 import junit.framework.TestResult; 21 import junit.framework.TestSuite; 22 import junit.runner.BaseTestRunner; 23 import junit.runner.StandardTestSuiteLoader; 24 import junit.runner.TestSuiteLoader; 25 26 import java.io.PrintStream; 27 28 /** 29 * A command line based tool to run tests. 30 * <pre> 31 * java junit.textui.TestRunner [-wait] TestCaseClass 32 * </pre> 33 * TestRunner expects the name of a TestCase class as argument. 34 * If this class defines a static <code>suite</code> method it 35 * will be invoked and the returned test is run. Otherwise all 36 * the methods starting with "test" having no arguments are run. 37 * <p> 38 * When the wait command line argument is given TestRunner 39 * waits until the users types RETURN. 40 * <p> 41 * TestRunner prints a trace as the tests are executed followed by a 42 * summary at the end. 43 */ 44 public class StatTestRunner extends BaseTestRunner { 45 private ResultPrinter fPrinter; 46 private PerfStatCollector fPerfStatCollector; 47 48 public static final int SUCCESS_EXIT= 0; 49 public static final int FAILURE_EXIT= 1; 50 public static final int EXCEPTION_EXIT= 2; 51 52 public static final String DEFAULT_DATABASE = "sqlite:/coretests.db"; 53 public static final String DEFAULT_DRIVER = "SQLite.JDBCDriver"; 54 55 public static String connectionURL; 56 public static String jdbcDriver; 57 58 /** 59 * Constructs a TestRunner. 60 */ 61 public StatTestRunner() { 62 this(System.out); 63 } 64 65 /** 66 * Constructs a TestRunner using the given stream for all the output 67 */ 68 public StatTestRunner(PrintStream writer) { 69 this(new ResultPrinter(writer)); 70 } 71 72 /** 73 * Constructs a TestRunner using the given ResultPrinter all the output 74 */ 75 public StatTestRunner(ResultPrinter printer) { 76 fPrinter= printer; 77 fPerfStatCollector = new PerfStatCollector(printer.getWriter()); 78 } 79 80 /** 81 * Runs a suite extracted from a TestCase subclass. 82 */ 83 static public void run(Class testClass) { 84 run(new TestSuite(testClass)); 85 } 86 87 /** 88 * Runs a single test and collects its results. 89 * This method can be used to start a test run 90 * from your program. 91 * <pre> 92 * public static void main (String[] args) { 93 * test.textui.TestRunner.run(suite()); 94 * } 95 * </pre> 96 */ 97 static public TestResult run(Test test) { 98 StatTestRunner runner= new StatTestRunner(); 99 try { 100 return runner.doRun(test, false); 101 } 102 catch (Exception e) { 103 return null; 104 } 105 } 106 107 /** 108 * Runs a single test and waits until the user 109 * types RETURN. 110 */ 111 static public void runAndWait(Test suite) { 112 StatTestRunner aTestRunner= new StatTestRunner(); 113 try { 114 aTestRunner.doRun(suite, true); 115 } 116 catch (Exception e) {} 117 } 118 119 /** 120 * Always use the StandardTestSuiteLoader. Overridden from 121 * BaseTestRunner. 122 */ 123 public TestSuiteLoader getLoader() { 124 return new StandardTestSuiteLoader(); 125 } 126 127 public void testFailed(int status, Test test, Throwable t) { 128 } 129 130 public void testStarted(String testName) { 131 } 132 133 public void testEnded(String testName) { 134 } 135 136 public TestResult doRun(Test suite, boolean wait) throws Exception { 137 StatsStore.open(jdbcDriver, connectionURL); 138 TestResult result = new TestResult(); 139 result.addListener(fPrinter); 140 result.addListener(fPerfStatCollector); 141 long startTime= System.currentTimeMillis(); 142 StatsStore.now = startTime; 143 suite.run(result); 144 long endTime= System.currentTimeMillis(); 145 long runTime= endTime-startTime; 146 fPrinter.print(result, runTime); 147 fPerfStatCollector.digest(); 148 StatsStore.close(); 149 150 pause(wait); 151 return result; 152 } 153 154 protected void pause(boolean wait) { 155 if (!wait) return; 156 fPrinter.printWaitPrompt(); 157 try { 158 System.in.read(); 159 } 160 catch(Exception e) { 161 } 162 } 163 164 public static void main(String args[]) { 165 StatTestRunner aTestRunner= new StatTestRunner(); 166 try { 167 TestResult r= aTestRunner.start(args); 168 if (!r.wasSuccessful()) 169 System.exit(FAILURE_EXIT); 170 System.exit(SUCCESS_EXIT); 171 } catch(Exception e) { 172 System.err.println(e.getMessage()); 173 System.exit(EXCEPTION_EXIT); 174 } 175 } 176 177 /** 178 * Starts a test run. Analyzes the command line arguments 179 * and runs the given test suite. 180 */ 181 protected TestResult start(String args[]) throws Exception { 182 String testCase= ""; 183 boolean wait= false; 184 185 jdbcDriver = System.getProperty("android.coretests.driver", DEFAULT_DRIVER); 186 connectionURL = System.getProperty("android.coretests.database", "jdbc:" + DEFAULT_DATABASE); 187 188 for (int i= 0; i < args.length; i++) { 189 if (args[i].equals("--all")) 190 fPerfStatCollector.listAll = true; 191 else if (args[i].equals("--bad")) 192 fPerfStatCollector.listBad = true; 193 else if (args[i].equals("--nobig")) 194 fPerfStatCollector.bigMarking = false; 195 else if (args[i].equals("--s")) { 196 fPerfStatCollector.thresholdDuration = 197 Integer.valueOf(args[++i]); 198 } else if (args[i].equals("-wait")) 199 wait= true; 200 else if (args[i].equals("-c")) 201 testCase= extractClassName(args[++i]); 202 else if (args[i].equals("-v")) 203 System.err.println("JUnit "+Version.id()+" (plus Android performance stats)"); 204 else 205 testCase= args[i]; 206 } 207 208 if (testCase.equals("")) 209 throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"); 210 211 try { 212 Test suite= getTest(testCase); 213 return doRun(suite, wait); 214 } 215 catch (Exception e) { 216 throw new Exception("Exception: " + e); 217 } 218 } 219 220 protected void runFailed(String message) { 221 System.err.println(message); 222 System.exit(FAILURE_EXIT); 223 } 224 225 public void setPrinter(ResultPrinter printer) { 226 fPrinter= printer; 227 } 228 229 230 } 231