XMLResultAggregator (Apache Ant API)
Description: declaration: package: org.apache.tools.ant.taskdefs.optional.junit, class: XMLResultAggregator

# Class XMLResultAggregator

java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator

All Implemented Interfaces:
  `Cloneable, XMLConstants`

public class XMLResultAggregator extends Task implements XMLConstants
Aggregates all <junit> XML formatter testsuite data under a specific directory and transforms the results via XSLT. It is not particularly clean but should be helpful while I am thinking about another technique.

The main problem is due to the fact that a JVM can be forked for a testcase thus making it impossible to aggregate all testcases since the listener is (obviously) in the forked JVM. A solution could be to write a TestListener that will receive events from the TestRunner via sockets. This is IMHO the simplest way to do it to avoid this file hacking thing.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `DEFAULT_DIR`
  The default directory: `.`.
  `static final String`
  `DEFAULT_FILENAME`
  the default file name: `TESTS-TestSuites.xml`
  `protected Vector<FileSet>`
  `filesets`
  the list of all filesets, that should contains the xml to aggregate
  `protected int`
  `generatedId`
  the current generated id
  `protected File`
  `toDir`
  the directory to write the file to
  `protected String`
  `toFile`
  the name of the result file
  `protected Vector<AggregateTransformer>`
  `transformers`

  ### Fields inherited from class Task

  `target, taskName, taskType, wrapper`
  Modifier and Type
  Field
  Description
  `protected Target`
  `target`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskName`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskType`
  Deprecated.
  since 1.6.x.
  `protected RuntimeConfigurable`
  `wrapper`
  Deprecated.
  since 1.6.x.

  ### Fields inherited from class ProjectComponent

  `description, location, project`
  Modifier and Type
  Field
  Description
  `protected String`
  `description`
  Deprecated.
  since 1.6.x.
  `protected Location`
  `location`
  Deprecated.
  since 1.6.x.
  `protected Project`
  `project`
  Deprecated.
  since 1.6.x.

  ### Fields inherited from interface XMLConstants

  `ATTR_CLASSNAME, ATTR_ERRORS, ATTR_FAILURES, ATTR_ID, ATTR_MESSAGE, ATTR_NAME, ATTR_PACKAGE, ATTR_SKIPPED, ATTR_TESTS, ATTR_TIME, ATTR_TYPE, ATTR_VALUE, ERROR, FAILURE, HOSTNAME, PROPERTIES, PROPERTY, SYSTEM_ERR, SYSTEM_OUT, TESTCASE, TESTSUITE, TESTSUITES, TIMESTAMP`
  Modifier and Type
  Field
  Description
  `static final String`
  `ATTR_CLASSNAME`
  classname attribute for testcase elements
  `static final String`
  `ATTR_ERRORS`
  errors attribute for testsuite elements
  `static final String`
  `ATTR_FAILURES`
  failures attribute for testsuite elements
  `static final String`
  `ATTR_ID`
  id attribute
  `static final String`
  `ATTR_MESSAGE`
  message attribute for failure elements
  `static final String`
  `ATTR_NAME`
  name attribute for property, testcase and testsuite elements
  `static final String`
  `ATTR_PACKAGE`
  package attribute for the aggregate document
  `static final String`
  `ATTR_SKIPPED`
  `static final String`
  `ATTR_TESTS`
  tests attribute for testsuite elements
  `static final String`
  `ATTR_TIME`
  time attribute for testcase and testsuite elements
  `static final String`
  `ATTR_TYPE`
  type attribute for failure and error elements
  `static final String`
  `ATTR_VALUE`
  value attribute for property elements
  `static final String`
  `ERROR`
  the error element
  `static final String`
  `FAILURE`
  the failure element
  `static final String`
  `HOSTNAME`
  name of host running the tests
  `static final String`
  `PROPERTIES`
  the properties element
  `static final String`
  `PROPERTY`
  the property element
  `static final String`
  `SYSTEM_ERR`
  the system-err element
  `static final String`
  `SYSTEM_OUT`
  the system-out element
  `static final String`
  `TESTCASE`
  the testcase element
  `static final String`
  `TESTSUITE`
  the testsuite element
  `static final String`
  `TESTSUITES`
  the testsuites element for the aggregate document
  `static final String`
  `TIMESTAMP`
  timestamp of test cases

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `XMLResultAggregator()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addFileSet(FileSet fs)`
  Add a new fileset containing the XML results to aggregate
  `protected void`
  `addTestSuite(Element root, Element testsuite)`
  Add a new testsuite node to the document.
  `protected Element`
  `createDocument()`
  Create a DOM tree.
  `AggregateTransformer`
  `createReport()`
  Generate a report based on the document created by the merge.
  `void`
  `execute()`
  Aggregate all testsuites into a single document and write it to the specified directory and file.
  `File`
  `getDestinationFile()`
  Get the full destination file where to write the result.
  `protected File[]`
  `getFiles()`
  Get all `.xml` files in the fileset.
  `void`
  `setTodir(File value)`
  Set the destination directory where the results should be written.
  `void`
  `setTofile(String value)`
  Set the name of the aggregated results file.
  `protected void`
  `writeDOMTree(Document doc, File file)`
  Write the DOM tree to a file.

  ### Methods inherited from class Task

  `bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType`
  Modifier and Type
  Method
  Description
  `final void`
  `bindToOwner(Task owner)`
  Bind a task to another; use this when configuring a newly created task to do work on behalf of another.
  `Target`
  `getOwningTarget()`
  Returns the container target of this task.
  `RuntimeConfigurable`
  `getRuntimeConfigurableWrapper()`
  Returns the wrapper used for runtime configuration.
  `String`
  `getTaskName()`
  Returns the name to use in logging messages.
  `String`
  `getTaskType()`
  Return the type of task.
  `protected RuntimeConfigurable`
  `getWrapper()`
  Return the runtime configurable structure for this task.
  `protected void`
  `handleErrorFlush(String output)`
  Handles an error line by logging it with the WARN priority.
  `protected void`
  `handleErrorOutput(String output)`
  Handles an error output by logging it with the WARN priority.
  `protected void`
  `handleFlush(String output)`
  Handles output by logging it with the INFO priority.
  `protected int`
  `handleInput(byte[] buffer, int offset, int length)`
  Handle an input request by this task.
  `protected void`
  `handleOutput(String output)`
  Handles output by logging it with the INFO priority.
  `void`
  `init()`
  Called by the project to let the task initialize properly.
  `protected final boolean`
  `isInvalid()`
  Has this task been marked invalid?
  `void`
  `log(String msg)`
  Logs a message with the default (INFO) priority.
  `void`
  `log(String msg, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(String msg, Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `maybeConfigure()`
  Configures this task - if it hasn't been done already.
  `final void`
  `perform()`
  Performs this task if it's still valid, or gets a replacement version and performs that otherwise.
  `void`
  `reconfigure()`
  Force the task to be reconfigured from its RuntimeConfigurable.
  `void`
  `setOwningTarget(Target target)`
  Sets the target container of this task.
  `void`
  `setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper)`
  Sets the wrapper to be used for runtime configuration.
  `void`
  `setTaskName(String name)`
  Sets the name to use in logging messages.
  `void`
  `setTaskType(String type)`
  Sets the name with which the task has been invoked.

  ### Methods inherited from class ProjectComponent

  `clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject`
  Modifier and Type
  Method
  Description
  `Object`
  `clone()`
  `String`
  `getDescription()`
  Returns the description of the current action.
  `Location`
  `getLocation()`
  Returns the file/location where this task was defined.
  `Project`
  `getProject()`
  Returns the project to which this component belongs.
  `void`
  `setDescription(String desc)`
  Sets a description of the current action.
  `void`
  `setLocation(Location location)`
  Sets the file/location where this task was defined.
  `void`
  `setProject(Project project)`
  Sets the project object of this component.

  ### Methods inherited from class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### filesets

    protected Vector<FileSet> filesets
    the list of all filesets, that should contains the xml to aggregate

  - ### toFile

    protected String toFile
    the name of the result file

  - ### toDir

    protected File toDir
    the directory to write the file to

  - ### transformers

    protected Vector<AggregateTransformer> transformers

  - ### DEFAULT_DIR

    public static final String DEFAULT_DIR
    The default directory: `.`. It is resolved from the project directory
    See Also:
      - Constant Field Values

  - ### DEFAULT_FILENAME

    public static final String DEFAULT_FILENAME
    the default file name: `TESTS-TestSuites.xml`
    See Also:
      - Constant Field Values

  - ### generatedId

    protected int generatedId
    the current generated id

- ## Constructor Details

  - ### XMLResultAggregator

    public XMLResultAggregator()

- ## Method Details

  - ### createReport

    public AggregateTransformer createReport()
    Generate a report based on the document created by the merge.
    Returns:
      the report

  - ### setTofile

    public void setTofile(String value)
    Set the name of the aggregated results file. It must be relative from the `todir` attribute. If not set it will use `DEFAULT_FILENAME`
    Parameters:
      `value` - the name of the file.
    See Also:
      - `setTodir(File)`

  - ### setTodir

    public void setTodir(File value)
    Set the destination directory where the results should be written. If not set if will use `DEFAULT_DIR`. When given a relative directory it will resolve it from the project directory.
    Parameters:
      `value` - the directory where to write the results, absolute or relative.

  - ### addFileSet

    public void addFileSet(FileSet fs)
    Add a new fileset containing the XML results to aggregate
    Parameters:
      `fs` - the new fileset of xml results.

  - ### execute

    public void execute() throws BuildException
    Aggregate all testsuites into a single document and write it to the specified directory and file.
    Overrides:
      `execute` in class `Task`
    Throws:
      `BuildException` - thrown if there is a serious error while writing the document.

  - ### getDestinationFile

    public File getDestinationFile()
    Get the full destination file where to write the result. It is made of the `todir` and `tofile` attributes.
    Returns:
      the destination file where should be written the result file.

  - ### getFiles

    protected File[] getFiles()
    Get all `.xml` files in the fileset.
    Returns:
      all files in the fileset that end with a '.xml'.

  - ### writeDOMTree

    protected void writeDOMTree(Document doc, File file) throws IOException
    Write the DOM tree to a file.
    Parameters:
      `doc` - the XML document to dump to disk.
      `file` - the filename to write the document to. Should obviously be a .xml file.
    Throws:
      `IOException` - thrown if there is an error while writing the content.

  - ### createDocument

    protected Element createDocument()
    Create a DOM tree. Has 'testsuites' as firstchild and aggregates all testsuite results that exists in the base directory.
    Returns:
      the root element of DOM tree that aggregates all testsuites.

  - ### addTestSuite

    protected void addTestSuite(Element root, Element testsuite)

    Add a new testsuite node to the document. The main difference is that it split the previous fully qualified name into a package and a name.

    For example: `org.apache.Whatever` will be split into `org.apache` and `Whatever`.

    Parameters:
      `root` - the root element to which the `testsuite` node should be appended.
      `testsuite` - the element to append to the given root. It will slightly modify the original node to change the name attribute and add a package one.