testsgen.taskant
Class UnitTestsGen

java.lang.Object
  |
  +--org.apache.tools.ant.ProjectComponent
        |
        +--org.apache.tools.ant.Task
              |
              +--testsgen.taskant.UnitTestsGen

public class UnitTestsGen
extends org.apache.tools.ant.Task

UnitTestsGen.java extends org.apache.tools.ant.Task extension. It implements task which allows use Unit Tests Generator from ANT and controll test classes genrating from build.xml file.
It is very simple at the moment and support only subset of features offered by 'ANT'.

To use it in your developing process you must define new task in your build.xml. To do this put in the beggining of your build file such lines:

     <taskdef name="unitgen"
              classpath="some_dir/unittestsgen.jar"
              classname="testsgen.taskant.UnitTestsGen"/>
 
Where, of course, 'some_dir' is directory where 'unittestsgen.jar' file exists.
Since we already defined new task we can use it at any time we need it. Of course it can be used in many ways depends of particular case. I recomend very simple but effective use. Look below to see almost complete very simple build.xml sample.
 <project name="UnitTestsGen" default="dist" basedir=".">
     <taskdef name="unitgen" classname="testsgen.taskant.UnitTestsGen"/>
     <property name="src" value="src"/>
     <property name="test-src" value="test-src"/>
     <property name="build" value="build"/>
 
     <target name="compile" description="Compile sources in ${tmpsrc} directory">
         <mkdir dir="${build}"/>
         <javac srcdir="${src};${test-src}" destdir="${build}"
                debug="on" deprecation="off">
             <classpath>
                 <pathelement location="${libs}/junit.jar"/>
             </classpath>
         </javac>
     </target>
     <target name="jar" depends="compile">
         <jar jarfile="jar/${jarfile}.jar" manifest="MANIFEST.MF" basedir="${build}">
             <exclude name="** /*TestCase*.*"/>
             <exclude name="TestAll.*"/>
         </jar>
         <unitgen update="true" classpath="${build};${libs}/junit.jar"
                  imput="${src}" output="${test-src}"/>
     </target>
 </project>
 
After excecuting command 'ant jar' you should see similar output:
 compile:
     [javac] Compiling 36 source files to /home/kobit/projects/unittestsgen/build
 jar:
       [jar] Building jar: /home/kobit/projects/unittestsgen/jar/unittestsgen.jar
   [unitgen] Finished work: files 21, sources 14, new classes 3, new methods 7
 
 BUILD SUCCESSFUL
 
Every time you will be building jar file all your source classes will be checked against changes made and test classes will be updated if necessary.
In above sample 'unitteg' task found 21 files in given directory and 14 files with source code which can be tested. So it generated 3 new test classes and added 7 new methods to existing test classes.

