<?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 default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
  <import file="../antunit-base.xml" />

  <target name="test-move-caseonly">
    <!-- this test is inspired by bugzilla 41948 -->
    <!-- Especially interesting if executed on case-insensitive file systems -->
    <mkdir dir="${output}"/>
    <touch file="${output}/abc"/>
    <move file="${output}/abc" tofile="${output}/aBc"/>
    <fileset dir="${output}" id="myfs">
      <include name="aBc"/>
    </fileset>
    <pathconvert refid="myfs" property="myproperty" setonempty="false"/>
    <au:assertPropertySet name="myproperty" message="abc was not renamed aBc"/>
  </target>

  <target name="test-regex-mapper"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=18656">
    <mkdir dir="${input}/AAA/foo"/>
    <touch file="${input}/AAA/foo/bar.txt"/>
    <mkdir dir="${input}/foo/AAA"/>
    <touch file="${input}/foo/bar.txt"/>
    <touch file="${input}/foo/AAA/bar.txt"/>
    <mkdir dir="${input}/foo/bar"/>
    <touch file="${input}/foo/bar/AAA.txt"/>
    <touch file="${input}/foo/bar/baz.txt"/>

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

    <move todir="${output}">
      <fileset dir="${input}"/>
      <firstmatchmapper>
        <regexpmapper from="(.*)AAA(.*)" to="\1BBB\2"/>
        <identitymapper/>
      </firstmatchmapper>
    </move>

    <au:assertFileExists file="${output}/BBB/foo/bar.txt"/>
    <au:assertFileExists file="${output}/foo/bar.txt"/>
    <au:assertFileExists file="${output}/foo/BBB/bar.txt"/>
    <au:assertFileExists file="${output}/foo/bar/BBB.txt"/>
    <au:assertFileExists file="${output}/foo/bar/baz.txt"/>
  </target>

  <target name="testNotModifiedSelector"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=43574"
          >
    <mkdir dir="${input}/images"/>
    <mkdir dir="${input}/cache"/>
    <touch file="${input}/images/foo.jpg"/>
    <mkdir dir="${output}"/>
    <selector id="cache.selector">
      <not>
        <modified update="true"
                  seldirs="false"
                  cache="propertyfile"
                  algorithm="digest"
                  comparator="equal">
          <param name="cache.cachefile"
                 value="${input}/cache/cache.properties"/>
          <param name="algorithm.algorithm" value="MD5"/>
        </modified>
      </not>
    </selector>
    <au:assertFileDoesntExist file="${input}/cache/cache.properties"/>
    <move todir="${output}" overwrite="true">
      <fileset dir="${input}/images">
        <include name="*.jpg" />
        <selector refid="cache.selector" />
      </fileset>
    </move>
    <au:assertFileExists file="${input}/cache/cache.properties"/>
    <au:assertFileExists file="${input}/images/foo.jpg"/>
    <au:assertFileDoesntExist file="${output}/foo.jpg"/>
    <move todir="${output}" overwrite="true">
      <fileset dir="${input}/images">
        <include name="*.jpg" />
        <selector refid="cache.selector" />
      </fileset>
    </move>
    <au:assertFileDoesntExist file="${input}/images/foo.jpg"/>
    <au:assertFileExists file="${output}/foo.jpg"/>
  </target>

  <target name="testOverwriteIsTrueByDefault">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/x.txt">X</echo>
    <sleep seconds="2"/>
    <echo file="${output}/y.txt">Y</echo>
    <move file="${input}/x.txt" tofile="${output}/y.txt"/>
    <au:assertFileDoesntExist file="${input}/x.txt"/>
    <au:assertResourceContains resource="${output}/y.txt"
                               value="X"/>
  </target>

  <target name="testOverwriteIsHonored"
          description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47755">
    <mkdir dir="${input}"/>
    <mkdir dir="${output}"/>
    <echo file="${input}/x.txt">X</echo>
    <sleep seconds="2"/>
    <echo file="${output}/y.txt">Y</echo>
    <move file="${input}/x.txt" tofile="${output}/y.txt" overwrite="false"/>
    <au:assertFileExists file="${input}/x.txt"/>
    <au:assertResourceContains resource="${output}/y.txt"
                               value="Y"/>
  </target>

  <!-- stolen from ../types/readwrite-test.xml - create a read-only file -->
  <property name="file" value="testfile"/>
  <condition property="unix">
    <os family="unix"/>
  </condition>
  <target name="createTestdir">
    <mkdir dir="${output}"/>
    <mkdir dir="${input}"/>
    <touch file="${output}/${file}"/>
  </target>
  <target name="makeFileUnwritable" depends="createTestdir">
    <setpermissions mode="444" nonPosixMode="tryDosOrFail">
      <file file="${output}/${file}"/>
    </setpermissions>
  </target>

  <target name="testMoveOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't replace read-only destination file ">
      <move toDir="${output}">
        <fileset dir="${input}"/>
      </move>
    </au:expectfailure>
  </target>

  <target name="testFilteredMoveOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <move toDir="${output}">
        <fileset dir="${input}"/>
        <filterset>
          <filter token="foo" value="bar"/>
        </filterset>
      </move>
    </au:expectfailure>
  </target>

  <target name="testMoveOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't replace read-only destination file ">
      <move toDir="${output}" overwrite="true">
        <fileset dir="${input}"/>
      </move>
    </au:expectfailure>
  </target>

  <target name="testFilteredMoveOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <au:expectfailure
        expectedMessage="can't write to read-only destination file ">
      <move toDir="${output}" overwrite="true">
        <fileset dir="${input}"/>
        <filterset>
          <filter token="foo" value="bar"/>
        </filterset>
      </move>
    </au:expectfailure>
  </target>

  <target name="testForcedMoveOverReadOnlyFile" depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <move toDir="${output}" force="true">
      <fileset dir="${input}"/>
    </move>
  </target>

  <target name="testForcedFilteredMoveOverReadOnlyFile"
          depends="makeFileUnwritable">
    <sleep seconds="2"/>
    <touch file="${input}/${file}"/>
    <move toDir="${output}" force="true">
      <fileset dir="${input}"/>
      <filterset>
        <filter token="foo" value="bar"/>
      </filterset>
    </move>
  </target>

  <target name="testForcedMoveOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <move toDir="${output}" overwrite="true" force="true">
      <fileset dir="${input}"/>
    </move>
  </target>

  <target name="testForcedFilteredMoveOverReadOnlyFileWithOverwrite"
          depends="makeFileUnwritable">
    <touch file="${input}/${file}"/>
    <move toDir="${output}" overwrite="true" force="true">
      <fileset dir="${input}"/>
      <filterset>
        <filter token="foo" value="bar"/>
      </filterset>
    </move>
  </target>
</project>
