jartege
Class ClassTester

java.lang.Object
  extended byjartege.ClassTester

public class ClassTester
extends java.lang.Object

Class that allows one to generate random tests for a specified set of classes.
A typical use of this class is as follows:

A weight is positive or null double value associated with each class and each visible operation of class (constructors, methods and operations get and set for each visible field). By default, all weights are equal to 1. The program generates a method call according to the specified weights. Weights can be changed with change weight methods. The following statement change the weight of packageName.ClassA to 9. A creation probability function is associated with each class under test. This function commands the creation of new objects of the class, according to the number of already created objects.

The statement changes the creation threshold function of class ClassA to the specified probability function, which allows the creation of at most one object of ClassA.

The statement add the package packageName to the set of imported packages. For each imported package, the generator generates a line in the generated test file.

The statement generates a test file "TestForAandB.java". This file consists of a class called "TestForAandB" which contains 10 test methods. For each test method, the tool tries to generate 50 method calls in classes ClassA and ClassB. For each generated call, ClassA will be choosen with a probablity of 0.9 while ClassB will be chosen with a probability of 0.1.


Field Summary
static CreationProbability creationProbabilityFew
          Probability function that allows the creation of few objects.
static CreationProbability creationProbabilityMany
          Probability function that allows the creation of many objects.
static CreationProbability creationProbabilityNormal
          Normal creation probability function.
 
Constructor Summary
ClassTester()
          Constructs a new class tester.
ClassTester(java.lang.String[] args)
          Constructs a new class tester.
 
Method Summary
 void addClass(java.lang.String className)
          Adds the class with the specified name to the set of classes under test.
 void addContext(java.lang.String className)
          Add the specified class to the context.
 void addImportedClass(java.lang.String className)
          Adds the specified class to the set of imported classes.
 void addImportedPackage(java.lang.String packageName)
          Adds the specified package to the set of imported packages.
 void changeAllConstructorsWeight(java.lang.String className, double weight)
          In class with the specified name, changes all constructors weight to the specified weight.
 void changeAllMethodsWeight(java.lang.String className, double weight)
          In class with the specified name, changes all methods weight to the specified weight.
 void changeClassWeight(java.lang.String className, double weight)
          Changes the weight of class with the specified name to the specified weight.
 void changeConstructorWeight(java.lang.String constructorName, java.lang.String[] theSignature, double weight)
          In class with the specified name className, changes the weight of the constructor with the specified name constructorName and specified signature to the specified weight.
 void changeCreationProbability(java.lang.String className, CreationProbability function)
          Changes the creation probability function of the specified class to the specified function.
 void changeGetWeight(java.lang.String className, java.lang.String fieldName, double weight)
          Changes the weight of the get operation associated with the specified field in the specified class to the specified weight.
 void changeMethodWeight(java.lang.String className, java.lang.String methodName, double weight)
          In class with the specified name className, changes the weight of all methods with the specified name methodName to the specified weight.
 void changeMethodWeight(java.lang.String className, java.lang.String methodName, java.lang.String[] theSignature, double weight)
          In class with the specified name className, changes the weight of the method with the specified name methodName and specified signature to the specified weight.
 void changeObjectGenerator(java.lang.String className, jartege.ObjectGenerator objectGen)
          Change the object generator associated with the class with the specified name className to the specified object generator.
 void changeSetWeight(java.lang.String className, java.lang.String fieldName, double weight)
          Changes the weight of the set operation associated with the specified field in the specified class to the specified weight.
 void generate(java.lang.String className, int numberOfMethodCalls)
          Generates a test class with the specified name className.
 void generate(java.lang.String className, int numberOfTests, int numberOfMethodCalls)
          Generates a test class with the specified name className.
 java.util.Set getClasses()
          Returns the set of classes under test of this class tester.
 CreationProbability getCreationProbability(java.lang.String className)
          Returns the creation probability associated with the specified class.
 java.lang.String getDir()
          Returns the directory in which the test file is generated.
 boolean getForJUnit()
          Returns the ForJUnit flag value.
 double getGetWeight(java.lang.String className, java.lang.String fieldName)
          Gets the weight of the get operation associated with the specified field in the specified class.
 java.util.Set getImportedClasses()
          Returns the set of imported classes.
 java.util.Set getImportedPackages()
          Returns the set of imported packages.
 java.lang.String getPackage()
          Returns the package name of the generated test file.
 double getSetWeight(java.lang.String className, java.lang.String fieldName)
          Gets the weight of the get operation associated with the specified field in the specified class.
 java.lang.Class getToExtendClass()
          Returns the class that the generated test class extends.
 void removeAllClasses()
          Removes all classes under test in this class tester.
 void removeClass(java.lang.String className)
          Remove the class with the specified name to the set of classes under test.
 void resetContext()
          Reinitialises the context.
 void setAllMethodsStoreResult(java.lang.String className, boolean storeResult)
          Sets the storeResult flag of all methods in the specified class to the specified boolean value.
 void setDir(java.lang.String dirName)
          Sets the directory in which the test file is generated to the specified directory name.
 void setForJUnit(boolean b)
          Sets the ForJUnit flag to the specified boolean value.
 void setMethodStoreResult(java.lang.String className, java.lang.String methodName, boolean storeResult)
          Sets the storeResult flag of method with the specified name in the specified class to the specified boolean value.
 void setMethodStoreResult(java.lang.String className, java.lang.String methodName, java.lang.String[] signature, boolean storeResult)
          Indicates that results of method calls with the specified name with the specified signature in the specified class should not be stored in variables.
 void setPackage(java.lang.String packageName)
          Sets the package name of the generated test file to the specified package name.
 void setToExtendClass(java.lang.String toExtendClassName)
          Defines the class that the generator extends.
 void unsetToExtendClass()
          Unsets the class that the generated test class extends.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

