<?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="length-test" default="antunit"
         xmlns:au="antlib:org.apache.ant.antunit">

  <import file="../antunit-base.xml" />

  <property name="dir.a" location="${input}/a" />
  <property name="dir.b" location="${input}/b" />
  <property name="zipfile" location="${output}/lengthtest.zip" />

  <target name="setUp">
    <mkdir dir="${output}" />
    <mkdir dir="${dir.a}" />
    <mkdir dir="${dir.b}" />
    <property name="foo" location="${dir.a}/foo" />
    <property name="bar" location="${dir.b}/bar" />
    <echo file="${foo}" message="foo" />
    <echo file="${bar}" message="bar" />
  </target>

  <target name="testEach" depends="setUp">
    <length mode="each" property="length.each">
      <fileset id="fs" dir="${input}" />
    </length>
    <length string="${length.each}" property="length.length.each" />
    <length string="${foo}${bar}........${line.separator}"
            property="length.expected" />

    <au:assertTrue>
      <!-- test that both files are represented, and that the
           output is the expected length; do not assume order. -->
      <and>
        <contains string="${length.each}" substring="${foo} : 3" />
        <contains string="${length.each}" substring="${bar} : 3" />
        <equals arg1="${length.length.each}" arg2="${length.expected}" />
      </and>
    </au:assertTrue>
  </target>

  <target name="testEachCondition" depends="setUp">
    <length mode="each" property="length.each">
      <fileset id="fs" dir="${input}" />
    </length>
    <length string="${foo}${bar}........${line.separator}"
            property="length.expected" />
    <au:assertTrue>
      <!-- test that both files are represented, and that the
           output is the expected length; do not assume order. -->
      <and>
        <contains string="${length.each}" substring="${foo} : 3" />
        <contains string="${length.each}" substring="${bar} : 3" />
        <length string="${length.each}" length="${length.expected}" />
      </and>
    </au:assertTrue>
  </target>

  <target name="testAll" depends="setUp">
    <length property="length.all">
      <fileset id="foo" file="${dir.a}/foo" />
      <fileset id="bar" file="${dir.b}/bar" />
    </length>
    <au:assertTrue>
      <equals arg1="6" arg2="${length.all}" />
    </au:assertTrue>
  </target>

  <target name="testAllCondition" depends="setUp">
    <au:assertTrue>
      <length length="6">
        <fileset id="foo" file="${dir.a}/foo" />
        <fileset id="bar" file="${dir.b}/bar" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testFile" depends="setUp">
    <length property="length.foo" file="${dir.a}/foo" />
    <au:assertTrue>
      <equals arg1="3" arg2="${length.foo}" />
    </au:assertTrue>
  </target>

  <target name="testFileCondition" depends="setUp">
    <au:assertTrue>
      <length length="3" file="${dir.a}/foo" />
    </au:assertTrue>
  </target>

  <target name="testBoth" depends="setUp">
    <length property="length.foo" file="${dir.a}/foo">
      <fileset file="${dir.b}/bar" />
    </length>
    <au:assertTrue>
      <equals arg1="6" arg2="${length.foo}" />
    </au:assertTrue>
  </target>

  <target name="testBothCondition" depends="setUp">
    <au:assertTrue>
      <length length="6" file="${dir.a}/foo">
        <fileset file="${dir.b}/bar" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testDupes" depends="setUp">
    <length property="length.foo" file="${dir.a}/foo">
      <fileset dir="${input}" />
    </length>
    <au:assertTrue>
      <equals arg1="9" arg2="${length.foo}" />
    </au:assertTrue>
  </target>

  <target name="testDupesCondition" depends="setUp">
    <au:assertTrue>
      <length length="9" file="${dir.a}/foo">
        <fileset dir="${input}" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testString">
    <length string="foo" property="length.string" />
    <au:assertTrue>
      <equals arg1="3" arg2="${length.string}" />
    </au:assertTrue>
  </target>

  <target name="testStringCondition">
    <au:assertTrue>
      <length string="foo" length="3" />
    </au:assertTrue>
  </target>

  <target name="testTrimString">
    <length string=" foo " trim="true" property="length.string" />
    <au:assertTrue>
      <equals arg1="3" arg2="${length.string}" />
    </au:assertTrue>
  </target>

  <target name="testTrimStringCondition">
    <au:assertTrue>
      <length string=" foo " trim="true" length="3" />
    </au:assertTrue>
  </target>

  <target name="testNoTrimString">
    <length string=" foo " property="length.string" />
    <au:assertTrue>
      <equals arg1="5" arg2="${length.string}" />
    </au:assertTrue>
  </target>

  <target name="testNoTrimStringCondition">
    <au:assertTrue>
      <length string=" foo " length="5" />
    </au:assertTrue>
  </target>

  <target name="testTrimFile" description="should fail">
    <au:expectfailure>
      <length file="${ant.file}" trim="false" />
    </au:expectfailure>
  </target>

  <target name="testStringFile" description="should fail">
    <au:expectfailure>
      <length string="foo" file="${ant.file}" />
    </au:expectfailure>
  </target>

  <target name="testImmutable">
    <length string="foo" property="length.string" />
    <length string="foobar" property="length.string" />
    <au:assertTrue>
      <equals arg1="3" arg2="${length.string}" />
    </au:assertTrue>
  </target>

  <target name="zip" depends="setUp">
    <zip destfile="${zipfile}">
      <fileset file="${foo}" />
      <fileset file="${bar}" />
    </zip>
  </target>

  <target name="testZipFileSet" depends="zip">
    <length property="length.zipfile1">
      <zipfileset src="${zipfile}" />
    </length>
    <length property="length.zipfile2">
      <zipfileset src="${zipfile}" includes="bar" />
    </length>
    <au:assertTrue>
      <and>
        <equals arg1="6" arg2="${length.zipfile1}" />
        <equals arg1="3" arg2="${length.zipfile2}" />
      </and>
    </au:assertTrue>
  </target>

  <target name="testZipFileSetCondition" depends="zip">
    <au:assertTrue>
      <and>
        <length length="6">
          <zipfileset src="${zipfile}" />
        </length>
        <length length="3">
          <zipfileset src="${zipfile}" includes="bar" />
        </length>
      </and>
    </au:assertTrue>
  </target>

</project>
