<?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="copy-test" basedir="." default="all"
  xmlns:au="antlib:org.apache.ant.antunit">
  <target name="all">
    <au:antunit>
      <fileset file="${ant.file}"/>
      <au:plainlistener/>
    </au:antunit>
  </target>

  <target name="test1">
    <au:assertFileDoesntExist file="copytest1.tmp"/>
    <copy file="copy.xml" tofile="copytest1.tmp"/>
    <au:assertFileExists file="copytest1.tmp"/>
  </target>
 
  <target name="test2">
    <au:assertFileDoesntExist file="copytest1dir/copy.xml"/>
    <copy file="copy.xml" todir="copytest1dir" overwrite="true"/>
    <au:assertFileExists file="copytest1dir/copy.xml"/>
  </target>

  <target name="filtertest">
    <!-- check fix for bugzilla 23154 -->
    <delete quiet="yes" file="copy.filter.out"/>
    <delete quiet="yes" file="copy.filter.inp"/>
    <concat destfile="copy.filter.inp">
PRODUCT_VERSION=6.2.1.4
PRODUCT_BUILD=6.5 (BLD_65036)
PRODUCT_VERSION=6.2.1.4
PRODUCT_BUILD=6.5 (BLD_65036)
    </concat>
    <copy file="copy.filter.inp" tofile="copy.filter.out">
      <filterset begintoken="6" endtoken="4">
        <filter token=".2.1." value="2.6.4" />
      </filterset>
    </copy>
    <concat><path path="copy.filter.out"/></concat>
    <au:assertLogDoesntContain text="loop in tokens" level="debug"/>
  </target>

  <target name="testinfinitetest">
    <delete quiet="yes" file="copy.filter.out"/>
    <delete quiet="yes" file="copy.filter.inp"/>
    <concat destfile="copy.filter.inp">
a=b=
    </concat>
    <copy file="copy.filter.inp" tofile="copy.filter.out">
      <filterset begintoken="=" endtoken="=">
        <filter token="b" value="=b="/>
      </filterset>
    </copy>
    <concat><path path="copy.filter.out"/></concat>
    <au:assertLogContains text="loop in tokens" level="debug"/>
  </target>

  <target name="test3">
    <!-- create an empty file -->
    <touch file="copytest3.tmp"/>
    <!--wait -->
    <sleep seconds="4"/>
    <!-- copy a different file to two places --> 
    <copy file="copy.xml" tofile="copytest3a.tmp" overwrite="true"/>
    <copy file="copy.xml" tofile="copytest3b.tmp" overwrite="true"/>
    <!--wait -->
    <sleep seconds="4"/>
    <!-- copy an old file onto a newer file (should not work) -->
    <copy file="copytest3.tmp" tofile="copytest3b.tmp" />
    <au:assertDestIsUptodate dest="copytest3b.tmp" src="copytest3.tmp"/>
    <!-- copy an older file onto a new one, should succeed -->
    <copy file="copytest3.tmp" tofile="copytest3c.tmp"
      overwrite="true"
      preservelastmodified="true" />
    <au:assertDestIsUptodate src="copytest3c.tmp" dest="copytest3b.tmp"/>
    <!-- copy a newer file onto an older one (should work) -->
    <copy file="copytest3a.tmp" tofile="copytest3.tmp" 
      preservelastmodified="true" />
    <au:assertDestIsUptodate src="copytest3c.tmp" dest="copytest3b.tmp"/>
    <!-- expected state :
      3a.tmp==3.tmp==copy.xml
      timeof(3a.tmp)==timeof(3.tmp)==now()-4
      sizeof(3c)==0
      timeof(3c.tmp)<timeof(3a.tmp);
      3b.tmp==copy.xml
    -->
  </target>
  
  <target name="test_single_file_fileset">
    <au:assertFileDoesntExist file="copytest_single_file_fileset.tmp"/>
    <copy tofile="copytest_single_file_fileset.tmp">
      <fileset dir="." includes="copy.xml"/>
    </copy>
    <au:assertFileExists file="copytest_single_file_fileset.tmp"/>
  </target>
  
  <target name="testFilterSet">
    <au:assertFileDoesntExist file="copy.filterset.tmp"/>
    <au:assertFilesDiffer actual="input/copy.filterset"
      expected="expected/copy.filterset.filtered"/>
    <copy file="input/copy.filterset" tofile="copy.filterset.tmp">
      <filterset>
        <filter token="TITLE" value="Apache Ant Project"/>
      </filterset>
    </copy>
    <au:assertFileExists file="copy.filterset.tmp"/>
    <au:assertFilesMatch actual="copy.filterset.tmp"
      expected="expected/copy.filterset.filtered"/>
  </target>

  <target name="testFilterChain">
    <au:assertFileDoesntExist file="copy.filterchain.tmp"/>
    <au:assertFilesDiffer actual="input/copy.filterset"
      expected="expected/copy.filterset.filtered"/>
    <copy file="input/copy.filterset" tofile="copy.filterchain.tmp">
      <filterchain>
        <replacetokens>
          <token key="TITLE" value="Apache Ant Project"/>
        </replacetokens>
      </filterchain>
    </copy>
    <au:assertFileExists file="copy.filterchain.tmp"/>
    <au:assertFilesMatch actual="copy.filterchain.tmp"
      expected="expected/copy.filterset.filtered"/>
  </target>

  <target name="testTranscoding">
    <au:assertFileDoesntExist file="copytest1.tmp"/>
    <au:assertFilesDiffer actual="input/iso8859-1" expected="expected/utf-8"/>
    <copy file="input/iso8859-1" tofile="copytest1.tmp"
          encoding="ISO8859_1" outputencoding="UTF8"/>
    <au:assertFileExists file="copytest1.tmp"/>
    <au:assertFilesMatch actual="copytest1.tmp" expected="expected/utf-8"/>
  </target>

  <target name="testMissingFileIgnore">
    <property name="not-there" location="not-there"/>
    <copy file="not-there" tofile="copytest1.tmp"
      failonerror="false"/>
    <au:assertLogContains text="Warning: Could not find file ${not-there} to copy."/>
  </target>

  <target name="testMissingFileBail">
    <property name="not-there" location="not-there"/>
    <au:expectfailure expectedMessage="Warning: Could not find file ${not-there} to copy.">
      <copy file="not-there" tofile="copytest1.tmp"
        failonerror="true"/>
    </au:expectfailure>
  </target>

  <target name="testMissingDirIgnore">
    <property name="not-there" location="not-there"/>
    <copy todir="copytest1dir" failonerror="false">
      <fileset dir="not-there"/>
    </copy>
    <au:assertLogContains text="${not-there} not found."/>
  </target>

  <target name="testMissingDirBail">
    <property name="not-there" location="not-there"/>
    <au:expectfailure expectedMessage="${not-there} not found.">
      <copy todir="copytest1dir" failonerror="true">
        <fileset dir="not-there"/>
      </copy>
    </au:expectfailure>
  </target>

  <target name="tearDown"> 
    <delete file="copytest1.tmp"/>
    <delete file="copytest3.tmp"/>
    <delete file="copytest3a.tmp"/>
    <delete file="copytest3b.tmp"/>
    <delete file="copytest3c.tmp"/>
    <delete file="copytest_single_file_fileset.tmp"/>
    <delete file="copy.filterset.tmp"/>
    <delete file="copy.filterchain.tmp"/>
    <delete dir="copytest1dir"/>
    <delete quiet="yes" file="copy.filter.out"/>
    <delete quiet="yes" file="copy.filter.inp"/>
  </target>

</project>
