<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<head>
    <name>Using GroboCoverage With Ant</name>
    <doc-version>$Date: 2004/04/18 20:11:05 $</doc-version>
    <author>Matt Albrecht</author>
</head>
<body>

<section>Getting Started</section>

<p>
This document describes how to get up and running with
simple build files.  The <link name="v2_antdoc">ant task guide</link> describes
each Ant task in detail.  If your build environment is "complex",
then <link name="complex_setup">this document</link> should help your more
basic questions.  If you need more detailed information regarding the
operation of GroboCoverage, <link name="advanced_users">try this
document</link>.
</p>

<h3>Downloading the GroboCoverage Files</h3>
<p>
Before you get started, download the latest GroboCoverage zip file
(you can find it <a
href="https://sourceforge.net/project/showfiles.php?group_id=22594">
here</a>), which, at the time of this document, is called
<code>GroboUtils-5-codecoverage.zip</code>.
</p>
<p>
Inside are two JAR files, the "ant" and "runtime" code-coverage files.
The "ant" file contains the code-coverage files required for compiling
the coverage probes into your class files and all the dependent libraries,
while the "runtime" jar only contains those class files necessary to
run the recompiled class files.
</p>


<h2>Changing Your Ant Build File</h2>
<P>
To test with the coverage-enabled class files, you'll need to change your
build files.
</P>

<h3><a name="referenceNewTasks">Referencing the New Tasks</a></h3>
<p>
First off, before using any of the provided Ant tasks, you'll need to
tell Ant about them.  You have two options:
    <ol>
        <li>
        Copy <code><project />-<version />-ant.jar</code> to the
        <code>lib</code> directory of your Ant installation.  Then add
        the next lines to your build file before you reference the tasks:
        <pre>
&lt;taskdef resource=&quot;ant-grobocoverage.properties&quot;/&gt;
</pre>
        </li>
        <li>
        Keep <code><project />-<version />-ant.jar</code> in a separate
        location.  You now have to tell Ant explicitly where to find it
        (say in <code>/usr/share/java/lib</code>):
        <pre>
&lt;taskdef resource=&quot;ant-grobocoverage.properties&quot;&gt;
  &lt;classpath&gt;
    &lt;pathelement location=&quot;/usr/share/java/lib/<project />-<version />-ant.jar&quot;/&gt;
  &lt;/classpath&gt;
&lt;/taskdef&gt;
</pre>
        </li>
    </ol>
</p>
<p>
In order to use the <code>&lt;grobo-report&gt;</code> task, you need to
have a Xalan-2 compatible XSL processor in the classpath.  If one is not in
your Ant classpath, then it needs to be referenced in the
<code>&lt;taskdef&gt;</code> classpath.  Ant versions 1.5 and above come with
one by default.
</p>

<h3>Post-Compile Your Classes</h3>
<p>
Next, you'll need to "post-compile" your classes, after they've been
compiled.  This "post-compilation" stage inserts probes into your
class files to tell when lines of code have been encountered.
</p>
<p>
Since post-compilation makes different class files that will most probably
be slower than the original, the author recommends putting these into
a different directory, separate from the original class files.
</p>
<p>
So, let's say you have a compilation step that looks like this:
    <pre>
    &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" debug="on"&gt;
        &lt;classpath&gt;
            &lt;path refid="classpath.base" /&gt;
        &lt;/classpath&gt;
    &lt;/javac&gt;
</pre>
You would then add after it the <code>&lt;grobo-instrument&gt;</code>
task:
    <pre>
    &lt;grobo-instrument logdir="coverage/logs" logger="fast"
            destdir="coverage/classes"&gt;
        &lt;fileset dir="${classes.dir}"&gt;
            &lt;exclude name="dont/check/coverage/*.class" /&gt;
        &lt;/fileset&gt;
        
        &lt;measure type="linecount" /&gt;
        &lt;measure type="function" /&gt;
    &lt;/grobo-instrument&gt;
</pre>
which would put all the coverage-enabled class files into the
<code>coverage/classes</code> directory.  It will insert probes into the
classes to check for Java code-lines executed, and which Java functions
were called, but only for the classes that were post-compiled.  In this
case, none of the classes in the package "dont.check.coverage" were
post-compiled.  Also, this task puts all the data discovered for the
compilation into the <code>coverage/data</code> directory.  It also
sets up the logging properties in the output classes directory, describing
the directory to put the log files in, and to use the "fast" logger.
</p>
<p>
This task is described in <link name="v2_antdoc_grobo-instrument">the Ant task
documentation</link>.
</p>
<p>
Note: if you want the coverage report to include line numbers (showing a
trace back to the original Java source files), then you <i>must</i> enable
debugging during the <code>&lt;javac&gt;</code> task.  See the
<ant-link href="CoreTasks/javac.html">Ant task reference</ant-link> for
details.
</p>


