<project name="GroboInstrumentCompilerTask" 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" />
    <property name="coverage.reports" location="${coverage}/reports" />


    <target name="none" />
    <taskdef resource="ant-grobocoverage.properties" />
    
    <!-- ================================================================= -->
    <!-- start of the tests that pass -->
    
    
    <target name="bug-1" depends="test-setup-ABC">
        <!-- the depends should do everything for this test -->
    </target>

    
    <!-- ================================================================= -->
    <!-- start of the tests that fail -->
    
    
    
    
    
    <!-- ================================================================= -->
    <!-- sets up the tests -->
    <target name="test-setup-A"
            depends="test-setup:dirs, test-setup:a.A, test-setup:final" />
    <target name="test-setup-B"
            depends="test-setup:dirs, test-setup:a.B, test-setup:final" />
    <target name="test-setup-AB"
            depends="test-setup:dirs,
                test-setup:a.A, test-setup:a.B,
                test-setup:final" />
    <target name="test-setup-ABC"
            depends="test-setup:dirs,
                test-setup:a.A, test-setup:a.B, test-setup:a.C,
                test-setup:final" />
    
    <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:x.main">
        <property name="x.main" value="${src}/x/main.java" />
        <mkdir dir="${src}/x" />
        <echo file="${x.main}">
package x;

public class main {
    public static void main( String args[] ) {
        System.out.println("x.main start");
</echo>
    </target>
    
    <target name="test-setup:a.A" depends="test-setup:x.main">
        <!-- 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>
        <echo file="${x.main}" append="true">
        System.out.println("x.main | a.A start");
        a.A tA = new a.A();
        // a.A.x();
        tA.y();
        System.out.println("x.main | a.A end");
</echo>
    </target>
    
    <target name="test-setup:a.B" depends="test-setup:x.main">
        <!-- 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;
    
    public static void x( String t )
    {
        s = t + "x";
    }
    
    public void y()
    {
        if (s != null)
        {
            s = s + s;
        }
    }
}
</echo>
        <echo file="${x.main}" append="true">
        System.out.println("x.main | a.B start");
        a.B tB = new a.B();
        tB.y();
        a.B.x( "asdf" );
        System.out.println("x.main | a.B end");
</echo>
    </target>
    
    <target name="test-setup:a.C" depends="test-setup:x.main">
        <!-- create the test source file a.B -->
        <mkdir dir="${src}/a" />
        <echo file="${src}/a/C.java">
package a;
public class C {
}
</echo>
        <echo file="${src}/a/C_NoCover.java">
package a;
public class C_NoCover {
    static C x = new C();
}
</echo>
        <echo file="${x.main}" append="true">
        System.out.println("x.main | a.C start");
        // force the static constructor to run
        // see bug 902884
        a.C_NoCover.class.getName();
        System.out.println("x.main | a.C end");
</echo>
    </target>

    <target name="test-setup:final"
        depends="test-setup:compile,test-setup:cover,test-setup:run" />
    
    <target name="test-setup:compile" depends="test-setup:x.main">
        <!-- finish the main class -->
        <echo file="${x.main}" append="true">
        System.out.println("x.main end");
    }
}
</echo>
        <loadfile srcfile="${x.main}" property="x.main.src" />
        <echo>Source of ${x.main}:
${x.main.src}</echo>
        
        <!-- compile the source files -->
        <javac srcdir="${src}" destdir="${classes}" debug="true" />
    </target>
    
    <target name="test-setup:cover" depends="test-setup:compile">
        <grobo-instrument logdir="${coverage.logs}" logger="cache"
                    destdir="${coverage.classes}">
            <fileset dir="${classes}" >
                <exclude name="**/*_NoCover.class" />
            </fileset>
            <measure type="line" />
            <measure type="bytecode" />
        </grobo-instrument>
        
        <available property="test-setup:cover:1" type="dir"
            file="${coverage.logs}/data" />
        <fail unless="test-setup:cover:1">
Did not create directory ${coverage.logs}/data
        </fail>
    </target>
    
    <target name="test-setup:run" depends="test-setup:cover">
        <echo>Running java...</echo>
        <java classname="x.main">
            <classpath>
                <pathelement location="${coverage.classes}" />
                <!--
                <pathelement location="${classes}" />
                -->
                
                <!-- hack -->
                <fileset dir="${basedir}/../../..">
                    <include name="GroboCodeCoverage-*-runtime.jar" />
                </fileset>
            </classpath>
        </java>
        
        <available property="test-setup:run:1" type="dir"
            file="${coverage.logs}/logs" />
        <fail unless="test-setup:run:1">
Did not create directory ${coverage.logs}/logs
        </fail>
        
        <available property="test-setup:run:2" type="dir"
            file="${coverage.logs}/logs/0" />
        <fail unless="test-setup:run:2">
Did not create directory ${coverage.logs}/logs/0
        </fail>
    </target>
    
    <target name="test-teardown">
        <!--
        <delete dir="${root}" />
        -->
    </target>
</project>
