1 package junit.framework; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 import java.lang.reflect.Modifier; 6 7 /** 8 * A test case defines the fixture to run multiple tests. To define a test case<br/> 9 * <ol> 10 * <li>implement a subclass of <code>TestCase</code></li> 11 * <li>define instance variables that store the state of the fixture</li> 12 * <li>initialize the fixture state by overriding {@link #setUp()}</li> 13 * <li>clean-up after a test by overriding {@link #tearDown()}.</li> 14 * </ol> 15 * Each test runs in its own fixture so there 16 * can be no side effects among test runs. 17 * Here is an example: 18 * <pre> 19 * public class MathTest extends TestCase { 20 * protected double fValue1; 21 * protected double fValue2; 22 * 23 * protected void setUp() { 24 * fValue1= 2.0; 25 * fValue2= 3.0; 26 * } 27 * } 28 * </pre> 29 * 30 * For each test implement a method which interacts 31 * with the fixture. Verify the expected results with assertions specified 32 * by calling {@link junit.framework.Assert#assertTrue(String, boolean)} with a boolean. 33 * <pre> 34 * public void testAdd() { 35 * double result= fValue1 + fValue2; 36 * assertTrue(result == 5.0); 37 * } 38 * </pre> 39 * 40 * Once the methods are defined you can run them. The framework supports 41 * both a static type safe and more dynamic way to run a test. 42 * In the static way you override the runTest method and define the method to 43 * be invoked. A convenient way to do so is with an anonymous inner class. 44 * <pre> 45 * TestCase test= new MathTest("add") { 46 * public void runTest() { 47 * testAdd(); 48 * } 49 * }; 50 * test.run(); 51 * </pre> 52 * The dynamic way uses reflection to implement {@link #runTest()}. It dynamically finds 53 * and invokes a method. 54 * In this case the name of the test case has to correspond to the test method 55 * to be run. 56 * <pre> 57 * TestCase test= new MathTest("testAdd"); 58 * test.run(); 59 * </pre> 60 * 61 * The tests to be run can be collected into a TestSuite. JUnit provides 62 * different <i>test runners</i> which can run a test suite and collect the results. 63 * A test runner either expects a static method <code>suite</code> as the entry 64 * point to get a test to run or it will extract the suite automatically. 65 * <pre> 66 * public static Test suite() { 67 * suite.addTest(new MathTest("testAdd")); 68 * suite.addTest(new MathTest("testDivideByZero")); 69 * return suite; 70 * } 71 * </pre> 72 * 73 * @see TestResult 74 * @see TestSuite 75 */ 76 @SuppressWarnings("deprecation") 77 public abstract class TestCase extends Assert implements Test { 78 /** 79 * the name of the test case 80 */ 81 private String fName; 82 83 /** 84 * No-arg constructor to enable serialization. This method 85 * is not intended to be used by mere mortals without calling setName(). 86 */ TestCase()87 public TestCase() { 88 fName = null; 89 } 90 91 /** 92 * Constructs a test case with the given name. 93 */ TestCase(String name)94 public TestCase(String name) { 95 fName = name; 96 } 97 98 /** 99 * Counts the number of test cases executed by run(TestResult result). 100 */ countTestCases()101 public int countTestCases() { 102 return 1; 103 } 104 105 /** 106 * Creates a default TestResult object. 107 * 108 * @see TestResult 109 */ createResult()110 protected TestResult createResult() { 111 return new TestResult(); 112 } 113 114 /** 115 * A convenience method to run this test, collecting the results with a 116 * default TestResult object. 117 * 118 * @see TestResult 119 */ run()120 public TestResult run() { 121 TestResult result = createResult(); 122 run(result); 123 return result; 124 } 125 126 /** 127 * Runs the test case and collects the results in TestResult. 128 */ run(TestResult result)129 public void run(TestResult result) { 130 result.run(this); 131 } 132 133 /** 134 * Runs the bare test sequence. 135 * 136 * @throws Throwable if any exception is thrown 137 */ runBare()138 public void runBare() throws Throwable { 139 Throwable exception = null; 140 setUp(); 141 try { 142 runTest(); 143 } catch (Throwable running) { 144 exception = running; 145 } finally { 146 try { 147 tearDown(); 148 } catch (Throwable tearingDown) { 149 if (exception == null) exception = tearingDown; 150 } 151 } 152 if (exception != null) throw exception; 153 } 154 155 /** 156 * Override to run the test and assert its state. 157 * 158 * @throws Throwable if any exception is thrown 159 */ runTest()160 protected void runTest() throws Throwable { 161 assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null); 162 Method runMethod = null; 163 try { 164 // use getMethod to get all public inherited 165 // methods. getDeclaredMethods returns all 166 // methods of this class but excludes the 167 // inherited ones. 168 runMethod = getClass().getMethod(fName, (Class[]) null); 169 } catch (NoSuchMethodException e) { 170 fail("Method \"" + fName + "\" not found"); 171 } 172 if (!Modifier.isPublic(runMethod.getModifiers())) { 173 fail("Method \"" + fName + "\" should be public"); 174 } 175 176 try { 177 runMethod.invoke(this); 178 } catch (InvocationTargetException e) { 179 e.fillInStackTrace(); 180 throw e.getTargetException(); 181 } catch (IllegalAccessException e) { 182 e.fillInStackTrace(); 183 throw e; 184 } 185 } 186 187 /** 188 * Asserts that a condition is true. If it isn't it throws 189 * an AssertionFailedError with the given message. 190 */ assertTrue(String message, boolean condition)191 public static void assertTrue(String message, boolean condition) { 192 Assert.assertTrue(message, condition); 193 } 194 195 /** 196 * Asserts that a condition is true. If it isn't it throws 197 * an AssertionFailedError. 198 */ assertTrue(boolean condition)199 public static void assertTrue(boolean condition) { 200 Assert.assertTrue(condition); 201 } 202 203 /** 204 * Asserts that a condition is false. If it isn't it throws 205 * an AssertionFailedError with the given message. 206 */ assertFalse(String message, boolean condition)207 public static void assertFalse(String message, boolean condition) { 208 Assert.assertFalse(message, condition); 209 } 210 211 /** 212 * Asserts that a condition is false. If it isn't it throws 213 * an AssertionFailedError. 214 */ assertFalse(boolean condition)215 public static void assertFalse(boolean condition) { 216 Assert.assertFalse(condition); 217 } 218 219 /** 220 * Fails a test with the given message. 221 */ fail(String message)222 public static void fail(String message) { 223 Assert.fail(message); 224 } 225 226 /** 227 * Fails a test with no message. 228 */ fail()229 public static void fail() { 230 Assert.fail(); 231 } 232 233 /** 234 * Asserts that two objects are equal. If they are not 235 * an AssertionFailedError is thrown with the given message. 236 */ assertEquals(String message, Object expected, Object actual)237 public static void assertEquals(String message, Object expected, Object actual) { 238 Assert.assertEquals(message, expected, actual); 239 } 240 241 /** 242 * Asserts that two objects are equal. If they are not 243 * an AssertionFailedError is thrown. 244 */ assertEquals(Object expected, Object actual)245 public static void assertEquals(Object expected, Object actual) { 246 Assert.assertEquals(expected, actual); 247 } 248 249 /** 250 * Asserts that two Strings are equal. 251 */ assertEquals(String message, String expected, String actual)252 public static void assertEquals(String message, String expected, String actual) { 253 Assert.assertEquals(message, expected, actual); 254 } 255 256 /** 257 * Asserts that two Strings are equal. 258 */ assertEquals(String expected, String actual)259 public static void assertEquals(String expected, String actual) { 260 Assert.assertEquals(expected, actual); 261 } 262 263 /** 264 * Asserts that two doubles are equal concerning a delta. If they are not 265 * an AssertionFailedError is thrown with the given message. If the expected 266 * value is infinity then the delta value is ignored. 267 */ assertEquals(String message, double expected, double actual, double delta)268 public static void assertEquals(String message, double expected, double actual, double delta) { 269 Assert.assertEquals(message, expected, actual, delta); 270 } 271 272 /** 273 * Asserts that two doubles are equal concerning a delta. If the expected 274 * value is infinity then the delta value is ignored. 275 */ assertEquals(double expected, double actual, double delta)276 public static void assertEquals(double expected, double actual, double delta) { 277 Assert.assertEquals(expected, actual, delta); 278 } 279 280 /** 281 * Asserts that two floats are equal concerning a positive delta. If they 282 * are not an AssertionFailedError is thrown with the given message. If the 283 * expected value is infinity then the delta value is ignored. 284 */ assertEquals(String message, float expected, float actual, float delta)285 public static void assertEquals(String message, float expected, float actual, float delta) { 286 Assert.assertEquals(message, expected, actual, delta); 287 } 288 289 /** 290 * Asserts that two floats are equal concerning a delta. If the expected 291 * value is infinity then the delta value is ignored. 292 */ assertEquals(float expected, float actual, float delta)293 public static void assertEquals(float expected, float actual, float delta) { 294 Assert.assertEquals(expected, actual, delta); 295 } 296 297 /** 298 * Asserts that two longs are equal. If they are not 299 * an AssertionFailedError is thrown with the given message. 300 */ assertEquals(String message, long expected, long actual)301 public static void assertEquals(String message, long expected, long actual) { 302 Assert.assertEquals(message, expected, actual); 303 } 304 305 /** 306 * Asserts that two longs are equal. 307 */ assertEquals(long expected, long actual)308 public static void assertEquals(long expected, long actual) { 309 Assert.assertEquals(expected, actual); 310 } 311 312 /** 313 * Asserts that two booleans are equal. If they are not 314 * an AssertionFailedError is thrown with the given message. 315 */ assertEquals(String message, boolean expected, boolean actual)316 public static void assertEquals(String message, boolean expected, boolean actual) { 317 Assert.assertEquals(message, expected, actual); 318 } 319 320 /** 321 * Asserts that two booleans are equal. 322 */ assertEquals(boolean expected, boolean actual)323 public static void assertEquals(boolean expected, boolean actual) { 324 Assert.assertEquals(expected, actual); 325 } 326 327 /** 328 * Asserts that two bytes are equal. If they are not 329 * an AssertionFailedError is thrown with the given message. 330 */ assertEquals(String message, byte expected, byte actual)331 public static void assertEquals(String message, byte expected, byte actual) { 332 Assert.assertEquals(message, expected, actual); 333 } 334 335 /** 336 * Asserts that two bytes are equal. 337 */ assertEquals(byte expected, byte actual)338 public static void assertEquals(byte expected, byte actual) { 339 Assert.assertEquals(expected, actual); 340 } 341 342 /** 343 * Asserts that two chars are equal. If they are not 344 * an AssertionFailedError is thrown with the given message. 345 */ assertEquals(String message, char expected, char actual)346 public static void assertEquals(String message, char expected, char actual) { 347 Assert.assertEquals(message, expected, actual); 348 } 349 350 /** 351 * Asserts that two chars are equal. 352 */ assertEquals(char expected, char actual)353 public static void assertEquals(char expected, char actual) { 354 Assert.assertEquals(expected, actual); 355 } 356 357 /** 358 * Asserts that two shorts are equal. If they are not 359 * an AssertionFailedError is thrown with the given message. 360 */ assertEquals(String message, short expected, short actual)361 public static void assertEquals(String message, short expected, short actual) { 362 Assert.assertEquals(message, expected, actual); 363 } 364 365 /** 366 * Asserts that two shorts are equal. 367 */ assertEquals(short expected, short actual)368 public static void assertEquals(short expected, short actual) { 369 Assert.assertEquals(expected, actual); 370 } 371 372 /** 373 * Asserts that two ints are equal. If they are not 374 * an AssertionFailedError is thrown with the given message. 375 */ assertEquals(String message, int expected, int actual)376 public static void assertEquals(String message, int expected, int actual) { 377 Assert.assertEquals(message, expected, actual); 378 } 379 380 /** 381 * Asserts that two ints are equal. 382 */ assertEquals(int expected, int actual)383 public static void assertEquals(int expected, int actual) { 384 Assert.assertEquals(expected, actual); 385 } 386 387 /** 388 * Asserts that an object isn't null. 389 */ assertNotNull(Object object)390 public static void assertNotNull(Object object) { 391 Assert.assertNotNull(object); 392 } 393 394 /** 395 * Asserts that an object isn't null. If it is 396 * an AssertionFailedError is thrown with the given message. 397 */ assertNotNull(String message, Object object)398 public static void assertNotNull(String message, Object object) { 399 Assert.assertNotNull(message, object); 400 } 401 402 /** 403 * Asserts that an object is null. If it isn't an {@link AssertionError} is 404 * thrown. 405 * Message contains: Expected: <null> but was: object 406 * 407 * @param object Object to check or <code>null</code> 408 */ assertNull(Object object)409 public static void assertNull(Object object) { 410 Assert.assertNull(object); 411 } 412 413 /** 414 * Asserts that an object is null. If it is not 415 * an AssertionFailedError is thrown with the given message. 416 */ assertNull(String message, Object object)417 public static void assertNull(String message, Object object) { 418 Assert.assertNull(message, object); 419 } 420 421 /** 422 * Asserts that two objects refer to the same object. If they are not 423 * an AssertionFailedError is thrown with the given message. 424 */ assertSame(String message, Object expected, Object actual)425 public static void assertSame(String message, Object expected, Object actual) { 426 Assert.assertSame(message, expected, actual); 427 } 428 429 /** 430 * Asserts that two objects refer to the same object. If they are not 431 * the same an AssertionFailedError is thrown. 432 */ assertSame(Object expected, Object actual)433 public static void assertSame(Object expected, Object actual) { 434 Assert.assertSame(expected, actual); 435 } 436 437 /** 438 * Asserts that two objects do not refer to the same object. If they do 439 * refer to the same object an AssertionFailedError is thrown with the 440 * given message. 441 */ assertNotSame(String message, Object expected, Object actual)442 public static void assertNotSame(String message, Object expected, Object actual) { 443 Assert.assertNotSame(message, expected, actual); 444 } 445 446 /** 447 * Asserts that two objects do not refer to the same object. If they do 448 * refer to the same object an AssertionFailedError is thrown. 449 */ assertNotSame(Object expected, Object actual)450 public static void assertNotSame(Object expected, Object actual) { 451 Assert.assertNotSame(expected, actual); 452 } 453 failSame(String message)454 public static void failSame(String message) { 455 Assert.failSame(message); 456 } 457 failNotSame(String message, Object expected, Object actual)458 public static void failNotSame(String message, Object expected, Object actual) { 459 Assert.failNotSame(message, expected, actual); 460 } 461 failNotEquals(String message, Object expected, Object actual)462 public static void failNotEquals(String message, Object expected, Object actual) { 463 Assert.failNotEquals(message, expected, actual); 464 } 465 format(String message, Object expected, Object actual)466 public static String format(String message, Object expected, Object actual) { 467 return Assert.format(message, expected, actual); 468 } 469 470 /** 471 * Sets up the fixture, for example, open a network connection. 472 * This method is called before a test is executed. 473 */ setUp()474 protected void setUp() throws Exception { 475 } 476 477 /** 478 * Tears down the fixture, for example, close a network connection. 479 * This method is called after a test is executed. 480 */ tearDown()481 protected void tearDown() throws Exception { 482 } 483 484 /** 485 * Returns a string representation of the test case. 486 */ 487 @Override toString()488 public String toString() { 489 return getName() + "(" + getClass().getName() + ")"; 490 } 491 492 /** 493 * Gets the name of a TestCase. 494 * 495 * @return the name of the TestCase 496 */ getName()497 public String getName() { 498 return fName; 499 } 500 501 /** 502 * Sets the name of a TestCase. 503 * 504 * @param name the name to set 505 */ setName(String name)506 public void setName(String name) { 507 fName = name; 508 } 509 } 510