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

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

  Unit and functional tests supported.

  Implemented using Cobertura, follows general contract described in harness README.
-->
    <property name="testcoverage.present" value="true"/>
    <condition property="cobertura.dir" value="${harness.dir}/testcoverage/cobertura" else="${netbeans.dest.dir}/harness/testcoverage/cobertura">
        <isset property="harness.dir"/>
    </condition>
    <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"/>

    <macrodef name="cobertura-init">
        <attribute name="test.type"/>
        <sequential>
            <property name="build.test.cobertura.classes.dir" value="build/cobertura-instrumented-classes"/>
            <property name="testcoverage.@{test.type}.report.dir" value="${basedir}/build/test/@{test.type}/cobertura-report"/>
            <property name="cobertura.@{test.type}.datafile" value="${basedir}/build/test/@{test.type}/cobertura.ser"/>
            <property name="continue.after.failing.tests" value="true" />
            <property name="testcoverage.@{test.type}.backup.dir" value="${basedir}/build/test/@{test.type}/modulebackup" />

            <path id="cobertura.test.cp">
                <pathelement location="${build.test.cobertura.classes.dir}" />
                <pathelement location="${cobertura.dir}/cobertura-1.9.3.jar" />
                <pathelement location="${test.@{test.type}.lib.cp}"/>
                <path refid="test.@{test.type}.run.cp"/>
                <pathelement location="build/classes" />
            </path>
        </sequential>
    </macrodef>

    <target name="cobertura-init" depends="build-init,init,test-init">
        <cobertura-init test.type="unit" />
    </target>

    <target name="cobertura-check-environment">
        <condition property="nbjdk.active.print" value="${nbjdk.active}" else="UNSET">
            <isset property="nbjdk.active"/>
        </condition>
        <condition property="nbjdk.home.print" value="${nbjdk.home}" else="UNSET">
            <isset property="nbjdk.home"/>
        </condition>
        <condition property="nbjdk.home.defaulted.print" value="YES" else="NO">
            <isset property="nbjdk.home.defaulted"/>
        </condition>
        <fail if="nbjdk.valid">
Test coverage tasks can be currently run only with default java platform, that is
with property nbjdk.active unset or set to 'default' and with property nbjdk.home
unset.

For NB.org module, specify properties:
nbjdk.home.defaulted=true
permit.jdk6.builds=true
when running the target.