creationProbabilityNormal

public static CreationProbability creationProbabilityNormal
Normal creation probability function. The probability function is defined by
    theFunction(n) = 
       1.0 / (1.0 + n) ; 
 


creationProbabilityFew

public static CreationProbability creationProbabilityFew
Probability function that allows the creation of few objects. The probability function is defined by
    theFunction(n) =
       1.0 / ((1.0 + n) * (1.0 + n)) ;
 


creationProbabilityMany

public static CreationProbability creationProbabilityMany
Probability function that allows the creation of many objects. The probability function is defined by
    theFunction(n) =
       1.0 / Math.sqrt(1.0 + n) ;
 

Constructor Detail

ClassTester

public ClassTester(java.lang.String[] args)
Constructs a new class tester.

Parameters:
args - the arguments of the command line.

ClassTester

public ClassTester()
Constructs a new class tester.

Method Detail

addContext

public void addContext(java.lang.String className)
Add the specified class to the context.


getPackage

public java.lang.String getPackage()
Returns the package name of the generated test file. Returns null if the package name has not been previously set.


resetContext

public void resetContext()
Reinitialises the context.


setPackage

public void setPackage(java.lang.String packageName)
Sets the package name of the generated test file to the specified package name.

Parameters:
packageName - the name of the package in which the test file is generated.

setDir

public void setDir(java.lang.String dirName)
Sets the directory in which the test file is generated to the specified directory name. If the directory is not set, then the file is generated in the current directory.

Parameters:
dirName - the name of the directory in which the test file is generated.

getDir

public java.lang.String getDir()
Returns the directory in which the test file is generated. Returns null if the directory has not been previously set.

See Also:
setDir(java.lang.String)

setForJUnit

public void setForJUnit(boolean b)
Sets the ForJUnit flag to the specified boolean value. If the ForJUnit flag is true, then the generator will generate a test for JUnit, otherwise the generator generate a stand alone class test. By default, the ForJUnit flag is false.


getForJUnit

public boolean getForJUnit()
Returns the ForJUnit flag value.


addImportedPackage

public void addImportedPackage(java.lang.String packageName)
Adds the specified package to the set of imported packages. The test generator will the generate a line
import packageName.* ;
in the generated test file.

Parameters:
packageName - the name of the package that has to be imported.
See Also:
addImportedClass(java.lang.String)

getImportedPackages

public java.util.Set getImportedPackages()
Returns the set of imported packages. Each element of the set is of type String.


addImportedClass

public void addImportedClass(java.lang.String className)
Adds the specified class to the set of imported classes. The test generator will generate a line
import className ;
in the generated test file.

Parameters:
className - the name of the class that has to be imported.
See Also:
addImportedPackage(java.lang.String)

getImportedClasses

public java.util.Set getImportedClasses()
Returns the set of imported classes. Each element of the set is of type Class.


setToExtendClass

public void setToExtendClass(java.lang.String toExtendClassName)
Defines the class that the generator extends. If the setToExtendClass method is not called, then the generator produces a class which extends java.lang.Object. When the ForJUnit flag is true, the class that the generator extends must extend the JUnit class TestCase. The class that the generator extends can be unset with the unsetToExtendClass() method.

Parameters:
toExtendClassName - the name of the class that will be extended by the generator.
See Also:
unsetToExtendClass()

getToExtendClass

