<?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="available-test" basedir="." default="test1">

  <import file="../buildfiletest-base.xml"/>

  <target name="setUp">
    <mkdir dir="${output}" />
  </target>

  <target name="test1">
    <available/>
  </target>

  <target name="test2">
    <available property="test"/>
  </target>

  <target name="test3">
    <available file="test"/>
  </target>

  <target name="test4">
    <available property="test" 
               file="src/etc/testcases/taskdefs/this_file_does_not_exist"/>
  </target>

  <target name="test5">
    <available property="test" 
               file="available.xml"/>
  </target>

  <target name="test6">
    <available property="test" 
               resource="org/apache/tools/ant/taskdefs/this_resource_does_not_exist"/>
  </target>

  <target name="test7">
    <available property="test" 
               resource="org/apache/tools/ant/taskdefs/defaults.properties"/>
  </target>

  <target name="test8">
    <available property="test" 
               classname="org.apache.tools.ant.taskdefs.this_class_does_not_exist"/>
  </target>

  <target name="test9">
    <available property="test" 
               classname="org.apache.tools.ant.taskdefs.Ant"/>
  </target>

  <target name="test10">
    <available property="test" 
               file="available.xml"
               resource="org/apache/tools/ant/taskdefs/defaults.properties"
               classname="org.apache.tools.ant.taskdefs.Ant"/>
  </target>
  
  <target name="test11">
    <available property="test" 
               file="src/etc/testcases/taskdefs/available.xml"
               resource="org/apache/tools/ant/taskdefs/defaults.properties"
               classname="org.apache.tools.ant.taskdefs.this_class_does_not_exist"/>
  </target>  
  
  <target name="test12">
    <available property="" 
               file="available.xml"/>
  </target>

  <target name="test13">
    <available property="test" 
               file="" type="file" />
  </target>

  <target name="test13b">
    <available property="test" 
               file=""/>
  </target>

  <target name="test14">
    <available property="test" 
               resource=""/>
  </target>

  <target name="test15">
    <available property="test" 
               classname="org.apache.tools.ant.taskdefs.this_class_does_not_exist"/>
  </target>

  <target name="test16">
    <available property="test" 
               file="" type="dir"/>
  </target>

  <target name="test17">
    <available property="test" 
               file="../taskdefs" type="dir"/>
  </target>

  <target name="test18">
    <available property="test" 
               file="../this_dir_should_never_exist" type="dir"/>
  </target>

  <target name="test19">
    <available property="test" 
               file="available.xml" type="Foo"/>
  </target>

  <target name="test20">
    <available property="test" ignoresystemclasses="true"
               classname="java.awt.Graphics"/>
  </target>

  <target name="test21">
    <available property="test" ignoresystemclasses="true"
               classname="org.apache.tools.ant.launch.AntMain">
      <classpath>
        <pathelement location="${ant.home}/lib/ant-launcher.jar" />
      </classpath>
    </available>
  </target>

  <target name="test22">
    <available property="test" ignoresystemclasses="false"
               classname="java.awt.Graphics"/>
  </target>

  <target name="test23">
    <available property="test"
               classname="java.awt.Graphics"/>
  </target>

  <target name="test24">
    <!-- create a dummy file and look for it -->
    <mkdir dir="${output}/test"/>
    <echo message="package test;public class test {}" file="${output}/test/test.java"/>
    <javac srcdir="${output}" includes="test/test.java"/>
    <jar destfile="${output}/test.jar" basedir="${output}" includes="test/test.class"/>
    <available property="test"
               classname="test.test" classpath="${output}/test.jar"/>
  </target>

  <target name="searchInPathNotThere">
    <available file="not_there" filepath="..:optional"
               property="test" />
  </target>

  <target name="searchInPathIsThere">
    <available file="pvcs.xml" filepath="..:optional"
               property="test" />
  </target>

  <target name="testDoubleBasedir">
    <echo>testing ${basedir}${file.separator}${ant.file}</echo>
    <fail>
      <condition>
        <available file="${basedir}${file.separator}${ant.file}" />
      </condition>
    </fail>
  </target>

  <target name="prep.parents">
    <mkdir  dir="${output}/greatgrandparent/grandparent/parent/dir"/>
    <touch  file="${output}/greatgrandparent/a.txt"/>
    <touch  file="${output}/greatgrandparent/grandparent/b.txt"/>
    <touch  file="${output}/greatgrandparent/grandparent/parent/c.txt"/>
    <touch  file="${output}/greatgrandparent/grandparent/parent/dir/d.txt"/>
    <property name="available.test.dir"
              value="${output}/greatgrandparent/grandparent/parent/dir"/>
  </target>
  <target name="search-parents" depends="prep.parents">
    <echo>testing greatgrandparent - should see</echo>

    <fail>
      <condition>
        <not>
          <available file="a.txt" searchparents="yes">
            <filepath path="${available.test.dir}"/>
          </available>
        </not>
      </condition>
    </fail>

    <echo>testing grandparent - should see</echo>
    <fail>
      <condition>
        <not>
          <available file="b.txt" searchparents="yes">
            <filepath path="${available.test.dir}"/>
          </available>
        </not>
      </condition>
    </fail>

    <echo>testing parent - should see</echo>
    <fail>
      <condition>
        <not>
          <available file="c.txt" searchparents="yes">
            <filepath path="${available.test.dir}"/>
          </available>
        </not>
      </condition>
    </fail>

    <echo>testing dir - should see</echo>
    <fail>
      <condition>
        <not>
          <available file="d.txt" searchparents="yes">
            <filepath path="${available.test.dir}"/>
          </available>
        </not>
      </condition>
    </fail>

  </target>

  <target name="search-parents-not" depends="prep.parents">
    <echo>testing grandparent - should not see</echo>
    <fail>
      <condition>
        <available file="b.txt">
          <filepath path="${available.test.dir}"/>
        </available>
      </condition>
    </fail>

    <echo>testing parent - should not see</echo>
    <fail>
      <condition>
        <available file="c.txt">
          <filepath path="${available.test.dir}"/>
        </available>
      </condition>
    </fail>

    <echo>testing dir - should see</echo>
    <fail>
      <condition>
        <not>
          <available file="d.txt">
            <filepath path="${available.test.dir}"/>
          </available>
        </not>
      </condition>
    </fail>

  </target>
</project>
