<?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.
-->
<!-- $Id: test-build.xml 608963 2008-01-04 19:22:40Z antonio $ -->
<project name="test">
  <description>Test targets</description>

  <!--+
      | Runs all tests
      +-->
  <target name="test" depends="junit-tests, htmlunit-tests, anteater-tests"
          description="Runs all tests"/>

  <!--+
      | Runs JUnit tests
      +-->

  <target name="junit-tests"
          depends="core-junit-tests, block-junit-tests, junit-tests-report"
          description="Runs JUnit tests"/>

  <target name="htmlunit-tests"
          depends="run-htmlunit-tests, htmlunit-notes, htmlunit-tests-report, check-htmlunit-present"
          description="Runs HtmlUnit tests"/>

  <target name="check-htmlunit-present" unless="htmlunit.present">
    <echo>**** HTMLUNIT NOT FOUND!!! ****************************************************************************************</echo>
    <echo>To use htmlunit, please install it (see http://htmlunit.sourceforge.net/), </echo>
    <echo>and set htmlunit.home (currently ${htmlunit.home}) in your local.build.properties</echo>
    <echo>*******************************************************************************************************************</echo>
    <fail>htmlunit not present, cannot run tests</fail>
  </target>

  <target name="htmlunit-notes">
    <echo>*** Htmlunit tests notes ***</echo>
    <echo>a) To run these tests, an instance of Cocoon must be</echo>
    <echo>   running at ${htmlunit.test.baseurl} (start it as usual, from another window)</echo>
    <echo>b) JDK 1.4.x is required to run these tests, but running them</echo>
    <echo>   under 1.4.x to test Cocoon running under 1.3.x should be ok.</echo>
    <echo>c) Settings in build.properties can allow a subset of tests to run,</echo>
    <echo>   see the htmlunit section there.</echo>
  </target>

  <target name="junit-tests-prepare">
    <delete dir="${build.test.output}"/>
    <delete dir="${build.test.report}"/>
    <mkdir dir="${build.test.output}"/>
  </target>

  <target name="htmlunit-tests-prepare" if="htmlunit.present">
    <delete dir="${build.test.htmlunit.output}"/>
    <delete dir="${build.test.htmlunit.report}"/>
    <mkdir dir="${build.test.htmlunit.output}"/>
  </target>

  <!-- Runs JUnit tests -->
  <target name="core-junit-tests"
          depends="compile-junit-tests, junit-tests-prepare">
    <junit printsummary="yes" fork="yes" failureproperty="junit.test.failed">
      <jvmarg value="-Djava.endorsed.dirs=lib/endorsed"/>
      <jvmarg value="-Djunit.test.loglevel=${junit.test.loglevel}"/>
    	<jvmarg value="-Xmx128M"/>
      <classpath>
        <path refid="test.classpath"/>
      </classpath>
      <formatter type="plain" usefile="no"/>
      <formatter type="xml"/>
      <batchtest todir="${build.test.output}">
        <fileset dir="${build.test}">
          <include name="**/*TestCase.class"/>
          <include name="**/*Test.class" />
          <exclude name="**/AllTest.class" />
          <exclude name="**/CocoonBeanTestCase.class" />
          <exclude name="**/*$$*Test.class" />
          <exclude name="**/Abstract*.class" />
          <exclude name="**/SitemapComponentTestCase*"/>
          <exclude name="**/ContainerTestCase*"/>
          <exclude name="htmlunit/**"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <!-- Runs HtmlUnit tests -->
  <target name="run-htmlunit-tests" if="htmlunit.present" depends="compile-htmlunit-tests, htmlunit-tests-prepare,block-htmlunit-tests-prepare">

    <!-- fail if the URL under test is not available -->
    <echo>Tests tests require another instance of Cocoon to run at ${htmlunit.test.baseurl}</echo>
    <get src="${htmlunit.test.baseurl}"
      dest="${build.temp}/htmlunit-tests-checkaccess.html"
      verbose="false"
      usetimestamp="false"/>

    <echo>To run a selected subset of the tests, use 'htmlunit.test.include' in build.properties (currently set to ${htmlunit.test.include})</echo>
    <junit printsummary="yes" fork="yes" failureproperty="htmlunit.test.failed">
      <jvmarg value="-Djava.endorsed.dirs=lib/endorsed"/>
      <jvmarg value="-Djunit.test.loglevel=${junit.test.loglevel}"/>
      <jvmarg value="-Dhtmlunit.test.baseurl=${htmlunit.test.baseurl}"/>
      <jvmarg value="-Dhtmlunit.test.source-dir=${webapp}"/>
      <jvmarg value="-Dhtmlunit.test.deploy-dir=${build.webapp}"/>
      <jvmarg value="-Dhtmlunit.test.Bug26186InternalRequestMemoryLeak.iterations=${htmlunit.test.Bug26186InternalRequestMemoryLeak.iterations}"/>
      <classpath>
        <path refid="htmlunit.classpath"/>
      </classpath>
      <formatter type="plain" usefile="no"/>
      <formatter type="xml"/>
      <batchtest todir="${build.test.htmlunit.output}">
        <fileset dir="${build.test.htmlunit}">
          <include name="${htmlunit.test.include}"/>
          <exclude name="**/HtmlUnitTestCase.class"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <!-- Runs JUnit tests in debug mode -->
  <target name="junit-test-debug" depends="compile-junit-tests, junit-tests-prepare">
    <fail unless="junit.testcase"
          message="Please set the property $${junit.testcase} to a JUnit testcase (package.Classname, e.g. org.test.MyTestCase)."/>

    <echo message="The JVM will be suspended until you connect with your favourite debugger to port ${junit.test.debugport}."/>
    <junit printsummary="yes" fork="yes" failureproperty="junit.test.failed">
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${junit.test.debugport}"/>
      <jvmarg value="-Djunit.test.loglevel=${junit.test.loglevel}"/>
      <classpath refid="test.classpath"/>
      <formatter type="plain"/>
      <formatter type="xml"/>
      <test name="${junit.testcase}" todir="${build.test.output}"/>
    </junit>
  </target>

  <!-- Block tests -->
  <target name="block-junit-tests"
          depends="compile-junit-tests, prepare-blocks">
    <ant antfile="${build.temp}/blocks-build.xml"
         inheritAll="true"
         inheritRefs="false"
         target="tests"/>
  </target>

  <target name="block-htmlunit-tests-prepare" if="htmlunit.present" depends="compile-htmlunit-tests, prepare-blocks">
    <ant antfile="${build.temp}/blocks-build.xml"
         inheritAll="true"
         inheritRefs="false"
         target="prepare-htmlunit-tests"/>
  </target>

  <target name="junit-tests-report" depends="init">
    <mkdir dir="${build.test.report}"/>
    <junitreport todir="${build.test.output}">
      <fileset dir="${build.test.output}">
       <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${build.test.report}"/>
    </junitreport>
    <echo message="Unit report is at ${build.test.report}/index.html"/>
    <fail if="junit.test.failed"
          message="One or more JUnit tests failed or caused errors. Please have a look into the report for details."/>
  </target>

  <target name="htmlunit-tests-report" if="htmlunit.present" depends="init">
    <mkdir dir="${build.test.htmlunit.report}"/>
    <junitreport todir="${build.test.htmlunit.output}">
      <fileset dir="${build.test.htmlunit.output}">
       <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${build.test.htmlunit.report}"/>
    </junitreport>
    <echo message="Unit report is at ${build.test.htmlunit.report}/index.html"/>
    <fail if="htmlunit.test.failed"
          message="One or more HtmlUnit tests failed or caused errors. Please have a look into the report for details."/>
  </target>


  <!--+
      | Anteater tests
      +-->

  <!-- Anteater tests  -->
  <target name="anteater-tests" depends="block-anteater-tests, core-anteater-tests"
          description="Runs AntEater tests"/>

  <!-- Ant macro that calls anteater -->
  <macrodef name="call-anteater">
    <attribute name="script"/>
    <attribute name="target"/>
    <attribute name="targetfile"/>
    <sequential>
      <java classname="org.apache.tools.ant.Main" fork="true" failonerror="true">
        <classpath>
          <fileset dir="${anteater.home}">
            <include name="lib/**/*.jar"/>
            <include name="tomcat/**/*.jar"/>
          </fileset>
          <pathelement location="${anteater.home}/resources"/>
        </classpath>
        <jvmarg value="-Dant.home=${anteater.home}"/>
        <jvmarg value="-Danteater.home=${anteater.home}"/>
        <jvmarg value="-Danteater.resources=${anteater.home}/resources"/>
        <jvmarg value="-Danteater.report=${anteater.home}/resources/scripts/report.xml"/>
        <jvmarg value="-Djava.endorsed.dirs=${anteater.home}/lib"/>
        <arg line="-f ${build.test}/anteater/@{script} -Dhost=${anteater.target.host} -Dport=${anteater.target.port} -Dbase=${anteater.target.base.path} -Ddefault.haltonerror=${anteater.option.haltonerror} -Dtargetfile=@{targetfile} @{target}"/>
      </java>
    </sequential>
  </macrodef>

  <target name="anteater-tests-prepare">
    <!-- check whether Anteater is installed and available -->
    <!-- anteater parameters are set in build.properties -->
    <available file="${anteater.home}" property="anteater.present"/>
    <fail unless="anteater.present"
          message="To use anteater, please install it and set anteater.home (currently ${anteater.home}) in your local.build.properties"/>

    <echo>*** Anteater notes ***</echo>
    <echo>a) To run these tests, another instance of Cocoon must be</echo>
    <echo>   running at ${anteater.target.host}:${anteater.target.port} (base path=${anteater.target.base.path})</echo>
    <echo>b) JDK 1.4.x is required to run these tests, but running them</echo>
    <echo>   under 1.4.x to test Cocoon running under 1.3.x should be ok.</echo>
    <echo>c) anteater options can be set in local.build.properties, and some</echo>
    <echo>   tests only run if enabled there.</echo>
  </target>

  <!-- Anteater tests  -->
  <target name="core-anteater-tests" depends="anteater-tests-prepare"
          description="Runs anteater tests">
    <call-anteater script="run-tests.xml" target="all" targetfile=""/>
  </target>

  <!-- Run a SINGLE Anteater tests -->
  <target name="anteater-test" depends="anteater-tests-prepare, block-anteater-tests"
          description="Runs a single Anteater test">
    <echo>d) to run the tests in 'calc.xml' call 'build anteater-test -Dtarget=calc'</echo>
    <call-anteater script="run-tests.xml" target="single" targetfile="${target}"/>
  </target>

  <!-- Block tests -->
  <target name="block-anteater-tests" depends="prepare-blocks">
    <ant antfile="${build.temp}/blocks-build.xml"
         inheritAll="true"
         inheritRefs="false"
         target="prepare-anteater-tests"/>
  </target>



  <!--+
      | Clover tasks
      +-->
  <target name="clover">
    <available property="clover.present"
               classname="com.cortexeb.tools.clover.tasks.CloverTask">
      <classpath refid="classpath"/>
    </available>
    <!-- echo message="Clover present: ${clover.present}"/ -->
  </target>

  <target name="clover.init" depends="clover" if="clover.present">
    <taskdef resource="clovertasks"/>
  </target>

  <target name="clover.on" depends="clover.init" if="clover.present">
    <!-- echo message="Clover is ON!"/ -->
    <echo message="Clover is present? ${clover.present}"/>
    <clover-setup initString="${build}/clover/coverage.db"
                  tmpdir="${build}/clover/src"
                  preserve="false"
                  enabled="true"/>
  </target>

  <target name="clover.off" depends="clover.init" if="clover.present">
    <!-- echo message="Clover is OFF!"/ -->
    <clover-setup enabled="false"/>
  </target>

  <target name="clover.historypoint" depends="clover.init" if="clover.present">
    <clover-historypoint historyDir="${build}/clover/history"/>
  </target>

  <target name="clover.report" depends="init, clover.on" if="clover.present">
    <clover-report>
      <current outfile="${build}/clover-report">
        <format type="html"/>
      </current>
      <historical outfile="${build}/clover-history"
                  historyDir="${build}/clover/history">
        <format type="html"/>
      </historical>
    </clover-report>
  </target>
</project>