public java.lang.Class getToExtendClass()
Returns the class that the generated test class extends.


unsetToExtendClass

public void unsetToExtendClass()
Unsets the class that the generated test class extends.

See Also:
setToExtendClass(java.lang.String)

addClass

public void addClass(java.lang.String className)
Adds the class with the specified name to the set of classes under test.

Parameters:
className - the name of the class which is added in the set of classes under test.

removeClass

public void removeClass(java.lang.String className)
Remove the class with the specified name to the set of classes under test.

Parameters:
className - the name of the class which is removed of the set of classes under test.

removeAllClasses

public void removeAllClasses()
Removes all classes under test in this class tester.


getClasses

public java.util.Set getClasses()
Returns the set of classes under test of this class tester.


changeClassWeight

public void changeClassWeight(java.lang.String className,
                              double weight)
Changes the weight of class with the specified name to the specified weight.


changeAllMethodsWeight

public void changeAllMethodsWeight(java.lang.String className,
                                   double weight)
In class with the specified name, changes all methods weight to the specified weight.


changeAllConstructorsWeight

public void changeAllConstructorsWeight(java.lang.String className,
                                        double weight)
In class with the specified name, changes all constructors weight to the specified weight.


changeMethodWeight

public void changeMethodWeight(java.lang.String className,
                               java.lang.String methodName,
                               double weight)
In class with the specified name className, changes the weight of all methods with the specified name methodName to the specified weight.


changeMethodWeight

public void changeMethodWeight(java.lang.String className,
                               java.lang.String methodName,
                               java.lang.String[] theSignature,
                               double weight)
In class with the specified name className, changes the weight of the method with the specified name methodName and specified signature to the specified weight.


changeConstructorWeight

public void changeConstructorWeight(java.lang.String constructorName,
                                    java.lang.String[] theSignature,
                                    double weight)
In class with the specified name className, changes the weight of the constructor with the specified name constructorName and specified signature to the specified weight.


changeGetWeight

public void changeGetWeight(java.lang.String className,
                            java.lang.String fieldName,
                            double weight)
Changes the weight of the get operation associated with the specified field in the specified class to the specified weight.


changeSetWeight

public void changeSetWeight(java.lang.String className,
                            java.lang.String fieldName,
                            double weight)
Changes the weight of the set operation associated with the specified field in the specified class to the specified weight.


getGetWeight

public double getGetWeight(java.lang.String className,
                           java.lang.String fieldName)
Gets the weight of the get operation associated with the specified field in the specified class.

Parameters:
className - name of the class.
fieldName - field name in the specified class.

getSetWeight

public double getSetWeight(java.lang.String className,
                           java.lang.String fieldName)
Gets the weight of the get operation associated with the specified field in the specified class.

Parameters:
className - name of the class.
fieldName - field name in the specified class.

changeCreationProbability

public void changeCreationProbability(java.lang.String className,
                                      CreationProbability function)
Changes the creation probability function of the specified class to the specified function.


getCreationProbability

public CreationProbability getCreationProbability(java.lang.String className)
Returns the creation probability associated with the specified class.


setAllMethodsStoreResult

public void setAllMethodsStoreResult(java.lang.String className,
                                     boolean storeResult)
Sets the storeResult flag of all methods in the specified class to the specified boolean value.


setMethodStoreResult

public void setMethodStoreResult(java.lang.String className,
                                 java.lang.String methodName,
                                 boolean storeResult)
Sets the storeResult flag of method with the specified name in the specified class to the specified boolean value.


setMethodStoreResult

public void setMethodStoreResult(java.lang.String className,
                                 java.lang.String methodName,
                                 java.lang.String[] signature,
                                 boolean storeResult)
Indicates that results of method calls with the specified name with the specified signature in the specified class should not be stored in variables.


changeObjectGenerator

public void changeObjectGenerator(java.lang.String className,
                                  jartege.ObjectGenerator objectGen)
Change the object generator associated with the class with the specified name className to the specified object generator.

Parameters:
className - the name of the class.
objectGen - the object generator.

generate

public void generate(java.lang.String className,
                     int numberOfTests,
                     int numberOfMethodCalls)
Generates a test class with the specified name className.

Parameters:
className - the name of the generated test class.
numberOfTests - the number of test methods generated in the class.
numberOfMethodCalls - the number of method calls the generator tries to generate for each test method.

generate

public void generate(java.lang.String className,
                     int numberOfMethodCalls)
Generates a test class with the specified name className. This is equivalent to generate(className, 1, numberOfMethodCalls)

Parameters:
className - the name of the generated test class.
numberOfMethodCalls - the number of method calls the generator tries to generate.