<!-- -*- Mode: SGML; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-     -->
<!--                                                                                -->
<!-- This file is part of the compiler and core tools for the AspectJ(tm)           -->
<!-- programming language; see http://aspectj.org                                   -->
<!--                                                                                -->
<!-- The contents of this file are subject to the Mozilla Public License            -->
<!-- Version 1.1 (the "License"); you may not use this file except in               -->
<!-- compliance with the License. You may obtain a copy of the License at           -->
<!-- either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.                 -->
<!--                                                                                -->
<!-- Software distributed under the License is distributed on an "AS IS" basis,     -->
<!-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License       -->
<!-- for the specific language governing rights and limitations under the           -->
<!-- License.                                                                       -->
<!--                                                                                -->
<!-- The Original Code is AspectJ.                                                  -->
<!--                                                                                -->
<!-- The Initial Developer of the Original Code is Xerox Corporation. Portions      -->
<!-- created by Xerox Corporation are Copyright (C) 1999, 2000 Xerox Corporation.   -->
<!-- All Rights Reserved.                                                           -->

<!-- ============================================================================== -->
<!-- To do a build, invoke build/bin/ant.[sh|bat] after defining JDKDIR.            -->
<!-- To customize properties, edit local.properties.examples as local.properties.   -->
<!-- ============================================================================== -->
<!-- todo: non-distribution license?? -->

<!-- standalone script to be run in examples dir -->
<!-- currently relies on deployed taskdefs to ant.home, instead of -->
<!-- using taskdef classpaths (aka safe install)  -->

