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

<!-- ======================================================================= -->
<!-- eXist build file : Run jUnit tests                                      -->
<!-- ======================================================================= -->
<project basedir="../.." default="test" name="jUnit tests">

    <description>jUnit tests for eXist</description>

    <target name="test"
            depends="test-prepare,test-recovery, test-local, test-jetty, test-concurrent, test-remote, test-wrapup"
            description="Run jUnit tests"/>

    <!-- Extra jUnit properties -->
    <property name="junit.reports.dat" value="${junit.reports}/junit/data"/>
    <property name="junit.reports.html" value="${junit.reports}/junit/html"/>

    <!-- Seperate target for creating folders -->
    <target name="test-prepare" depends="jar">
        <delete failonerror="false">
            <fileset dir="webapp/WEB-INF/data" includes="*.dbx,*.log"/>
        </delete>

        <delete dir="${junit.reports.dat}"/>
        <delete dir="${junit.reports.html}"/>

        <mkdir dir="${junit.reports}"/>
        <mkdir dir="${junit.reports.dat}"/>
        <mkdir dir="${junit.reports.html}"/>
    </target>

    <!-- Generate HTML reports -->
    <target name="test-wrapup">
        <echo message="-----------------------------"/>
        <echo message="Creating reports, please wait"/>
        <echo message="-----------------------------"/>
        <junitreport todir="${junit.reports.dat}">
            <fileset dir="${junit.reports.dat}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" styledir="${tools.ant}/etc" todir="${junit.reports.html}"/>
        </junitreport>
    </target>

    <!-- Test set 1 : local database instance -->
    <target name="test-local">
        <echo message="------------------------------------------"/>
        <echo message="Running tests on a local database instance"/>
        <echo message="------------------------------------------"/>

        <junit haltonfailure="false" printsummary="yes" showoutput="${junit.output}">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <classpath refid="classpath.jetty"/>
            <formatter type="plain"/>
            <formatter type="xml"/>
            <test fork="yes" name="org.exist.xmldb.test.LocalTests" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.xupdate.test.AllTests" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.xquery.test.AllTests" todir="${junit.reports.dat}"/>
            
            <!-- Execute all other tests except those that have to be called manually.   -->
            <batchtest fork="yes" todir="${junit.reports.dat}">
                <fileset dir="${src}">
                    <include name="**/test/*Test.java"/>
                    <exclude name="**/xmldb/test/*.java"/>
                    <exclude name="**/xupdate/test/*.java"/>
                    <exclude name="**/xquery/test/*.java"/>
                    <exclude name="**/xmlrpc/test/*.java"/>
                    <exclude name="**/http/test/*.java"/>
                    <exclude name="**/storage/test/*.java"/>
                    <exclude name="**/soap/test/*.java"/>
                    <exclude name="**/numbering/test/*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
	
    <!-- Test set 2 : jetty server -->
    <target name="test-jetty">
        <echo message="------------------------------------------"/>
        <echo message="Running tests on a jetty server"/>
        <echo message="------------------------------------------"/>

        <junit haltonfailure="false" printsummary="yes" showoutput="${junit.output}">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <classpath refid="classpath.jetty"/>
            <formatter type="plain"/>
            <formatter type="xml"/>

            <batchtest fork="yes" todir="${junit.reports.dat}">
                <fileset dir="${src}">
                    <include name="org/exist/soap/test/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>


    <!-- Test set 3 : concurrent tests -->
    <target name="test-concurrent">
        <delete failonerror="false">
            <fileset dir="webapp/WEB-INF/data" includes="*.dbx"/>
        </delete>
        <echo message="-----------------"/>
        <echo message="Concurrency tests"/>
        <echo message="-----------------"/>
        <junit haltonfailure="false" printsummary="on" showoutput="${junit.output}" fork="no">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <classpath refid="classpath.jetty"/>
            <formatter type="plain"/>
            <formatter type="xml"/>
            <test name="org.exist.xmldb.test.concurrent.AllTests" todir="${junit.reports.dat}"/>
        </junit>
    </target>

    <!-- Test set 4 : recovery tests -->
    <target name="test-recovery">
        <echo message="--------------"/>
        <echo message="Recovery tests"/>
        <echo message="--------------"/>
        <junit haltonfailure="false" printsummary="on" showoutput="${junit.output}" fork="no">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <formatter type="plain"/>
            <formatter type="xml"/>
            <test name="org.exist.storage.test.AllTests" todir="${junit.reports.dat}"/>
        </junit>
    </target>
	
    <!-- Helper target for starting server -->
    <target name="start-server">
        <echo message="---------------------------------------"/>
        <echo message="Starting stand-alone database server..."/>
        <echo message="---------------------------------------"/>
        <delete failonerror="false">
            <fileset dir="webapp/WEB-INF/data" includes="*.dbx"/>
        </delete>
        <java classname="org.exist.StandaloneServer">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <classpath refid="classpath.jetty"/>
            <!--arg value="-d"/-->
        </java>
        <echo>Server started.</echo>
    </target>

    <!-- Test set 5 : tests on remote server -->
    <target name="test-remote" depends="jar">
        <mkdir dir="${junit.reports}"/>
        <echo message="--------------------------------"/>
        <echo message="Running tests on remote database"/>
        <echo message="--------------------------------"/>
        <junit haltonfailure="false" printsummary="yes" showoutput="${junit.output}">
            <sysproperty key="exist.home" value="${basedir}" />
            <classpath refid="classpath.core"/>
            <classpath refid="classpath.jetty"/>
            <formatter type="plain"/>
            <formatter type="xml"/>
            <test fork="yes" name="org.exist.xmlrpc.test.XmlRpcTest" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.xmldb.test.RemoteTests" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.xquery.test.RemoteTests" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.http.test.RESTServiceTest" todir="${junit.reports.dat}"/>
            <test fork="yes" name="org.exist.storage.test.XIncludeSerializerTest" todir="${junit.reports.dat}"/>
        </junit>
    </target>

    <!-- Helper target for stopping server -->
    <!-- This target must be called after all tests because it shutsdown -->
    <!-- the current Jvm -->
    <target name="test-shutdown-server">
        <!-- Register Ant tasks for eXist -->
        <typedef resource="org/exist/ant/antlib.xml" uri="http://exist-db.org/ant">
            <classpath refid="classpath.core"/>
        </typedef>

        <echo message="Shutting down the server ..."/>
        <xmldb:shutdown xmlns:xmldb="http://exist-db.org/ant"
                uri="xmldb:exist://localhost:8088/xmlrpc/db"
                user="admin" password=""/>
        <sleep seconds="10"/>
    </target>

</project>