diva.util.jester
Class TestCase

java.lang.Object
  extended by diva.util.jester.TestCase
Direct Known Subclasses:
FigureTest.RegionTestCase

public abstract class TestCase
extends java.lang.Object

An abstract superclass for all test cases. This is intended for subclassing (generally anonymously), and to be passed to a TestHarness for execution. Subclasses are required to implement the run() and check() methods, and can optionally override the failed() method. Note that test suites may choose to subclass this with another abstract class, and then to anonymously override that new class in the methods. This allows, for example, the addition of methods for setting up scenarios shared by multiple tests, common result testing methods, and so on.

Version:
$Id: TestCase.java 38798 2005-07-08 20:00:01Z cxh $
Author:
John Reekie

Field Summary
private  java.lang.String _name
           
private  long _startTime
           
private  long _stopTime
           
 
Constructor Summary
TestCase(java.lang.String name)
          Construct a test case with the given name
 
Method Summary
 void assertEquals(java.lang.Object first, java.lang.Object second, java.lang.String msg)
          Assert the equality of two objects.
 void assertExpr(boolean passed, java.lang.String msg)
          Make an assertion, and fail the test if it isn't satisfied.
abstract  void check()
          Report on the results of running the test.
 void fail(java.lang.String msg)
          Fail a test.
 int getExecutionTime()
          Get the execution time of this test case.
 java.lang.String getName()
          Get the name of this test case
 void init()
          Initialize the test case.
abstract  void run()
          Run the test case.
 void startTimer()
          Start the execution timer.
 void stopTimer()
          Stop the execution timer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_name

private java.lang.String _name

_startTime

private long _startTime

_stopTime

private long _stopTime
Constructor Detail

TestCase

public TestCase(java.lang.String name)
Construct a test case with the given name

Method Detail

check

public abstract void check()
                    throws TestFailedException
Report on the results of running the test. This method should perform a series of checks on the data produced by the run() method, and for each, call fail() if the produced data does not conform to the expected result.

Throws:
TestFailedException

assertExpr

public void assertExpr(boolean passed,
                       java.lang.String msg)
                throws TestFailedException
Make an assertion, and fail the test if it isn't satisfied. This is a handy way of doing boolean results checking. The second argument is a string, which should generally be a copy of the expression that produced the first argument.

Throws:
TestFailedException

assertEquals

public void assertEquals(java.lang.Object first,
                         java.lang.Object second,
                         java.lang.String msg)
                  throws TestFailedException
Assert the equality of two objects. This method uses the equals() method of the first object to compare it with the second object. If the objects are not equal, then the test is failed. If the verbosity of the test harness is sufficiently high, then the harness may print out or log additional information on the two objects. The third argument is a string which should generally be something like "foo == bar".

Throws:
TestFailedException

fail

public void fail(java.lang.String msg)
          throws TestFailedException
Fail a test. This method constructs and throws a TestFailedException that includes the passed string as part of the messages. It is intended as a simple convenience method to make it easier to write test cases.

Throws:
TestFailedException

getExecutionTime

public int getExecutionTime()
Get the execution time of this test case. If the time is zero (and the timers were called) then return 1.


getName

public java.lang.String getName()
Get the name of this test case


init

public void init()
          throws java.lang.Exception
Initialize the test case. This is a concrete method that does nothing. It is not requires that this method be overridden, but any test that is more than a few lines should do so to distinguish between the initialization and execution phases of the test. It also allows local classes to be created that override init() to create an object or set of objects that will be used in multiple tests.

Throws:
java.lang.Exception

run

public abstract void run()
                  throws java.lang.Exception
Run the test case. This method actually runs the test. Result checking should not be done here, but in the check() method. The method signature includes the completely general Exception class, since this method can contain arbitrary code.

Throws:
java.lang.Exception

startTimer

public void startTimer()
Start the execution timer. This can be called only once in the execution of the test case. If it is called (and the stopTimer() method id also called), the execution time is printed by the test harness.


stopTimer

public void stopTimer()
Stop the execution timer. The difference between the start and stop times will be reported by the test harness.