Below you can read detailed list of all supported attributes. All given attributes are translated to command line parameters form for GenerateTests class. So you can find similar list but with a little different names in this class documentation.
In description titles you have sample use code. Given values are default in most cases. If there are given multiple options first one is default. There are some differencies in defaults values between ANT task mode and command line mode.

  1. config="gentest.cfg" This attribute is useful for command line running this tool. It is not very suitable for using it in build.xml file but it is possible. Additionaly you must note that content of this file should contain names of the command line form. See GenerateTests.java documentation for complete list of command line parameters or run this tool from command line as following:
     java -jar unittestsgen.jar --help
     
    As you can see there are a lot of possible parameters for running this application. Although almost all defaults are suitable for most cases if you use different options it is very difficult to run tool every time with long list of command line parameters.
    So you can put all parameters in file and use it each time you need. Default file name is 'gentest.cfg'. Content of this file should be of the following form:
     key = value
     
    Where key is name of command line parameter you want to set. You can also use there comments: Lines which starts with '#' character. and blank lines.
    If you use both config file and parameters in command line they will be processed in giving order. So last given parameter will be used during processing.
  2. classpath="your_class_files;libs.jar;junit.jar" Where your class file should be jar file containing your all compiled files including test classes or directory where lies all your compiled files.
    It is the most important parameter because this is only parameter which is always required.
    Since there is no way to set additional CLASSPATH during call: 'java -jar package.jar' here I provided parameter to set location of additional necessary classes. For example it is required to set location of jar file with compiled classes for sources found in '--input-directory' or set location of additional libraries for code parsers and generators like junit.jar. Please remember: if your project contains already some test classes you must put here jar file with compiled test classes. This allow analyze them and extend current test classes with additional test code for new created methods in source classes.
    It supports also nested classpath element in common ANT use.
  3. pattern="*TestCase.java" Pattern for creating names of test classes. The most commonly used standards are to put 'Test' string at the beginning of source class name or at the end of class name.
  4. output="test-src" Store test files in this given directory.
    You can store your test files in different directories. There are two common used posibilities: store in the same directory tree where source classes are or create separated directory tree only for test classes. Please note that if you even put test classes in separated directory tree all related test classes will be always in the same packages where source classes are. It allows test classes to access to all non-private methods in source classes. It is good idea to test all non-private classes members.
  5. input="src" Process files in given directory. But note that test classes will be searched in both input and output directories. But stored will always in output directory tree.
  6. content="both|empty|code|comments" Set here what you want to have in new generated test methods.
  7. overwrite="false|true" Force overwriting existing files. And additionaly please note that new test classes will be always created in output directory even if they found in input directory.
  8. update="true|false" Allow overwriting existing files if they need updating. This parameter is required for extending existing test classes with new code. Whole existing code will be left unchanged. And additionaly please note that new test classes will be always created in output directory even if they found in input directory.
  9. mask="*.java" Process source files with given file mask. At the moment there is very limited support for this feature.
  10. recursive="true|false" Recursive directories processing.
  11. test="false|true" Test only mode don't make any changes on disk.
  12. quiet="true|false" Don't display any messages on screen.
  13. debug="false|true" Display additional debug messages.
  14. help="false|true" Display help message and exit program. I don't expect it as useful feature for ANT task mode but I left it here for full compatibility with command line mode.
  15. version="false|true" Display version info and exit program. I don't expect it as useful feature for ANT task mode but I left it here for full compatibility with command line mode.
  16. implementations="testsgen.wttools" Package where are all modules implementations necessary for file processing.
  17. target="testsgen.wttools.CodeGenImpl" Name of class which generate code for unit test. It must be 'CodeGenIfc' implementation.
  18. logimpl="testsgen.wttools.LogImpl" Name of class which implements logging facility. It must be 'LogIfc' implementation.
  19. propertyimpl="testsgen.wttools.PropertyImpl" Name of class which implements property access facility. It must be 'PropertyIfc' implementation.
  20. dirwalker="testsgen.wttools.DirWalkerImpl" Name of class which provides file names list to process. It must be 'DirWalkerIfc' implementation.
  21. parser="testsgen.wttools.SrcParserImpl" Name of class which parses source code. In other words it should provide list of preparsed structures which identifies source files. It must be 'SrcParserIfc' implementation.
  22. codegen="testsgen.wttools.CodeGenImpl" Name of class which generate code for unit test. It is synonym for --target-unit-test class.

Created: Thu Jan 31 08:37:42 2002

Version:
$Revision: 1.4 $
Author:
Artur Hefczyc

Field Summary
protected  org.apache.tools.ant.types.CommandlineJava commandline
           
static java.lang.String created
           
static java.lang.String dedicated
           
protected static java.lang.String PAR_CLASSPATH
           
protected static java.lang.String PAR_CODEGEN
           
protected static java.lang.String PAR_CONFIG
           
protected static java.lang.String PAR_CONTENT
           
protected static java.lang.String PAR_DEBUG
           
protected static java.lang.String PAR_DIRWALKER
           
protected static java.lang.String PAR_FORCE
           
protected static java.lang.String PAR_HELP
           
protected static java.lang.String PAR_IMPLEMENTATIONS
           
protected static java.lang.String PAR_INPUT
           
protected static java.lang.String PAR_LOGIMPL
           
protected static java.lang.String PAR_MASK
           
protected static java.lang.String PAR_OUTPUT
           
protected static java.lang.String PAR_PARSER
           
protected static java.lang.String PAR_PATTERN
           
protected static java.lang.String PAR_PROPERTYIMPL
           
protected static java.lang.String PAR_QUIET
           
protected static java.lang.String PAR_RECURSIVE
           
protected static java.lang.String PAR_TARGET
           
protected static java.lang.String PAR_TEST
           
protected static java.lang.String PAR_UPDATE
           
protected static java.lang.String PAR_VERSION
           
protected  java.util.Properties prop
           
static java.lang.String version
           
 
Fields inherited from class org.apache.tools.ant.Task
description, location, target, taskName, taskType, wrapper
 
Fields inherited from class org.apache.tools.ant.ProjectComponent
project
 
Constructor Summary
UnitTestsGen()
          Creates a new UnitTestsGen instance.
 
