<?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

      https://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="depend" basedir="." default="help">
  <import file="../../../buildfiletest-base.xml"/>

    <property name="tempsrc.dir" value="${output}/tempsrc.dir"/>
    <property name="cache.dir" value="${output}/tempsrc.dir"/>
    <property name="classes.dir" value="${output}/classes"/>

  <target name="setUp">
    <mkdir dir="${output}" />

  </target>

  <property name="src1.dir" value="src1"/>
  <property name="src2.dir" value="src2"/>
  <property name="src3.dir" value="src3"/>
  <property name="src4.dir" value="src4"/>
  <property name="src5.dir" value="src5"/>
  <property name="src6.dir" value="src6"/>

  <condition property="atleast.java9">
    <javaversion atleast="9"/>
  </condition>

  <target name="help">
    <echo>This buildfile is used as part of Ant's test suite.</echo>
  </target>

  <target name="src1setup" depends="setUp">
    <copy todir="${tempsrc.dir}">
      <fileset dir="${src1.dir}"/>
    </copy>
  </target>

  <target name="src2setup" depends="setUp">
    <copy todir="${tempsrc.dir}">
      <fileset dir="${src2.dir}"/>
    </copy>
  </target>

  <target name="src3setup" depends="setUp">
    <copy todir="${tempsrc.dir}">
      <fileset dir="${src3.dir}"/>
    </copy>
  </target>

  <target name="src4setup" depends="setUp">
    <copy todir="${tempsrc.dir}">
      <fileset dir="${src4.dir}"/>
    </copy>
  </target>

  <target name="src5setup" depends="setUp">
    <copy todir="${tempsrc.dir}">
      <fileset dir="${src5.dir}"/>
    </copy>
  </target>

  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${tempsrc.dir}" destdir="${classes.dir}"/>
  </target>

  <target name="testdirect">
    <delete file="${tempsrc.dir}/C.java"/>
    <copy file="${src1.dir}/C.java" tofile="${tempsrc.dir}/C.java"/>
    <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}"/>
    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testclosure">
    <delete file="${tempsrc.dir}/C.java"/>
    <copy file="${src1.dir}/C.java" tofile="${tempsrc.dir}/C.java"/>
    <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}" closure="yes"/>
    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testbasicset" depends="src1setup, compile">
    <classfileset id="result" dir="${classes.dir}" rootclass="A"/>
  </target>

  <target name="testsmallset" depends="src1setup, compile">
    <classfileset id="result" dir="${classes.dir}" rootclass="B"/>
  </target>

  <target name="testresourcecollection" depends="testsmallset">
    <fail>
      <condition>
        <not>
          <and>
            <resourcecount count="2" refid="result" />
            <resourcecount count="1">
              <intersect>
                <resources refid="result" />
                <file file="${classes.dir}/B.class" />
              </intersect>
            </resourcecount>
            <resourcecount count="1">
              <intersect>
                <resources refid="result" />
                <file file="${classes.dir}/C.class" />
              </intersect>
            </resourcecount>
          </and>
        </not>
      </condition>
    </fail>
  </target>

  <target name="testcomboset" depends="src1setup, compile">
    <classfileset id="result" dir="${classes.dir}" rootclass="B">
      <include name="**/C.class"/>
    </classfileset>
  </target>

  <target name="testbyreference" depends="src1setup, compile">
    <classfileset id="classSet" dir="${classes.dir}" rootclass="A">
      <include name="**/C.class"/>
    </classfileset>
    <jar destfile="${tempsrc.dir}/test.jar">
      <fileset refid="classSet"/>
    </jar>
  </target>

  <target name="testmethodparam" depends="src1setup, compile">
    <classfileset id="result" dir="${classes.dir}" rootclass="E"/>
  </target>

  <target name="testinner">
    <delete file="${tempsrc.dir}/B.java"/>
    <copy file="${src2.dir}/B.java" tofile="${tempsrc.dir}/B.java"/>
    <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}" closure="yes"/>
    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testinnerinner">
    <delete file="${tempsrc.dir}/B.java"/>
    <copy file="${src3.dir}/B.java" tofile="${tempsrc.dir}/B.java"/>
    <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}" closure="yes"/>
    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testmethodparaminner" depends="src4setup, compile">
    <classfileset id="result" dir="${classes.dir}" rootclass="test.MethodParam"/>
  </target>  

  <target name="testnosource" depends="src1setup, compile">
    <depend destdir="${classes.dir}" closure="yes"/>
  </target>

  <target name="testemptysource" depends="src1setup, compile">
    <depend srcdir="" destdir="${classes.dir}" closure="yes"/>
  </target>

  <target name="testinnerclosure" depends="src4setup">
    <mkdir dir="${classes.dir}"/>
    <path id="path.compile">
        <pathelement location="${classes.dir}"/>
    </path>

    <javac srcdir="${tempsrc.dir}" destdir="${classes.dir}"
           classpathref="path.compile" fork="false" />

   <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}"
            closure="yes" dump="yes"
            classpathref="path.compile"/>

    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testcache" depends="src1setup, compile">
    <depend cache="${cache.dir}" srcdir="${tempsrc.dir}" 
            destdir="${classes.dir}" closure="yes"/>
    <depend cache="${cache.dir}" srcdir="${tempsrc.dir}" 
            destdir="${classes.dir}" closure="yes"/>
  </target>

  <target name="testnonpublic">
    <delete file="${tempsrc.dir}/B.java"/>
    <copy file="${src2.dir}/B.java" tofile="${tempsrc.dir}/B.java"/>
    <depend srcdir="${tempsrc.dir}" destdir="${classes.dir}" closure="yes"/>
    <fileset id="result" dir="${classes.dir}"/>
  </target>

  <target name="testmoduleinfo" if="${atleast.java9}">
    <mkdir dir="${output}/java9"/>
    <javac srcdir="${src6.dir}" destdir="${output}/java9" source="9" target="9"/>
    <depend srcdir="${src6.dir}" destdir="${output}/java9"/>
  </target>
</project>
