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

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

  <property name="dirname" value="work" />
  <property name="dir" location="${output}/${dirname}" />
  <property name="zip" location="${output}/${dirname}.zip" />
  <property name="jar" location="${antunit.tmpdir}/${dirname}.jar" />
  <property name="file" location="${output}/${dirname}/file.txt" />
    
  <condition property="offline">
    <not>
      <or>
        <http url="http://www.apache.org"/>
        <http url="http://www.google.com"/>
      </or>
    </not>
  </condition>

  <target name="setUp">

    <touch mkdirs="true">
      <filelist dir="${dir}/foo/a" files="x,y,z" />
    </touch>

    <copy todir="${dir}/foo" enablemultiplemappings="true">
      <fileset dir="${dir}/foo" />
      <mapper>
        <globmapper handledirsep="true" from="a/*" to="b/*" />
        <globmapper handledirsep="true" from="a/*" to="c/*" />
      </mapper>
    </copy>

    <copy todir="${dir}/bar">
      <fileset dir="${dir}/foo" />
    </copy>

    <property name="foo" location="${dir}/foo" />
    <property name="foo.a" location="${dir}/foo/a" />
    <property name="foo.b" location="${dir}/foo/b" />
    <property name="foo.c" location="${dir}/foo/c" />

    <property name="bar" location="${dir}/bar" />
    <property name="bar.a" location="${dir}/bar/a" />
    <property name="bar.b" location="${dir}/bar/b" />
    <property name="bar.c" location="${dir}/bar/c" />
  </target>

  <target name="tearDown">
    <delete dir="${dir}" />
    <delete file="${zip}" />
    <delete file="${jar}" deleteonexit="true" />
    <delete file="${file}" />
  </target>

  <target name="testfiles1" depends="setUp">
    <au:assertTrue>
      <resourcecount count="26">
        <files>
          <include name="${dir}/foo/" />
          <include name="${dir}/bar/" />
        </files>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfiles2" depends="setUp">
    <au:assertTrue>
      <resourcecount count="8">
        <files>
          <include name="${dir}/foo/" />
          <include name="${dir}/bar/" />
          <type type="dir" />
        </files>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfiles3" depends="setUp">
    <au:assertTrue>
      <resourcecount count="18">
        <files>
          <include name="${dir}/foo/" />
          <include name="${dir}/bar/" />
          <type type="file" />
        </files>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testnestedresources" depends="setUp">
    <au:assertTrue>
      <resourcecount count="27"><!-- include duplicates! -->
        <resources>
          <files>
            <include name="${dir}/foo/" />
            <include name="${dir}/bar/" />
            <type type="file" />
          </files>
          <files>
            <include name="${dir}/foo/" />
            <type type="file" />
          </files>
        </resources>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testresourcesref" depends="setUp">
    <files id="files">
      <include name="${dir}/foo/" />
      <include name="${dir}/bar/" />
      <type type="file" />
    </files>
    <au:assertTrue>
      <resourcecount count="18">
        <resources refid="files" />
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfileset" depends="setUp">
    <au:assertTrue>
      <resourcecount count="9">
        <fileset dir="${dir}/foo" />
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testdirset" depends="setUp">
    <au:assertTrue>
      <resourcecount count="4">
        <dirset dir="${dir}/foo" />
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfilelist">
    <au:assertTrue>
      <resourcecount count="5">
        <filelist dir="${dir}/foo" files="1,2,3,4,5" />
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testpath" depends="setUp">
    <au:assertTrue>
      <resourcecount count="24">
        <path>
          <path id="p">
            <fileset id="fs" dir="${foo.a}" /> <!-- 3 -->
            <dirset id="ds" dir="${foo}" /> <!-- 4 -->
          </path>
          <pathelement id="pe" location="${dir}" /> <!-- 1 -->
          <filelist id="fl" dir="${bar}" files="1,2,3,a,b,c" /> <!-- 6 -->
          <files id="f"> <!-- 13; 3 overlap fl -->
            <include name="${bar}/"/>
          </files>
        </path>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="createzip" depends="setUp">
    <zip destfile="${zip}" basedir="${dir}" />
  </target>

  <target name="testzipfileset" depends="createzip">
    <au:assertTrue>
      <resourcecount count="9">
        <zipfileset src="${zip}" includes="foo/" />
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testpropertyset">
    <property name="testpropertyset.a" value="a" />
    <property name="testpropertyset.aa" value="aa" />
    <property name="testpropertyset.aaa" value="aaa" />
    <propertyset id="testpropertyset">
      <propertyref prefix="testpropertyset." />
    </propertyset>
    <au:assertTrue>
      <and>
        <resourcecount refid="testpropertyset" count="3" />
        <length length="12">
          <resources>
            <resources refid="testpropertyset" />
            <propertyset refid="testpropertyset" />
          </resources>
        </length>
      </and>
    </au:assertTrue>
  </target>

  <target name="testunion" depends="setUp">
    <au:assertTrue>
      <resourcecount count="4">
        <union>
          <files> <!-- 1 -->
            <include name="${foo.a}/w"/>
            <include name="${foo.a}/x"/>
          </files>
          <fileset dir="${foo.a}" includes="x,y" /> <!-- 2; net 1 -->
          <filelist dir="${foo.a}" files="v" /> <!-- 1 -->
          <files>  <!-- 2; net 1 -->
            <include name="${foo.a}/y"/>
            <include name="${foo.a}/z"/>
          </files>
        </union>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testintersect">
    <au:assertTrue>
      <resourcecount count="3">
        <intersect>
          <filelist dir="${dir}" files="1,2,3,4,5" />
          <filelist dir="${dir}" files="2,3,4,5,6" />
          <filelist dir="${dir}" files="3,4,5,6,7" />
        </intersect>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testdifference">
    <au:assertTrue>
      <resourcecount count="2">
        <difference id="diff">
          <filelist dir="${dir}" files="1,2,3,4,5" />
          <filelist dir="${dir}" files="2,3,4,5,6" />
          <filelist dir="${dir}" files="3,4,5,6,7" />
        </difference>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfileurl">
    <au:assertTrue>
      <length when="greater" length="0">
        <url file="${ant.file}" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testfileurlref">
    <url id="fileurl" file="${ant.file}" />
    <au:assertTrue>
      <length when="greater" length="0">
        <url refid="fileurl" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testhttpurl1" unless="offline">
    <au:assertTrue>
      <length when="greater" length="0">
        <url url="http://www.w3.org/MarkUp/" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testhttpurl2" unless="offline">
    <concat destfile="${file}" force="true" append="false">
      <url id="httpurl" url="http://ant.apache.org/index.html" />
      <url refid="httpurl" />
    </concat>
    <length property="httpurl.length">
      <url refid="httpurl" />
    </length>
    <length property="file.length">
      <file file="${file}"/>
    </length>
    <au:assertTrue message="length of url ${httpurl.length} length of file ${file.length} file should be twice as big">
      <length file="${file}" when="greater" length="${httpurl.length}" />
    </au:assertTrue>
  </target>

  <target name="createjar" depends="setUp">
    <jar destfile="${jar}" basedir="${dir}" />
  </target>

  <target name="testjarurl" depends="createjar">
    <pathconvert property="jarurl">
      <url file="${jar}" />
    </pathconvert>
    <au:assertTrue>
      <length when="greater" length="0">
        <url url="jar:${jarurl}!/META-INF/MANIFEST.MF" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testfile" depends="setUp">
    <echo file="${file}">This is a test.</echo>
    <au:assertTrue>
      <length length="15">
        <file file="${file}" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testzipentry" depends="createjar">
    <au:assertTrue>
      <length when="greater" length="0">
        <zipentry zipfile="${jar}" name="META-INF/MANIFEST.MF" />
      </length>
    </au:assertTrue>
  </target>

  <target name="teststring1">
    <au:assertTrue>
      <length length="15">
        <string value="This is a test." />
      </length>
    </au:assertTrue>
  </target>

  <target name="teststring2">
    <property name="test" value="foo" />
    <au:assertTrue>
      <length length="14">
        <string value="This is a ${test}." />
      </length>
    </au:assertTrue>
  </target>

  <target name="testresource">
    <au:assertTrue>
      <length length="4096">
        <resource size="4096" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testproperty">
    <property name="testproperty" value="abcdefghij" />
    <au:assertTrue>
      <length length="10">
        <propertyresource name="testproperty" />
      </length>
    </au:assertTrue>
  </target>

  <target name="testPropertyResolvedAsResource">
    <string id="s" value="abcdefghij" />
    <au:assertTrue>
      <resourcesmatch>
        <resource refid="s" />
        <propertyresource name="ant.refid:s" />
      </resourcesmatch>
    </au:assertTrue>
  </target>

  <target name="testfirst0">
    <au:assertTrue>
      <resourcecount count="0">
        <first count="0">
          <filelist dir="${dir}" files="1,2,3,4,5" />
        </first>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfirst1">
    <au:assertTrue>
      <resourcecount count="1">
        <first>
          <filelist dir="${dir}" files="1,2,3,4,5" />
        </first>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testfirst2">
    <au:assertTrue>
      <resourcecount count="2">
        <first count="2">
          <filelist dir="${dir}" files="1,2,3,4,5" />
        </first>
      </resourcecount>
    </au:assertTrue>
  </target>

  <target name="testJavaConstant">
    <property name="test.tmp.dir" value="${antunit.tmpdir}/testJavaConstant"/>
    <mkdir dir="${test.tmp.dir}"/>
    <echo file="${test.tmp.dir}/SomeClass.java">
      public class SomeClass {
          public  static final String CONSTANT   = "constant";
          public         final String NOT_STATIC = "not-static";
          private static final String PRIVATE    = "private";
      }
    </echo>
    <javac srcdir="${test.tmp.dir}" destdir="${test.tmp.dir}"/>
    <path id="tmp.cp">
      <pathelement location="${test.tmp.dir}"/>
    </path>

    <loadresource property="actual">
      <javaconstant name="org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME"/>
    </loadresource>
    <au:assertEquals message="could not read java constant" expected="build.xml" actual="${actual}" />

    <!-- 
    We can't test for special error messages using built-in tasks 
    because they catch these messages 
    -->
    <au:expectfailure>
      <loadresource property="p">
        <javaconstant/>
      </loadresource>
    </au:expectfailure>
    <au:expectfailure>
      <loadresource property="p">
        <javaconstant name="org.apache.tools.ant.MissingClass"/>
      </loadresource>
    </au:expectfailure>
    <au:expectfailure>
      <loadresource property="p">
        <javaconstant name="SomeClass.CONSTANT2" classpathref="tmp.cp"/>
      </loadresource>
     </au:expectfailure>
    <au:expectfailure>
      <loadresource property="p">
        <javaconstant name="SomeClass.PRIVATE">
          <classpath>
            <pathelement location="${test.tmp.dir}"/>
          </classpath>
        </javaconstant>
      </loadresource>
     </au:expectfailure>
    <au:expectfailure>
      <loadresource property="p">
        <javaconstant name="SomeClass.NOT_STATIC"/>
      </loadresource>
     </au:expectfailure>
    
    <delete dir="${test.tmp.dir}"/>
  </target>

</project>