Method Summary
 org.apache.tools.ant.types.Path createClasspath()
          Method createClasspath sets internal parameter to given Classpath attribute value.
 void execute()
          execute method performs ANT task work here.
 void init()
          init method sets start parameters for unit tests generator such as 'imput' and 'output' dirs to default values.
 void setClasspath(java.lang.String clp)
          Method setClasspath sets internal parameter to given Classpath attribute value.
 void setCodegen(java.lang.String codeg)
          Method setCodegen sets internal parameter to given Codegen attribute value.
 void setConfig(java.lang.String cfg)
          Method setConfig sets internal parameter to given Config attribute value.
 void setContent(java.lang.String cont)
          Method setContent sets internal parameter to given Content attribute value.
 void setDebug(java.lang.String debug)
          Method setDebug sets internal parameter to given Debug attribute value.
 void setDirwalker(java.lang.String dirw)
          Method setDirwalker sets internal parameter to given Dirwalker attribute value.
 void setHelp(java.lang.String help)
          Method setHelp sets internal parameter to given Help attribute value.
 void setImplementations(java.lang.String imp)
          Method setImplementations sets internal parameter to given Implementations attribute value.
 void setInput(java.lang.String indir)
          Method setInput sets internal parameter to given Input attribute value.
 void setLogimpl(java.lang.String log)
          Method setLogimpl sets internal parameter to given Logimpl attribute value.
 void setMask(java.lang.String mask)
          Method setMask sets internal parameter to given Mask attribute value.
 void setOutput(java.lang.String outdir)
          Method setOutput sets internal parameter to given Output attribute value.
 void setOverwrite(java.lang.String over)
          Method setOverwrite sets internal parameter to given Overwrite attribute value.
 void setParser(java.lang.String par)
          Method setParser sets internal parameter to given Parser attribute value.
 void setPattern(java.lang.String pat)
          Method setPattern sets internal parameter to given Pattern attribute value.
 void setPropertyimpl(java.lang.String pr)
          Method setPropertyimpl sets internal parameter to given Propertyimpl attribute value.
 void setQuiet(java.lang.String quiet)
          Method setQuiet sets internal parameter to given Quiet attribute value.
 void setRecursive(java.lang.String rec)
          Method setRecursive sets internal parameter to given Recursive attribute value.
 void setTarget(java.lang.String tar)
          Method setTarget sets internal parameter to given Target attribute value.
 void setTest(java.lang.String test)
          Method setTest sets internal parameter to given Test attribute value.
 void setUpdate(java.lang.String update)
          Method setUpdate sets internal parameter to given Update attribute value.
 void setVersion(java.lang.String ver)
          Method setVersion sets internal parameter to given Version attribute value.
 
Methods inherited from class org.apache.tools.ant.Task
getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, handleErrorOutput, handleOutput, log, log, maybeConfigure, perform, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName
 
Methods inherited from class org.apache.tools.ant.ProjectComponent
getProject, setProject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAR_CLASSPATH

protected static final java.lang.String PAR_CLASSPATH

PAR_CONFIG

protected static final java.lang.String PAR_CONFIG

PAR_FORCE

protected static final java.lang.String PAR_FORCE

PAR_INPUT

protected static final java.lang.String PAR_INPUT

PAR_PATTERN

protected static final java.lang.String PAR_PATTERN

PAR_OUTPUT

protected static final java.lang.String PAR_OUTPUT

PAR_CONTENT

protected static final java.lang.String PAR_CONTENT

PAR_MASK

protected static final java.lang.String PAR_MASK

PAR_RECURSIVE

protected static final java.lang.String PAR_RECURSIVE

PAR_TEST

protected static final java.lang.String PAR_TEST

PAR_QUIET

protected static final java.lang.String PAR_QUIET

PAR_DEBUG

protected static final java.lang.String PAR_DEBUG

PAR_HELP

protected static final java.lang.String PAR_HELP

PAR_VERSION

protected static final java.lang.String PAR_VERSION

PAR_IMPLEMENTATIONS

protected static final java.lang.String PAR_IMPLEMENTATIONS

PAR_TARGET

protected static final java.lang.String PAR_TARGET

PAR_LOGIMPL

protected static final java.lang.String PAR_LOGIMPL

PAR_PROPERTYIMPL

protected static final java.lang.String PAR_PROPERTYIMPL

PAR_DIRWALKER

protected static final java.lang.String PAR_DIRWALKER

PAR_PARSER

