<project default="all"
         xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">

  <property name="dirname" value="work" />
  <property name="dir" location="${dirname}" />
  <property name="zip" location="${dirname}.zip" />
  <property name="jar" location="${dirname}.jar" />
  <property name="file" location="${dirname}/file.txt" />

  <target name="init">

    <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="init">
    <fail>
      <condition>
        <not>
          <resourcecount count="26">
            <files>
              <include name="${dir}/foo/" />
              <include name="${dir}/bar/" />
            </files>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testfiles2" depends="init">
    <fail>
      <condition>
        <not>
          <resourcecount count="8">
            <files>
              <include name="${dir}/foo/" />
              <include name="${dir}/bar/" />
              <type type="dir" />
            </files>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testfiles3" depends="init">
    <fail>
      <condition>
        <not>
          <resourcecount count="18">
            <files>
              <include name="${dir}/foo/" />
              <include name="${dir}/bar/" />
              <type type="file" />
            </files>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="files" depends="testfiles1,testfiles2,testfiles3" />

  <target name="testnestedresources" depends="init">
    <fail>
      <condition>
        <not>
          <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>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testresourcesref" depends="init">
    <files id="files">
      <include name="${dir}/foo/" />
      <include name="${dir}/bar/" />
      <type type="file" />
    </files>
    <fail>
      <condition>
        <not>
          <resourcecount count="18">
            <resources refid="files" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="resources" depends="testnestedresources,testresourcesref" />

  <target name="testfileset" depends="init">
    <fail>
      <condition>
        <not>
          <resourcecount count="9">
            <fileset dir="${dir}/foo" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testdirset" depends="init">
    <fail>
      <condition>
        <not>
          <resourcecount count="4">
            <dirset dir="${dir}/foo" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testfilelist">
    <fail>
      <condition>
        <not>
          <resourcecount count="5">
            <filelist dir="${dir}/foo" files="1,2,3,4,5" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testpath" depends="init">
    <fail>
      <condition>
        <not>
          <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" includes="${bar}/" /> <!-- 13; 3 overlap fl -->
            </path>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

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

  <target name="testzipfileset" depends="createzip">
    <fail>
      <condition>
        <not>
          <resourcecount count="9">
            <zipfileset src="${zip}" includes="foo/" />
          </resourcecount>
        </not>
      </condition>
    </fail>
  </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>
    <fail>
      <condition>
        <not>
          <and>
            <resourcecount refid="testpropertyset" count="3" />
            <length length="12">
              <resources>
                <resources refid="testpropertyset" />
                <propertyset refid="testpropertyset" />
              </resources>
            </length>
          </and>
        </not>
      </condition>
    </fail>
  </target>

  <target name="legacy"
depends="testfileset,testdirset,testfilelist,testpath,testzipfileset,testpropertyset" />

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

  <target name="testintersect">
    <fail>
      <condition>
        <not>
          <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>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testdifference">
    <fail>
      <condition>
        <not>
          <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>
        </not>
      </condition>
    </fail>
  </target>

  <target name="setlogic" depends="testunion,testintersect,testdifference" />

  <target name="testfileurl">
    <condition property="blah">
      <length when="less" length="1">
        <url file="${ant.file}" />
      </length>
    </condition>
    <fail if="blah" />
  </target>

  <target name="testfileurlref">
    <url id="fileurl" file="${ant.file}" />
    <condition property="blah">
      <length when="less" length="1">
        <url refid="fileurl" />
      </length>
    </condition>
    <fail if="blah" />
  </target>

  <target name="testhttpurl1" unless="offline">
    <fail>
      <condition>
        <length when="less" length="1">
          <url url="http://ant.apache.org/index.html" />
        </length>
      </condition>
    </fail>
  </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>
    <fail>
      <condition>
        <not>
          <length file="${file}" when="greater" length="${httpurl.length}" />
        </not>
      </condition>
    </fail>
  </target>

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

  <target name="testjarurl" depends="createjar">
    <pathconvert property="jarurl">
      <url file="${jar}" />
    </pathconvert>
    <fail>
      <condition>
        <length when="less" length="1">
          <url url="jar:${jarurl}!/META-INF/MANIFEST.MF" />
        </length>
      </condition>
    </fail>
  </target>

  <target name="url"
depends="testfileurl,testfileurlref,testhttpurl1,testhttpurl2,testjarurl" />

  <target name="testfile" depends="init">
    <echo file="${file}">This is a test.</echo>
    <fail>
      <condition>
        <not>
          <length length="15">
            <file file="${file}" />
          </length>
        </not>
      </condition>
    </fail>
  </target>

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

  <target name="teststring1">
    <fail>
      <condition>
        <not>
          <length length="15">
            <string value="This is a test." />
          </length>
        </not>
      </condition>
    </fail>
  </target>

  <target name="teststring2">
    <property name="test" value="foo" />
    <fail>
      <condition>
        <not>
          <length length="14">
            <string value="This is a ${test}." />
          </length>
        </not>
      </condition>
    </fail>
  </target>

  <target name="string" depends="teststring1,teststring2" />

  <target name="testresource">
    <fail>
      <condition>
        <not>
          <length length="4096">
            <resource size="4096" />
          </length>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testproperty">
    <property name="testproperty" value="abcdefghij" />
    <fail>
      <condition>
        <not>
          <length length="10">
            <propertyresource name="testproperty" />
          </length>
        </not>
      </condition>
    </fail>
  </target>

  <target name="single"
    depends="testresource,url,testfile,string,testzipentry,testproperty" />

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

  <target name="testfirst1">
    <fail>
      <condition>
        <not>
          <resourcecount count="1">
            <first>
              <filelist dir="${dir}" files="1,2,3,4,5" />
            </first>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testfirst2">
    <fail>
      <condition>
        <not>
          <resourcecount count="2">
            <first count="2">
              <filelist dir="${dir}" files="1,2,3,4,5" />
            </first>
          </resourcecount>
        </not>
      </condition>
    </fail>
  </target>

  <target name="first" depends="testfirst0,testfirst1,testfirst2" />

  <target name="all" depends="legacy,files,resources,setlogic,single,first" />

</project>
