<?xml version="1.0"?>

<project
        xmlns:j="jelly:core"
        xmlns:ant="jelly:ant"
        xmlns:define="jelly:define"
        xmlns:maven="jelly:maven"
        xmlns:resources="resources"
        xmlns:doc="doc"
        xmlns:test="test"
        >
    
    <test:dependency-handle />
    
    
    <!-- Load external tasks -->
    
  

    <!-- ================================================================== -->
    <!-- P R E P A R E  F I L E  S Y S T E M                                -->
    <!-- ================================================================== -->

    <goal name="GroboCoverage:prepare-filesystem"
            description="Create the directory structure needed to post-compile">
        <ant:mkdir dir="${GroboCoverage.postcompile.classes}"/>
        <ant:mkdir dir="${GroboCoverage.postcompile.details}"/>
        <ant:mkdir dir="${GroboCoverage.runtime.logs}"/>
    </goal>

    <!-- ================================================================== -->
    <!-- P O S T - C O M P I L E                                            -->
    <!-- ================================================================== -->
    <goal name="GroboCoverage:post-compile"
            prereqs="java:compile, java:jar-resources
                GroboCoverage:prepare-filesystem">
        <j:choose>
            <j:when test="${sourcesPresent == 'true'}">
                <j:if test="${context.getVariable('maven.coverage.skip') != 'true'}">
                    <ant:taskdef name="GroboCoveragepostcompiler"
                        classname="net.sourceforge.groboutils.testing.codecoverage.v2.ant.CoveragePostCompilerTask"
                    />
                    <ant:GroboCoveragepostcompiler
                            outclassdir="${GroboCoverage.postcompile.classes}"
                            datadir="${GroboCoverage.postcompile.details}">
                        <u:tokenize var="modules"
                            delim=",">${GroboCoverage.postcompile.analysis}</u:tokenize>
                        <j:forEach items="${modules}" var="module"
                                indexVar="moduleNumber">
                            <ant:analysismodule name="${module}" />
                        </j:forEach>
                        <ant:fileset dir="${maven.build.dest}" />
                    </ant:GroboCoveragepostcompiler>
                    
                    
                    <!-- Create the coverage runtime class file -->
                    <ant:propertyfile
                            file="${GroboCoverage.postcompile.classes}/GroboCoverage.properties">
                        <ant:entry key="factory"
                            value="${GroboCoverage.runtime.factory}" />
                        <ant:entry key="logger.dir"
                            value="${GroboCoverage.runtime.logs}" />
                        <ant:entry key="channel-count"
                            value="5" />
                    </ant:propertyfile>
                    
                    
                    <!-- Augment the JUnit test classpath to include the
                    runtime files
                    <j:forEach var="dep" items="${pom.getPluginContext('GroboCoverage-plugin').dependencies}">
                        <j:if test="${dep.groupId == 'net.sourceforge.groboutils' and dep.id == 'GroboCodeCoverage-runtime'}">
               <j:set var="dummy" value="${thirdPartyJars.put(dep.getArtifactId(), dep)}"/>               
               <echo>${dep.id} ${dep.artifactId} ${dep.artifact} ${dep.version}</echo>
                    </j:if>
         </j:forEach>
                </j:if>
                    -->
            </j:when>
            <j:otherwise>
                <ant:echo>No java class files to post-compile.</ant:echo>
            </j:otherwise>
        </j:choose>
        
        
        <!--
        To allow for testing to run with or without coverage in the
        exact same way, we copy EVERYTHING into the classes directory, including
        resources.
        -->
        <ant:copy destdir="${GroboCoverage.postcompile.classes}"
                overwrite="false">
            <ant:fileset dir="${maven.build.dest}" />
        </ant:copy>
    </goal>
    
    

    <!-- ================================================================== -->
    <!-- R E P O R T   G O A L S                                            -->
    <!-- ================================================================== -->
    <goal name="GroboCoverage-plugin:register">
        <j:if test="${sourcesPresent == 'true'}">
            <doc:registerReport 
                name="GroboCoverage" 
                pluginName="GroboCoverage-report-plugin" 
                link="GroboCoverage/index"
                description="Unit tests code coverage report."/>
        </j:if>
    </goal>
    
    <goal name="GroboCoverage-plugin:deregister">
        <j:if test="${sourcesPresent == 'true'}">
            <doc:deregisterReport name="GroboCoverage"/>
        </j:if>
    </goal>
    
    <goal name="GroboCoverage-plugin:report">
        <attainGoal name="clover:html-report"/>
    </goal>


    <!-- ================================================================== -->
    <!-- C R E A T E  C O V E R A G E  X M L  R E P O R T                   -->
    <!-- ================================================================== -->
    <goal
        name="GroboCoverage-plugin:report"
        description="Generate a report from the coverage results">

        <j:if test="${context.getVariable('maven.coverage.skip') != 'true'}">
            <!-- Make sure that the report is generated whether the tests pass or
                not -->
            <j:set var="ignoreTestFailureOld"
                value="${maven.test.failure.ignore}"/>
            <j:set var="maven.test.failure.ignore" scope="parent" value="true"/>
            <attainGoal name="test:test"/>
            <j:set var="maven.test.failure.ignore" scope="parent"
                value="${ignoreTestFailureOld}"/>

            <!-- small work-around for test generation -->
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/0"/>
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/1"/>
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/2"/>
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/3"/>
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/4"/>
            <ant:mkdir dir="${GroboCoverage.runtime.logs}/5"/>

            <ant:taskdef name="GroboCoveragereport"
                classname="net.sourceforge.groboutils.testing.codecoverage.v2.ant.CoverageReportTask"
            />
            
            <!-- Consolidate the reports into a single XML file -->
            <ant:GroboCoveragereport
                datadir="${GroboCoverage.postcompile.details}"
                logdir="${GroboCoverage.runtime.logs}"
                outdir="${GroboCoverage.coverage.dir}" />
            <ant:style
                style="${plugin.resources}/${GroboCoverage.report.xsl}"
                basedir="${GroboCoverage.coverage.dir}"
                destdir="${maven.build.dir}"
                extension=".html"
                >
                <include name="*.xml" />
            </ant:style>
        </j:if>
    </goal>
    
    

    <!-- ================================================================== -->
    <!-- T E S T   G O A L S                                                -->
    <!-- ================================================================== -->
    <!--
    For testing, we simply setup the pre and post goals for the standard
    tests to run the post-compile process, then change the maven.build.dest
    directory to be the coverage directory instead.
    -->

    <goal
        name="GroboCoverage:pretest"
        description="Setup the project for coverage generation during tests">
        
        
        <ant:path id="GroboCoverage.runtime-path">
            <ant:pathelement path="${plugin.getDependencyPath('net.sourceforge.groboutils:GroboCodeCoverage-runtime')}" />
        </ant:path>
    
        <maven:addPath id="maven.dependency.classpath"
            refid="GroboCoverage.runtime-path"/>
        
        <j:set var="tmp" value="${GroboCoverage.postcompile.classes}" />
        ${pom.getPluginContext('maven-test-plugin').setVariable('maven.build.dest',tmp)}
    </goal>


    
     <preGoal name="test:test"
            prereqs="GroboCoverage:post-compile, GroboCoverage:pretest">
    </preGoal>
    
    
    
    <preGoal name="test:ui"
            prereqs="GroboCoverage:post-compile, GroboCoverage:pretest">
    </preGoal>
    
    
    
    <preGoal name="test:single"
            prereqs="GroboCoverage:post-compile, GroboCoverage:pretest">
    </preGoal>
    
    
    
    <preGoal name="test:match"
            prereqs="GroboCoverage:post-compile, GroboCoverage:pretest">
    </preGoal>
    
    
    
    <define:taglib uri="GroboCoverage">
        <define:tag name="dependency-handle">
        <!-- XXX Use this tag to allow this plugin to be loaded into another -->
        </define:tag>
    </define:taglib>
</project>


