<?xml version="1.0"?>

<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}" force="false">
      <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>

  <target name="testResources" unless="offline">
    <concat destfile="concat.urls" binary="true">
      <url url="http://ant.apache.org" />
      <url url="http://ant.apache.org" />
    </concat>
    <length property="expected" includeduplicates="true">
      <resources>
        <url url="http://ant.apache.org" />
        <url url="http://ant.apache.org" />
      </resources>
    </length>
    <length property="actual">
      <fileset file="concat.urls" />
    </length>
    <fail>
      <condition>
        <or>
          <equals arg1="${actual}" arg2="0" />
          <not>
            <equals arg1="${actual}" arg2="${expected}" />
          </not>
        </or>
      </condition>
    </fail>
  </target>

</project>
