<!--
 * @author    Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
 * @version   $Date: 2004/04/09 19:26:51 $
 * @since     March 9, 2004
-->
<project name="GroboInstrumentTask" default="none">
    <!-- ================================================================= -->
    <!-- some general defines -->
    <property name="root" location="instrument" />
    <property name="src" location="${root}/src" />
    <property name="classes" location="${root}/classes" />
    <property name="coverage" location="${root}/coverage" />
    <property name="coverage.classes" location="${coverage}/classes" />
    <property name="coverage.logs" location="${coverage}/logs" />
    
    
    <target name="none" />
    <taskdef resource="ant-grobocoverage.properties" />
    
    <!-- ================================================================= -->
    <!-- start of the tests that pass -->
    
    
    <!-- test the simplest setup for the grobo-instrument task -->
    <target name="instrument-1" depends="test-setup-A">
        <echo file="instrument-1" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
        
        <!-- ensure that the expected files exist -->
        <available file="${coverage.classes}/a/A.class" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Did not create the file a/A.class.
        </fail>
        
        <available file="${coverage.classes}/grobocoverage.properties"
            type="file" property="!did-create-2" />
        <fail unless="!did-create-2">
Did not create the grobocoverage.properties file.
        </fail>
        
        <available file="${coverage.logs}/data"
            type="dir" property="!did-create-3" />
        <fail unless="!did-create-3">
Did not create the logs/data directory.
        </fail>
    </target>
    
    
    <!-- test a measure -->
    <target name="instrument-2" depends="test-setup-A">
        <echo file="instrument-2" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <!-- ensure that the expected files exist -->
        <available file="${coverage.classes}/a/A.class" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Did not create the file a/A.class.
        </fail>
        
        <available file="${coverage.classes}/grobocoverage.properties"
            type="file" property="!did-create-2" />
        <fail unless="!did-create-2">
Did not create the grobocoverage.properties file.
        </fail>
        
        <available file="${coverage.logs}/data"
            type="dir" property="!did-create-3" />
        <fail unless="!did-create-3">
Did not create the logs/data directory.
        </fail>
        
        <available file="${coverage.logs}/data/moduleset.txt"
            type="file" property="!did-create-4" />
        <fail unless="!did-create-4">
Did not create the logs/data/moduleset file.
        </fail>
        
        <available file="${coverage.logs}/data/LineCount"
            type="dir" property="!did-create-5" />
        <fail unless="!did-create-5">
Did not create the logs/data/LineCount directory.
        </fail>
    </target>
    
    
    <!-- test the cleaning of the data files (clean all) -->
    <target name="instrument-3" depends="test-setup-AB">
        <echo file="instrument-3" message="test" />
        <!-- run the instrumentation once -->
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <available file="${coverage.classes}/a/B.class" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Did not create the file a/B.class on first pass.
        </fail>
        
        <!-- change the files around, so the CRC changes -->
        <delete file="${classes}/a/A.class" />
        <echo file="${src}/a/A.java">
package a;

public class A {
    class X { int x; }
}
</echo>
        <delete file="${classes}/a/B.class" />
        <echo file="${src}/a/B.java">
package a;

public class B extends A {
    class X { int x; }
}
</echo>
        <antcall target="test-setup:compile" />
        <available file="${classes}/a/A$$X.class" type="file"
            property="!did-create-3a" />
        <fail unless="!did-create-3a">
Did not create the file a/A$$X.class on first pass.
        </fail>

        <!-- rerun the instrumentation, requesting a delete of all
        old stuff -->
        <grobo-instrument logdir="${coverage.logs}"
                ifexists="clean"
                destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <available file="${coverage.classes}/a/A.class" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Did not create the file a/A.class on second pass.
        </fail>
        
        <available file="${coverage.classes}/a/A$$X.class" type="file"
            property="!did-create-3" />
        <fail unless="!did-create-3">
Did not create the file a/A$$X.class on second pass.
        </fail>
        
        <available file="${coverage.classes}/a/B.class" type="file"
            property="!did-create-4" />
        <fail unless="!did-create-4">
Did not create the file a/B.class on second pass.
        </fail>
        
        <available file="${coverage.classes}/a/B$$X.class" type="file"
            property="!did-create-5" />
        <fail unless="!did-create-5">
Did not create the file a/B$$X.class on second pass.
        </fail>
        
        <!-- the test code needs to verify only one type of data file for
        each class -->
    </target>
    
    
    <!-- test the cleaning of the data files (clean all) -->
    <target name="instrument-3a" depends="test-setup-AB">
        <echo file="instrument-3a" message="test" />
        <!-- run the instrumentation once -->
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <!-- change the files around, so the CRC changes -->
        <delete file="${classes}/a/A.class" />
        <echo file="${src}/a/A.java">
