<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<project name="Torque" default="test" basedir=".">

  <!-- Allow any user specific values to override the defaults -->
  <property file="${user.home}/build.properties" />
  <!-- Allow user defaults for this project -->
  <property file="project.properties" />
  <!-- Set default values for the build -->
  <property file="default.properties" />

  <!-- Set values in test profile -->
  <property file="${torque.testProfile}"/>

  <!-- ============================================================== -->
  <!-- T O R Q U E  T E S T S                                         -->
  <!-- ============================================================== -->
  <!-- These are meant to be the comprehensive tests for Torque.      -->
  <!-- ============================================================== -->

  <target
    name="test"
    description="run the testbed">
    <antcall target="test-libs"/>
    <antcall target="test-pre-runtime"/>
    <antcall target="test-runtime"/>
    <antcall target="test-post-runtime"/>
  </target>

  <target
    name="test-classpath"
    description="run the testbed using the useClasspath property">
    <property name="useClasspath" value="true"/>
    <property name="templatePath" value="no_templatePath"/>
    <antcall target="test"/>
  </target>

  <target
    name="test-ojb"
    description="run the testbed using ojb">
    <antcall target="test-libs"/>
    <antcall target="test-ojb-prepare"/>
    <antcall target="test-ojb-generate"/>
    <antcall target="test-ojb-compile"/>
  </target>

  <!-- ============================================================== -->
  <!-- P R E   R U N T I M E   T E S T S                              -->
  <!-- ============================================================== -->
  <!-- These are the tests for the torque tests which are             -->
  <!-- primarily generative in nature.                                -->
  <!--                                                                -->
  <!-- The tests will be run from the distribution that is            -->
  <!-- produced by the build process. This is a test of Torque's      -->
  <!-- generative abilities.                                          -->
  <!--                                                                -->
  <!-- These are the tasks that we will be testing:                   -->
  <!--                                                                -->
  <!-- sql                                                            -->
  <!-- om                                                             -->
  <!--                                                                -->
  <!-- The sql is also another complete set of tests:                 -->
  <!-- we need to run the task for each of the supported databases    -->
  <!-- to make sure that generated SQL remains coherent across        -->
  <!-- changes to the templates.                                      -->
  <!-- ============================================================== -->

  <target
    name="test-pre-runtime">

    <antcall target="test-doc"/>
    <antcall target="test-create-db"/>
    <antcall target="test-sql"/>
    <antcall target="test-id-table-init-sql"/>
    <antcall target="test-insert-sql"/>
    <antcall target="test-om"/>
  </target>

  <!-- ============================================================== -->
  <!-- P O S T   R U N T I M E   T E S T S                            -->
  <!-- ============================================================== -->

  <target
    name="test-post-runtime">

    <antcall target="test-datadtd"/>
    <antcall target="test-datadump"/>
    <antcall target="test-datasql"/>
    <antcall target="test-jdbc"/>

    <!--
    <antcall target="test-sql-classpath"/>
    <antcall target="test-om-classpath"/>
    <antcall target="test-sql2xml"/>
    -->

  </target>

  <target
    name="test-sql">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="sql">
    </ant>
  </target>

  <target
    name="test-id-table-init-sql">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="id-table-init-sql">
    </ant>
  </target>

  <target
    name="test-doc">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="doc">
    </ant>
  </target>

  <target
    name="test-create-db">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="create-db">
    </ant>
  </target>

  <target
    name="test-insert-sql">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="insert-sql">
    </ant>
  </target>

  <target
    name="test-om">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="om">
    </ant>
    <!-- Compile the classes -->
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="compile"
      inheritAll="false">
    </ant>
  </target>

  <!-- Still uses ${project} references. BAD!!! -->
  <target
    name="test-datadtd">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="datadtd"
      inheritAll="false">
    </ant>
  </target>

  <!-- Still uses ${project} references. BAD!!! -->
  <target
    name="test-datadump">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="datadump"
      inheritAll="false">
    </ant>
  </target>

  <!-- Still uses ${project} references. BAD!!! -->
  <target
    name="test-datasql">
    <copy
      tofile="${torque.distDir}/src/schema/bookstore-data.xml"
      file="${torque.distDir}/src/bookstore-bookstore-all-data.xml"/>
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="datasql"
      inheritAll="false">
    </ant>
  </target>

  <target
    name="test-jdbc">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="jdbc">
      <property name="torque.home" value="${torque.distDir}"/>
    </ant>
  </target>

  <target
    name="test-ojb-prepare">
    <delete>
      <fileset dir="${torque.distDir}/src/schema/">
        <include name="*"/>
      </fileset>
    </delete>
    <copy
      todir="${torque.distDir}/src/schema"
      overwrite="yes">
      <fileset dir="src/ojbtest">
        <include name="**/*.xml"/>
      </fileset>
    </copy>
    <antcall target="test-create-db"/>
    <antcall target="test-sql"/>
    <antcall target="test-insert-sql"/>
  </target>

  <target
    name="test-ojb-generate">
    <ant
      dir="${torque.buildFileDir}"
      antfile="${torque.buildFile}"
      target="ojb">
    </ant>
  </target>

  <target
    name="test-ojb-compile">
    <mkdir dir="${torque.distDir}/bin/ojb"/>

    <javac
      debug="${torque.compile.debug}"
      deprecation="${torque.compile.deprecation}"
      destdir="${torque.distDir}/bin/ojb"
      optimize="${torque.compile.optimize}"
      srcdir="${torque.distDir}/src/ojb">
      <classpath refid="classpath"/>
    </javac>

  </target>


  <!-- ============================================================== -->
  <!-- R U N T I M E  T E S T S                                       -->
  <!-- ============================================================== -->

  <!-- Build classpath -->
  <path id="classpath">
    <pathelement location="${build.dest}"/>
    <pathelement location="${torque.destDir}"/>
    <pathelement location="${torque.testDatabaseJar}"/>
    <fileset dir="${build.test}/rttest/lib">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <target
    name="test-runtime">

    <!-- Runs the actual rttest tests -->
    <junit printsummary="no" haltonfailure="yes" fork="true">
      <classpath>
        <pathelement location="${torque.destDir}"/>
        <path refid="classpath"/>
      </classpath>

      <formatter type="xml"/>
      <formatter type="plain" usefile="false"/>

      <batchtest todir="target/test-reports/">
        <fileset dir="${torque.destDir}">
          <include name="**/*Test.class"/>
          <exclude name="**/test/**/*.class"/>
          <exclude name="**/LargeSelectTest.class"/>
        </fileset>
      </batchtest>
    </junit>

  </target>

  <target
    name="test-libs">

    <mkdir dir="${test.libDir}"/>
    <mkdir dir="${build.test}/logs"/>

    <copy file="${velocity.jar}" todir="${test.libDir}"/>
  </target>

  <!-- ============================================================== -->
  <!-- S Q L  T E S T S                                               -->
  <!-- ============================================================== -->

  <target
    name="sql-test"
    depends="test-libs">

    <filter token="DATABASE_DEFAULT" value="${torque.sqlTest.defaultDatabase}"/>
    <filter token="DATABASE_URL" value="${torque.sqlTest.databaseUrl}"/>
    <filter token="DATABASE_USER" value="${torque.database.user}"/>
    <filter token="DATABASE_DRIVER" value="${torque.database.driver}"/>
    <filter token="DATABASE_PASSWORD" value="${torque.database.password}"/>
    <filter token="DATABASE_ID_METHOD" value="${torque.idMethod}"/>
    <filter token="DATABASE_ADAPTER" value="${torque.database.adapter}"/>

    <mkdir dir="${build.test}/sqltest"/>

    <copy
      tofile="${build.test}/sqltest/build.properties"
      file="${torque.testProfile}"
      overwrite="yes"
    />

    <copy
      tofile="${build.test}/sqltest/build-torque.xml"
      file="${src.dir}/conf/build-torque.xml"
      overwrite="yes"
    />

    <copy
      todir="${build.test}/sqltest/schema"
      file="${src.dir}/sqltest/type-test-schema.xml"
      filtering="yes"
      overwrite="yes"
    />

    <ant
      dir="${build.test}/sqltest"
      antfile="${torque.buildFile}"
      target="create-db">
    </ant>

    <ant
      dir="${build.test}/sqltest"
      antfile="${torque.buildFile}"
      target="sql">
    </ant>

    <ant
      dir="${build.test}/sqltest"
      antfile="${torque.buildFile}"
      target="insert-sql">
    </ant>
  </target>

</project>
