<?xml version="1.0" encoding="UTF-8"?>
<project name="hsqldb.test.suite"
         default="run.test.suite"
         basedir="..">

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- B U I L D  T I M E S T A M P                                         -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <tstamp>
        <format property="_tmpstamp"
                pattern="yyyy/MM/dd-hh:mm:ss"
                locale="en"/>
    </tstamp>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- U S E R  B U I L D  P R O P E R T I E S  O V E R R I D E S           -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <property file="${basedir}/build/build.properties"/>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- D E F A U L T  B U I L D  P R O P E R T I E S                        -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <property name="test.suite.version"
              value="2.7.0"/>

    <property name="test.suite.title"
              value="HSQLDB Test Suite"/>

    <property name="test.suite.vendor"
              value="The HSQL Development Group"/>

    <property name="test.suite.implementation.title"
              value="SQL:2016 / JDBC 4.3 Conformance and Supporing Library Test Cases"/>

    <property name="test.suite.implementation.label"
              value="private-${_tmpstamp}"/>

    <property name="test.suite.implementation.vendor"
              value="${user.name}"/>

    <property name="junit.jar" 
              value="${basedir}/lib/junit.jar" />

    <property name="preprocessor.jar"
              value="${basedir}/lib/preprocessor.jar" />

    <property name="test.run.dir"
              value="${basedir}/testrun" />
    
    <property name="test.suite.jar"
              value="${basedir}/lib/testsuite.jar" />

    <property name="test.src.dir"
              value="${basedir}/test-src" />
   
    <property name="test.src.tmp.dir"
              value="${basedir}/tmp/testsrc" />

    <property name="test.classes.dir"
              value="${basedir}/test-classes" />

    <property name="test.results.dir"
              value="${basedir}/test-results" />

    <property name="build.debug"
              value="true" />

    <property name="build.optimize"
              value="false" />

    <target name="-set-jvmvars">
        <condition property="_crosswithoutbcp"> 
            <and>
                <isset property="ant.build.javac.target"/>
                <not> 
                    <isset property="javac.bootcp.override"/> 
                </not>
            </and> 
        </condition>
        
        <fail if="_crosswithoutbcp">
            If cross-compiling, you  must set property 'javac.bootcp.override'
        </fail>
        
        <condition property="ant.build.javac.source"
                   value="${ant.build.javac.target}">
            <isset property="ant.build.javac.target"/>
        </condition>
        
        <condition property="ant.java.iscjava08">
            <and>
                <available classname="java.sql.JDBCType" property="ant.java.iscjava08"/>
                <not>
                    <matches string="${ant.build.javac.target}"
                             pattern="^(?:1[.])?(1|2|3|4|5|6|7)$"/>
                </not>
            </and>
        </condition>
        
        <condition property="ant.java.iscjava11">
            <and>
                <available classname="java.sql.ShardingKey" property="ant.java.iscjava11"/>
                <not>
                    <matches string="${ant.build.javac.target}"
                             pattern="^(?:1[.])?(1|2|3|4|5|6|7|8)$"/>
                </not>
            </and>
        </condition>
        
        <condition property="jar-name-suffix"
                   value=""
                   else="-jdk8">
            <isset property="ant.java.iscjava11"/>
        </condition>
        
        <fail if="debug.targetjvm">
            specified compile target: ${ant.build.javac.target}
            derived version:  8?${ant.java.iscjava08}  11?${ant.java.iscjava11}
            src (${ant.build.javac.source})
            targ (${ant.build.javac.target})
            bcp (${javac.bootcp.override})
        </fail>
    </target>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- S H A R E D  I N I T I A L I Z A T I O N                             -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <target name="-init" depends="-set-jvmvars">

        <fail unless='ant.java.iscjava08'
              message="JDK must be 1.8 or greater"/>
        
        <property name="hsqldb.jar" 
                  value="${basedir}/lib/hsqldb${jar-name-suffix}.jar"/>
        

        <available file="${hsqldb.jar}"
                   type='file'
                   property="hsqldb.available"/>
        
        <fail unless="hsqldb.available" 
              message="hsqldb jar must reside at ${hsqldb.jar}"/>

        <available file="${preprocessor.jar}"
                   type='file'
                   property="preprocessor.available"/>

        <fail unless='preprocessor.available'
              message="'preprocessor.jar' must reside at ${preprocessor.jar}"/>
        
        <property name="sqltool.jar"
                  value="${basedir}/lib/sqltool${jar-name-suffix}.jar" />

        <available file="${sqltool.jar}"
                   type='file'
                   property="sqltool.available"/>

        <fail unless="sqltool.available"
              message="sqltool jar must reside at ${sqltool.jar}"/>

        <available file="${junit.jar}"
                   type="file"
                   property="junit.available"/>

        <fail unless='junit.available'
              message="junit jar must reside at ${junit.jar}"/>
        
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!-- B U I L D  C L A S S P A T H                                    -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

        <path id="test.suite.javac.classpath">
            <pathelement location="${hsqldb.jar}"/>
            <pathelement location="${preprocessor.jar}"/>
            <pathelement location="${sqltool.jar}"/>
            <pathelement location="${junit.jar}" />
        </path>
    </target>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- M A K E  T E S T  S U I T E                                          -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <target name="make.test.suite"
            description="Builds test classes and creates test suite jar"
            depends="-init">
        <mkdir dir="${test.classes.dir}"/>
        <mkdir dir="${test.src.tmp.dir}/org/hsqldb/test"/>
        <copy todir="${test.src.tmp.dir}/org/hsqldb/test">
            <fileset dir="${basedir}/src/org/hsqldb/test">
                <include name="TestBase.java"/>
                <include name="TestConnectionSettings.java"/>
                <include name="TestDirectorySettings.java"/>
                <include name="TestUtil.java"/>
                <include name="Waiter.java"/>
                <include name="TestStoredProcedure.java"/>
            </fileset>
        </copy>

        <!-- Compile Classes -->
        <javac destdir="${test.classes.dir}"
               debug="${build.debug}"
               optimize="${build.optimize}"
               includeantruntime="false" 
        >
            <compilerarg value="-Xlint:-path"/>
            <src path="${test.src.dir}"/>
            <src path="${test.src.tmp.dir}"/>
            <classpath refid="test.suite.javac.classpath"/>
            <include name="**/*.java"/>
            <!-- @todo -->
            <exclude name="/org/hsqldb/testbase/jtm/*.java"/>
        </javac>

        <!-- Build Jar -->
        <jar jarfile="${test.suite.jar}"
             basedir="${test.classes.dir}">
            <manifest>
                <attribute name='Specification-Title'
                           value='${test.suite.title}'/>

                <attribute name='Specification-Version'
                           value='${test.suite.version}'/>

                <attribute name='Specification-Vendor'
                           value='${test.suite.vendor}'/>

                <attribute name='Implementation-Title'
                           value='${test.suite.implementation.title}'/>

                <attribute name='Implementation-Version'
                           value='${test.suite.implementation.label}'/>

                <attribute name='Implementation-Vendor'
                           value='${test.suite.implementation.vendor}'/>
            </manifest>
            <fileset dir="test-src" includes="org/hsqldb/resources/*"/>
            <fileset dir="test-src" includes="org/hsqldb/jdbc/*.sql"/>
            <fileset dir="test-src" includes="org/hsqldb/jdbc/resources/sql/*"/>
            <fileset dir="test-src" includes="org/hsqldb/jdbc/resources/xml/*"/>
        </jar>
    </target>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- R U N  T E S T  S U I T E                                            -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <target name="run.test.suite"
            description="Runs the test suite and generates test result reports"
            depends="-init">

        <available file="${test.suite.jar}"
                   type='file'
                   property="test.suite.jar.available"/>

        <fail unless="test.suite.jar.available"
              message="The test suite jar must reside at ${test.suite.jar}.