package a;

public class A {
    class X { int x; }
}
</echo>
        <delete file="${src}/a/B.java" />
        <delete file="${classes}/a/B.class" />
        <antcall target="test-setup:compile" />
        <available file="${classes}/a/A$$X.class" type="file"
            property="!did-create-3a" />
        <fail unless="!did-create-3a">
Did not create the file a/A$$X.class on first pass.
        </fail>
        
        <!-- rerun the instrumentation, requesting a delete of all
        old stuff -->
        <grobo-instrument logdir="${coverage.logs}"
                ifexists="clean"
                destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <available file="${coverage.classes}/a/A.class" type="file"
            property="!did-create-2" />
        <fail unless="!did-create-2">
Did not create the file a/A.class on second pass.
        </fail>
        
        <available file="${coverage.classes}/a/A$$X.class" type="file"
            property="!did-create-3" />
        <fail unless="!did-create-3">
Did not create the file a/A$$X.class on second pass.
        </fail>
        
        <!-- the test code needs to verify only one type of data file for
        each class, and that the data for B no longer exists -->
    </target>
    
    
    <!-- test the cleaning of the data files (do nothing) -->
    <target name="instrument-4" depends="test-setup-AB">
        <echo file="instrument-4" message="test" />
        <!-- run the instrumentation once -->
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <!-- change the files around, so the CRC changes -->
        <delete file="${classes}/a/A.class" />
        <echo file="${src}/a/A.java">
package a;

public class A {
    class X { int x; }
    
    A() { System.out.println("A"); }
}
</echo>
        <delete file="${classes}/a/B.class" />
        <echo file="${src}/a/B.java">
package a;

public class B extends A {
    class X { int x; }
    B() { super(); System.out.println("B"); }
}
</echo>
        <antcall target="test-setup:compile" />
        <available file="${classes}/a/A$$X.class" type="file"
            property="!did-create-3a" />
        <fail unless="!did-create-3a">
Did not create the file a/A$$X.class on first pass.
        </fail>

        <!-- rerun the instrumentation, requesting a delete of all
        old stuff -->
        <grobo-instrument logdir="${coverage.logs}"
                ifexists="keep"
                destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <!-- the test code needs to verify only one type of data file for
        each class -->
    </target>
    
    
    <!-- test the cleaning of the data files (default = replace) -->
    <target name="instrument-5" depends="test-setup-AB">
        <echo file="instrument-5" message="test" />
        <!-- run the instrumentation once -->
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        
        <!-- ensure that only class A gets replaced, and that B's class
        doesn't exist, but its data stays around. -->
        <delete file="${classes}/a/A.class" />
        <echo file="${src}/a/A.java">
package a;

public class A {
    class X { int x; }
}
</echo>
        <delete file="${src}/a/B.java" />
        <delete file="${classes}/a/B.class" />
        <antcall target="test-setup:compile" />

        <!-- rerun the instrumentation, requesting a replacement
        old stuff, but don't do anything if it wasn't revisited -->
        <grobo-instrument logdir="${coverage.logs}"
                destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            <measure type="line" />
        </grobo-instrument>
        

        <available file="${coverage.classes}/a/B.class" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Did not keep the file a/B.class on second pass.
        </fail>
        

        <available file="${coverage.classes}/a/A$$X.class" type="file"
            property="!did-create-1" />
        <fail unless="!did-create-1">
Did not create the file a/A$$X.class on second pass.
        </fail>
        
        
        <!-- the test code needs to verify only one type of data file for
        each class -->
    </target>


    
    
    
    <!-- test the properties setting -->
    <target name="instrument-6" depends="test-setup-A">
        <echo file="instrument-6" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            
            <loggerprop key="1" value="A" />
            <loggerprop key="2" location="${coverage}" />
        </grobo-instrument>
        
        <available file="${coverage.classes}/grobocoverage.properties"
            type="file" property="!did-create-2" />
        <fail unless="!did-create-2">
Did not create the grobocoverage.properties file.
        </fail>
        
        <available file="${coverage.logs}/data"
            type="dir" property="!did-create-3" />
        <fail unless="!did-create-3">
Did not create the logs/data directory.
        </fail>
    </target>
    
    
    <!-- test no logger -->
    <target name="instrument-7" depends="test-setup-A">
        <echo file="instrument-7" message="test" />
        <grobo-instrument logdir="${coverage.logs}" logger="none"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
        
        <available file="${coverage.classes}/grobocoverage.properties"
            type="file" property="!did-create-1" />
        <fail if="!did-create-1">
