<?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="selectors-test" basedir="." default="setupfiles">

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

  <target name="setUp">
    <mkdir dir="${output}"/>
  </target>
  <property name="etc.dir" value=".."/>
  <property name="test.dir"
          value="${output}/selectortest"/>
  <property name="testregexpsrc.dir"
          value="${output}/regexpseltestsrc"/>
  <property name="testregexpdest.dir"
          value="${output}/regexpseltestdest"/>
  <property name="mirror.dir"
          value="${output}/selectortest2"/>

  <target name="setupfiles">
    <mkdir dir="${test.dir}" />
    <mkdir dir="${test.dir}/zip" />
    <mkdir dir="${test.dir}/tar" />
    <mkdir dir="${test.dir}/tar/gz" />
    <mkdir dir="${test.dir}/tar/bz2" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.md5"
            tofile="${test.dir}/asf-logo.gif.md5" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.bz2"
            tofile="${test.dir}/asf-logo.gif.bz2" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.gz"
            tofile="${test.dir}/asf-logo.gif.gz" />
    <copy file="${etc.dir}/taskdefs/expected/copy.filterset.filtered"
            tofile="${test.dir}/copy.filterset.filtered" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.zip"
            tofile="${test.dir}/zip/asf-logo.gif.zip" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.tar"
            tofile="${test.dir}/tar/asf-logo.gif.tar" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo-huge.tar.gz"
            tofile="${test.dir}/tar/asf-logo-huge.tar.gz" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.tar.gz"
            tofile="${test.dir}/tar/gz/asf-logo.gif.tar.gz" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo.gif.tar.bz2"
            tofile="${test.dir}/tar/bz2/asf-logo.gif.tar.bz2" />
    <copy file="${etc.dir}/taskdefs/expected/asf-logo-huge.tar.bz2"
            tofile="${test.dir}/tar/bz2/asf-logo-huge.tar.bz2" />
    <!-- Make linefeeds consistent between platforms -->
    <fixcrlf srcdir="${test.dir}" includes="*.filtered" eol="lf"/>
    <!-- Set a known base time for all files -->
    <touch datetime="11/21/2001 4:55 AM">
        <fileset dir="${test.dir}">
            <include name="**/*"/>
        </fileset>
    </touch>
    <!-- Then adjust individual ones -->
    <touch file="${test.dir}/asf-logo.gif.bz2"
            datetime="01/01/2001 12:00 AM"/>
    <touch file="${test.dir}/asf-logo.gif.gz"
            datetime="04/15/2002 2:30 PM"/>
    <touch file="${test.dir}/zip/asf-logo.gif.zip"
            datetime="05/10/2002 2:30 PM"/>
    <touch file="${test.dir}/tar/asf-logo.gif.tar"
            datetime="05/10/2002 2:29 PM"/>
    <touch file="${test.dir}/tar/asf-logo-huge.tar.gz"
            datetime="05/10/2002 2:29 AM"/>
  </target>

  <target name="mirrorfiles">
    <mkdir dir="${mirror.dir}" />
    <mkdir dir="${mirror.dir}/zip" />
    <mkdir dir="${mirror.dir}/tar" />
    <mkdir dir="${mirror.dir}/tar/gz" />
    <mkdir dir="${mirror.dir}/tar/bz2" />
    <touch file="${mirror.dir}/asf-logo.gif.md5"/>
    <touch file="${mirror.dir}/asf-logo.gif.bz2"/>
    <touch file="${mirror.dir}/zip/asf-logo.gif.zip"/>
    <touch file="${mirror.dir}/tar/asf-logo.gif.tar"/>
    <touch file="${mirror.dir}/tar/asf-logo-huge.tar.gz"/>
    <touch file="${mirror.dir}/tar/gz/asf-logo.gif.tar.gz"/>
    <touch file="${mirror.dir}/tar/bz2/asf-logo.gif.tar.bz2"/>
    <touch file="${mirror.dir}/tar/bz2/asf-logo-huge.tar.bz2"/>
  </target>

  <target name="containsregexp">
    <mkdir dir="${testregexpsrc.dir}" />
    <mkdir dir="${testregexpdest.dir}" />
    <!-- Make two test files, shouldcopy.txt will get selected if everything works
         shouldnotcopy.txt will not get selected for copy.  The test looks to see
         that only one file is copied
    -->
    <echo message="Some testregexp text 2.0" file="${testregexpsrc.dir}/shouldcopy.txt" />
    <echo message="Some testregexp text 20" file="${testregexpsrc.dir}/shouldnotcopy.txt" />
    <copy todir="${testregexpdest.dir}">
      <fileset dir="${testregexpsrc.dir}">
        <include name="*.txt" />
        <containsregexp expression="[0-9]\.[0,1,2]" />
      </fileset>
    </copy>
  </target>

  <!-- ==========  Test for ModifiedSelector  ========== -->

  <target name="modifiedselectortest-makeDirty">
      <!-- Load propertyfile generated by SelectorTest-class -->
      <property file="ModifiedSelectorTest.properties"/>

      <!-- Modify only timestamp -->
      <touch file="${test.dir}/${f2name}" datetime="02/28/2003 9:55 AM"/>
      <!-- Change content but keep timestamp -->
      <echo file="${test.dir}/${f3name}" append="true" message="new content"/>
      <touch file="${test.dir}/${f3name}" datetime="11/21/2001 4:55 AM"/>
      <!-- Change content and timestamp -->
      <echo file="${test.dir}/${f4name}" append="true" message="new content"/>
  </target>

  <target name="modifiedselectortest-scenario-clean">
      <delete dir="${test.dir}"/>
  </target>

  <target name="modifiedselectortest-scenario-prepare">
      <mkdir dir="${test.dir}/src"/>
      <copy todir="${test.dir}/src">
          <fileset dir="${ant.home}/lib" includes="ant.jar">
              <type type="file"/>
          </fileset>
          <fileset dir="${ant.home}/bin">
                <type type="file"/>
          </fileset>
      </copy>
      <touch datetime="12/24/2002 4:00 pm">
          <fileset dir="${test.dir}"/>
      </touch>
      <mkdir dir="${test.dir}/to-1"/>
      <mkdir dir="${test.dir}/to-2"/>
      <mkdir dir="${test.dir}/to-3"/>
  </target>

  <target name="modifiedselectortest-scenario-makeDirty">
      <touch file="${test.dir}/src/ant.jar"/>
      <echo file="${test.dir}/src/ant.bat" append="true" message="new-content"/>
      <echo file="${test.dir}/src/antRun.pl" append="true" message="new-content"/>
      <touch file="${test.dir}/src/antRun.pl" datetime="12/24/2002 4:00 pm"/>
  </target>

  <target name="modifiedselectortest-scenario-coreselector-defaults" depends="modifiedselectortest-scenario-prepare">
      <!-- copy first time and create cachefile -->
      <copy todir="${test.dir}/to-1">
          <fileset dir="${test.dir}/src">
              <modified/>
          </fileset>
      </copy>
      <!-- copy second time: nothing should be copied -->
      <copy todir="${test.dir}/to-2">
          <fileset dir="${test.dir}/src">
              <modified/>
          </fileset>
      </copy>
      <!-- 'modify' the source files -->
      <antcall target="modifiedselectortest-scenario-makeDirty"/>
      <!-- copy third time: only the files with new CONTENT should be copied -->
      <copy todir="${test.dir}/to-3">
          <fileset dir="${test.dir}/src">
              <modified/>
          </fileset>
      </copy>
  </target>

  <target name="modifiedselectortest-scenario-coreselector-settings" depends="modifiedselectortest-scenario-prepare">
      <!-- copy first time and create cachefile -->
      <copy todir="${test.dir}/to-1">
          <fileset dir="${test.dir}/src">
              <modified cache="propertyfile" algorithm="hashvalue" update="true">
                  <param name="cache.cachefile" value="core.cache.properties" />
              </modified>
          </fileset>
      </copy>
      <!-- copy second time: nothing should be copied -->
      <copy todir="${test.dir}/to-2">
          <fileset dir="${test.dir}/src">
              <modified cache="propertyfile" algorithm="hashvalue" update="true">
                  <param name="cache.cachefile" value="core.cache.properties" />
              </modified>
          </fileset>
      </copy>
      <!-- 'modify' the source files -->
      <antcall target="modifiedselectortest-scenario-makeDirty"/>
      <!-- copy third time: only the files with new CONTENT should be copied -->
      <copy todir="${test.dir}/to-3">
          <fileset dir="${test.dir}/src">
              <modified cache="propertyfile" algorithm="hashvalue" update="true">
                  <param name="cache.cachefile" value="core.cache.properties" />
              </modified>
          </fileset>
      </copy>
  </target>

  <target name="modifiedselectortest-scenario-customselector-settings" depends="modifiedselectortest-scenario-prepare">
      <!-- copy first time and create cachefile -->
      <copy todir="${test.dir}/to-1">
          <fileset dir="${test.dir}/src">
              <custom classname="org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector">
                  <param name="cache"      value="propertyfile"/>
                  <param name="algorithm"  value="hashvalue"/>
                  <param name="update"     value="true"/>
                  <param name="cache.cachefile" value="core.cache.properties"/>
              </custom>
          </fileset>
      </copy>
      <!-- copy second time: nothing should be copied -->
      <copy todir="${test.dir}/to-2">
          <fileset dir="${test.dir}/src">
              <custom classname="org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector">
                  <param name="cache"      value="propertyfile"/>
                  <param name="algorithm"  value="hashvalue"/>
                  <param name="update"     value="true"/>
                  <param name="cache.cachefile" value="core.cache.properties"/>
              </custom>
          </fileset>
      </copy>
      <!-- 'modify' the source files -->
      <antcall target="modifiedselectortest-scenario-makeDirty"/>
      <!-- copy third time: only the files with new CONTENT should be copied -->
      <copy todir="${test.dir}/to-3">
          <fileset dir="${test.dir}/src">
              <custom classname="org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector">
                  <param name="cache"      value="propertyfile"/>
                  <param name="algorithm"  value="hashvalue"/>
                  <param name="update"     value="true"/>
                  <param name="cache.cachefile" value="core.cache.properties"/>
              </custom>
          </fileset>
      </copy>
  </target>

  <target name="modifiedselectortest-customClasses" depends="modifiedselectortest-scenario-prepare">
      <property name="pkg.live" value="org.apache.tools.ant.types.selectors.modifiedselector"/>
      <property name="pkg.test" value="org.apache.tools.ant.types.selectors"/>
      <fileset id="fs.mod" dir="${test.dir}/src">
          <modified
              algorithmclass="${pkg.test}.MockAlgorithm"
              cacheclass="${pkg.test}.MockCache"
              comparatorclass="${pkg.test}.MockComparator"
          >
              <classpath>
                  <pathelement location="${build.tests.value}"/>
              </classpath>
          </modified>
      </fileset>
      <fileset id="fs.full" dir="${test.dir}/src"/>
      <property name="fs.mod.value"  refid="fs.mod"/>
      <property name="fs.full.value" refid="fs.full"/>
  </target>

  <target name="modifiedselectortest-ResourceSimple">
    <fail message="Didnt get the required numbers of Resources.">
      <condition>
        <not>
          <resourcecount when="equal" count="3">
            <restrict>
              <resources>
                <file file="foo" />
                <resource name="foo" />
                <file file="foo" basedir="${basedir}" />
              </resources>
              <modified selres="true" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/>
            </restrict>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="modifiedselectortest-ResourceSelresTrue">
    <fail message="Got the Resource, but should.">
      <condition>
        <not>
          <resourcecount when="equal" count="1">
            <restrict>
              <resources>
                <resource name="notExisting" />
              </resources>
              <modified selres="true" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/>
            </restrict>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="modifiedselectortest-ResourceSelresFalse">
    <fail message="Got the Resource, but should not.">
      <condition>
        <not>
          <resourcecount when="equal" count="0">
            <restrict>
              <resources>
                <resource name="notExisting" />
              </resources>
              <modified selres="false" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/>
            </restrict>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="modifiedselectortest-scenario-resourceSimple" depends="modifiedselectortest-scenario-prepare">
    <macrodef name="check">
      <attribute name="count"/>
      <attribute name="message"/>
      <sequential>
        <fail message="@{message}">
          <condition>
            <not>
              <resourcecount when="equal" count="@{count}">
                <restrict>
                  <resources>
                    <fileset dir="${test.dir}/src"/>
                  </resources>
                  <modified selres="false" xmlns="antlib:org.apache.tools.ant.types.resources.selectors"/>
                </restrict>
              </resourcecount>
            </not>
          </condition>
        </fail>	
      </sequential>
    </macrodef>
    <!-- select first time and create cachefile -->
    <check count="14" message="Initial set of files not ok."/>

    <!-- check second time: nothing should be selected -->
    <check count="0" message="Selected files but shouldnt."/>

    <!-- 'modify' the source files -->
    <antcall target="modifiedselectortest-scenario-makeDirty"/>

    <!-- copy third time: only the files with new CONTENT should be copied -->
    <check count="2" message="Didnt select the 2 modified files."/>
  </target>	

</project>
