<?xml version="1.0" encoding="UTF-8"?>
<project name="DMDirc-tests" default="default" basedir=".">
    <description>Test-related tasks for DMDirc.</description>

    <property name="clover.jar" location="/home/dmdirc/clover/clover/lib/clover.jar"/>
    <taskdef resource="cloverlib.xml" classpath="${clover.jar}"/>
    <taskdef resource="cloverjunitlib.xml" classpath="${clover.jar}"/>
    <available property="clover.installed" classname="com.cenqua.clover.CloverInstr" />
    <available file="/usr/bin/xvfb-run" property="has.xvfb"/>

    <target name="with.clover" depends="clean" if="clover.installed"
            description="Set up Clover to instrument tests.">
        <clover-clean/>
        <clover-env/>
        <clover-setup>
            <fileset dir="src/com/" includes="**/*.java" />
            <fileset dir="test/com/">
                <include name="**/*.java" />
                <exclude name="**/harness/**/*.java" />
            </fileset>
            <methodContext name="private ctor" regexp="(.* )?private +[a-zA-Z0-9_$]+ *\( *\).*"/>
        </clover-setup>
    </target>

    <target name="clover.snapshot" depends="with.clover">
        <clover-snapshot/>
    </target>

    <target name="clover.xml" if="clover.installed"
            description="Generate a Clover XML report.">
        <mkdir dir="reports/clover" />
        <clover-report>
            <current outfile="reports/clover/clover.xml">
                <format type="xml" filter="private ctor"/>
            </current>
        </clover-report>
    </target>

    <target name="clover.html" if="clover.installed"
            description="Generate a Clover HTML report.">
        <mkdir dir="reports/clover" />
        <mkdir dir="reports/clover/history" />
        <clover-historypoint historydir="reports/clover/history" />
        <clover-report>
            <current outfile="reports/clover">
                <format type="html" filter="private ctor"/>
             </current>
             <historical outfile="reports/clover" historydir="reports/clover/history" />
        </clover-report>
    </target>

    <property name="test.reports" value="./junitreports" />

    <target name="-do-test-reports">
        <mkdir dir="${test.reports}" />
        <junitreport todir="${test.reports}">
            <fileset dir="${build.test.results.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report todir="${test.reports}" />
        </junitreport>
    </target>

    <target name="test" depends="check-use-xvfb, -test-xvfb, -test-no-xvfb"/>

    <target name="check-use-xvfb">
        <condition property="use.xvfb">
            <and>
                <not>
                    <equals arg1="${xvfb.disable}" arg2="true" />
                </not>
                <equals arg1="${has.xvfb}" arg2="true" />
            </and>
        </condition>
    </target>

    <target name="-test-xvfb" if="use.xvfb">
        <exec executable="/usr/bin/xvfb-run">
            <arg value="-a"/>
            <arg value="-s"/>
            <arg value="-screen 0 1280x1024x24"/>
            <arg value="-w"/>
            <arg value="5"/>
            <arg value="/usr/bin/ant"/>
            <arg value="with.clover"/>
            <arg value="do-real-test"/>
            <arg value="clover.snapshot"/>
        </exec>
    </target>

    <target name="-test-no-xvfb" unless="use.xvfb">
        <antcall target="with.clover" />
        <antcall target="do-real-test" />
        <antcall target="clover.snapshot" />
    </target>

    <target name="test-html" depends="with.clover,compile-test,test,clover.html,clover.snapshot"
            description="Run tests and generate a Clover HTML report."/>
    <target name="test-xml" depends="with.clover,compile-test,test,clover.xml,clover.snapshot"
            description="Run tests and generate a Clover XML report."/>
    <target name="test-both" depends="test-html,test-xml"
            description="Run tests and generate Clover XML and HTML reports."/>

</project>
