git clone of logicmail with some fixes/features added
at master 308 lines 7.3 kB view raw
1//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2// This file is part of J2MEUnit, a Java 2 Micro Edition unit testing framework. 3// 4// J2MEUnit is free software distributed under the Common Public License (CPL). 5// It may be redistributed and/or modified under the terms of the CPL. You 6// should have received a copy of the license along with J2MEUnit. It is also 7// available from the website of the Open Source Initiative at 8// http://www.opensource.org. 9// 10// J2MEUnit is distributed in the hope that it will be useful, but WITHOUT ANY 11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12// FOR A PARTICULAR PURPOSE. 13//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14package j2meunit.framework; 15 16import java.util.*; 17 18 19/******************************************************************** 20 * A <code>TestResult</code> collects the results of executing a test case. It 21 * is an instance of the Collecting Parameter pattern. The test framework 22 * distinguishes between <i>failures</i> and <i>errors</i>. A failure is 23 * anticipated and checked for with assertions. Errors are unanticipated 24 * problems like an <code>ArrayIndexOutOfBoundsException</code>. 25 * 26 * @see Test 27 */ 28public class TestResult 29{ 30 //~ Instance fields -------------------------------------------------------- 31 32 protected Vector fErrors; 33 protected Vector fFailures; 34 protected Vector fListeners; 35 protected int fAssertions; 36 protected int fRunTests; 37 private boolean fStop; 38 39 //~ Constructors ----------------------------------------------------------- 40 41 /*************************************** 42 * Creates a new TestResult object. 43 */ 44 public TestResult() 45 { 46 fFailures = new Vector(); 47 fErrors = new Vector(); 48 fListeners = new Vector(); 49 fRunTests = 0; 50 fStop = false; 51 } 52 53 //~ Methods ---------------------------------------------------------------- 54 55 /*************************************** 56 * Adds an error to the list of errors. The passed in exception caused the 57 * error. 58 * 59 * @param test - 60 * @param t - 61 */ 62 public void addError(Test test, Throwable t) 63 { 64 fErrors.addElement(new TestFailure(test, t)); 65 66 for (int i = 0, cnt = fListeners.size(); i < cnt; i++) 67 ((TestListener) fListeners.elementAt(i)).addError(test, t); 68 } 69 70 /*************************************** 71 * Adds a failure to the list of failures. The passed in exception caused 72 * the failure. 73 * 74 * @param test - 75 * @param t - 76 */ 77 public synchronized void addFailure(Test test, AssertionFailedError t) 78 { 79 fFailures.addElement(new TestFailure(test, t)); 80 81 for (int i = 0, cnt = fListeners.size(); i < cnt; i++) 82 ((TestListener) fListeners.elementAt(i)).addFailure(test, t); 83 } 84 85 /*************************************** 86 * Registers a TestListener 87 * 88 * @param listener The listener 89 */ 90 public synchronized void addListener(TestListener listener) 91 { 92 fListeners.addElement(listener); 93 } 94 95 /*************************************** 96 * Gets the number of detected failures. 97 * 98 * @return - 99 */ 100 public synchronized int assertionCount() 101 { 102 return fAssertions; 103 } 104 105 /*************************************** 106 * Adds a failure to the list of failures. The passed in exception caused 107 * the failure. 108 */ 109 public synchronized void assertionMade() 110 { 111 fAssertions++; 112 } 113 114 /*************************************** 115 * Informs the result that a test was completed. 116 * 117 * @param test - 118 */ 119 public void endTest(Test test) 120 { 121 for (int i = 0, cnt = fListeners.size(); i < cnt; i++) 122 ((TestListener) fListeners.elementAt(i)).endTest(test); 123 } 124 125 /*************************************** 126 * Informs the result that a test was completed. 127 * 128 * @param test - 129 */ 130 public void endTestStep(Test test) 131 { 132 for (int i = 0, cnt = fListeners.size(); i < cnt; i++) 133 ((TestListener) fListeners.elementAt(i)).endTestStep(test); 134 } 135 136 /*************************************** 137 * Gets the number of detected errors. 138 * 139 * @return - 140 */ 141 public synchronized int errorCount() 142 { 143 return fErrors.size(); 144 } 145 146 /*************************************** 147 * Returns an Enumeration for the errors 148 * 149 * @return - 150 */ 151 public synchronized Enumeration errors() 152 { 153 return fErrors.elements(); 154 } 155 156 /*************************************** 157 * Gets the number of detected failures. 158 * 159 * @return - 160 */ 161 public synchronized int failureCount() 162 { 163 return fFailures.size(); 164 } 165 166 /*************************************** 167 * Returns an Enumeration for the failures 168 * 169 * @return - 170 */ 171 public synchronized Enumeration failures() 172 { 173 return fFailures.elements(); 174 } 175 176 /*************************************** 177 * Gets the number of run tests. 178 * 179 * @return - 180 */ 181 public synchronized int runCount() 182 { 183 return fRunTests; 184 } 185 186 /*************************************** 187 * Runs a TestCase. 188 * 189 * @param test - 190 * @param p - 191 */ 192 public void runProtected(final Test test, Protectable p) 193 { 194 try 195 { 196 p.protect(); 197 } 198 catch (AssertionFailedError e) 199 { 200 addFailure(test, e); 201 } 202 catch (Throwable e) 203 { 204 addError(test, e); 205 } 206 } 207 208 /*************************************** 209 * Gets the number of run tests. 210 * 211 * @return - 212 * 213 * @deprecated use <code>runCount</code> instead 214 */ 215 public synchronized int runTests() 216 { 217 return runCount(); 218 } 219 220 /*************************************** 221 * Checks whether the test run should stop 222 * 223 * @return - 224 */ 225 public synchronized boolean shouldStop() 226 { 227 return fStop; 228 } 229 230 /*************************************** 231 * Informs the result that a test will be started. 232 * 233 * @param test - 234 */ 235 public void startTest(Test test) 236 { 237 synchronized (this) 238 { 239 fRunTests++; 240 } 241 242 for (int i = 0, cnt = fListeners.size(); i < cnt; i++) 243 ((TestListener) fListeners.elementAt(i)).startTest(test); 244 } 245 246 /*************************************** 247 * Marks that the test run should stop. 248 */ 249 public synchronized void stop() 250 { 251 fStop = true; 252 } 253 254 /*************************************** 255 * Gets the number of detected errors. 256 * 257 * @return - 258 * 259 * @deprecated use <code>errorCount</code> instead 260 */ 261 public synchronized int testErrors() 262 { 263 return errorCount(); 264 } 265 266 /*************************************** 267 * Gets the number of detected failures. 268 * 269 * @return - 270 * 271 * @deprecated use <code>failureCount</code> instead 272 */ 273 public synchronized int testFailures() 274 { 275 return failureCount(); 276 } 277 278 /*************************************** 279 * Returns whether the entire test was successful or not. 280 * 281 * @return - 282 */ 283 public synchronized boolean wasSuccessful() 284 { 285 return (testFailures() == 0) && (testErrors() == 0); 286 } 287 288 /*************************************** 289 * Runs a TestCase. 290 * 291 * @param test - 292 */ 293 protected void run(final TestCase test) 294 { 295 startTest(test); 296 297 Protectable p = new Protectable() 298 { 299 public void protect() throws Throwable 300 { 301 test.runBare(); 302 } 303 }; 304 305 runProtected(test, p); 306 endTest(test); 307 } 308}