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 public abstract class TestCase extends Assert implements Test {
77 /**
78 * the name of the test case
79 */
80 private String fName;
81
82 /**
83 * No-arg constructor to enable serialization. This method
84 * is not intended to be used by mere mortals without calling setName().
85 */
86 public TestCase() {
87 fName = null;
88 }
89
90 /**
91 * Constructs a test case with the given name.
92 */
93 public TestCase(String name) {
94 fName = name;
95 }
96
97 /**
98 * Counts the number of test cases executed by run(TestResult result).
99 */
100 public int countTestCases() {
101 return 1;
102 }
103
104 /**
105 * Creates a default TestResult object
106 *
107 * @see TestResult
108 */
109 protected TestResult createResult() {
110 return new TestResult();
111 }
112
113 /**
114 * A convenience method to run this test, collecting the results with a
115 * default TestResult object.
116 *
117 * @see TestResult
118 */
119 public TestResult run() {
120 TestResult result = createResult();
121 run(result);
122 return result;
123 }
124
125 /**
126 * Runs the test case and collects the results in TestResult.
127 */
128 public void run(TestResult result) {
129 result.run(this);
130 }
131
132 /**
133 * Runs the bare test sequence.
134 *
135 * @throws Throwable if any exception is thrown
136 */
137 public void runBare() throws Throwable {
138 Throwable exception = null;
139 setUp();
140 try {
141 runTest();
142 } catch (Throwable running) {
143 exception = running;
144 } finally {
145 try {
146 tearDown();
147 } catch (Throwable tearingDown) {
148 if (exception == null) exception = tearingDown;
149 }
150 }
151 if (exception != null) throw exception;
152 }
153
154 /**
155 * Override to run the test and assert its state.
156 *
157 * @throws Throwable if any exception is thrown
158 */
159 protected void runTest() throws Throwable {
160 assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null);
161 Method runMethod = null;
162 try {
163 // use getMethod to get all public inherited
164 // methods. getDeclaredMethods returns all
165 // methods of this class but excludes the
166 // inherited ones.
167 runMethod = getClass().getMethod(fName, (Class[]) null);
168 } catch (NoSuchMethodException e) {
169 fail("Method \"" + fName + "\" not found");
170 }
171 if (!Modifier.isPublic(runMethod.getModifiers())) {
172 fail("Method \"" + fName + "\" should be public");
173 }
174
175 try {
176 runMethod.invoke(this);
177 } catch (InvocationTargetException e) {
178 e.fillInStackTrace();
179 throw e.getTargetException();
180 } catch (IllegalAccessException e) {
181 e.fillInStackTrace();
182 throw e;
183 }
184 }
185
186 /**
187 * Asserts that a condition is true. If it isn't it throws
188 * an AssertionFailedError with the given message.
189 */
190 @SuppressWarnings("deprecation")
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 */
199 @SuppressWarnings("deprecation")
200 public static void assertTrue(boolean condition) {
201 Assert.assertTrue(condition);
202 }
203
204 /**
205 * Asserts that a condition is false. If it isn't it throws
206 * an AssertionFailedError with the given message.
207 */
208 @SuppressWarnings("deprecation")
209 public static void assertFalse(String message, boolean condition) {
210 Assert.assertFalse(message, condition);
211 }
212
213 /**
214 * Asserts that a condition is false. If it isn't it throws
215 * an AssertionFailedError.
216 */
217 @SuppressWarnings("deprecation")
218 public static void assertFalse(boolean condition) {
219 Assert.assertFalse(condition);
220 }
221
222 /**
223 * Fails a test with the given message.
224 */
225 @SuppressWarnings("deprecation")
226 public static void fail(String message) {
227 Assert.fail(message);
228 }
229
230 /**
231 * Fails a test with no message.
232 */
233 @SuppressWarnings("deprecation")
234 public static void fail() {
235 Assert.fail();
236 }
237
238 /**
239 * Asserts that two objects are equal. If they are not
240 * an AssertionFailedError is thrown with the given message.
241 */
242 @SuppressWarnings("deprecation")
243 public static void assertEquals(String message, Object expected, Object actual) {
244 Assert.assertEquals(message, expected, actual);
245 }
246
247 /**
248 * Asserts that two objects are equal. If they are not
249 * an AssertionFailedError is thrown.
250 */
251 @SuppressWarnings("deprecation")
252 public static void assertEquals(Object expected, Object actual) {
253 Assert.assertEquals(expected, actual);
254 }
255
256 /**
257 * Asserts that two Strings are equal.
258 */
259 @SuppressWarnings("deprecation")
260 public static void assertEquals(String message, String expected, String actual) {
261 Assert.assertEquals(message, expected, actual);
262 }
263
264 /**
265 * Asserts that two Strings are equal.
266 */
267 @SuppressWarnings("deprecation")
268 public static void assertEquals(String expected, String actual) {
269 Assert.assertEquals(expected, actual);
270 }
271
272 /**
273 * Asserts that two doubles are equal concerning a delta. If they are not
274 * an AssertionFailedError is thrown with the given message. If the expected
275 * value is infinity then the delta value is ignored.
276 */
277 @SuppressWarnings("deprecation")
278 public static void assertEquals(String message, double expected, double actual, double delta) {
279 Assert.assertEquals(message, expected, actual, delta);
280 }
281
282 /**
283 * Asserts that two doubles are equal concerning a delta. If the expected
284 * value is infinity then the delta value is ignored.
285 */
286 @SuppressWarnings("deprecation")
287 public static void assertEquals(double expected, double actual, double delta) {
288 Assert.assertEquals(expected, actual, delta);
289 }
290
291 /**
292 * Asserts that two floats are equal concerning a positive delta. If they
293 * are not an AssertionFailedError is thrown with the given message. If the
294 * expected value is infinity then the delta value is ignored.
295 */
296 @SuppressWarnings("deprecation")
297 public static void assertEquals(String message, float expected, float actual, float delta) {
298 Assert.assertEquals(message, expected, actual, delta);
299 }
300
301 /**
302 * Asserts that two floats are equal concerning a delta. If the expected
303 * value is infinity then the delta value is ignored.
304 */
305 @SuppressWarnings("deprecation")
306 public static void assertEquals(float expected, float actual, float delta) {
307 Assert.assertEquals(expected, actual, delta);
308 }
309
310 /**
311 * Asserts that two longs are equal. If they are not
312 * an AssertionFailedError is thrown with the given message.
313 */
314 @SuppressWarnings("deprecation")
315 public static void assertEquals(String message, long expected, long actual) {
316 Assert.assertEquals(message, expected, actual);
317 }
318
319 /**
320 * Asserts that two longs are equal.
321 */
322 @SuppressWarnings("deprecation")
323 public static void assertEquals(long expected, long actual) {
324 Assert.assertEquals(expected, actual);
325 }
326
327 /**
328 * Asserts that two booleans are equal. If they are not
329 * an AssertionFailedError is thrown with the given message.
330 */
331 @SuppressWarnings("deprecation")
332 public static void assertEquals(String message, boolean expected, boolean actual) {
333 Assert.assertEquals(message, expected, actual);
334 }
335
336 /**
337 * Asserts that two booleans are equal.
338 */
339 @SuppressWarnings("deprecation")
340 public static void assertEquals(boolean expected, boolean actual) {
341 Assert.assertEquals(expected, actual);
342 }
343
344 /**
345 * Asserts that two bytes are equal. If they are not
346 * an AssertionFailedError is thrown with the given message.
347 */
348 @SuppressWarnings("deprecation")
349 public static void assertEquals(String message, byte expected, byte actual) {
350 Assert.assertEquals(message, expected, actual);
351 }
352
353 /**
354 * Asserts that two bytes are equal.
355 */
356 @SuppressWarnings("deprecation")
357 public static void assertEquals(byte expected, byte actual) {
358 Assert.assertEquals(expected, actual);
359 }
360
361 /**
362 * Asserts that two chars are equal. If they are not
363 * an AssertionFailedError is thrown with the given message.
364 */
365 @SuppressWarnings("deprecation")
366 public static void assertEquals(String message, char expected, char actual) {
367 Assert.assertEquals(message, expected, actual);
368 }
369
370 /**
371 * Asserts that two chars are equal.
372 */
373 @SuppressWarnings("deprecation")
374 public static void assertEquals(char expected, char actual) {
375 Assert.assertEquals(expected, actual);
376 }
377
378 /**
379 * Asserts that two shorts are equal. If they are not
380 * an AssertionFailedError is thrown with the given message.
381 */
382 @SuppressWarnings("deprecation")
383 public static void assertEquals(String message, short expected, short actual) {
384 Assert.assertEquals(message, expected, actual);
385 }
386
387 /**
388 * Asserts that two shorts are equal.
389 */
390 @SuppressWarnings("deprecation")
391 public static void assertEquals(short expected, short actual) {
392 Assert.assertEquals(expected, actual);
393 }
394
395 /**
396 * Asserts that two ints are equal. If they are not
397 * an AssertionFailedError is thrown with the given message.
398 */
399 @SuppressWarnings("deprecation")
400 public static void assertEquals(String message, int expected, int actual) {
401 Assert.assertEquals(message, expected, actual);
402 }
403
404 /**
405 * Asserts that two ints are equal.
406 */
407 @SuppressWarnings("deprecation")
408 public static void assertEquals(int expected, int actual) {
409 Assert.assertEquals(expected, actual);
410 }
411
412 /**
413 * Asserts that an object isn't null.
414 */
415 @SuppressWarnings("deprecation")
416 public static void assertNotNull(Object object) {
417 Assert.assertNotNull(object);
418 }
419
420 /**
421 * Asserts that an object isn't null. If it is
422 * an AssertionFailedError is thrown with the given message.
423 */
424 @SuppressWarnings("deprecation")
425 public static void assertNotNull(String message, Object object) {
426 Assert.assertNotNull(message, object);
427 }
428
429 /**
430 * Asserts that an object is null. If it isn't an {@link AssertionError} is
431 * thrown.
432 * Message contains: Expected: <null> but was: object
433 *
434 * @param object Object to check or <code>null</code>
435 */
436 @SuppressWarnings("deprecation")
437 public static void assertNull(Object object) {
438 Assert.assertNull(object);
439 }
440
441 /**
442 * Asserts that an object is null. If it is not
443 * an AssertionFailedError is thrown with the given message.
444 */
445 @SuppressWarnings("deprecation")
446 public static void assertNull(String message, Object object) {
447 Assert.assertNull(message, object);
448 }
449
450 /**
451 * Asserts that two objects refer to the same object. If they are not
452 * an AssertionFailedError is thrown with the given message.
453 */
454 @SuppressWarnings("deprecation")
455 public static void assertSame(String message, Object expected, Object actual) {
456 Assert.assertSame(message, expected, actual);
457 }
458
459 /**
460 * Asserts that two objects refer to the same object. If they are not
461 * the same an AssertionFailedError is thrown.
462 */
463 @SuppressWarnings("deprecation")
464 public static void assertSame(Object expected, Object actual) {
465 Assert.assertSame(expected, actual);
466 }
467
468 /**
469 * Asserts that two objects do not refer to the same object. If they do
470 * refer to the same object an AssertionFailedError is thrown with the
471 * given message.
472 */
473 @SuppressWarnings("deprecation")
474 public static void assertNotSame(String message, Object expected, Object actual) {
475 Assert.assertNotSame(message, expected, actual);
476 }
477
478 /**
479 * Asserts that two objects do not refer to the same object. If they do
480 * refer to the same object an AssertionFailedError is thrown.
481 */
482 @SuppressWarnings("deprecation")
483 public static void assertNotSame(Object expected, Object actual) {
484 Assert.assertNotSame(expected, actual);
485 }
486
487 @SuppressWarnings("deprecation")
488 public static void failSame(String message) {
489 Assert.failSame(message);
490 }
491
492 @SuppressWarnings("deprecation")
493 public static void failNotSame(String message, Object expected, Object actual) {
494 Assert.failNotSame(message, expected, actual);
495 }
496
497 @SuppressWarnings("deprecation")
498 public static void failNotEquals(String message, Object expected, Object actual) {
499 Assert.failNotEquals(message, expected, actual);
500 }
501
502 @SuppressWarnings("deprecation")
503 public static String format(String message, Object expected, Object actual) {
504 return Assert.format(message, expected, actual);
505 }
506
507 /**
508 * Sets up the fixture, for example, open a network connection.
509 * This method is called before a test is executed.
510 */
511 protected void setUp() throws Exception {
512 }
513
514 /**
515 * Tears down the fixture, for example, close a network connection.
516 * This method is called after a test is executed.
517 */
518 protected void tearDown() throws Exception {
519 }
520
521 /**
522 * Returns a string representation of the test case
523 */
524 @Override
525 public String toString() {
526 return getName() + "(" + getClass().getName() + ")";
527 }
528
529 /**
530 * Gets the name of a TestCase
531 *
532 * @return the name of the TestCase
533 */
534 public String getName() {
535 return fName;
536 }
537
538 /**
539 * Sets the name of a TestCase
540 *
541 * @param name the name to set
542 */
543 public void setName(String name) {
544 fName = name;
545 }
546 }