<?xml version="1.0" encoding="UTF-8"?>
<project name="testcoverage-suite">

<!--
  Test coverage measurement for apisupport suite projects.

  At present, this only measure coverage for unit tests.  Functional
  tests, performance tests and other types of tests are not considered.

  NOTE: Implemented using Cobertura, follows general contract described in harness README.
-->
    <property name="testcoverage.suite.present" value="true"/>
    <property name="cobertura.dir" value="${harness.dir}/testcoverage/cobertura"/>

    <path id="cobertura.classpath">
        <fileset dir="${cobertura.dir}">
            <include name="cobertura-1.9.3.jar" />
            <include name="lib/**/*.jar" />
        </fileset>
    </path>

    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <target name="cobertura-init">
        <property name="cobertura.datafiles.merge.dir" value="${basedir}/build/test/cobertura-datafiles"/>
        <property name="cobertura.report.sources.dir" value="${basedir}/build/test/sources-for-report"/>
        <property name="testcoverage.report.dir" value="build/test/cobertura-report"/>
        <property name="cobertura.datafile" value="build/test/global-cobertura.ser"/>        
    </target>

    <target name="merge-coverage-reports" depends="-init,cobertura-init">
        <!-- Creates a report by merging data collected from tests previously run on modules -->

        <mkdir dir="build/test"/>
        <delete file="${cobertura.datafile}" failonerror="false"/>
        <delete dir="${testcoverage.report.dir}" failonerror="false"/>

        <delete dir="${cobertura.datafiles.merge.dir}" quiet="true" />
        <mkdir dir="${cobertura.datafiles.merge.dir}" />

        <delete dir="${cobertura.report.sources.dir}" quiet="true" />
        <mkdir dir="${cobertura.report.sources.dir}" />

        <!-- have each module copy its .ser file to the suite for merging -->
        <subant target="-copy-cobertura-datafile" buildpath="${modules.sorted}" inheritrefs="false" inheritall="false">
            <property name="suite.merge.dir" value="${cobertura.datafiles.merge.dir}"/>
        </subant>

        <cobertura-merge datafile="${cobertura.datafile}">
            <fileset dir="${cobertura.datafiles.merge.dir}">
                <include name="**/*" />
            </fileset>
        </cobertura-merge>

        <!-- have each module copy its sources to the suite for reporting -->
        <subant target="-copy-source-folder" buildpath="${modules.sorted}" inheritrefs="false" inheritall="false">
            <property name="suite.sources.dir" value="${cobertura.report.sources.dir}"/>
        </subant>

        <cobertura-report datafile="${cobertura.datafile}" destdir="${testcoverage.report.dir}" format="html">
            <fileset dir="${cobertura.report.sources.dir}">
                <include name="**/*.java" />
            </fileset>
        </cobertura-report> 
        <cobertura-report datafile="${cobertura.datafile}" destdir="${testcoverage.report.dir}" format="xml">
            <fileset dir="${cobertura.report.sources.dir}">
                <include name="**/*.java" />
            </fileset>
        </cobertura-report> 
    </target>

    <target name="generate-all-coverage-reports" depends="-init">
        <subant target="coverage-report" buildpath="${modules.sorted}" inheritrefs="false" inheritall="false"/>
    </target>
    
    <target name="coverage-report" depends="generate-all-coverage-reports,merge-coverage-reports"
        description="Generates test coverage report for entire suite">
    </target>
    
</project>
