<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
 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.
-->

<!--
  This file contains all the jar download targets to build Velocity.
  Its tasks are referenced from the build.xml file and should not be
  called directly.

  This is here so that the main build file is not cluttered any further.
-->

<project name="VelocityTools" basedir=".">

  <!-- test classpath -->
  <path id="test.classpath">
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
    </fileset>
    <pathelement location="${dist.dir}/${view.id}.jar"/>
    <fileset dir="${test.lib.dir}">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <!-- prepare tests -->
  <target name="prepare.test">
    <!-- <property name="cp" refid="test.classpath"/>
         <echo message="using classpath=${cp}"/>
    -->
    <condition property="java.version.ok">
      <not>
        <or>
          <equals arg1="${ant.java.version}" arg2="1.1"/>
          <equals arg1="${ant.java.version}" arg2="1.2"/>
          <equals arg1="${ant.java.version}" arg2="1.3"/>
          <equals arg1="${ant.java.version}" arg2="1.4"/>
        </or>
      </not>
    </condition>
    <fail message="VelocityTools tests require at least Java 1.5" unless="java.version.ok"/>
    <mkdir dir="${test.lib.dir}"/>
    <mkdir dir="${test.rst.dir}"/>
    <mkdir dir="${test.classes.dir}"/>
    <mkdir dir="${test.log.dir}"/>
    <pathconvert property="test.conf.dir.java" targetos="unix">
        <path location="${test.conf.dir}"/>
    </pathconvert>
    <pathconvert property="test.file.dir" targetos="unix">
        <path location="${test.src.dir}"/>
    </pathconvert>
    <filter token="test.file.dir" value="${test.file.dir}"/>
    <filter token="test.conf.dir" value="${test.conf.dir.java}"/>
    <filter token="test.log.dir" value="${test.log.dir}"/>
    <filter token="test.webcontainer.port" value="${test.webcontainer.port}"/>
    <copy todir="${test.build.src.dir}" filtering="yes">
      <fileset dir="${test.src.dir}">
        <include name="**/*.java"/>
        <include name="**/*.properties"/>
        <include name="**/*.xml"/>
      </fileset>
    </copy>
    <copy tofile="${test.dir}/etc/jetty.xml" file="${test.dir}/etc/jetty.xml.tmpl" filtering="yes"/>
  </target>

  <!-- clean tests -->
  <target name="test.clean">
    <delete file="${test.dir}/etc/jetty.xml"/>
  </target>

  <!-- clean test libraries -->
  <target name="test.clean.lib">
    <delete dir="${test.lib.dir}"/>
  </target>

  <!-- whitebox tests downloads -->
  <target name="whitebox.test.download" depends="prepare.test">
    <!-- download dependency jars if needed -->
    <ant antfile="${basedir}/download.xml" target="whitebox-test-download"/>
  </target>

  <!-- blackbox tests downloads -->
  <target name="blackbox.test.download" depends="prepare.test">
    <!-- download dependency jars if needed -->
    <ant antfile="${basedir}/download.xml" target="blackbox-test-download"/>
  </target>

  <!-- compile whitebox tests -->
  <target name="compile.whitebox.test" depends="whitebox.test.download">
    <javac srcdir="${test.build.src.dir}"
      destdir="${test.classes.dir}"
      encoding="${compile.encoding}"
      debug="${compile.debug}"
      deprecation="${compile.deprecation}"
      optimize="${compile.optimize}"
      includes="**/whitebox/*.java"
      source="${test.compile.source}"
      target="${test.compile.target}"
      classpathref="test.classpath">
      <include name="*.java"/>
      <include name="**/*Tests.java"/>
      <exclude name="**/blackbox/*.java"/>
    </javac>
    <!-- copy non-java resources -->
    <copy todir="${test.classes.dir}">
      <fileset dir="${test.build.src.dir}" excludes="**/*.java"/>
    </copy>
  </target>

  <!-- compile blackbox tests -->
  <target name="compile.blackbox.test" depends="blackbox.test.download">
    <property name="name" value="showcase" />
    <property name="base" value="${examples.home}/${name}" />
    <property name="lib" value="${base}/WEB-INF/lib"/>
    <ant antfile="${basedir}/examples.xml" target="example-compile"/>
    <javac srcdir="${test.build.src.dir}"
      destdir="${test.classes.dir}"
      encoding="${compile.encoding}"
      debug="${compile.debug}"
      deprecation="${compile.deprecation}"
      optimize="${compile.optimize}"
      includes="**/blackbox/*.java,org/apache/velocity/tools/test/JettyLogger.java"
      classpathref="test.classpath"/>
    <jar jarfile="${test.lib.dir}/jettylogger.jar" basedir="${test.classes.dir}" includes="org/apache/velocity/tools/test/JettyLogger.class"/>
    <copy todir="${lib}" file="${lib.dir}/commons-logging-${jar.commons-logging.version}.jar"/>
    <copy todir="${base}/WEB-INF/classes" file="${test.dir}/conf/commons-logging.properties"/>
    <javac srcdir="${test.build.src.dir}"
      destdir="${base}/WEB-INF/classes"
      encoding="${compile.encoding}"
      debug="${compile.debug}"
      deprecation="${compile.deprecation}"
      optimize="${compile.optimize}"
      includes="org/apache/velocity/tools/test/FilteredLogChuteCommonsLog.java"
      classpathref="test.classpath"/>
  </target>

  <!-- generic (whitebox) tests -->
  <target name="test.generic" depends="compile.whitebox.test">
    <junit fork="false"
           printSummary="yes"
           haltonerror="${test.haltonerror}"
           haltonfailure="${test.haltonfailure}">
      <classpath>
        <path refid="test.classpath"/>
        <pathelement location="${test.classes.dir}"/>
      </classpath>
      <batchtest todir="${test.rst.dir}">
        <fileset dir="${test.classes.dir}">
          <include name="org/apache/velocity/tools/**/*Tests.class"/>
          <exclude name="org/apache/velocity/tools/test/blackbox/**/*"/>
        </fileset>
      </batchtest>
      <formatter type="plain"/>
    </junit>
  </target>

  <!-- view tools (blackbox) tests -->
  <target name="test.view">
    <antcall target="start-showcase-webapp"/>
    <junit fork="false"
           printSummary="withOutAndErr"
           haltonerror="false"
           haltonfailure="false"
           errorproperty="test.error"
           failureproperty="test.failure">
      <classpath>
        <path refid="test.classpath"/>
        <pathelement location="${test.classes.dir}"/>
      </classpath>
      <batchtest todir="${test.rst.dir}">
        <fileset dir="${test.classes.dir}">
            <include name="**/blackbox/*Test*"/>
        </fileset>
      </batchtest>
      <formatter type="plain"/>
    </junit>
    <antcall target="stop-showcase-webapp"/>
    <antcall target="clean-showcase-webapp"/>
    <condition property="test.success">
      <and>
        <isfalse value="${test.error}"/>
        <isfalse value="${test.failure}"/>
      </and>
    </condition>
    <fail message="blackbox tests failed" unless="test.success"/>
  </target>

  <!-- start showcases webapp -->
  <target name="start-showcase-webapp" depends="compile.blackbox.test">
    <fail message="Localhost port ${test.webcontainer.control.port} is busy. Is the showcase webapp already running?">
      <condition>
        <socket server="127.0.0.1" port="${test.webcontainer.port}"/>
      </condition>
    </fail>
    <java fork="true" spawn="true" jar="${test.lib.dir}/start-6.0.1.jar" dir="${test.dir}">
      <arg value="etc/jetty.xml"/>
      <sysproperty key="jetty.home" value="${test.dir}"/>
      <sysproperty key="jetty.port" value="${test.webcontainer.port}"/>
      <sysproperty key="STOP.PORT" value="${test.webcontainer.control.port}"/>
      <sysproperty key="STOP.KEY" value="verysecret"/>
      <sysproperty key="org.mortbay.log.class" value="org.apache.velocity.tools.test.JettyLogger"/>
      <sysproperty key="jetty.log.file" value="${test.log.dir}/showcase.log"/>
    </java>
    <waitfor maxwait="2" maxwaitunit="minute" checkevery="500">
      <http url="http://localhost:${test.webcontainer.port}/"/>
    </waitfor>
    <echo message="web server launched successfully."/>
  </target>

  <!-- stop showcase webapp -->
  <target name="stop-showcase-webapp">
    <java fork="true" jar="${test.lib.dir}/start-6.0.1.jar" dir="${test.dir}">
      <arg value="--stop"/>
      <sysproperty key="STOP.PORT" value="${test.webcontainer.control.port}"/>
      <sysproperty key="STOP.KEY" value="verysecret"/>
    </java>
  </target>

  <!-- cleanup showcase webapp -->
  <target name="clean-showcase-webapp">
    <waitfor maxwait="30" maxwaitunit="second" checkevery="1000">
      <not>
        <isreachable url="http://localhost:${test.webcontainer.port}/"/>
      </not>
    </waitfor>
    <property name="target" value="clean"/>
    <ant antfile="${basedir}/examples.xml" target="showcase-example" />
  </target>

  <!-- Analyze code with FindBugs -->
  <target name="findbugs">
    <echo>Working with FindBugs at: ${findbugs.home}</echo>
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
      <classpath>
        <fileset dir="${findbugs.home}/lib">
          <include name="findbugs-ant.jar"/>
        </fileset>
      </classpath>
    </taskdef>
    <echo>Analyzing ${dist.dir}/${project.id}.jar built from ${source.home}</echo>
    <findbugs home="${findbugs.home}"
              output="html"
              outputFile="${test.build.dir}\findbugs-report.html"
              failOnError="true">
      <auxClasspath>
        <fileset dir="${lib.dir}">
          <include name="**/*.jar"/>
        </fileset>
      </auxClasspath>
      <sourcePath path="${source.home}" />
      <class location="${dist.dir}/${project.id}.jar" />
    </findbugs>
  </target>


  <!-- Analyze code with PMD -->
  <target name="pmd">
    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
      <classpath>
        <fileset dir="${pmd.home}/lib">
          <include name="*.jar"/>
        </fileset>
      </classpath>
    </taskdef>
    <echo>Analyzing ${source.home}/main/java...</echo>
    <pmd shortFilenames="true"
         failonerror="true">
<!-- To minimize noise keep rulesets that you are done checking here...
-->
      <ruleset>unusedcode</ruleset>
      <ruleset>imports</ruleset>
      <ruleset>braces</ruleset>
<!-- ... and rulesets you're not ready to check here.
      <ruleset>basic</ruleset>
      <ruleset>strings</ruleset>
      <ruleset>design</ruleset>
      <ruleset>codesize</ruleset>
-->
      <formatter type="html"
                 toFile="${test.build.dir}\pmd_report.html"/>
      <fileset dir="${source.home}/main/java">
        <include name="**/*.java"/>
      </fileset>
    </pmd>
    <echo>Generated report is at ${test.build.dir}\pmd_report.html</echo>
  </target>
</project>