<project name="test-taskdefs" default="test" basedir="..">

    <!-- ========================================================================== -->
    <!-- Init                                                                       -->
    <!-- ========================================================================== -->
    <target name="init" 
     description="caller must define jdk.tools.jar and product.dir or run from product.examples.dir">
      <echo message="init test-taskdefs.xml"/> 

      <property name="PS" value="${path.separator}"/> 

      <!-- define product.dir or run from it -->
      <property name="product.dir"
            location="${basedir}"/> 

      <!-- define local.deploy.dir to get different tools and taskdefs -->
      <property name="local.deploy.dir"
            location="${product.dir}"/> 
      <!-- expected from install -->
      <property name="ex.dir"
            location="${product.dir}/examples"/> 
      <property name="product.examples.dir"
            location="${ex.dir}"/> 
      <!-- created after install -->
      <property name="test.dir"
            location="${product.dir}/test"/> 
      <property name="expect.file"
            location="${test.dir}/expect.out"/> 
      <property name="actual.file"
            location="${test.dir}/actual.out"/> 
      <property name="empty.dir"
            location="${test.dir}/emptydir"/> 
      <property name="classes.dir"
            location="${test.dir}/classes"/> 
      <property name="expected.classes.dir"
            location="${test.dir}/expectedClasses"/> 
      <property name="working.dir"
            location="${product.dir}/workingdir"/> 
      <property name="expected.working.dir"
            location="${test.dir}/expectedWorkingdir"/> 
      <mkdir dir="${empty.dir}"/>
      <mkdir dir="${classes.dir}"/>
      <mkdir dir="${expected.classes.dir}"/>
      <mkdir dir="${working.dir}"/>
      <mkdir dir="${expected.working.dir}"/>


      <property name="build-tools.jar"
            location="${ant.home}/lib/build-tools.jar"/> 

      <property name="safe.classpath" value="yes"/> 

      <!-- unsafe classpaths in ant/lib -->
      <property name="aj-testing.jar"
            location="${ant.home}/lib/aj-testing.jar"/> 
      <property name="aj-testing-taskdefs.jar"
            location="${ant.home}/lib/aj-testing-taskdefs.jar"/> 
      <property name="aspectj-ant.jar"
            location="${ant.home}/lib/aspectj-ant.jar"/> 
      <property name="aspectjtools.jar"
            location="${ant.home}/lib/aspectjtools.jar"/> 
      <property name="aspectjrt.jar"
            location="${product.dir}/lib/aspectjrt.jar"/> 

      <!-- safe classpaths in ant/lib/optional -->
      <property name="safe.aj-testing.jar"
            location="${ant.home}/lib/optional/aj-testing.jar"/> 
      <property name="safe.aj-testing-taskdefs.jar"
            location="${ant.home}/lib/optional/aj-testing-taskdefs.jar"/> 
      <property name="safe.aspectj-ant.jar"
            location="${ant.home}/lib/optional/aspectj-ant.jar"/> 
      <property name="safe.aspectjtools.jar"
            location="${ant.home}/lib/optional/aspectjtools.jar"/> 
      <property name="safe.aspectjrt.jar"
            location="${ant.home}/lib/optional/aspectjrt.jar"/> 

      <available file="${build-tools.jar}"
             property="build-tools.jar.available"/>

      <available file="${aspectjtools.jar}"
             property="aspectjtools.jar.available"/>
      <available file="${aspectjrt.jar}"
             property="aspectjrt.jar.available"/>
      <available file="${aspectj-ant.jar}"
             property="aspectj-ant.jar.available"/>
      <available file="${aj-testing.jar}"
             property="aj-testing.jar.available"/>
      <available file="${aj-testing-taskdefs.jar}"
             property="aj-testing-taskdefs.jar.available"/>

      <available file="${safe.aspectjtools.jar}"
             property="safe.aspectjtools.jar.available"/>
      <available file="${safe.aspectjrt.jar}"
             property="safe.aspectjrt.jar.available"/>
      <available file="${safe.aspectj-ant.jar}"
             property="safe.aspectj-ant.jar.available"/>
      <available file="${safe.aj-testing.jar}"
             property="safe.aj-testing.jar.available"/>
      <available file="${safe.aj-testing-taskdefs.jar}"
             property="safe.aj-testing-taskdefs.jar.available"/>

      <property name="run.classpath"
            location="${classes.dir}${PS}${aspectjrt.jar}"/> 
      <property name="simple.run.classpath"
            location="${product.examples.dir}${PS}${aspectjrt.jar}"/> 

      <property name="aj.public.taskdef.package"
               value="org.aspectj.tools.ant.taskdefs"/> 

    </target>

    <target name="setup.taskdef.classpath"
         depends="setup.taskdef.classpath.unsafe,setup.taskdef.classpath.safe" />

    <target name="setup.taskdef.classpath.safe" if="safe.classpath" >
      <property name="safe.classpath" value="yes"/> 
      <property name="use.safe.classpath" value="yes"/> 
      <property name="use.fork" value="${use.safe.classpath}"/> 
    </target>
    <target name="setup.taskdef.classpath.unsafe" unless="safe.classpath" >
      <property name="use.safe.classpath" value="no"/> 
      <property name="use.fork" value="${use.safe.classpath}"/> 
    </target>

    <target name="init.taskdefs"
         depends="setup.taskdef.classpath,init.taskdefs.safe,init.taskdefs.unsafe" />

    <target name="init.taskdefs.unsafe"
         depends="init,setup.taskdef.classpath" 
          unless="safe.classpath"
     description="initialize taskdefs relying on ant.home/lib" >
      <antcall target="unsafe.jars.available"/>
      <taskdef name="ajc"
          classname="${aj.public.taskdef.package}.Ajc" />
      <taskdef name="ajdoc"
          classname="${aj.public.taskdef.package}.Ajdoc"/>
      <taskdef name="comparefiles"
          classname="org.aspectj.testing.taskdefs.CompareFiles"/>
    </target>

    <target name="init.taskdefs.safe"
         depends="init,setup.taskdef.classpath" 
              if="safe.classpath"
     description="initialize taskdefs without ant.home/lib" >
      <antcall target="safe.jars.available"/>
      <taskdef name="ajc"
          classname="${aj.public.taskdef.package}.Ajc" >
          <classpath> 
            <pathelement path="${safe.aspectjtools.jar}"/> 
            <pathelement path="${safe.aspectj-ant.jar}"/> 
          </classpath>
       </taskdef>
      <taskdef name="ajdoc"
          classname="${aj.public.taskdef.package}.Ajdoc" >
          <classpath> 
            <pathelement path="${safe.aspectjtools.jar}"/> 
            <pathelement path="${safe.aspectj-ant.jar}"/> 
          </classpath>
       </taskdef>
      <taskdef name="comparefiles"
          classname="org.aspectj.testing.taskdefs.CompareFiles">
          <classpath> 
            <pathelement path="${safe.aj-testing.jar}"/> 
            <pathelement path="${safe.aj-testing-taskdefs.jar}"/> 
          </classpath>
       </taskdef>
    </target>


    <!--   -->
    <target name="safe.jars.available" 
         depends="safe.aspectjtools.jar.available,safe.aspectj-ant.jar.available,
                  safe.aj-testing.jar.available,safe.aj-testing-taskdefs.jar.available,"/>

    <target name="unsafe.jars.available" 
         depends="aspectjtools.jar.available,aspectj-ant.jar.available,
                  aj-testing.jar.available,aj-testing-taskdefs.jar.available,"/>
   
    <target name="build-tools.jar.available" depends="init" 
          unless="build-tools.jar.available" >
      <fail message="expecting build-tools.jar at ${build-tools.jar}"/>
    </target>
    <target name="aj-testing.jar.available" depends="init" 
          unless="aj-testing.jar.available" >
      <fail message="expecting aj-testing.jar at ${aj-testing.jar}"/>
    </target>
    <target name="aj-testing-taskdefs.jar.available" depends="init" 
          unless="aj-testing-taskdefs.jar.available" >
      <fail message="expecting aj-testing-taskdefs.jar at ${aj-testing-taskdefs.jar}"/>
    </target>
    <target name="aspectjtools.jar.available" depends="init" 
          unless="aspectjtools.jar.available" >
      <fail message="expecting aspectjtools.jar at ${aspectjtools.jar}"/>
    </target>
    <target name="aspectj-ant.jar.available" depends="init" 
          unless="aspectj-ant.jar.available" >
      <fail message="expecting aspectj-ant.jar at ${aspectj-ant.jar}"/>
    </target>

    <target name="safe.aj-testing.jar.available" depends="init" 
          unless="safe.aj-testing.jar.available" >
      <fail message="expecting safe.aj-testing.jar at ${safe.aj-testing.jar}"/>
    </target>
    <target name="safe.aj-testing-taskdefs.jar.available" depends="init" 
          unless="safe.aj-testing-taskdefs.jar.available" >
      <fail message="expecting safe.aj-testing-taskdefs.jar at ${safe.aj-testing-taskdefs.jar}"/>
    </target>
    <target name="safe.aspectjtools.jar.available" depends="init" 
          unless="safe.aspectjtools.jar.available" >
      <fail message="expecting safe.aspectjtools.jar at ${safe.aspectjtools.jar}"/>
    </target>
    <target name="safe.aspectj-ant.jar.available" depends="init" 
          unless="safe.aspectj-ant.jar.available" >
      <fail message="expecting safe.aspectj-ant.jar at ${safe.aspectj-ant.jar}"/>
    </target>

    <target name="clean" depends="init"
     description="todo clean output created by this script">
    </target>

    <target name="test" depends="test.ajc,test.ajdoc"/>

    <target name="test.ajdoc" depends="init,init.taskdefs"
     description="run variants of ajdoc taskdef on bean, other examples">

        <mkdir dir="${expected.classes.dir}"/>
        <delete quiet="on" dir="${expected.classes.dir}"/>
        <mkdir dir="${expected.classes.dir}"/>
        <mkdir dir="${classes.dir}"/>
        <delete quiet="on" dir="${classes.dir}"/>
        <mkdir dir="${classes.dir}"/>
        <antcall target="cleanajc"/>

        <echo message="## ajdoc setup classes dir" />
        <ajdoc classpath="${aspectjrt.jar}" destdir="${expected.classes.dir}" 
               srcdir="${ex.dir}/bean"  packagenames="bean" 
               argfile="${ex.dir}/bean/files.lst"
               private="on" />

        <echo message="## ajdoc @testcase same as setup" />
        <antcall target="cleanajc"/>
        <ajdoc classpath="${aspectjrt.jar}" destdir="${classes.dir}" 
               srcdir="${ex.dir}/bean" packagenames="bean"
               argfile="${ex.dir}/bean/files.lst"
               private="on" />
        <antcall target="verifyAndCleanup.ajdoc"/>

        <echo message="## ajdoc @testcase same as setup, with -standard doclet" />
        <antcall target="cleanajc"/>
        <ajdoc classpath="${aspectjrt.jar}" destdir="${classes.dir}" 
               srcdir="${ex.dir}/bean" packagenames="bean"
               argfile="${ex.dir}/bean/files.lst"
               private="on" standard="on"/>
        <antcall target="verifyAndCleanup.ajdoc"/>

        <echo message="## ajdoc @testcase srcdir with include argument" />
        <ajdoc classpath="${aspectjrt.jar}" destdir="${classes.dir}" 
               srcdir="${ex.dir}/bean" includes="*.java" 
               private="on" />
        <antcall target="verifyAndCleanup.ajdoc"/>

        <echo message="## ajdoc @testcase srcdir with nested include argument" />
        <ajdoc classpath="${aspectjrt.jar}" destdir="${classes.dir}" 
               private="on" >
            <sourcepath path="${ex.dir}/bean"/>
            <include name="*.java"/>
        </ajdoc>
        <antcall target="verifyAndCleanup.ajdoc"/>

        <echo message="## ajdoc @testcase argfile only with nested classpath" />
        <ajdoc destdir="${classes.dir}" 
               argfile="${ex.dir}/bean/files.lst"
               private="on" >
          <classpath> <pathelement path="${aspectjrt.jar}"/> </classpath>
        </ajdoc>
        <antcall target="verifyAndCleanup.ajdoc"/>