<h3>Running Your Tests With the Post-Compiled Classes</h3>
<p>
If you want to run your tests with code coverage enabled, and you're using
<a href="http://junit.org">JUnit</a>, here's an easy method.
</p>
<p>
Let's say you run your tests with JUnit using the optional Ant
<ant-link href="OptionalTasks/junit.html">junit</ant-link> task
like this:
    <pre>
    &lt;junit printsummary="yes" fork="yes" dir="${test-output.dir}"&gt;
        &lt;classpath&gt;
            &lt;pathelement location="${classes.dir}" /&gt;
            &lt;pathelement location="${test-classes.dir}" /&gt;
            &lt;path refid="classpath.base" /&gt;
        &lt;/classpath&gt;
        &lt;formatter type="xml" usefile="yes" /&gt;
        &lt;batchtest todir="${test-output.dir}"&gt;
            &lt;fileset dir="${test-classes.dir}"&gt;
                &lt;include name="*Test.class" /&gt;
            &lt;/fileset&gt;
        &lt;/batchtest&gt;
    &lt;/junit&gt;
</pre>
Since the post-compilation step above only puts coverage-enabled
versions of the specified classes into the directory
<code>coverage/classes</code>, this class directory, then, doesn't contain
any special files or classes excluded from coverage.  So, we need to
tell the JUnit task to reference these covered classes, but also to
include the other files.  We do this by replacing the JUnit task's
classpath with this:
    <pre>
        &lt;classpath&gt;
            &lt;pathelement location="coverage/classes" /&gt;
            &lt;pathelement location="${classes.dir}" /&gt;
            &lt;pathelement location="${test-classes.dir}" /&gt;
            &lt;path refid="classpath.base" /&gt;
            &lt;pathelement location="<project />-<version />-runtime.jar" /&gt;
        &lt;/classpath&gt;
</pre>
We added the coverage-enabled classes before everything else (so they have
priority in the class loader), and we also added the runtime jar, as the
coverage-enabled classes require these to run.
</p>
<p>
The <code>&lt;grobo-instrument&gt;</code> task told the logger running these
tests will put all the coverage log information into the
<code>coverage/logs</code> directory, and to use the "fast" logger.
</p>


<h3>Generating a Report</h3>
<p>
Now that coverage numbers are being generated, we need to add in additional
Ant script to generate a coverage-number report.
</p>
<p>
If you're using the <code>&lt;junit&gt;</code> task, then you're probably
also using the <code>&lt;junitreport&gt;</code> task.  If not, check it out.
The ant documentation for it can be found
<ant-link href="OptionalTasks/junitreport.html">here</ant-link>
</p>
<p>
Whether you use the <code>&lt;junitreport&gt;</code> task or not,
we can add in the coverage report task after the <code>&lt;junit&gt;</code>
task, when the reports should be generated:
    <pre>
    &lt;grobo-report logdir="coverage/logs"&gt;
        &lt;simple destdir="coverage" removeempty="true" /&gt;
        &lt;source destdir="coverage/source-report" removeempty="true"
            srcdir="src" title="Summary Coverage Report of My Code" /&gt;
    &lt;/grobo-report&gt;
</pre>
The <code>&lt;grobo-report&gt;</code> task takes all the different
generated data from the data and log directories (as specified in the
<code>&lt;grobo-instrument&gt;</code> task above) and generates reports.
</p>
<p>
The embedded style tags define what kind of reports to generate.  The
"simple" tag generates one HTML file for each measure, and
the "source" tag generates a JavaDoc-style collection of web pages
that link the Java souce code to the coverage reports.
</p>
<p>
On large class-file systems, this report generator may consume large
amounts of memory.  If you see Ant generate an OutOfMemoryError, then
you can increase the amount of memory that the JVM uses with the
environment variable <code>ANT_OPTS</code>.  For instance, to increase
the memory usage to 128 MB, on a 1.4 JVM, you would use:
    <pre>
    &gt; set ANT_OPTS=-Xmx128M
</pre>
on Windows, and
    <pre>
    $ ANT_OPTS=-Xmx128M
    $ export ANT_OPTS
</pre>
on the Bourne shell.
</p>

</body>
</document>
