<!--
    Build script for the project "ant-jsunit-hieatt".

    This script is an Ant script using Ivy extension to resolve dependencies.
    You will need both Ant (http://ant.apache.org/) and Ivy (http://ant.apache.org/ivy) to use it.

	TODO ? organise build files in /tests/, /bin/, ... so we can package easily ?
-->
<project name="ant-jsunit-hieatt" default="verify"
	xmlns:ivy="antlib:org.apache.ivy.ant"
	xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">

    <!-- ==================== -->
    <!-- Settings -->
    <!-- ==================== -->

    <!-- custom, user specific, properties -->
    <property file="local.properties"/>
    <!-- default properties -->
    <property file="build.properties"/>

	<!-- Source path : sources and resources -->
    <path id="srcpath">
    	<dirset dir="${src.dir}">
    	  <include name="java"/>
    	  <include name="resources"/>
    	</dirset>
    	<dirset dir="${tests.dir}">
    	  <include name="java"/>
    	  <include name="resources"/>
    	</dirset>
    </path>

	<!-- Resources only -->
    <path id="rc.files">
    	<fileset dir="${src.dir}/java" excludes="**/*.java"/>
       	<fileset dir="${src.dir}/resources"/>
       	<fileset dir="${tests.dir}/java" excludes="**/*.java"/>
       	<fileset dir="${tests.dir}/resources"/>
    </path>

	<!-- Classpath for Java operations -->
	<path id="lib.files">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar"/>
			<exclude name="jsunit/java/lib/ant.jar"/>
			<exclude name="jsunit/java/lib/junit.jar"/>
		</fileset>
	</path>

    <!--
        Runs JsUnit tests. This target saves a lot of verbosity in the projects' build scripts.

        Firefox 3 users : change the security.fileuri.strict_origin_policy parameter to 'false' for the following target to work

        // static configuration
        @in jsunit.dir          the directory where JsUnit resides
        @in jsunit.coreJs       the jsUnitCore.js file
        @in jsunit.testRunner   the test runner's HTML page
    -->
    <taskdef name="jsunit" classname="net.jsunit.ant.JsUnitTask" classpathref="lib.files">
		<classpath>
        	<path refid="lib.files" />
    		<pathelement path="${classes.dir}"/>
		</classpath>
    </taskdef>


    <!-- ==================== -->
    <!-- Public targets : they are greatly inspired from the Maven build lifecycle -->
    <!-- ==================== -->


    <target name="clean" description="Restores the initial state of the project, deleting any generated file.">
        <delete includeemptydirs="true" dir="${target.dir}"/>
        <delete includeemptydirs="true" dir="${build.dir}"/>
    </target>


    <target name="validate" description="Validates the project is correct and all necessary information is available.">
        <!-- Retreives dependencies with ivy -->
        <ivy:retrieve/>
        <!-- Creates necessary directories -->
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${tests.logs}"/>
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${target.dir}" />
    </target>


    <target name="compile" depends="validate" description="Compiles the source code of the project.">

    	<javac destdir="${classes.dir}" classpathref="lib.files" source="1.4" debug="true">
    		<src refid="srcpath" />
    	</javac>

    	<!-- Copies all remaining files (= all files under the source tree, except Java sources) to the build dir. -->
    	<copy todir="${classes.dir}">
    		<path refid="rc.files"/>
    	</copy>

    </target>


    <!-- NOTE : These tests should not require the code be packaged or deployed. -->
    <target name="test" depends="compile" description="Tests the compiled source code using a suitable unit testing framework.">

    	<path id="testspath">
        	<path refid="lib.files" />
    		<pathelement path="${classes.dir}"/>
           	<!-- TODO : make the following a parameter -->
            <dirset dir="${jsunit.dir}/java/config"/>
    	</path>

    	<!-- Unit tests for TestLibRunner -->
        <junit fork="true" haltonerror="true" haltonfailure="true">

        	<sysproperty key="jsunit.testRunner" value="${jsunit.testRunner}" />
        	<sysproperty key="jsunit.coreJs" value="${jsunit.coreJs}" />
            <sysproperty key="logsDirectory" value="${tests.logs}" />
            <sysproperty key="browserFileNames" value="${jsunit.browsers}" />

        	<classpath refid="testspath"/>

            <test name="net.jsunit.TestLibRunnerTest" todir="${tests.logs}">
                <formatter type="brief" usefile="false" />
                <formatter type="xml" />
            </test>

        </junit>

    	<!-- Unit test for the jsunit task itself : given an HTML test page -->
        <jsunit showoutput="true"
                printsummary="true"
                filtertrace="false"
                jsUnitRoot="${jsunit.dir}"
                haltonfailure="true"
                haltonerror="true"
        		testPage="${tests.dir}/resources/SuccessTest1.html"
                >

            <sysproperty key="browserFileNames" value="${jsunit.browsers}"/>
            <sysproperty key="logsDirectory" value="${tests.logs}"/>

        	<classpath refid="testspath"/>

        </jsunit>

    	<!-- Unit test for the jsunit task itself : given Javascript libraries -->
        <jsunit showoutput="true"
                printsummary="true"
                filtertrace="false"
                jsUnitRoot="${jsunit.dir}"
                haltonfailure="true"
                haltonerror="true"
        		fork="false"
                >

            <sysproperty key="browserFileNames" value="${jsunit.browsers}"/>
            <sysproperty key="logsDirectory" value="${tests.logs}"/>

            <fileset file="${tests.dir}/resources/success1.js"/>
            <fileset file="${tests.dir}/resources/success2.js"/>

        	<classpath refid="testspath"/>

        </jsunit>

    </target>


    <target name="package" depends="test, doc" description="Takes the compiled code and package it in its distributable format.">

    	<jar destfile="${target.dir}/${ant.project.name}-${ivy.revision}.jar"
    		basedir="${classes.dir}">
    	    <include name="**/*"/>
    	</jar>

    </target>


    <target name="integration-test" depends="package" description="Processes and deploys the package if necessary into an environment where integration tests can be run.">
        <!-- nothing to do -->
    </target>


    <target name="verify" depends="integration-test" description="Runs any checks to verify the package is valid and meets quality criteria.">
        <!-- nothing to do -->
    </target>


    <target name="install" depends="verify" description="Installs the package into the local repository, for use as a dependency in other projects locally.">
        <!-- nothing to do -->
    </target>


    <target name="deploy" depends="verify" description="Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.">
        <!-- nothing to do -->
    </target>


    <!-- ==================== -->
    <!-- Internal targets -->
    <!-- ==================== -->


    <!-- Generates developer documentation. -->
    <target name="doc" depends="validate">
    </target>


</project>
