org.jwalk.core
Class ValueGenerator

java.lang.Object
  extended by org.jwalk.core.ValueGenerator
All Implemented Interfaces:
MasterGenerator, Generator
Direct Known Subclasses:
ArrayGenerator

public abstract class ValueGenerator
extends java.lang.Object
implements MasterGenerator

ValueGenerator is an abstract generator for synthesising simple values. Since Generators can be quite complicated, the standard ObjectGenerator is split into three parts, with ValueGenerator being the root of a hierarchy, whose descendants are ArrayGenerator and finally ObjectGenerator. ValueGenerator provides the basic facility to synthesise primitive types, wrapped types, enumerated types and Strings. It cannot be instantiated directly. The methods canCreate and nextValue are still abstract in ValueGenerator. ObjectGenerator is the first MasterGenerator which may be used directly by JWalkers.

ValueGenerator provides the ability to synthesise values of every primitive Java type from: {byte, boolean, char, int, short, long, float, double}. It also provides the ability to synthesise values of every wrapped simple value type from: {Byte, Boolean, Character, Integer, Short, Long, Float, Double}. Other printable basic types, such as String and Enum, are handled by custom generators.

Version:
1.0
Author:
Anthony Simons

Field Summary
protected  java.util.List<CustomGenerator> delegates
          The list of CustomGenerator delegates.
 
Constructor Summary
protected ValueGenerator()
          Initialises the ValueGenerator ancestor part of an ObjectGenerator.
 
Method Summary
 void addDelegate(CustomGenerator generator)
          Adds a delegate CustomGenerator to this ValueGenerator.
protected  java.lang.Object createPrimitive(java.lang.Class<?> type)
          Creates an instance of a primitive Java type.
protected  java.lang.Object createWrapped(java.lang.Class<?> type)
          Creates an instance of a wrapped Java type.
protected  boolean isEnumerated(java.lang.Class<?> type)
          Reports if a type is an enumerated Java type.
protected  boolean isPrimitive(java.lang.Class<?> type)
          Reports if a type is a primitive Java type.
protected  boolean isPrintable(java.lang.Class<?> type)
          Reports whether a type is printable.
protected  boolean isWrapped(java.lang.Class<?> type)
          Reports if a type is a wrapped primitive Java type.
 java.lang.String oracleValue(java.lang.Object object)
          Converts any synthesised object into its oracle value format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jwalk.gen.MasterGenerator
getJWalker, getTarget, getTargetType, logObject, logTarget
 
Methods inherited from interface org.jwalk.Generator
canCreate, nextValue
 

Field Detail

delegates

protected java.util.List<CustomGenerator> delegates
The list of CustomGenerator delegates. These are generators which take precedence over this MasterGenerator when synthesising values of any particular type which must be treated specially.

Constructor Detail

ValueGenerator

protected ValueGenerator()
Initialises the ValueGenerator ancestor part of an ObjectGenerator. This method is protected, since no instances of ValueGenerator may be created directly. Initialises the seed values for each primitive type. Installs an empty list of delegate CustomGenerators.

Method Detail

isPrimitive

protected boolean isPrimitive(java.lang.Class<?> type)
Reports if a type is a primitive Java type.

Returns:
the result of type.isPrimitive().

isWrapped

protected boolean isWrapped(java.lang.Class<?> type)
Reports if a type is a wrapped primitive Java type.

Returns:
true if type is in the set of wrapped types.

isEnumerated

protected boolean isEnumerated(java.lang.Class<?> type)
Reports if a type is an enumerated Java type.

Returns:
the result of type.isEnum();

isPrintable

protected boolean isPrintable(java.lang.Class<?> type)
Reports whether a type is printable. Some types have directly printable instances, whereas others need to be encoded in some way before display. Printable types (so far) include primitive, wrapped, enumerated and String types.

Parameters:
type - a type to be tested.
Returns:
true if the type is printable.

createPrimitive

protected java.lang.Object createPrimitive(java.lang.Class<?> type)
                                    throws GeneratorException
Creates an instance of a primitive Java type. For compatibility with other object types, creates a wrapped instance of a primitive Java type. The wrapped value is the current seed for that type; which is incremented prior to the next request.

Parameters:
type - a primitive Java type.
Returns:
a unique value of this type, wrapped.
Throws:
GeneratorException, - if no value could be synthesised.
GeneratorException

createWrapped

protected java.lang.Object createWrapped(java.lang.Class<?> type)
                                  throws GeneratorException
Creates an instance of a wrapped Java type. Creates an instance of the requested wrapper type, containing the current seed value for the corresponding primitive type, which is incremented prior to the next request.

Parameters:
type - a Java wrapper-type.
Returns:
a unique value of this type.
Throws:
GeneratorException, - if no value could be synthesised.
GeneratorException

oracleValue

public java.lang.String oracleValue(java.lang.Object object)
Converts any synthesised object into its oracle value format. By default, all value-based objects are converted using the standard method String.valueOf(object), which also works for null and void (which may both be treated as values). Two kinds of value are treated specially: Character instances are conventionally surrounded by single quotes. String instances are conventionally surrounded by double quotes.

Specified by:
oracleValue in interface MasterGenerator
Parameters:
object - a value created by this ValueGenerator.
Returns:
the encoded oracle value format.

addDelegate

public void addDelegate(CustomGenerator generator)
Adds a delegate CustomGenerator to this ValueGenerator. The delegate is added at the front of the list of delegates, so that it takes priority over any existing delegates. This allows testers to supply CustomGenerators that replace existing CustomGenerators.

Specified by:
addDelegate in interface MasterGenerator
Parameters:
generator - the delegate CustomGenerator to add.