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

  <property name="tmp.file" value="concat.tmp" />
  <property name="tmp.file.2" value="concat.tmp.2" />

  <property name="world" value="World" />

  <target name="cleanup">
    <delete file="TESTDEST"/>
    <delete file="${tmp.file}"/>
    <delete file="${tmp.file.2}"/>
    <delete file="concat.line4"/>
    <delete file="concat.noeol"/>
    <delete file="concat.linecr"/>
    <delete file="concat.utf8"/>
    <delete file="concat.urls"/>
  </target>

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

  <target name="test2">
    <concat destfile="">Hello, ${world}!</concat>
  </target>

  <target name="test3">
    <concat destfile="${tmp.file}">Hello, ${world}!</concat>
  </target>

  <target name="test4">
    <concat destfile="${tmp.file.2}">
      <fileset dir="${basedir}" includes="${tmp.file}" />
      <filelist dir="${basedir}" files="${tmp.file},${tmp.file}" />
    </concat>
  </target>

  <target name="test5">
    <concat>Hello, ${world}!</concat>
  </target>

  <target name="test6">
    <concat destfile="TESTDEST" append="true">
      <filelist dir="${basedir}" files="thisfiledoesnotexist"/>
    </concat>
    <available file="TESTDEST" property="TESTDEST.was.created"/>
    <fail message="TESTDEST created for nonexistant files"
          if="TESTDEST.was.created"/>
  </target>

  <target name="testConcatNoNewline">
    <concat>
      <fileset dir="concat-input"/>
    </concat>
  </target>

  <target name="testConcatNoNewlineEncoding">
    <concat encoding="ASCII">
      <fileset dir="concat-input"/>
    </concat>
  </target>

  <target name="testPath">
    <concat destfile="${tmp.file.2}">
      <path path="${tmp.file}"/>
    </concat>
  </target>

  <target name="testAppend">
    <concat destfile="${tmp.file.2}">
      <path path="${tmp.file}"/>
    </concat>
    <concat destfile="${tmp.file.2}" append="true">
      <path path="${tmp.file}"/>
    </concat>
  </target>

  <target name="testfilter">
    <concat destfile="${tmp.file}">@REPLACEME@</concat>
    <concat>
      <path path="${tmp.file}"/>
      <filterchain>
        <replacetokens>
          <token key="REPLACEME" value="REPLACED"/>
        </replacetokens>
      </filterchain>
    </concat>
  </target>

  <target name="testnooverwrite">
    <touch file="${tmp.file.2}"/>
    <!-- concat.xml is now older than tmp.file.2
         so the following should not do anything -->
    <concat destfile="${tmp.file.2}" overwrite="false">
      <path path="concat.xml"/>
    </concat>
  </target>

  <target name="testoverwrite">
    <touch file="${tmp.file.2}"/>
    <!-- concat.xml is now older than tmp.file.2
         so the following should still overwrite it -->
    <concat destfile="${tmp.file.2}" overwrite="true">
      <path path="concat.xml"/>
    </concat>
  </target>

  <target name="testheaderfooter">
    <concat>
      <header filtering="false" trim="yes">
        header
      </header>
      <path path="${tmp.file}"/>
      <footer filtering="no">footer</footer>
    </concat>
  </target>

  <target name="testfileheader">
    <concat>
      <header file="${tmp.file}"/>
      <path path="${tmp.file}"/>
    </concat>
  </target>

  <target name="samefile">
    <touch file="${tmp.file}"/>
    <concat destfile="${tmp.file}">
      <path path="${tmp.file}"/>
    </concat>
  </target>

  <target name="testfilterinline">
    <concat>
      @REPLACEME@
      <filterchain>
        <replacetokens>
          <token key="REPLACEME" value="REPLACED"/>
        </replacetokens>
      </filterchain>
    </concat>
  </target>
  
  <target name="testmultireader">
    <concat destfile="${tmp.file}">Hello, World
    </concat>
    <concat destfile="${tmp.file.2}">Bye, World
    </concat>
    <concat>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <path path="${tmp.file}"/>
      <!-- tailfilter seems to behave a little stange, place two
           here in case the implementation changes -->
      <path path="${tmp.file.2}"/>
      <path path="${tmp.file.2}"/>
      <filterchain>
        <tailfilter lines="2"/>
      </filterchain>
    </concat>
  </target>
  
  <target name="create-noel">
    <concat destfile="concat.noeol">This has no end of line</concat>
  </target>

  <target name="testfixlastline" depends="create-noel">
    <concat destfile="concat.line4" fixlastline="yes">
      <path path="concat.noeol"/>
      <path path="concat.noeol"/>
      <path path="concat.noeol"/>
      <path path="concat.noeol"/>
    </concat>
  </target>

  <target name="testfixlastlineeol" depends="create-noel">
    <concat destfile="concat.linecr" fixlastline="yes" eol="mac">
      <path path="concat.noeol"/>
      <path path="concat.noeol"/>
    </concat>
  </target>

  <target name="testTranscoding">
    <concat destfile="concat.utf8"
            encoding="ISO8859_1" outputencoding="UTF8">
      <path path="copy/input/iso8859-1"/>
    </concat>
  </target>

</project>
