org.jwalk.core
Class StateInspector

java.lang.Object
  extended by org.jwalk.core.StateInspector

public class StateInspector
extends java.lang.Object

StateInspector inspects the concrete state of a test object. A StateInspector computes an integral encoding of the concrete state of a test object. Each time a test object re-enters the same concrete state, the StateInspector must return the same state code. The notion of state is nonetheless variable, depending on whether the test object is explored to a shallow, medium or deep level. This is controlled by the depth parameter supplied at construction, obtained from the treeDepth parameter in the owning JWalker's Settings.

Version:
1.0
Author:
Anthony Simons

Constructor Summary
StateInspector()
          Creates a StateInspector that examines to the 0 depth.
StateInspector(int depth)
          Creates a StateInspector that examines to the given depth.
 
Method Summary
protected  java.util.List<java.lang.reflect.Field> getFields(java.lang.Class<?> testClass)
          Determines all the fields of the current test class.
 int inspect(java.lang.Object testObject)
          Inspects the current state of the test object.
protected  int inspectArray(java.lang.Class<?> type, java.lang.Object array, int depth)
          Returns an encoding of the state of a primitive array.
protected  int inspectObject(java.lang.Class<?> testClass, java.lang.Object testObject, int depth)
          Returns an encoding of the state of the test object to a given depth.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StateInspector

public StateInspector()
Creates a StateInspector that examines to the 0 depth. This will examine the shallow state of the test object, based only on its primitive field values, array elements and object references.


StateInspector

public StateInspector(int depth)
Creates a StateInspector that examines to the given depth. This will examine the deeper state of the test object by recursively exploring all objects reachable from the test object, up to the specified depth. The specified depth may not be less than zero.

Parameters:
depth - the depth of inspection.
Method Detail

inspect

public int inspect(java.lang.Object testObject)
Inspects the current state of the test object. The returned hash code is an encoding of the test object's current state. When the test object re-enters a given concrete state, the same hash code should be returned. The sensitivity of the state encoding is influenced by this StateInspector's depth setting.

Parameters:
testObject - the test object in a given state.
Returns:
a hash code encoding the test object's state.

inspectObject

protected int inspectObject(java.lang.Class<?> testClass,
                            java.lang.Object testObject,
                            int depth)
Returns an encoding of the state of the test object to a given depth. This method is the main recursively-invoked workhorse that computes a unique hash code encoding the state of the test object. If the depth parameter is greater than zero, objects reachable from the test object will be recursively inspected to depth-1.

Parameters:
testClass - the class of the test object.
testObject - a non-null test object in a given state.
depth - the depth to which the reachable object tree is explored.
Returns:
a hash code encoding the test object's state.

inspectArray

protected int inspectArray(java.lang.Class<?> type,
                           java.lang.Object array,
                           int depth)
Returns an encoding of the state of a primitive array. The array is part of the test object and its elements, possibly null, are part of the state of that object. The array is treated as being at the same depth as the test object's other primitive fields, but objects stored in the array will be recursively inspected to depth-1. Note that this method does not distinguish the active, and total length, of a primitive array. To re-enter a state, the array must contain exactly the same values (or null) in the same order.

Parameters:
type - the type of the array
array - a non-null primitive array in a given filled state.
depth - the depth to which the reachable object tree is explored.
Returns:
a hash code encoding the array's state.

getFields

protected java.util.List<java.lang.reflect.Field> getFields(java.lang.Class<?> testClass)
Determines all the fields of the current test class. The test class is the class of the current test object. To access the state of the test object requires determining the fields of its class, as a template for extracting its field values. This method extracts the locally declared fields, and recursively extracts the inherited fields of the test class, returning the fields in a template. Any access restriction on fields is bypassed, to allow StateInspector later to inspect their values directly.

Parameters:
testClass - the class from which to extract fields.
Returns:
a list of all surface fields.