protected static final java.lang.String PAR_PARSER

PAR_CODEGEN

protected static final java.lang.String PAR_CODEGEN

PAR_UPDATE

protected static final java.lang.String PAR_UPDATE

version

public static final java.lang.String version

created

public static final java.lang.String created

dedicated

public static final java.lang.String dedicated

commandline

protected org.apache.tools.ant.types.CommandlineJava commandline

prop

protected java.util.Properties prop
Constructor Detail

UnitTestsGen

public UnitTestsGen()
Creates a new UnitTestsGen instance.
Method Detail

init

public void init()
init method sets start parameters for unit tests generator such as 'imput' and 'output' dirs to default values.
Overrides:
init in class org.apache.tools.ant.Task

execute

public void execute()
             throws org.apache.tools.ant.BuildException
execute method performs ANT task work here. in particular it calls GenerateTests class for performing standard processing actions. As parameter GenerateTests gets Properties structure containing all options set by user or as default values.
Overrides:
execute in class org.apache.tools.ant.Task
Throws:
org.apache.tools.ant.BuildException - if an error occurs

setConfig

public void setConfig(java.lang.String cfg)
Method setConfig sets internal parameter to given Config attribute value. config="gentest.cfg" This attribute is useful for command line running this tool. It is not very suitable for using it in build.xml file but it is possible. Additionaly you must note that content of this file should contain names of the command line form. See GenerateTests.java documentation for complete list of command line parameters or run this tool from command line as following:
 java -jar unittestsgen.jar --help
 
As you can see there are a lot of possible parameters for running this application. Although almost all defaults are suitable for most cases if you use different options it is very difficult to run tool every time with long list of command line parameters.
So you can put all parameters in file and use it each time you need. Default file name is 'gentest.cfg'. Content of this file should be of the following form:
 key = value
 
Where key is name of command line parameter you want to set. You can also use there comments: Lines which starts with '#' character. and blank lines.
If you use both config file and parameters in command line they will be processed in giving order. So last given parameter will be used during processing.
Parameters:
cfg - a String value

setClasspath

public void setClasspath(java.lang.String clp)
Method setClasspath sets internal parameter to given Classpath attribute value. classpath="your_class_files;libs.jar;junit.jar" Where your class file should be jar file containing your all compiled files including test classes or directory where lies all your compiled files.
It is the most important parameter because this is only parameter which is always required.
Since there is no way to set additional CLASSPATH during call: 'java -jar package.jar' here I provided parameter to set location of additional necessary classes. For example it is required to set location of jar file with compiled classes for sources found in '--input-directory' or set location of additional libraries for code parsers and generators like junit.jar. Please remember: if your project contains already some test classes you must put here jar file with compiled test classes. This allow analyze them and extend current test classes with additional test code for new created methods in source classes.
It supports also nested classpath element in common ANT use.
Parameters:
clp - a String value

createClasspath

public org.apache.tools.ant.types.Path createClasspath()
Method createClasspath sets internal parameter to given Classpath attribute value. classpath="your_class_files;libs.jar;junit.jar" Where your class file should be jar file containing your all compiled files including test classes or directory where lies all your compiled files.
It is the most important parameter because this is only parameter which is always required.
Since there is no way to set additional CLASSPATH during call: 'java -jar package.jar' here I provided parameter to set location of additional necessary classes. For example it is required to set location of jar file with compiled classes for sources found in '--input-directory' or set location of additional libraries for code parsers and generators like junit.jar. Please remember: if your project contains already some test classes you must put here jar file with compiled test classes. This allow analyze them and extend current test classes with additional test code for new created methods in source classes.
It supports also nested classpath element in common ANT use.
Returns:
a Path value

setInput

public void setInput(java.lang.String indir)
Method setInput sets internal parameter to given Input attribute value. input="src" Process files in given directory. But note that test classes will be searched in both input and output directories. But stored will always in output directory tree.
Parameters:
indir - a String value

setPattern

public void setPattern(java.lang.String pat)
Method setPattern sets internal parameter to given Pattern attribute value. pattern="*TestCase.java" Pattern for creating names of test classes. The most commonly used standards are to put 'Test' string at the beginning of source class name or at the end of class name.
Parameters:
pat - a String value

setOutput

public void setOutput(java.lang.String outdir)
Method setOutput sets internal parameter to given Output attribute value. output="test-src" Store test files in this given directory.
You can store your test files in different directories. There are two common used posibilities: store in the same directory tree where source classes are or create separated directory tree only for test classes. Please note that if you even put test classes in separated directory tree all related test classes will be always in the same packages where source classes are. It allows test classes to access to all non-private methods in source classes. It is good idea to test all non-private classes members.
Parameters:
outdir - a String value