Current java platform setup is:
  nbjdk.active:         ${nbjdk.active.print}
  nbjdk.home:           ${nbjdk.home.print}
  nbjdk.home defaulted: ${nbjdk.home.defaulted.print}
        </fail>
    </target>

    <macrodef name="coverage-report">
        <attribute name="test.type" />
        <sequential>
            <property name="coverage.report.format" value="html" />
            <delete dir="${testcoverage.@{test.type}.report.dir}" failonerror="false"/>
            <cobertura-report format="${coverage.report.format}" datafile="${cobertura.@{test.type}.datafile}" srcdir="${src.dir}" destdir="${testcoverage.@{test.type}.report.dir}"/>
        </sequential>
    </macrodef>

    <target name="-copy-cobertura-datafile" depends="cobertura-init">
        <!-- rename using code-name-base so the files will be unique in the suite -->
        <copy file="${cobertura.unit.datafile}" tofile="${suite.merge.dir}/${code.name.base.dashes}.ser" failonerror="false" />
    </target>


    <target name="-copy-source-folder" depends="cobertura-init">
        <!--
            note that this does not inherently handle if there are multiple source
            folders; it might be possible to handle this by overriding in the
            module's build.xml.
        -->
        <property name="cobertura.src.dir" value="${suite.sources.dir}" />
        <mkdir dir="${cobertura.src.dir}" />
        <copy todir="${cobertura.src.dir}/">
            <fileset dir="${src.dir}" includes="**/*.java"/>
        </copy>
    </target>

    <target name="cobertura-instrument" depends="cobertura-init, test-build, cobertura-check-environment">
        <delete dir="${build.test.cobertura.classes.dir}" failonerror="false"/>
        <delete file="${cobertura.unit.datafile}" failonerror="false"/>
        <cobertura-instrument todir="${build.test.cobertura.classes.dir}" datafile="${cobertura.unit.datafile}">
            <fileset dir="${build.classes.dir}" includes="**/*.class" excludes="${test.coverage.exclude.files}"/>
        </cobertura-instrument>
    </target>

    <target name="test-with-cobertura" depends="cobertura-init,init,test-init,netbeans,test-build">
        <pathconvert pathsep="${line.separator} " property="echo.cobertura.test.cp" refid="cobertura.test.cp"/>
        <mkdir dir="${build.test.unit.results.dir}"/>

        <junit showoutput="true" fork="true" failureproperty="tests.failed" errorproperty="tests.failed" filtertrace="${test.filter.trace}" tempdir="${build.test.unit.results.dir}" timeout="${test.timeout}">
            <batchtest todir="${build.test.unit.results.dir}">
                <fileset dir="${build.test.unit.classes.dir}" erroronmissingdir="false">
                    <include name="**/*Test.class"/>
                </fileset>

            </batchtest>
            <!--
               This somewhat duplicates the normal JUnit testing target, but the
               cobertura.test.cp  must come first in the testing classpath for
               coverage to work properly and there is no other good way to inject it.
            -->
            <classpath refid="cobertura.test.cp"/>
            <syspropertyset refid="test.unit.properties"/>
            <jvmarg line="${test.run.args}"/>
            <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.unit.datafile}" />
            <formatter type="brief" usefile="false"/>
            <formatter type="xml"/>
        </junit>
        <fail if="tests.failed" unless="continue.after.failing.tests">Some tests failed; see details above.</fail>
    </target>

    <target name="test-coverage" depends="cobertura-init, cobertura-instrument, test-with-cobertura"/>

    <target name="coverage-report" depends="netbeans, test-coverage" description="Creates unit test coverage report for this module">
      <coverage-report test.type="unit" />
    </target>

     <!-- Support for functional tests
     Those tasks in some ways duplicate those above, but the code coverage setting for qa-functional
     tests is different from unit tests.
     -->

     <target name="cobertura-init-qa-functional" depends="build-init,init,test-init,cobertura-check-environment">
        <cobertura-init test.type="qa-functional" />
        <delete file="${cobertura.qa-functional.datafile}" failonerror="false"/>
        <delete dir="${testcoverage.qa-functional.backup.dir}" failonerror="false" />
        <mkdir dir="${testcoverage.qa-functional.backup.dir}" />
     </target>

    <!-- In case test.coverage.qa-functional.coveredmodules property is not set backup and instrument
        just the module we're running code coverage from -->
    <target name="cobertura-instrument-default" unless="test.coverage.qa-functional.coveredmodules">
        <echo message="Instrumenting module jar." />
        <copy file="${cluster}/${module.jar}" todir="${testcoverage.qa-functional.backup.dir}/modules" />
        <cobertura-instrument datafile="${cobertura.qa-functional.datafile}" >
            <fileset dir="${cluster}" includes="${module.jar}" />
        </cobertura-instrument>
    </target>

    <target name="cobertura-instrument-specified" if="test.coverage.qa-functional.coveredmodules">
        <echo message="Instrumenting specified files: ${test.coverage.qa-functional.coveredmodules}" />
        <copy todir="${testcoverage.qa-functional.backup.dir}" verbose="true">
            <fileset dir="${netbeans.dest.dir}" includes="${test.coverage.qa-functional.coveredmodules}" />
        </copy>
        <cobertura-instrument datafile="${cobertura.qa-functional.datafile}">
            <fileset dir="${netbeans.dest.dir}" includes="${test.coverage.qa-functional.coveredmodules}" />
        </cobertura-instrument>
    </target>

    <target name="testcoverage-restore-default" unless="test.coverage.qa-functional.coveredmodules">
        <delete file="${cluster}/${module.jar}" />
        <copy file="${testcoverage.qa-functional.backup.dir}/${module.jar}" todir="${cluster}" />
    </target>

    <target name="testcoverage-restore-specified" if="test.coverage.qa-functional.coveredmodules">
        <delete verbose="true">
            <fileset dir="${netbeans.dest.dir}" includes="${test.coverage.qa-functional.coveredmodules}" />
        </delete>
        <copy todir="${netbeans.dest.dir}" verbose="true" >
            <fileset dir="${testcoverage.qa-functional.backup.dir}" includes="${test.coverage.qa-functional.coveredmodules}" />
        </copy>
    </target>

    <target name="cobertura-instrument-qa-functional" depends="cobertura-init-qa-functional, test-build, cobertura-instrument-default,cobertura-instrument-specified"/>
    <target name="test-coverage-restore" depends="cobertura-init-qa-functional, test-build, testcoverage-restore-default,testcoverage-restore-specified" />

    <target name="test-with-cobertura-qa-functional" depends="cobertura-init-qa-functional,init,test-init,netbeans,test-build">
        <pathconvert pathsep="${line.separator} " property="echo.cobertura.test.cp" refid="cobertura.test.cp"/>
        <mkdir dir="${build.test.qa-functional.results.dir}"/>
        <property name="code.coverage.classpath" value="${cobertura.dir}" />
        <propertyset id="test.qa-functional.coverage.properties">
            <propertyset refid="test.qa-functional.properties"/>
            <propertyref name="code.coverage.classpath" />
        </propertyset>
        <junit showoutput="true" fork="true" failureproperty="tests.failed" errorproperty="tests.failed" filtertrace="${test.filter.trace}" tempdir="${build.test.qa-functional.results.dir}" timeout="${test.timeout}">
            <batchtest todir="${build.test.qa-functional.results.dir}">
                <fileset dir="${build.test.qa-functional.classes.dir}" erroronmissingdir="false">
                    <include name="**/*.class"/>
                </fileset>

            </batchtest>
            <!--
               This somewhat duplicates the normal JUnit testing target, but the
               cobertura.test.cp  must come first in the testing classpath for
               coverage to work properly and there is no other good way to inject it.
            -->
            <classpath refid="cobertura.test.cp"/>

            <syspropertyset refid="test.qa-functional.coverage.properties"/>

            <jvmarg line="${test.run.args}"/>
            <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.qa-functional.datafile}" />
            <formatter type="brief" usefile="false"/>
            <formatter type="xml"/>
        </junit>
        <fail if="tests.failed" unless="continue.after.failing.tests">Some tests failed; see details above.</fail>
    </target>

    <target name="test-coverage-qa-functional" depends="netbeans,cobertura-init-qa-functional,cobertura-instrument-qa-functional,test-with-cobertura-qa-functional,test-coverage-restore"/>

    <target name="coverage-report-qa-functional" depends="netbeans, test-coverage-qa-functional" description="Creates qa-functional test coverage report for this module">
        <coverage-report test.type="qa-functional" />
    </target>

</project>
