<?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

      https://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="delete-test" basedir="." default="test1">

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

  <property name="dir" location="${output}" />
  <basename property="dirname" file="${output}"/>

  <macrodef name="expectabsent">
    <attribute name="target" default="${dir}"/>
    <sequential>
      <fileset id="detritus" dir="@{target}" erroronmissingdir="false"/>
      <fail message="@{target} still has: ${toString:detritus}">
        <condition>
          <available file="@{target}" />
        </condition>
    </fail>
    </sequential>
  </macrodef>

  <macrodef name="expectdirsonly">
    <sequential>
      <fail>
        <condition>
          <or>
            <resourcecount when="greater" count="0">
              <fileset dir="${dir}" />
            </resourcecount>
            <not>
              <resourcecount count="${srcdirs}">
                <dirset dir="${dir}" />
              </resourcecount>
            </not>
          </or>
        </condition>
      </fail>
    </sequential>
  </macrodef>

  <target name="init">
    <resourcecount property="srcdirs">
      <dirset dir="${basedir}" />
    </resourcecount>

    <resourcecount property="srcsize">
      <files includes="${basedir}/" />
    </resourcecount>

    <mkdir dir="${dir}" />

    <copy todir="${dir}">
      <fileset dir="${basedir}" excludes="${dirname},${dirname}/**" />
    </copy>
  </target>

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

  <target name="test2" depends="init">
    <delete file="${dir}" />
    <fail>
      <condition>
        <not>
          <resourcecount count="${srcsize}">
            <files includes="${dir}/" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="test4" depends="init">
    <delete dir="${dir}" />
    <expectabsent />
  </target>

  <target name="test5" depends="init">
    <delete dir="${dir}" includes="**" />
    <expectdirsonly />
  </target>

  <target name="test6" depends="init">
    <delete dir="${dir}" includes="**" includeemptydirs="true" />
    <expectabsent />
  </target>

  <target name="test7" depends="init">
    <delete>
      <fileset id="fs" dir="${dir}" />
    </delete>
    <expectdirsonly />
  </target>

  <target name="test8" depends="init">
    <delete includeemptydirs="true">
      <fileset dir="${dir}" />
    </delete>
    <expectabsent />
  </target>

  <target name="test9" depends="init">
    <delete>
      <files>
          <include name="${dir}/**"/>
      </files>
    </delete>
    <expectabsent />
  </target>

  <target name="test10">
    <delete>
      <filelist dir="${dir}" files="test10absentfile" />
    </delete>
  </target>

  <target name="test11">
    <delete failonerror="false">
      <fileset dir="thisdenotesadirectorythatwillneverexistblah" />
    </delete>
  </target>

  <target name="test12">
    <delete failonerror="false" includeemptydirs="true">
      <fileset dir="thisdenotesadirectorythatwillneverexistblah" />
    </delete>
  </target>

  <target name="test13" depends="init">
    <delete includeemptydirs="true">
      <fileset dir="${dir}" />
      <fileset dir="${dir}" />
    </delete>
    <expectabsent />
  </target>

  <target name="test14" depends="init">
	<delete quiet="false">
		<fileset dir="${dir}" />
		<fileset dir="${dir}" />
	</delete>
  </target>

  <target name="test15" depends="init">
	<delete quiet="true">
		<fileset dir="${dir}" />
		<fileset dir="${dir}" />
	</delete>
  </target>
  <!-- Bugzilla 40313 -->
  <target name="test16.init">
    <mkdir dir="${dir}/CVS"/>
    <touch file="${dir}/CVS/lala"/>
    <mkdir dir="${dir}/subdir"/>
  </target>

  <target name="test16" depends="test16.init">
    <delete defaultexcludes="false" dir="${dir}" includeemptydirs="true"/>
    <expectabsent/>
  </target>

  <target name="test17" depends="test16.init">
    <delete dir="${dir}" defaultexcludes="true" includeemptydirs="true"/>
    <fail message="file in CVS dir deleted">
      <condition>
        <not>
          <available file="${dir}/CVS/lala"/>
        </not>
      </condition>
    </fail>
    <expectabsent target="${dir}/subdir"/>
  </target>



</project>
