<?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="fixcrlf" default="cleanup" basedir=".">
  <import file="../../buildfiletest-base.xml"/>

  <target name="setUp">
    <mkdir dir="${input}" />
    <unzip src="input.zip" dest="${input}"/>
    <unzip src="expected.zip" dest="${input}"/>
    <mkdir dir="${output}" />
  </target>


  <macrodef name="assertequal">
    <attribute name="junk" default="" />
    <attribute name="name" default="Junk@{junk}.java" />
    <attribute name="file1" default="${output}/@{name}" />
    <attribute name="file2" default="${input}/expected/@{name}" />
    <sequential>
      <fail message="@{file1} and @{file2} are different">
        <condition>
          <not>
            <filesmatch file1="@{file1}" file2="@{file2}" />
          </not>
        </condition>
      </fail>
    </sequential>
  </macrodef>

  <target name="test1" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk1.java"
             javafiles="true" tab="add" eol="crlf" eof="asis" />
    <assertequal junk="1" />
  </target>

  <target name="test2" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk2.java"
             javafiles="true" tab="add" cr="add" eol="crlf" eof="asis" />
    <assertequal junk="2" />
  </target>

  <target name="test3" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk3.java"
             javafiles="true" tab="remove" eol="lf" eof="asis" />
    <assertequal junk="3" />
  </target>

  <target name="test4" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk4.java"
             javafiles="true" tab="remove" eol="lf" eof="asis" />
    <assertequal junk="4" />
  </target>

  <target name="test5" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk5.java"
             tab="remove" eol="lf" eof="asis" />
    <assertequal junk="5" />
  </target>

  <target name="test6" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk6.java"
             tab="add" cr="remove" eol="crlf" eof="asis" />
    <assertequal junk="6" />
  </target>

  <target name="test7" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk7.java"
             tab="add" cr="add" eof="asis" />
    <assertequal junk="7" />
  </target>

  <target name="test8" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk8.java"
             javafiles="true" tab="add" cr="add" eof="add" />
    <assertequal junk="8" />
  </target>

  <target name="test9" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk9.java"
             javafiles="true" tab="remove" cr="remove" eof="remove" />
    <assertequal junk="9" />
  </target>

  <target name="testMacLines" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Mac2Unix" eol="lf" />
    <assertequal name="Mac2Unix" />
  </target>

  <target name="testNoOverwrite" depends="test1">
    <touch file="${output}/Junk1.java" millis="0" />
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="Junk1.java" preservelastmodified="false"
             javafiles="true" tab="add" eol="crlf" eof="asis" />
    <fail message="overwrote unchanged output file">Q
      <condition>
        <not>
          <isfileselected file="${output}/Junk1.java">
            <date when="equal" millis="0" />
          </isfileselected>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testEncoding" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="input.crlf.utf16"
             javafiles="false" cr="remove" encoding="UnicodeBig" />
    <assertequal file1="${output}/input.crlf.utf16"
                 file2="${input}/expected/input.lf.utf16" />
  </target>

  <target name="testOutputEncoding" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="input.crlf.utf16"
             javafiles="false" eol="lf" encoding="UnicodeBig"
             outputencoding="ascii" />
    <assertequal file1="${output}/input.crlf.utf16"
                 file2="${input}/expected/input.lf.ascii" />
  </target>

  <target name="testLongLines" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="longlines.crlf"
             javafiles="false" cr="remove" />
    <assertequal file1="${output}/longlines.crlf"
                 file2="${input}/expected/longlines.lf" />
  </target>

  <target name="testCrCrLfSequence-unix" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="crcrlf" eol="lf" />
    <assertequal file1="${output}/crcrlf"
                 file2="${input}/expected/crcrlf.unix" />
  </target>

  <target name="testCrCrLfSequence-dos" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="crcrlf" eol="crlf" />
    <assertequal file1="${output}/crcrlf"
                 file2="${input}/expected/crcrlf.dos" />
  </target>

  <target name="testCrCrLfSequence-mac" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="crcrlf" eol="cr" />
    <assertequal file1="${output}/crcrlf"
                 file2="${input}/expected/crcrlf.mac" />
  </target>

  <target name="testFixlastDos" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="fixlastfalse.lf" eol="crlf" />
    <assertequal file1="${output}/fixlastfalse.lf"
                 file2="${input}/expected/fixlast.dos" />
  </target>

  <target name="testFixlastFalseMac" depends="setUp">
    <fixcrlf srcdir="${input}/input" destdir="${output}"
             includes="fixlastfalse.lf" eol="cr" fixlast="false" />
    <assertequal file1="${output}/fixlastfalse.lf"
                 file2="${input}/expected/fixlastfalse.mac" />
  </target>

  <!-- Bugzilla Report 20840 -->
  <target name="createParentDirs" depends="setUp">
    <fixcrlf srcdir="${input}" destdir="${output}" includes="input/Junk1.java" />
  </target>

  <target name="testFixFile" depends="setUp">
    <fixcrlf file="${input}/input/longlines.crlf" destdir="${output}" />
    <fail message="didn't create output file">
      <condition>
        <not>
          <available file="${output}/longlines.crlf" />
        </not>
      </condition>
    </fail>
  </target>

  <target name="testFixFileExclusive" depends="setUp">
    <fixcrlf file="${input}/input/longlines.crlf" srcdir="${input}/input" destdir="${output}"/>
  </target>

  <target name="testPreserveLastModified" depends="setUp">
    <fixcrlf file="${input}/input/longlines.crlf" destdir="${output}"
             preservelastmodified="true" />
    <fail>
      <condition>
        <not>
          <uptodate srcfile="${output}/longlines.crlf"
                    targetfile="${input}/input/longlines.crlf" />
        </not>
      </condition>
    </fail>

    <touch file="${output}/longlines.crlf" millis="0" />

    <fixcrlf file="${output}/longlines.crlf" destdir="${output}" eol="lf"
             preservelastmodified="true" />

    <fileset id="fs" file="${output}/longlines.crlf">
      <date when="equal" millis="0" />
    </fileset>
    <property name="fs" refid="fs" />
    <fail unless="fs" />
  </target>

  <target name="testFilter1" depends="setUp">
    <copy file="${input}/input/Junk1.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="add"
                 eol="crlf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="1" />
  </target>

  <target name="testFilter2" depends="setUp">
    <copy file="${input}/input/Junk2.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="add" cr="add" eol="crlf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="2" />
  </target>

  <target name="testFilter3" depends="setUp">
    <copy file="${input}/input/Junk3.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="remove" eol="lf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="3" />
  </target>

  <target name="testFilter4" depends="setUp">
    <copy file="${input}/input/Junk4.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="remove" eol="lf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="4" />
  </target>

  <target name="testFilter5" depends="setUp">
    <copy file="${input}/input/Junk5.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf tab="remove" eol="lf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="5" />
  </target>

  <target name="testFilter6" depends="setUp">
    <copy file="${input}/input/Junk6.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf tab="add" cr="remove" eol="crlf" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="6" />
  </target>

  <target name="testFilter7" depends="setUp">
    <copy file="${input}/input/Junk7.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf tab="add" cr="add" eof="asis" />
      </filterchain>
    </copy>
    <assertequal junk="7" />
  </target>

  <target name="testFilter8" depends="setUp">
    <copy file="${input}/input/Junk8.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="add" cr="add" eof="add" />
      </filterchain>
    </copy>
    <assertequal junk="8" />
  </target>

  <target name="testFilter9" depends="setUp">
    <copy file="${input}/input/Junk9.java" todir="${output}" overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="remove" cr="remove" eof="remove" />
      </filterchain>
    </copy>
    <assertequal junk="9" />
  </target>

  <target name="testCannotDoubleEof" depends="test8">
    <fixcrlf file="${output}/Junk8.java"
             javafiles="true" tab="add" cr="add" eof="add" />
    <assertequal junk="8" />
  </target>

  <target name="testTabInLiteralInComment" depends="setUp">
    <copy file="${input}/input/tab_in_literal_in_comment" todir="${output}"
          overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="remove" eol="lf" fixlast="false" />
      </filterchain>
    </copy>
    <assertequal name="tab_in_literal_in_comment" />
  </target>
</project>
