<?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.
-->
<project name="FindTask" basedir="." default="test">

    <property name="src.dir"     value="src"/>
    <property name="classes.dir" value="classes"/>

    <property name="ant.test.lib" value="ant-testutil.jar"/>
    <property name="report.dir"   value="report"/>
    <property name="junit.out.dir.xml"  value="${report.dir}/junit/xml"/>
    <property name="junit.out.dir.html" value="${report.dir}/junit/html"/>

    <path id="classpath.run">
        <path path="${java.class.path}"/>
        <path location="${ant.project.name}.jar"/>
    </path>

    <path id="classpath.test">
        <path refid="classpath.run"/>
        <path location="${ant.test.lib}"/>
    </path>

    <target name="clean" description="Delete all generated files">
        <delete failonerror="false" includeEmptyDirs="true">
            <fileset dir="." includes="${ant.project.name}.jar"/>
            <fileset dir="${classes.dir}"/>
            <fileset dir="${report.dir}"/>
        </delete>
    </target>

    <target name="compile" description="Compiles the Task">
        <mkdir dir="${classes.dir}"/>
        <javac includeantruntime="true" srcdir="${src.dir}" destdir="${classes.dir}" classpath="${ant.test.lib}"/>
    </target>

    <target name="jar" description="JARs the Task" depends="compile">
        <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
    </target>

    <target name="use.init" description="Taskdef the Find-Task" depends="jar">
        <taskdef name="find" classname="Find" classpath="${ant.project.name}.jar"/>
    </target>

    <target name="use.simple" depends="use.init">
        <find file="ant.jar" location="location.ant-jar">
            <path>
                <fileset dir="${ant.home}" includes="**/*.jar"/>
                <fileset dir="c:/seu" includes="ant*/**/*.jar"/>
            </path>
        </find>
        <echo>ant.jar found on ${location.ant-jar}</echo>
    </target>

    <target name="use.simple2" depends="use.init">
        <find file="ant.jar" location="location.ant-jar" delimiter=";">
            <path>
                <fileset dir="${ant.home}" includes="**/*.jar"/>
                <fileset dir="c:/seu" includes="ant*/**/*.jar"/>
            </path>
        </find>
        <echo>ant.jar found on ${location.ant-jar}</echo>
    </target>

    <target name="testFileNotPresent" depends="use.init">
        <find file="a-not-existing-file.jar" location="location.ant-jar">
            <path>
                <fileset dir="${ant.home}" includes="**/*.jar"/>
            </path>
        </find>
    </target>

    <target name="testFilePresent" depends="use.init">
        <find file="ant.jar" location="location.ant-jar">
            <path>
                <fileset dir="${ant.home}" includes="**/*.jar"/>
            </path>
        </find>
    </target>

    <target name="test.init">
        <mkdir dir="test1/dir11/dir111"/>
        <mkdir dir="test1/dir11/dir112"/>
        <mkdir dir="test1/dir12/"/>
        <mkdir dir="test1/dir13/dir131"/>
        <mkdir dir="test1/dir13/dir132"/>
        <touch file="test1/dir11/dir111/test"/>
        <touch file="test1/dir11/dir111/not"/>
        <touch file="test1/dir11/dir111/not2"/>
        <touch file="test1/dir11/dir112/test"/>
        <touch file="test1/dir11/dir112/not"/>
        <touch file="test1/dir11/dir112/not2"/>
        <touch file="test1/dir12/test"/>
        <touch file="test1/dir12/not"/>
        <touch file="test1/dir12/not2"/>
        <touch file="test1/dir13/dir131/test"/>
        <touch file="test1/dir13/dir131/not"/>
        <touch file="test1/dir13/dir131/not2"/>
        <touch file="test1/dir13/dir132/test"/>
        <touch file="test1/dir13/dir132/not"/>
        <touch file="test1/dir13/dir132/not2"/>
        <mkdir dir="test2"/>
        <copy todir="test2">
            <fileset dir="test1"/>
        </copy>
    </target>

    <target name="testMultipleFiles" depends="use.init,test.init">
        <touch file="test1/dir11/dir111/test"/>
        <find file="test" location="location.test" delimiter=";">
            <path>
                <fileset dir="test1"/>
                <fileset dir="test2"/>
            </path>
        </find>
        <delete>
            <fileset dir="test1"/>
            <fileset dir="test2"/>
        </delete>
    </target>

    <target name="junit" description="Runs the unit tests" depends="jar">
        <delete dir="${junit.out.dir.xml}" />
        <mkdir  dir="${junit.out.dir.xml}" />
        <junit printsummary="yes" haltonfailure="no">
            <classpath refid="classpath.test"/>
            <sysproperty key="ant.home" value="${ant.home}"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out.dir.xml}">
                <fileset dir="${src.dir}" includes="**/*Test.java"/>
            </batchtest>
        </junit>
    </target>

    <target name="junitreport" description="Create a report for the rest result">
        <mkdir dir="${junit.out.dir.html}" />
        <junitreport todir="${junit.out.dir.html}">
            <fileset dir="${junit.out.dir.xml}">
                <include name="*.xml"/>
            </fileset>
            <report format="frames" todir="${junit.out.dir.html}"/>
        </junitreport>
    </target>

    <target name="test" depends="jar">
        <junit printsummary="no" haltonfailure="no">
            <classpath refid="classpath.test"/>
            <sysproperty key="ant.home" value="${ant.home}"/>
            <formatter type="brief" usefile="false"/>
            <batchtest fork="true" todir="${junit.out.dir.xml}">
                <fileset dir="${src.dir}" includes="**/*Test.java"/>
            </batchtest>
        </junit>
    </target>

    <target name="test2"
            depends="junit,junitreport"
            description="Runs unit tests and creates a report"/>

</project>
