
<project default="test" xmlns:junit4="antlib:com.carrotsearch.junit4">
    <presetdef name="javac">
        <javac deprecation="false" debug="true" target="1.6" source="1.6" encoding="UTF-8" includeantruntime="false" />
    </presetdef>

    <property name="tmp.dir" location="${basedir}/target" />

    <!--
    This setup is a bit complex because it needs to work for integration tests and for distribution examples.
      -->
    <target name="classpath.resolve">
        <available property="libs.dir" value="${basedir}/target/lib" file="${basedir}/target/lib" type="dir" />
        <available property="libs.dir" value="${basedir}/../../lib" file="${basedir}/../../lib" type="dir" />

        <fail message="No classpath?">
            <condition>
                <not>
                    <isset property="libs.dir" />
                </not>
            </condition>
        </fail>
    </target>

    <!--
    Compile the sources from maven examples to avoid duplication.
      -->
    <target name="compile" depends="classpath.resolve">
        <mkdir dir="${tmp.dir}/classes" />
        <javac destdir="${tmp.dir}/classes">
            <src location="${basedir}/../maven/src/main/java" />
            <classpath>
                <fileset dir="${libs.dir}" includes="*.jar" />
            </classpath>
        </javac>
    </target>

    <!--
    Run the test.
      -->
    <target name="test" depends="classpath.resolve,compile">
        <!-- Make room for tests output and report. -->
        <delete dir="${tmp.dir}" failonerror="false" quiet="true">
            <include name="test-report*/**" />
        </delete>

        <!-- 
            Load JUnit4. This can be loaded as an antlib or into the default
            namespace using resource=com/carrotsearch/junit4/antlib.xml
        -->
        <taskdef uri="antlib:com.carrotsearch.junit4">
            <classpath>
                <fileset dir="${libs.dir}">
                    <include name="junit4-ant*.jar" />
                    <include name="junit-*.jar" />
                </fileset>
            </classpath>
        </taskdef>

        <!--
        Run junit4 tests.
        -->
        <junit4:junit4 jvm="" dir="${tmp.dir}" maxmemory="50m" haltonfailure="false" failureProperty="tests.failed" parallelism="1" shuffleOnSlave="true" leaveTemporary="false" seed="" printSummary="false">
            <classpath>
                <pathelement location="${tmp.dir}/classes" />
                <fileset dir="${libs.dir}" includes="*.jar" />
            </classpath>

            <!-- 
            Test selector. junit4 supports *.class files (and these are preferred over .java files).
            -->
            <fileset dir="${tmp.dir}/classes">
                <include name="**/Test*.class" />
                <exclude name="**/*$*" />
            </fileset>

            <!-- Nested elements much like in "standard" ANT 'junit' task. -->
            <jvmarg value="-ea" />
            <jvmarg value="-Dfoo=bar" />
            <sysproperty key="foo" value="bar" />
            <env key="env.foo" value="bar" />

            <assertions>
                <enable package="com.carrotsearch" />
            </assertions>

            <!-- Attached listeners -->
            <listeners>
                <!-- 
                    'verbose' config in which each suite and its test case is individually 
                    shown along with its output, exceptions, stack traces etc. 
                -->
                <junit4:report-text file="${tmp.dir}/test-report-txt/results-verbose.txt" 
                    showThrowable="true" 
                    showStackTraces="true" 
                    showOutput="always" 
                    showStatusOk="true" 
                    showStatusError="true" 
                    showStatusFailure="true" 
                    showStatusIgnored="true" 
                    showSuiteSummary="true" />

                <!-- 
                    'suite' maven-like output in which each test case is individually shown along
                    with its output. No exceptions or stacks.
                -->
                <junit4:report-text file="${tmp.dir}/test-report-txt/results-suite.txt" 
                    showThrowable="false" 
                    showStackTraces="false" 
                    showOutput="never" 
                    showStatusOk="false" 
                    showStatusError="false" 
                    showStatusFailure="false" 
                    showStatusIgnored="false" 
                    showSuiteSummary="true" />

                <!--
                    'quiet' output in which only errors are shown (without stacks).
                -->
                <junit4:report-text file="${tmp.dir}/test-report-txt/results-quiet.txt" 
                    showThrowable="true" 
                    showStackTraces="false" 
                    showOutput="onerror" 
                    showStatusOk="false" 
                    showStatusError="true"
                    showStatusFailure="true"
                    showStatusIgnored="false"
                    showSuiteSummary="false" />

                <!--
                    All tests with status information.
                -->
                <junit4:report-text file="${tmp.dir}/test-report-txt/results-testlist.txt" 
                    showThrowable="false" 
                    showStackTraces="false" 
                    showOutput="never" 
                    showStatusOk="true" 
                    showStatusError="true" 
                    showStatusFailure="true" 
                    showStatusIgnored="true" 
                    showSuiteSummary="false" />

                <!-- JSON report with HTML scaffolding. -->
                <junit4:report-json file="${tmp.dir}/test-report-json/results.html" />

                <!-- XML reports compatible with ant-report task. -->
                <junit4:report-ant-xml dir="${tmp.dir}/test-report-xml" />
            </listeners>
        </junit4:junit4>

        <!-- Run a "standard" JUnit report on the generated XML files. JSON report is recommended. -->
        <junitreport todir="${tmp.dir}/test-report-xml">
            <fileset dir="${tmp.dir}/test-report-xml">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${tmp.dir}/test-report-xml" />
        </junitreport>

        <echo>

Done. See reports under target/.

        </echo>
    </target>
</project>