setContent

public void setContent(java.lang.String cont)
Method setContent sets internal parameter to given Content attribute value. content="both|empty|code|comments" Set here what you want to have in new generated test methods.
Parameters:
cont - a String value

setOverwrite

public void setOverwrite(java.lang.String over)
Method setOverwrite sets internal parameter to given Overwrite attribute value. overwrite="false|true" Force overwriting existing files. And additionaly please note that new test classes will be always created in output directory even if they found in input directory.
Parameters:
over - a String value

setMask

public void setMask(java.lang.String mask)
Method setMask sets internal parameter to given Mask attribute value. mask="*.java" Process source files with given file mask. At the moment there is very limited support for this feature.
Parameters:
mask - a String value

setRecursive

public void setRecursive(java.lang.String rec)
Method setRecursive sets internal parameter to given Recursive attribute value. recursive="true|false" Recursive directories processing.
Parameters:
rec - a String value

setTest

public void setTest(java.lang.String test)
Method setTest sets internal parameter to given Test attribute value. test="false|true" Test only mode don't make any changes on disk.
Parameters:
test - a String value

setQuiet

public void setQuiet(java.lang.String quiet)
Method setQuiet sets internal parameter to given Quiet attribute value. quiet="true|false" Don't display any messages on screen.
Parameters:
quiet - a String value

setDebug

public void setDebug(java.lang.String debug)
Method setDebug sets internal parameter to given Debug attribute value. debug="false|true" Display additional debug messages.
Parameters:
debug - a String value

setHelp

public void setHelp(java.lang.String help)
Method setHelp sets internal parameter to given Help attribute value. help="false|true" Display help message and exit program. I don't expect it as useful feature for ANT task mode but I left it here for full compatibility with command line mode.
Parameters:
help - a String value

setVersion

public void setVersion(java.lang.String ver)
Method setVersion sets internal parameter to given Version attribute value. version="false|true" Display version info and exit program. I don't expect it as useful feature for ANT task mode but I left it here for full compatibility with command line mode.
Parameters:
ver - a String value

setImplementations

public void setImplementations(java.lang.String imp)
Method setImplementations sets internal parameter to given Implementations attribute value. implementations="testsgen.wttools" Package where are all modules implementations necessary for file processing.
Parameters:
imp - a String value

setTarget

public void setTarget(java.lang.String tar)
Method setTarget sets internal parameter to given Target attribute value. target="testsgen.wttools.CodeGenImpl" Name of class which generate code for unit test. It must be 'CodeGenIfc' implementation.
Parameters:
tar - a String value

setLogimpl

public void setLogimpl(java.lang.String log)
Method setLogimpl sets internal parameter to given Logimpl attribute value. logimpl="testsgen.wttools.LogImpl" Name of class which implements logging facility. It must be 'LogIfc' implementation.
Parameters:
log - a String value

setPropertyimpl

public void setPropertyimpl(java.lang.String pr)
Method setPropertyimpl sets internal parameter to given Propertyimpl attribute value. propertyimpl="testsgen.wttools.PropertyImpl" Name of class which implements property access facility. It must be 'PropertyIfc' implementation.
Parameters:
pr - a String value

setDirwalker

public void setDirwalker(java.lang.String dirw)
Method setDirwalker sets internal parameter to given Dirwalker attribute value. dirwalker="testsgen.wttools.DirWalkerImpl" Name of class which provides file names list to process. It must be 'DirWalkerIfc' implementation.
Parameters:
dirw - a String value

setParser

public void setParser(java.lang.String par)
Method setParser sets internal parameter to given Parser attribute value. parser="testsgen.wttools.SrcParserImpl" Name of class which parses source code. In other words it should provide list of preparsed structures which identifies source files. It must be 'SrcParserIfc' implementation.
Parameters:
par - a String value

setCodegen

public void setCodegen(java.lang.String codeg)
Method setCodegen sets internal parameter to given Codegen attribute value.
Parameters:
codeg - a String value

setUpdate

public void setUpdate(java.lang.String update)
Method setUpdate sets internal parameter to given Update attribute value. codegen="testsgen.wttools.CodeGenImpl" Name of class which generate code for unit test. It is synonym for --target-unit-test class.
Parameters:
update - a String value


Copyright GNU, wttools developers Team.