Try running the make.test.suite target first."/>

        <delete dir="${test.results.dir}"/>

        <!-- FILE VERSJUS JAR RESOURCES AVOID ISSUES SUCH AS XML SYSTEM ID HANDLING -->
       
        <mkdir dir="${test.results.dir}/resources/org/hsqldb/util/preprocessor"/>

        <copy todir="${test.results.dir}/resources/org/hsqldb/util/preprocessor">
            <fileset dir="${test.src.dir}/org/hsqldb/util/preprocessor"
                     includes="*.exp"/>
        </copy>
        <copy todir="${test.results.dir}/resources/org/hsqldb/util/preprocessor">
            <fileset dir="${test.src.dir}/org/hsqldb/util/preprocessor"
                     includes="*.inc"/>
        </copy>
        <copy todir="${test.results.dir}/resources/org/hsqldb/util/preprocessor">
            <fileset dir="${test.src.dir}/org/hsqldb/util/preprocessor"
                     includes="*.src"/>
        </copy>

        <mkdir dir="${test.results.dir}/resources/org/hsqldb/jdbc"/>

        <copy todir="${test.results.dir}/resources/org/hsqldb/jdbc">
            <fileset dir="${test.src.dir}/org/hsqldb/jdbc"
                     includes="*.sql"/>
        </copy>

        <mkdir dir="${test.results.dir}/resources/org/hsqldb/jdbc/resources"/>
        <mkdir dir="${test.results.dir}/resources/org/hsqldb/jdbc/resources/xml"/>

        <copy todir="${test.results.dir}/resources/org/hsqldb/jdbc/resources/xml">
            <fileset dir="${test.src.dir}/org/hsqldb/jdbc/resources/xml"
                     includes="*.*"/>
        </copy>

        <mkdir dir="${test.results.dir}/resources/org/hsqldb/jdbc/resources/sql"/>

        <copy todir="${test.results.dir}/resources/org/hsqldb/jdbc/resources/sql">
            <fileset dir="${test.src.dir}/org/hsqldb/jdbc/resources/sql"
                     includes="*.*"/>
        </copy>

        <mkdir dir="${test.results.dir}/resources/org/hsqldb/resources"/>

        <copy todir="${test.results.dir}/resources/org/hsqldb/resources">
            <fileset dir="${test.src.dir}/org/hsqldb/resources" includes="*.*"/>
        </copy>

        <echo>Starting JUnit Tests...</echo>

        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!-- U S E R  T E S T  P R O P E R T I E S                                -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!-- Now using BundleHandler facility as primary properties loader, but   -->
        <!-- System properties still work (e.g. on command line -Dfoo=xyz or      -->
        <!-- using Ant to load properties files) and now take  precedence over    -->
        <!-- bundle handler resources.                                            -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!-- In this way, the bulk of the standard properties (that will probably -->
        <!-- never need to be overriden) stay under the test source folder, hence -->
        <!-- only specific property overrides need to be placed in the build      -->
        <!-- folder properties files or on the command line as -Dx=y directives.  -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!-- The only (minor) disadvantage of this new scheme is that the bulk of -->
        <!-- the test properties will no longer show up in the test report output -->
        <!-- as environment settings                                              -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <property file="${basedir}/build/test.properties"/>
        <property file="${basedir}/build/test.dbmd.convert.properties"/>

        <!-- Execute JUNIT Tests -->
        <junit showoutput="true"
               fork="false"
               failureproperty="tests.failed"
               errorproperty="tests.failed" >
            <batchtest todir="${test.results.dir}">
                <fileset dir="${test.src.dir}"
                         includes="**/*Test.java">
                    <exclude name="org/hsqldb/testbase/**"/>
                    <exclude name="org/hsqldb/jdbc/testbase/**"/>
                </fileset>
            </batchtest>
            <classpath>
                <!-- Placed first to avoid issues with JAR resource URL handling -->
                <!-- e.g. relative system id XML DTD, XSD and entites -->
                <pathelement location="${test.results.dir}/resources"/>
                <pathelement location="${test.run.dir}"/>
                <path path="${hsqldb.jar}"/>
                <path path="${preprocessor.jar}"/>
                <path path="${sqltool.jar}"/>
                <path path="${junit.jar}"/>
                <path path="${test.suite.jar}"/>
            </classpath>
            <syspropertyset id="test.suite.system.properties">
                <propertyref prefix="hsqldb.test.suite."/>
            </syspropertyset>
            <formatter type="xml"/>
        </junit>

        <echo>Generating Test Reports...</echo>

        <mkdir dir="${test.results.dir}/reports"/>
        <mkdir dir="${test.results.dir}/reports/html"/>

        <!-- Generate Test Reports -->
        <junitreport todir="${test.results.dir}/reports">
            <fileset dir="${test.results.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames"
                    todir="${test.results.dir}/reports/html"/>
        </junitreport>
    </target>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- C L E A N  T E S T  S U I T E                                        -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <target name="clean.test.suite"
            description="Deletes compiled classes, test results and suite jar">
        <delete dir="${test.classes.dir}" />
        <delete dir="${test.results.dir}" />
        <delete dir="${test.src.tmp.dir}" />
        <delete file="${test.suite.jar}" />
    </target>
</project>
