git clone of logicmail with some fixes/features added
at master 403 lines 11 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 16/******************************************************************** 17 * A test case defines the fixture to run multiple tests. To define a test case 18 * 19 * <ol> 20 * <li>implement a subclass of TestCase</li> 21 * <li>define instance variables that store the state of the fixture</li> 22 * <li>initialize the fixture state by overriding <code>setUp</code></li> 23 * <li>clean-up after a test by overriding <code>tearDown</code>.</li> 24 * </ol> 25 * 26 * <p>Each test runs in its own fixture so there can be no side effects among 27 * test runs. Here is an example:</p> 28 * 29 * <pre> 30 public class MathTest extends TestCase 31 { 32 protected int nValue1; 33 protected int nValue2; 34 35 protected void setUp() 36 { 37 nValue1= 2; 38 nValue2= 3; 39 } 40 } 41 * </pre> 42 * 43 * For each test implement a method which interacts with the fixture. Verify the 44 * expected results with assertions specified by calling <code>assertTrue</code> 45 * (or another assert method) with a boolean: 46 * 47 * <pre> 48 public void testAdd() 49 { 50 double result= nValue1 + nValue2; 51 assertTrue(result == 5); 52 } 53 * </pre> 54 * 55 * Once the methods are defined you can run them. The framework supports both a 56 * static type safe and more dynamic way to run a test. In the static way you 57 * override the runTest method and define the method to be invoked. A convenient 58 * way to do so is with an anonymous inner class: 59 * 60 * <pre> 61 TestCase test= new MathTest("add") 62 { 63 public void runTest() 64 { 65 testAdd(); 66 } 67 }; 68 test.run(); 69 * </pre> 70 * 71 * In JUnit, the dynamic way uses reflection in the default implementation of 72 * <code>runTest</code>. Because reflection is not available in J2ME, here a 73 * different approach is necessary. Together with a method name, an instance of 74 * the class TestMethod can be given to the constructor of TestCase. It can be 75 * created as an anonymous inner class that invokes the actual test method in 76 * the implementation of the 77 * 78 * <pre>run</pre> 79 * 80 * method: 81 * 82 * <pre> 83 TestCase test = new MathTest("testAdd", new TestMethod() 84 { public void run(TestCase tc) { ((MathTest) tc).testAdd(); } }); 85 test.run(); 86 * </pre> 87 * 88 * To make this work subclasses need to implement the additional constructor and 89 * forward the parameters to the corresponding TestCase constructor. Multiple 90 * tests to be run can be grouped into a TestSuite. J2MEUnit provides different 91 * <i>test runners</i> which can run a test suite and collect the results. 92 * Because J2ME doesn't provide reflection, a test suite must always be created 93 * manually from test instances associated with TestMethods. The missing 94 * reflection is also the reason that the 95 * 96 * <pre>suite</pre> 97 * 98 * method cannot be static in J2MEUnit: 99 * 100 * <pre> 101 public Test suite() 102 { 103 TestSuite suite = new TestSuite(); 104 { 105 suite.addTest(new MathTest("testAdd", new TestMethod() 106 { public void run(TestCase tc) {((MathTest) tc).testAdd(); } })); 107 suite.addTest(new MathTest("testDivideByZero", new TestMethod() 108 { public void run(TestCase tc) {((MathTest) tc).testDivideByZero(); } })); 109 110 return suite; 111 } 112 * </pre> 113 * 114 * @see TestResult 115 * @see TestSuite 116 * @see TestMethod 117 */ 118public abstract class TestCase extends Assert implements Test 119{ 120 //~ Instance fields -------------------------------------------------------- 121 122 private String sName = null; 123 private TestMethod rTestMethod = null; 124 private TestResult rResult = null; 125 126 //~ Constructors ----------------------------------------------------------- 127 128 /*************************************** 129 * Default constructor. 130 */ 131 public TestCase() 132 { 133 } 134 135 /*************************************** 136 * Constructor for a named Test. 137 * 138 * @param name The name of the test (method) 139 */ 140 public TestCase(String name) 141 { 142 sName = name; 143 } 144 145 /*************************************** 146 * Constructor for a test that will execute a particular test method. 147 * 148 * @param name The name of the test method 149 * @param testMethod The TestMethod wrapper for the method to execute 150 */ 151 public TestCase(String name, TestMethod testMethod) 152 { 153 setTestMethod(name, testMethod); 154 } 155 156 //~ Methods ---------------------------------------------------------------- 157 158 /*************************************** 159 * Always return 1. 160 * 161 * @return 1 162 */ 163 public int countTestCases() 164 { 165 return 1; 166 } 167 168 /*************************************** 169 * Returns the same as countTestCases. 170 * 171 * @see j2meunit.framework.Test#countTestSteps() 172 */ 173 public int countTestSteps() 174 { 175 return countTestCases(); 176 } 177 178 /*************************************** 179 * Returns the test name. 180 * 181 * @return The test name 182 * 183 * @since 1.1 184 */ 185 public String getName() 186 { 187 return sName; 188 } 189 190 /*************************************** 191 * Returns the Method to be executed by the test case instance. 192 * 193 * @return A TestMethod instance 194 */ 195 public TestMethod getTestMethod() 196 { 197 return rTestMethod; 198 } 199 200 /*************************************** 201 * Deprecated, replaced by getName(). 202 * 203 * @return The test instance name 204 * 205 * @deprecated Replaced by getName() 206 */ 207 public String getTestMethodName() 208 { 209 return getName(); 210 } 211 212 /*************************************** 213 * A convenience method to run this test, collecting the results with a 214 * default TestResult object. 215 * 216 * @see TestResult 217 */ 218 public TestResult run() 219 { 220 TestResult result = createResult(); 221 222 run(result); 223 224 return result; 225 } 226 227 /*************************************** 228 * Runs the test case and collects the results in TestResult. 229 * 230 * @param result The TestResult to collect the data in 231 */ 232 public void run(TestResult result) 233 { 234 rResult = result; 235 result.run(this); 236 } 237 238 /*************************************** 239 * Runs the bare test sequence. 240 * 241 * @exception Throwable if any exception is thrown 242 */ 243 public void runBare() throws Throwable 244 { 245 setUp(); 246 247 try 248 { 249 runTest(); 250 } 251 finally 252 { 253 tearDown(); 254 } 255 } 256 257 /*************************************** 258 * To set the test name. 259 * 260 * @param name The new test name 261 * 262 * @since 1.1 263 */ 264 public void setName(String name) 265 { 266 sName = name; 267 } 268 269 /*************************************** 270 * To set the method to be executed by the test case instance. 271 * 272 * @param testMethod The TestMethod to execute 273 */ 274 public void setTestMethod(TestMethod testMethod) 275 { 276 this.rTestMethod = testMethod; 277 } 278 279 /*************************************** 280 * Convenience method to set the name and wrapper of the method to be 281 * executed by the test case instance. 282 * 283 * @param methodName The name of the test methoto execute 284 * @param testMethod The method wrapper 285 */ 286 public void setTestMethod(String methodName, TestMethod testMethod) 287 { 288 setName(methodName); 289 setTestMethod(testMethod); 290 } 291 292 /*************************************** 293 * Deprecated, replaced by setName(String). 294 * 295 * @param name The test instance name 296 * 297 * @deprecated Replaced by setName(String) 298 */ 299 public void setTestMethodName(String name) 300 { 301 setName(name); 302 } 303 304 /*************************************** 305 * This method should be overridden if a test case contains multiple test 306 * methods. It must return a TestSuite containing the test methods to be 307 * executed for this test case. This TestSuite can be constructed as below: 308 * 309 * <pre> 310 new TestSuite(new MyTestCase("testMethodOne", new TestMethod() 311 { public void run(TestCase tc) 312 { ((MyTestCase) tc).testMethodOne(); } 313 })); 314 * </pre> 315 * 316 * @return A new test suite 317 */ 318 public Test suite() 319 { 320 return null; 321 } 322 323 /*************************************** 324 * Returns a string representation of the test case. 325 * 326 * @return The name of the test case or, if not set, the class name 327 */ 328 public String toString() 329 { 330 if (sName != null) 331 { 332 return sName + "(" + getClass().getName() + ")"; 333 } 334 else 335 { 336 return getClass().getName(); 337 } 338 } 339 340 /*************************************** 341 * Creates a default TestResult object 342 * 343 * @see TestResult 344 */ 345 protected TestResult createResult() 346 { 347 return new TestResult(); 348 } 349 350 /*************************************** 351 * Callback from the Assert base class that will be invoked on assertions. 352 * 353 * @see j2meunit.framework.Assert#onAssertion() 354 */ 355 protected void onAssertion() 356 { 357 rResult.assertionMade(); 358 } 359 360 /*************************************** 361 * The default implementation will run the TestMethod associated with the 362 * TestCase instance and asserts that it is not null. To use the version 1.0 363 * test case style override this method (but don't call super!) and invoke 364 * the method corresponding to the name of the TestCase instance. Since 365 * there is no reflection this must be done by a string compare and an 366 * explicit call of the necessary method. 367 * 368 * @exception Throwable if any exception is thrown 369 */ 370 protected void runTest() throws Throwable 371 { 372 assertNotNull(rTestMethod); 373 rTestMethod.run(this); 374 } 375 376 /*************************************** 377 * Sets up the fixture, for example, open a network connection. This method 378 * is called before a test is executed. 379 * 380 * @throws Exception An arbitrary exception may be thrown 381 */ 382 protected void setUp() throws Exception 383 { 384 } 385 386 /*************************************** 387 * Tears down the fixture, for example, close a network connection. This 388 * method is called after a test is executed. 389 * 390 * @throws Exception An arbitrary exception may be thrown 391 */ 392 protected void tearDown() throws Exception 393 { 394 } 395 396 /*************************************** 397 * Notifies listeners that a test step has finished. 398 */ 399 protected void testStepFinished() 400 { 401 rResult.endTestStep(this); 402 } 403}