<?xml version="1.0" encoding="UTF-8"?>
<!--
 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.
-->

<!-- ======================================================================== -->
<!--                                                                          -->
<!--  maven Buildfile for the test project                                    -->
<!--  Defines the goals for running the tests                                 -->
<!--                                                                          -->
<!-- $Id$ -->
<!--                                                                          -->
<!-- ======================================================================== -->

<project
  xmlns:j="jelly:core"
  xmlns:maven="jelly:maven"
  xmlns:ant="jelly:ant"
  default="torque-test:run">

  <goal name="torque-test:run" description="builds and runs the test project">
    <attainGoal name="torque-test:pre-test"/>
    <attainGoal name="torque-test:test"/>
    <attainGoal name="torque-test:post-test1"/>
    <attainGoal name="torque-test:post-test2"/>
  </goal>


  <goal name="torque-test:prepare"
      description="copies resources and clears garbage from old tests">

    <property file="../profile/${torque.test.profile}/build.properties" />
    <property file="../profile/${torque.test.profile}/project.properties" />

    <delete dir="target/sql" />
    <copy todir="${torque.schema.dir}">
      <fileset dir="src/schema">
        <include name="*" />
      </fileset>
      <filterset>
        <filter token="DATABASE_DEFAULT" value="${torque.project}"/>
        <filter token="DATABASE_ID_METHOD" value="${torque.test.base.idMethod}"/>
      </filterset>
    </copy>
    <copy todir="${torque.test.sql2xml.dir}">
      <fileset dir="src/sql" />
    </copy>
  </goal>


  <goal name="torque-test:pre-test"
      prereqs="torque-test:prepare"
      description="creates the docs, fills the database and creates om classes">
    <attainGoal name="torque:doc"/>
    <attainGoal name="torque:create-db"/>
    <attainGoal name="torque-test:shutdown-db-skip-exception"/>
    <attainGoal name="torque:sql"/>
    <attainGoal name="torque:id-table-init-sql"/>
    <attainGoal name="torque:insert-sql"/>
    <attainGoal name="torque-test:shutdown-db-skip-exception"/>
    <attainGoal name="torque:om"/>

    <mkdir dir="${torque.test.compile.dir}" />

    <echo>Compiling sources in ${torque.test.src.dir}</echo>
    <javac
      debug="${torque.compile.debug}"
      deprecation="${torque.compile.deprecation}"
      destdir="${torque.test.compile.dir}"
      optimize="${torque.compile.optimize}"
      srcdir="${torque.test.src.dir}">
      <classpath refid="maven.dependency.classpath"/>
    </javac>

    <j:set var="javaDir" value="${torque.java.dir}"/>
    <j:set var="javaBaseDir" value="${torque.java.base.dir}"/>
    <j:if test="${javaDir != javaBaseDir}">
      <echo>Compiling sources in ${torque.java.base.dir} and ${torque.java.dir}</echo>
      <javac
        debug="${torque.compile.debug}"
        deprecation="${torque.compile.deprecation}"
        destdir="${torque.test.compile.dir}"
        optimize="${torque.compile.optimize}">
        <classpath>
          <pathelement location="${torque.test.compile.dir}"/>
          <path refid="maven.dependency.classpath"/>
        </classpath>
        <src path="${torque.java.base.dir}" />
        <src path="${torque.java.dir}" />
      </javac>
    </j:if>
    <j:if test="${javaDir == javaBaseDir}">
    <echo>Compiling sources in ${torque.java.dir}</echo>
      <javac
        debug="${torque.compile.debug}"
        deprecation="${torque.compile.deprecation}"
        destdir="${torque.test.compile.dir}"
        optimize="${torque.compile.optimize}"
        srcdir="${torque.java.dir}">
        <classpath>
          <pathelement location="${torque.test.compile.dir}"/>
          <path refid="maven.dependency.classpath"/>
        </classpath>
      </javac>
    </j:if>

    <echo>Compiling sources in ${torque.test.junit.src.dir}</echo>
    <javac
      debug="${torque.compile.debug}"
      deprecation="${torque.compile.deprecation}"
      destdir="${torque.test.compile.dir}"
      optimize="${torque.compile.optimize}"
      srcdir="${torque.test.junit.src.dir}">
      <include name="**/*Test.java" />

      <j:set var="managers" value="${torque.useManagers}"/>
      <j:if test="${managers == 'true'}">
        <include name="**/ManagerTestConditional.java"/>
      </j:if>
      <j:set var="beans" value="${torque.generateBeans}"/>
      <j:if test="${beans == 'true'}">
        <include name="**/BeanTestConditional.java"/>
      </j:if>
      <j:set var="complex" value="${torque.complexObjectModel}"/>
      <j:if test="${complex == 'true'}">
        <j:set var="caching" value="${torque.objectIsCaching}"/>
        <j:if test="${caching == 'true'}">
          <include name="**/CopyTestConditional.java"/>
        </j:if>
      </j:if>
      <classpath>
        <pathelement location="${torque.test.compile.dir}"/>
        <path refid="maven.dependency.classpath"/>
      </classpath>
    </javac>
  </goal>

  <postGoal name="torque-test:pre-test">
    <j:set var="managers" value="${torque.useManagers}"/>
    <j:if test="${managers == 'true'}">
      <copy todir="${torque.test.compile.dir}">
        <fileset dir="${torque.test.junit.src.dir}">
          <include name="cache.ccf"/>
        </fileset>
      </copy>
    </j:if>

    <copy todir="${torque.test.compile.dir}">
      <fileset dir="src/logging">
        <include name="*"/>
      </fileset>
    </copy>
  </postGoal>

  <goal name="torque-test:test"
      description="run the tests for the om classes">

    <taskdef
      name="junit"
      classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" />

    <mkdir dir="${torque.test.log.dir}" />
    <mkdir dir="${torque.test.junit.report.dir}" />

    <echo>UseManagers is ${torque.useManagers}</echo>
    <echo>GenerateBeans is ${torque.generateBeans}</echo>
    <echo>ComplexObjectModel is ${torque.complexObjectModel}</echo>
    <echo>ObjectIsCaching is ${torque.objectIsCaching}</echo>

    <j:set var="haltonfailure" value="${torque.test.haltonfailure}" />
    <j:if test="${empty(haltonfailure)}" >
       <j:set var="haltonfailure" value="yes" />
    </j:if>

    <junit printsummary="no" haltonfailure="${haltonfailure}" fork="true">
      <sysproperty
        key="torque.configuration.file"
        value="../profile/${torque.test.profile}/Torque.properties" />
      <classpath>
        <pathelement location="${torque.test.compile.dir}"/>
        <path refid="maven.dependency.classpath"/>
      </classpath>

      <formatter type="xml"/>
      <formatter type="plain" usefile="false"/>

      <batchtest todir="${torque.test.junit.report.dir}">
        <fileset dir="${torque.test.junit.src.dir}">
          <j:set var="managers" value="${torque.useManagers}"/>
          <j:if test="${managers == 'true'}">
            <include name="**/ManagerTestConditional.java"/>
          </j:if>
          <j:set var="beans" value="${torque.generateBeans}"/>
          <j:if test="${beans == 'true'}">
            <include name="**/BeanTestConditional.java"/>
          </j:if>
          <j:set var="complex" value="${torque.complexObjectModel}"/>
          <j:if test="${complex == 'true'}">
            <j:set var="caching" value="${torque.objectIsCaching}"/>
            <j:if test="${caching == 'true'}">
              <include name="**/CopyTestConditional.java"/>
            </j:if>
          </j:if>
          <include name="**/*Test.java"/>
          <exclude name="**/test/**/*.java"/>
        </fileset>
      </batchtest>
    </junit>
  </goal>


  <goal name="torque-test:post-test1"
      description="tests tasks needing a filled database - part 1">
    <attainGoal name="torque:datadtd"/>
    <attainGoal name="torque:datadump"/>
    <attainGoal name="torque-test:shutdown-db-skip-exception"/>

    <!-- copy files generated by torque:datadump and torque:datadtd
         to directory where it can be read by torque:datasql -->
    <copy file="${torque.output.dir}/bookstore-bookstore-all-data.xml"
      toFile="${torque.schema.dir}/bookstore-data.xml" />
    <copy file="${torque.output.dir}/bookstore-data.dtd"
      toDir="${torque.schema.dir}" />
  </goal>

  <goal name="torque-test:post-test2"
      description="tests tasks needing a filled database - part 2">
    <attainGoal name="torque:datasql"/>
    <attainGoal name="torque:jdbc"/>
    <attainGoal name="torque-test:shutdown-db-skip-exception"/>
  </goal>

  <goal
    name="torque-test:install"
    description="Builds and installs all Torque subprojects to the local repository">
    <maven:reactor
      basedir="../.."
      includes="templates/project.xml,generator/project.xml,runtime/project.xml"
      excludes=""
      goals="jar:install"
      ignoreFailures="false"
    />
    <maven:reactor
      basedir="../.."
      includes="maven-plugin/project.xml"
      excludes=""
      goals="plugin:install-now"
      ignoreFailures="false"
    />
  </goal>

  <goal name="torque-test:install-run"
      description="Builds and installs all Torque components and run the test project"
      prereqs="torque-test:install">
    <attainGoal name="torque-test:run" />
  </goal>

  <goal
      name="torque-test:clean"
      description="clean all subproject affected by the test">
    <maven:reactor
      basedir="../.."
      includes="templates/project.xml,generator/project.xml,runtime/project.xml,maven-plugin/project.xml"
      excludes=""
      goals="clean"
      ignoreFailures="false"
    />
    <attainGoal name="clean" />
  </goal>

  <goal name="torque-test:clean-install-run"
      description="Cleans, Builds and installs all Torque components and run the test project"
      prereqs="torque-test:clean">
    <attainGoal name="torque-test:install-run" />
  </goal>

  <goal name="torque-test:shutdown-db-skip-exception"
        description="Shuts down embedded DBs if the torque.database.shutdownUrl has been set" >
    <j:set var="shutdownURL" value="${torque.database.shutdownUrl}"/>
    <j:if test="${! empty(shutdownURL)}" >
      <maven:reactor
        basedir="."
        includes="project.xml"
        excludes=""
        goals="torque-test:shutdown-db"
        ignoreFailures="true"
      />
    </j:if>
  </goal>

  <goal name="torque-test:shutdown-db"
        description="Shut down an embedded DB. Should be called from shutdown-db-skip-exceptions" >

    <echo>Shutting down DB instance.</echo>
    <echo>Shutdown URL = ${torque.database.shutdownUrl}</echo>
    <ant:sql
      autocommit="true"
      driver="${torque.database.driver}"
      onerror="continue"
      password="${torque.database.password}"
      url="${torque.database.shutdownUrl}"
      userid="${torque.database.user}">
      <classpath>
        <pathelement path="${pom.getDependencyClasspath()}"/>
        <path refid="maven.dependency.classpath"/>
      </classpath>
-- Do nothing script;
    </ant:sql>
  </goal>

</project>
