org.jwalk.core
Class TestCase

java.lang.Object
  extended by org.jwalk.core.TestCase
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
EnumTestCase, ParamTestCase

public abstract class TestCase
extends java.lang.Object
implements java.lang.Cloneable

TestCase represents a single invocation, construction or constant selection. A TestCase is a single element of a TestSequence, and represents one of a Method invocation, a Constructor invocation, or an Enum value selection. The descendants of TestCase handle these three different kinds of case: InvokeTestCase, CreateTestCase, EnumTestCase. All of these return the created (or selected) value, when they are executed.

A TestCase also stores an encoded representation of the state of the test object, which is computed when the TestCase is executed. This allows JWalkers to determine if a sequence of invocations caused the test object to change state, or visit a previously-visited state.

Version:
1.0
Author:
Anthony Simons

Field Summary
protected static StateInspector inspector
          The StateInspector to use, when inspecting object states.
protected  int state
          The encoded state of the target object.
 
Constructor Summary
protected TestCase()
          Initialises the abstract part of a TestCase.
 
Method Summary
 TestCase clone()
          Clones a shallow copy of this TestCase.
abstract  java.lang.Object execute(ObjectGenerator generator, java.lang.Object target)
          Executes this TestCase, returning the result.
abstract  java.lang.String getKey(ObjectGenerator generator)
          Returns a unique indexing key for this TestCase.
abstract  java.lang.reflect.Member getOperation()
          Returns the tested operation for this TestCase.
abstract  java.lang.Class<?> getReturnType()
          Returns the result type of the tested operation.
 int getState()
          Returns the encoded state of the target object.
static void setInspector(StateInspector inspector)
          Sets the StateInspector to use for all TestCases.
abstract  java.lang.String toString(ObjectGenerator generator)
          Returns a pretty-printed representation of this TestCase.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inspector

protected static StateInspector inspector
The StateInspector to use, when inspecting object states.


state

protected int state
The encoded state of the target object. By default, initially zero. The state is only relevant after this TestCase has been executed.

Constructor Detail

TestCase

protected TestCase()
Initialises the abstract part of a TestCase. Defined explicitly to ensure a non-public constructor for this abstract class.

Method Detail

setInspector

public static void setInspector(StateInspector inspector)
Sets the StateInspector to use for all TestCases. This allows JWalk to supply an inspector that inspects object trees to custom depths, rather than use the default StateInspector that inspects to the STANDARD depth.

Parameters:
inspector - the StateInspector to use.

clone

public TestCase clone()
Clones a shallow copy of this TestCase. Ensures that the state of the copy is always initialised to zero.

Overrides:
clone in class java.lang.Object

getState

public int getState()
Returns the encoded state of the target object. Only valid after this TestCase has been executed.

Returns:
the encoded state of the target object.

getOperation

public abstract java.lang.reflect.Member getOperation()
Returns the tested operation for this TestCase. This may be a Method, a Constructor, or null if this TestCase simply enumerates a value. The result has the generic interface-type Member, which may be cast down to a Method or Constructor. It is possible to access the name of a Member using getName().

Returns:
the tested operation, or null if none.

getReturnType

public abstract java.lang.Class<?> getReturnType()
Returns the result type of the tested operation. This is one of the result-type of a Method; or the owning class-type of a Constructor; or the enumerated type of the constant value.

Returns:
the initial constructed (or enumerated) type, or method return type.

execute

public abstract java.lang.Object execute(ObjectGenerator generator,
                                         java.lang.Object target)
                                  throws java.lang.reflect.InvocationTargetException,
                                         GeneratorException,
                                         ExecutionException
Executes this TestCase, returning the result. Depending on the kind of TestCase, creates the next Enum value; or invokes the Constructor under test; or invokes the Method under test. For Method-invocations, the target is a valid instance of the test class; for the other cases the target is ignored (and is null). The returned result is one of: a unique enumerated value; a newly-constructed instance of the test-class; or the result of invoking the method on the target.

Parameters:
generator - the ObjectGenerator for synthesising test inputs.
target - the target object, initially null.
Returns:
the result of this TestCase.
Throws:
java.lang.reflect.InvocationTargetException - if the tested operation raises an exception internally. This is later handled by TestSequence, which reports the exception as the result of the test.
GeneratorException - if any constructor or method argument value could not be synthesised. A CustomGenerator must be provided that is capable of synthesising values of the argument type.
ExecutionException - if any constructor or method could not be invoked, or if it exhibited unpredictable random behaviour.

toString

public abstract java.lang.String toString(ObjectGenerator generator)
Returns a pretty-printed representation of this TestCase. This allows JWalk to supply a printable output, after the TestCase has been run. Requires the same ObjectGenerator which synthesised the parameters to obtain the pretty-printed format for these values.

Parameters:
generator - the ObjectGenerator which synthesised the parameters.
Returns:
a pretty-printed representation of this TestCase.

getKey

public abstract java.lang.String getKey(ObjectGenerator generator)
Returns a unique indexing key for this TestCase. This key is part of the indexing key for a TestSequence in an Oracle. Requires the same ObjectGenerator which synthesised the parameters to obtain the correct unique pretty-printed format for these values.

Parameters:
generator - the ObjectGenerator which synthesised the parameters.
Returns:
a unique indexing key.