<!-- todo ignoring include ajdoc cases until fixed 
        <echo message="## ajdoc need @testcase nested src with kitchen sink" />
        <echo message="## ajdoc @testcase argfile todo " />
        <echo message="## ajdoc @testcase Vincent Massol bug" />
        <ajdoc destdir="${classes.dir}" 
                srcdir="${ex.dir}/bean"
               private="on" >
          <include name="*.java"/>
          <include name="**.java"/>
          <classpath> <pathelement path="${aspectjrt.jar}"/> </classpath>
        </ajdoc>
        <antcall target="verifyAndCleanup.ajdoc"/>

todo bug ajdoc taskdef not handling any includes or nested src or 
includes argument 
-->
        <!-- todo: version bootclasspath, extdirs 
             Xporting="on"    # todo: after Jim updates compiler for Xporting
             todo: nested src n/a
        -->

    </target>

    <!-- to use safe classpaths, must fork -->
    <target name="test.ajc" depends="init,init.taskdefs"
     description="run variants of ajc taskdef on bean, other examples">

        <echo message="## setup expected working dir, classes dir, and output file" />
        <antcall target="cleanajc"/>
        <ajc srcdir="${ex.dir}/bean" classpath="${aspectjrt.jar}" 
            destDir="${expected.classes.dir}"
            usejavac="yes" fork="${use.fork}"
         workingdir="${expected.working.dir}"/>
        <java classname="bean.Demo" 
              classpath="${expected.classes.dir}${PS}${aspectjrt.jar}" 
                    dir="${ex.dir}" fork="yes"
                 output="${expect.file}"/>

        <echo message="## @testcase simple srcdir example" />
        <antcall target="cleanajc"/>
        <ajc srcdir="${ex.dir}/bean" fork="${use.fork}" classpath="${aspectjrt.jar}" />
        <echo message="## simple.run.classpath: ${simple.run.classpath} " />
        <java classname="bean.Demo" 
              classpath="${simple.run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="cleanajc"/>

        <echo message="## @testcase srcdir with include argument" />
        <ajc srcdir="${ex.dir}/bean" fork="${use.fork}" classpath="${aspectjrt.jar}" includes="*.java"/>
        <java classname="bean.Demo" 
              classpath="${simple.run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.noworkingdir.noclassesdir"/>

        <echo message="## @testcase srcdir with destDir and working workingdir argument" />
        <ajc srcdir="${ex.dir}/bean" fork="${use.fork}" classpath="${aspectjrt.jar}" 
            destDir="${classes.dir}"
           usejavac="yes"
         workingdir="${working.dir}"/>
        <java classname="bean.Demo" 
              classpath="${run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.workingdir.classesdir"/>

        <echo message="## @testcase simple srcdir with nested include and nested classpath" />
        <ajc fork="${use.fork}" srcdir="${ex.dir}/bean"> 
          <include name="**.java"/>
          <classpath> <pathelement path="${aspectjrt.jar}"/> </classpath>
        </ajc>
        <java classname="bean.Demo" 
              classpath="${simple.run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.noworkingdir.noclassesdir"/>

        <echo message="## @testcase nested src with kitchen sink" />
        <antcall target="cleanajc"/>
        <ajc fork="${use.fork}"
             destDir="${classes.dir}"
             workingdir="${working.dir}"
             usejavac="on"
             preprocess="no"
             verbose="on"
             deprecation="on"
             optimize="on"
             encoding="encodingIgnored"
             target="targetIgnored"
             depend="dependIgnored"
             includeantruntime="includeantruntimeIgnored"
             includejavaruntime="includejavaruntimeIgnored"
             failonerror="on"

             nocomments="on"
             nosymbols="on"
             emacssym="on"
             strict="on"
           > 
          <src path="${ex.dir}/bean"/> 
          <include name="**.java"/>
          <classpath> <pathelement path="${aspectjrt.jar}"/> </classpath>
        </ajc>
        <java classname="bean.Demo" 
              classpath="${run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.workingdir.classesdir"/>

        <echo message="## @testcase srcdir in preprocess mode" />
        <ajc srcdir="${ex.dir}/bean" classpath="${aspectjrt.jar}" 
             lenient="on" fork="${use.fork}"
             workingdir="${working.dir}" usejavac="on" preprocess="yes"/>
        <antcall target="verifyAndCleanup.workingdir.noclassesdir.nooutput"/>

        <echo message="## @testcase argfile in preprocess mode" />
        <ajc argfile="${ex.dir}/bean/files.lst" classpath="${aspectjrt.jar}" 
             workingdir="${working.dir}" fork="${use.fork}" usejavac="on" preprocess="yes"/>
        <antcall target="verifyAndCleanup.workingdir.noclassesdir.nooutput"/>

        <echo message="## @testcase argfile in normal mode" />
        <ajc argfile="${ex.dir}/bean/files.lst" classpath="${aspectjrt.jar}" 
             destdir="${classes.dir}" fork="${use.fork}" usejavac="off" preprocess="no" verbose="yes"/>
        <java classname="bean.Demo" 
              classpath="${run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.noworkingdir.classesdir"/>

        <echo message="## @testcase multiple argfile todo restore" />
        <ajc argfiles="${ex.dir}/bean/files.lst,${ex.dir}/introduction/files.lst"        
           classpath="${aspectjrt.jar}" fork="${use.fork}" destdir="${classes.dir}" >
        </ajc>

        <java classname="bean.Demo" 
              classpath="${run.classpath}" 
                    dir="${ex.dir}" fork="${use.fork}" 
                 output="${actual.file}"/>
        <copy todir="${expected.classes.dir}" >
          <fileset dir="${classes.dir}" includes="**.class" />
        </copy>
        <antcall target="cleanajc"/> <!-- too many classes -->

        <echo message="## @testcase Vincent Massol bug" />
        <ajc  classpath="${aspectjrt.jar}" fork="${use.fork}" 
             workingdir="${working.dir}"  preprocess="yes" >
            <src path="${ex.dir}/bean"/>
            <src path="${ex.dir}/introduction"/>
            <include name="*.java"/>
            <include name="**.java"/>
        </ajc>
        <antcall target="verifyAndCleanup.workingdir.noclassesdir.nooutput"/>

        <echo message="## @testcase warning when no source files specified but have srcdir # " />
        <ajc srcdir="${ex.dir}/bean" fork="${use.fork}" classpath="${aspectjrt.jar}" />
        <antcall target="cleanajc"/>

        <!-- @testcase -preprocess not supported -->
        <echo message="## @testcase -preprocess srcdir no destDir, post-javac compile (-preprocess ignored) # " />
        <mkdir dir="workingdir" /> <!-- todo: failing to create workingdir from taskdef -->
        <ajc srcdir="${ex.dir}/bean" fork="${use.fork}" includes="**.java" classpath="${aspectjrt.jar}" 
           usejavac="no" preprocess="yes"/>
        <javac srcdir="workingdir" 
              destDir="${classes.dir}">
          <classpath>
            <pathelement path="${aspectjrt.jar}"/>
          </classpath>
        </javac>
        
        <java classname="bean.Demo" 
              classpath="${run.classpath}" 
                    dir="${ex.dir}" fork="yes"
                 output="${actual.file}"/>
        <antcall target="verifyAndCleanup.noworkingdir.classesdir"/>

        <!-- todo: version bootclasspath, extdirs 
             Xporting="on"    # todo: after Jim updates compiler for Xporting
        -->

    </target>

    <target name="verifyAndCleanup.ajdoc" depends="verifyAndCleanup.noworkingdir.classesdir.nooutput" />

    <target name="verifyAndCleanup.noworkingdir.noclassesdir">
      <antcall target="expectfile"/>
      <antcall target="noworkingdir"/>
      <antcall target="noclassesdir"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="verifyAndCleanup.workingdir.noclassesdir">
      <antcall target="expectfile"/>
      <antcall target="workingdir"/>
      <antcall target="noclassesdir"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="verifyAndCleanup.workingdir.noclassesdir.nooutput">
      <antcall target="workingdir"/>
      <antcall target="noclassesdir"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="verifyAndCleanup.noworkingdir.classesdir.nooutput">
      <antcall target="noworkingdir"/>
      <antcall target="classesdir"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="verifyAndCleanup.noworkingdir.classesdir" >
      <antcall target="noworkingdir"/>
      <antcall target="classesdir"/>
      <antcall target="expectfile"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="verifyAndCleanup.workingdir.classesdir">
      <antcall target="expectfile"/>
      <antcall target="workingdir"/>
      <antcall target="classesdir"/>
      <antcall target="cleanajc"/>
    </target>

    <target name="expectfile" depends="init">
        <comparefiles lhsFile="${expect.file}" rhsFile="${actual.file}"/>
        <delete quiet="on" file="${actual.file}"/>
    </target>
    <target name="noworkingdir" depends="init">
        <comparefiles lhsFile="${empty.dir}"   rhsFile="${working.dir}"/>
    </target>
    <target name="workingdir" depends="init">
        <comparefiles lhsFile="${expected.working.dir}"   rhsFile="${working.dir}"/>
    </target>
    <target name="noclassesdir" depends="init">
        <comparefiles lhsFile="${empty.dir}"   rhsFile="${classes.dir}"/>
    </target>
    <target name="classesdir" depends="init">
        <comparefiles lhsFile="${expected.classes.dir}"   rhsFile="${classes.dir}"/>
    </target>

    <target name="cleanajc" depends="init">
      <mkdir dir="${classes.dir}"/>
      <delete quiet="on" dir="${classes.dir}"/>
      <mkdir dir="${classes.dir}"/>
      <mkdir dir="${working.dir}"/>
      <delete quiet="on" dir="${working.dir}"/>
      <mkdir dir="${working.dir}"/>
      <delete quiet="on" >
        <fileset dir="${product.examples.dir}" includes="**.class,**.ajesym"/>
      </delete>
    </target>
</project>