001package junit.framework;
002
003import java.lang.reflect.InvocationTargetException;
004import java.lang.reflect.Method;
005import java.lang.reflect.Modifier;
006
007/**
008 * A test case defines the fixture to run multiple tests. To define a test case<br/>
009 * <ol>
010 *   <li>implement a subclass of <code>TestCase</code></li>
011 *   <li>define instance variables that store the state of the fixture</li>
012 *   <li>initialize the fixture state by overriding {@link #setUp()}</li>
013 *   <li>clean-up after a test by overriding {@link #tearDown()}.</li>
014 * </ol>
015 * Each test runs in its own fixture so there
016 * can be no side effects among test runs.
017 * Here is an example:
018 * <pre>
019 * public class MathTest extends TestCase {
020 *    protected double fValue1;
021 *    protected double fValue2;
022 *
023 *    protected void setUp() {
024 *       fValue1= 2.0;
025 *       fValue2= 3.0;
026 *    }
027 * }
028 * </pre>
029 *
030 * For each test implement a method which interacts
031 * with the fixture. Verify the expected results with assertions specified
032 * by calling {@link junit.framework.Assert#assertTrue(String, boolean)} with a boolean.
033 * <pre>
034 *    public void testAdd() {
035 *       double result= fValue1 + fValue2;
036 *       assertTrue(result == 5.0);
037 *    }
038 * </pre>
039 *
040 * Once the methods are defined you can run them. The framework supports
041 * both a static type safe and more dynamic way to run a test.
042 * In the static way you override the runTest method and define the method to
043 * be invoked. A convenient way to do so is with an anonymous inner class.
044 * <pre>
045 * TestCase test= new MathTest("add") {
046 *    public void runTest() {
047 *       testAdd();
048 *    }
049 * };
050 * test.run();
051 * </pre>
052 * The dynamic way uses reflection to implement {@link #runTest()}. It dynamically finds
053 * and invokes a method.
054 * In this case the name of the test case has to correspond to the test method
055 * to be run.
056 * <pre>
057 * TestCase test= new MathTest("testAdd");
058 * test.run();
059 * </pre>
060 *
061 * The tests to be run can be collected into a TestSuite. JUnit provides
062 * different <i>test runners</i> which can run a test suite and collect the results.
063 * A test runner either expects a static method <code>suite</code> as the entry
064 * point to get a test to run or it will extract the suite automatically.
065 * <pre>
066 * public static Test suite() {
067 *    suite.addTest(new MathTest("testAdd"));
068 *    suite.addTest(new MathTest("testDivideByZero"));
069 *    return suite;
070 * }
071 * </pre>
072 *
073 * @see TestResult
074 * @see TestSuite
075 */
076@SuppressWarnings("deprecation")
077public abstract class TestCase extends Assert implements Test {
078    /**
079     * the name of the test case
080     */
081    private String fName;
082
083    /**
084     * No-arg constructor to enable serialization. This method
085     * is not intended to be used by mere mortals without calling setName().
086     */
087    public TestCase() {
088        fName = null;
089    }
090
091    /**
092     * Constructs a test case with the given name.
093     */
094    public TestCase(String name) {
095        fName = name;
096    }
097
098    /**
099     * Counts the number of test cases executed by run(TestResult result).
100     */
101    public int countTestCases() {
102        return 1;
103    }
104
105    /**
106     * Creates a default TestResult object.
107     *
108     * @see TestResult
109     */
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     */
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     */
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     */
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     */
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     */
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    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     */
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     */
215    public static void assertFalse(boolean condition) {
216        Assert.assertFalse(condition);
217    }
218
219    /**
220     * Fails a test with the given message.
221     */
222    public static void fail(String message) {
223        Assert.fail(message);
224    }
225
226    /**
227     * Fails a test with no message.
228     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
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     */
450    public static void assertNotSame(Object expected, Object actual) {
451        Assert.assertNotSame(expected, actual);
452    }
453
454    public static void failSame(String message) {
455        Assert.failSame(message);
456    }
457
458    public static void failNotSame(String message, Object expected, Object actual) {
459        Assert.failNotSame(message, expected, actual);
460    }
461
462    public static void failNotEquals(String message, Object expected, Object actual) {
463        Assert.failNotEquals(message, expected, actual);
464    }
465
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     */
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     */
481    protected void tearDown() throws Exception {
482    }
483
484    /**
485     * Returns a string representation of the test case.
486     */
487    @Override
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     */
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     */
506    public void setName(String name) {
507        fName = name;
508    }
509}