Incorrectly created the grobocoverage.properties file.
        </fail>
    </target>
    
    
    <!-- test different logger -->
    <target name="instrument-8" depends="test-setup-A">
        <echo file="instrument-8" message="test" />
        <grobo-instrument logdir="${coverage.logs}" logger="fast"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
    </target>
    
    
    <!-- test different logger -->
    <target name="instrument-9" depends="test-setup-A">
        <echo file="instrument-9" message="test" />
        <grobo-instrument logdir="${coverage.logs}" loggerfactory="X"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
    </target>
    
    
    <!-- test different logger -->
    <target name="instrument-10" depends="test-setup-A">
        <echo file="instrument-10" message="test" />
        <grobo-instrument logdir="${coverage.logs}" loggerfactory="a.X"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
    </target>
    
    
    
    <!-- ================================================================= -->
    <!-- start of the tests that fail -->
    
    
    <!-- Fails due to no log dir being specified -->
    <target name="f-instrument-1" depends="test-setup-A">
        <echo file="f-instrument-1" message="test" />
        <grobo-instrument destdir="${coverage.classes}">
            <fileset dir="${classes}" />
        </grobo-instrument>
    </target>
    
    
    <!-- Fails due to no destdir specified -->
    <target name="f-instrument-2" depends="test-setup-A">
        <echo file="f-instrument-2" message="test" />
        <grobo-instrument logdir="${coverage.logs}">
            <fileset dir="${classes}" />
        </grobo-instrument>
    </target>
    
    
    
    <!-- Fails due to no value/location on loggerprop.  -->
    <target name="f-instrument-3" depends="test-setup-A">
        <echo file="f-instrument-3" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            
            <loggerprop key="3" />
        </grobo-instrument>
    </target>
    
    
    
    <!-- Fails due to no key on loggerprop.  -->
    <target name="f-instrument-4" depends="test-setup-A">
        <echo file="f-instrument-4" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            
            <loggerprop value="4" />
        </grobo-instrument>
    </target>
    
    
    
    <!-- Fails due to no key on loggerprop.  -->
    <target name="f-instrument-5" depends="test-setup-A">
        <echo file="f-instrument-5" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            
            <loggerprop location="${basedir}" />
        </grobo-instrument>
    </target>
    
    
    
    <!-- Fails due to no key or value on loggerprop.  -->
    <target name="f-instrument-6" depends="test-setup-A">
        <echo file="f-instrument-6" message="test" />
        <grobo-instrument logdir="${coverage.logs}"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" />
            
            <loggerprop />
        </grobo-instrument>
    </target>
    
    
    <!-- ================================================================= -->
    <!-- sets up the tests -->
    <target name="test-setup-A"
            depends="test-setup:dirs, test-setup:a.A, test-setup:compile" />
    <target name="test-setup-B"
            depends="test-setup:dirs, test-setup:a.B, test-setup:compile" />
    <target name="test-setup-AB"
            depends="test-setup:dirs,
                test-setup:a.A, test-setup:a.B,
                test-setup:compile" />
    
    <target name="test-setup:dirs">
        <!-- setup the directories -->
        <delete dir="${root}" failonerror="false" />
        <mkdir dir="${root}" />
        <mkdir dir="${src}" />
        <mkdir dir="${classes}" />
        <mkdir dir="${coverage}" />
        <mkdir dir="${coverage.classes}" />
        <mkdir dir="${coverage.logs}" />
    </target>
    
    <target name="test-setup:a.A">
        <!-- create the test source file a.A -->
        <mkdir dir="${src}/a" />
        <echo file="${src}/a/A.java">
package a;
import java.util.*;

public class A {
    static final Hashtable H = new Hashtable();
    
    static void x()
    {
        H.put("a", "B");
    }
    
    public void y()
    {
        H.get("a");
    }
}
</echo>
    </target>
    
    <target name="test-setup:a.B">
        <!-- create the test source file a.B -->
        <mkdir dir="${src}/a" />
        <echo file="${src}/a/B.java">
package a;
import java.util.*;

public class B {
    static String s = null;
    
    static void x( String t )
    {
        s = t + "x";
    }
    
    public void y()
    {
        if (s != null)
        {
            s = s + s;
        }
    }
}
</echo>
    </target>
    
    <target name="test-setup:compile">
        <!-- compile the source files -->
        <javac srcdir="${src}" destdir="${classes}" debug="true" />
    </target>
    
    
    
    <target name="test-teardown">
        <!--
        <delete dir="${root}" />
        -->
    </target>
</project>