<?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=".">
  <target name="init">
    <mkdir dir="result" />
  </target>

  <target name="cleanup">
    <delete dir="result" />
  </target>

  <macrodef name="assertequal">
    <attribute name="junk" default="" />
    <attribute name="name" default="Junk@{junk}.java" />
    <attribute name="file1" default="result/@{name}" />
    <attribute name="file2" default="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="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Junk1.java"
             javafiles="true" tab="add" eol="crlf" eof="asis" />
    <assertequal junk="1" />
  </target>

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

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

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

  <target name="test5" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Junk5.java"
             tab="remove" eol="lf" eof="asis" />
    <assertequal junk="5" />
  </target>

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

  <target name="test7" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Junk7.java"
             tab="add" cr="add" eof="asis" />
    <assertequal junk="7" />
  </target>

  <target name="test8" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Junk8.java"
             javafiles="true" tab="add" cr="add" eof="add" />
    <assertequal junk="8" />
  </target>

  <target name="test9" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Junk9.java"
             javafiles="true" tab="remove" cr="remove" eof="remove" />
    <assertequal junk="9" />
  </target>

  <target name="testMacLines" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="Mac2Unix" eol="lf" />
    <assertequal name="Mac2Unix" />
  </target>

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

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

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

  <target name="testLongLines" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="longlines.crlf"
             javafiles="false" cr="remove" />
    <assertequal file1="result/longlines.crlf"
                 file2="expected/longlines.lf" />
  </target>

  <target name="testCrCrLfSequence-unix" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="crcrlf" eol="lf" />
    <assertequal file1="result/crcrlf"
                 file2="expected/crcrlf.unix" />
  </target>

  <target name="testCrCrLfSequence-dos" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="crcrlf" eol="crlf" />
    <assertequal file1="result/crcrlf"
                 file2="expected/crcrlf.dos" />
  </target>

  <target name="testCrCrLfSequence-mac" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="crcrlf" eol="cr" />
    <assertequal file1="result/crcrlf"
                 file2="expected/crcrlf.mac" />
  </target>

  <target name="testFixlastDos" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="fixlastfalse.lf" eol="crlf" />
    <assertequal file1="result/fixlastfalse.lf"
                 file2="expected/fixlast.dos" />
  </target>

  <target name="testFixlastFalseMac" depends="init">
    <fixcrlf srcdir="input" destdir="result"
             includes="fixlastfalse.lf" eol="cr" fixlast="false" />
    <assertequal file1="result/fixlastfalse.lf"
                 file2="expected/fixlastfalse.mac" />
  </target>

  <!-- Bugzilla Report 20840 -->
  <target name="createParentDirs" depends="init">
    <fixcrlf srcdir="." destdir="result" includes="input/Junk1.java" />
  </target>

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

  <target name="testFixFileExclusive" depends="init">
    <fixcrlf file="input/longlines.crlf" srcdir="input" destdir="result"/>
  </target>

  <target name="testPreserveLastModified" depends="init">
    <fixcrlf file="input/longlines.crlf" destdir="result"
             preservelastmodified="true" />
    <fail>
      <condition>
        <not>
          <uptodate srcfile="result/longlines.crlf"
                    targetfile="input/longlines.crlf" />
        </not>
      </condition>
    </fail>

    <touch file="result/longlines.crlf" millis="0" />

    <fixcrlf file="result/longlines.crlf" destdir="result" eol="lf"
             preservelastmodified="true" />

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

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

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

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

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

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

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

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

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

  <target name="testFilter9" depends="init">
    <copy file="input/Junk9.java" todir="result" 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="result/Junk8.java"
             javafiles="true" tab="add" cr="add" eof="add" />
    <assertequal junk="8" />
  </target>

  <target name="testTabInLiteralInComment">
    <copy file="input/tab_in_literal_in_comment" todir="result"
          overwrite="true">
      <filterchain>
        <fixcrlf javafiles="true" tab="remove" eol="lf" fixlast="false" />
      </filterchain>
    </copy>
    <assertequal name="tab_in_literal_in_comment" />
  </target>
</project>
