<?xml version="1.0" encoding="UTF-8"?>
<project name="testsrc" basedir="..">
  <!--
    Location of mozilla/js/tests directory
  -->
  <property name="test.library.dir" location="../tests" />
  <property name="test.jstests.jar" location="${test.library.dir}/src/jstests.jar" />

  <!--
    URL from which junit.jar can be retrieved
  -->
  <property name="test.junit.url" value="http://mirrors.ibiblio.org/pub/mirrors/maven2/junit/junit/4.5/junit-4.5.jar"/>

  <!--
    Destination to which testing classes should be built
  -->
  <property name="test.classes" value="${build.dir}/test/classes" />

  <!--
    Output directory for HTML files generated by jsdriver
  -->
  <property name="test.output" value="${build.dir}/test/output" />

  <!--
    Timeout in milliseconds for tests
  -->
  <property name="test.timeout" value="60000" />

  <!--
    Maximum heap size for VM executing test cases.
  -->
  <property name="test.vm.mx" value="256m" />

  <target name="get-junit">
	<mkdir dir="lib"/>
	<get src="${test.junit.url}" dest="lib/junit.jar" usetimestamp="true"/>
  </target>

  <target name="junit-compile">
    <mkdir dir="${test.classes}" />
	<antcall target="get-junit"/>
    <javac
      destdir="${test.classes}" debug="true"
      target="${target-jvm}"
      source="${source-level}"
    >
      <classpath>
        <pathelement path="lib/junit.jar" />
        <pathelement path="${classes}" />
      </classpath>
        <src path="testsrc"/>
        <src path="examples"/>
    </javac>
    <antcall target="copy-files" />
  </target>

  <target name="compile">
    <mkdir dir="${test.classes}" />
	<antcall target="get-junit"/>
    <javac
      srcdir="testsrc"
      destdir="${test.classes}" debug="true"
      target="${target-jvm}"
      source="${source-level}"
    >
      <classpath>
        <pathelement path="lib/junit.jar" />
        <pathelement path="${classes}" />
      </classpath>
      <sourcepath path="testsrc" />
        <include name="org/mozilla/javascript/drivers/JsDriver.java" />
    </javac>
    <antcall target="copy-files" />
  </target>

  <target name="copy-files">
    <copy todir="${test.classes}">
       <fileset dir="testsrc">
         <exclude name="**/*.java build.xml"/>
       </fileset>
    </copy>
  </target>
  	
  <target name="clean">
    <delete dir="${test.classes}" />
  </target>

  <target name="coverage-instrument">
    <get src="http://mirrors.ibiblio.org/pub/mirrors/maven2/emma/emma/2.0.5312/emma-2.0.5312.jar" dest="lib/emma.jar" usetimestamp="true"/>
    <get src="http://mirrors.ibiblio.org/pub/mirrors/maven2/emma/emma_ant/2.0.5312/emma_ant-2.0.5312.jar" dest="lib/emma_ant.jar" usetimestamp="true"/>
    <property name="coverage.dir" location="${build.dir}/coverage"/>
    <property name="coverage.classes.dir" location="${build.dir}/coverage/classes"/>
    <mkdir dir="${coverage.classes.dir}"/>
    <path id="emma.lib">
      <pathelement location="lib/emma.jar" />
      <pathelement location="lib/emma_ant.jar" />
    </path>
    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
    <property name="coverage.instrumentationfile" location="${coverage.dir}/instrumentation"/>
    <emma enabled="true">
      <instr 
        instrpath="${classes}" outdir="${coverage.classes.dir}" 
        outfile="${coverage.instrumentationfile}" mode="copy"/>
    </emma>
    <copy todir="${coverage.classes.dir}">
      <fileset dir="src" excludes="**/*.java"/>
    </copy>
    <copy todir="${coverage.classes.dir}">
      <fileset dir="${classes}"/>
    </copy>
    <property name="coverage.outfile" location="${coverage.dir}/coverage"/>
  </target>
	
  <target name="junit" depends="junit-compile,coverage-instrument">
    <junit printsummary="on" fork="true" forkmode="once" maxmemory="${test.vm.mx}" showoutput="true"
    	failureproperty="junitFailed">
      <sysproperty key="java.awt.headless" value="true" />
      <sysproperty key="mozilla.js.tests" value="${test.library.dir}" />
      <sysproperty key="mozilla.js.tests.timeout" value="${test.timeout}" />
      <sysproperty key="emma.coverage.out.file" value="${coverage.outfile}"/>
      <classpath>
        <pathelement location="${xbean.jar}"/>
        <pathelement location="${jsr173.jar}"/>
        <pathelement path="${coverage.classes.dir}" />
        <pathelement path="${classes}" />
        <pathelement path="${test.classes}" />
        <pathelement path="lib/emma.jar"/>
        <pathelement path="lib/junit.jar" />
        <pathelement location="${test.jstests.jar}"/>
      </classpath>
      <batchtest todir="build/test">
        <fileset dir="${test.classes}" includes="**/tests/**/*Test.class"/>
        <fileset dir="${test.classes}" includes="**/StandardTests.class"/>
      </batchtest>
      <formatter type="xml"/>
    </junit>
    <mkdir dir="build/test/report"/>
    <junitreport todir="build/test/report">
       <fileset dir="build/test" includes="*.xml"/>
       <report todir="build/test/report"/>
    </junitreport>
  </target>

  <target name="junit-coveragereport" depends="junit">
    <property name="coverage.report.dir" location="${build.dir}/coverage/report"/>
    <mkdir dir="${coverage.report.dir}"/>
    <delete dir="${coverage.report.dir}"/>
    <mkdir dir="${coverage.report.dir}"/>
    <emma enabled="true">
      <report>
        <fileset dir="${basedir}">
          <include name="build/coverage/instrumentation"/>
          <include name="build/coverage/coverage"/>
        </fileset>
        <sourcepath>
          <dirset dir="${basedir}">
            <include name="src"/>
          </dirset>
        </sourcepath>
        <html outfile="${coverage.report.dir}/index.html"/>
      </report>
    </emma>
  	<fail if="junitFailed" message="JUnit test(s) failed"/>
  </target>

  <target name="jsdriver" depends="compile">
    <tstamp>
      <format property="test.timestamp" pattern="yyyy.MM.dd.HH.mm.ss" />
    </tstamp>
    <mkdir dir="${test.output}" />
    <java
      fork="true"
      classname="org.mozilla.javascript.drivers.JsDriver"
      maxmemory="${test.vm.mx}"
    >
      <classpath>
        <pathelement location="${xbean.jar}"/>
        <pathelement location="${jsr173.jar}"/>
        <pathelement path="${classes}" />
        <pathelement path="${test.classes}" />
        <pathelement location="${test.jstests.jar}"/>
      </classpath>
      <arg value="-p" />
      <arg file="${test.library.dir}" />
      <arg value="-f" />
      <arg value="${test.output}/rhino-test-results.${test.timestamp}.html" />
      <arg value="--timeout" />
      <arg value="${test.timeout}" />
    </java>
  </target>

  <target name="copy-source">
    <mkdir dir="${dist.dir}/testsrc"/>
    <copy todir="${dist.dir}/testsrc">
      <fileset dir="testsrc"
        includes="**/*.java,**/*.properties,**/*.xml,**/*.html,**/*.skip" />
    </copy>
  </target>
</project>
