<?xml version="1.0" encoding="UTF-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<project name="testsrc" basedir="..">
  <!--
    Location of mozilla/js/tests directory
  -->
  <property name="test.library.dir" location="testsrc/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://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar"/>
  <property name="test.hamcrest.url" value="http://central.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>

  <!--
    URL from which emma.jar can be retrieved
  -->
  <property name="test.emma.url" value="http://repo1.maven.org/maven2/emma/emma/2.1.5320/emma-2.1.5320.jar"/>

  <!--
    URL from which emma_ant.jar can be retrieved
  -->
  <property name="test.emma_ant.url" value="http://repo1.maven.org/maven2/emma/emma_ant/2.1.5320/emma_ant-2.1.5320.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" unless="offline">
    <mkdir dir="lib"/>
    <get src="${test.junit.url}" dest="lib/junit.jar" usetimestamp="true"/>
    <get src="${test.hamcrest.url}" dest="lib/hamcrest.jar" usetimestamp="true"/>
  </target>

  <target name="junit-compile">
    <mkdir dir="${test.classes}" />
    <antcall target="get-junit"/>
    <javac
      destdir="${test.classes}" debug="true"
      encoding="UTF-8"
      target="${target-jvm}"
      source="${source-level}"
    >
      <classpath>
        <pathelement path="lib/junit.jar" />
        <pathelement path="lib/hamcrest.jar" />
        <pathelement path="${classes}" />
      </classpath>
      <src path="testsrc"/>
      <src path="examples"/>
      <exclude name="tests/**"/>
    </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="lib/hamcrest.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="get-emma" unless="offline">
    <get src="${test.emma.url}" dest="lib/emma.jar" usetimestamp="true"/>
    <get src="${test.emma_ant.url}" dest="lib/emma_ant.jar" usetimestamp="true"/>
  </target>

  <target name="coverage-instrument" depends="get-emma">
    <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">
      <!--
      <jvmarg value="-Xdebug"/>
      <jvmarg value="-Xrunjdwp:transport=dt_socket,suspend=y,server=y,address=localhost:14000"/>
      -->
      <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}"/>
      <!-- mozilla test cases are locale dependent! -->
      <sysproperty key="user.language" value="en"/>
      <sysproperty key="user.country" value="US"/>
      <sysproperty key="user.timezone" value="America/Los_Angeles"/>
      <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 path="lib/hamcrest.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"/>
        <!-- -->
        <!-- <fileset dir="${test.classes}" includes="**/tests/**/*ExtensionTest.class"/> -->
      </batchtest>
      <formatter type="xml"/>
      <assertions>
        <enable/>
      </assertions>
    </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-benchmarks" depends="junit-compile">
    <mkdir dir="build/test/benchmark"/>
    <junit printsummary="on" fork="true" forkmode="perTest"
           maxmemory="${test.vm.mx}" showoutput="true"
           dir="testsrc/benchmarks">
        <sysproperty key="rhino.benchmark.report" value="../../build" />
        <classpath>
        <pathelement path="${classes}" />
        <pathelement path="${test.classes}" />
        <pathelement path="lib/junit.jar" />
        <pathelement path="lib/hamcrest.jar" />
        <pathelement location="${test.jstests.jar}"/>
      </classpath>

      <batchtest todir="build/test/benchmark">
        <fileset dir="${test.classes}" includes="**/benchmarks/SunSpiderBenchmark.class"/>
      </batchtest>
      <formatter type="xml"/>
      <assertions>
        <disable/>
      </assertions>
    </junit>

    <junit printsummary="on" fork="true" forkmode="perTest"
           maxmemory="${test.vm.mx}" showoutput="true"
           dir="testsrc/benchmarks">
      <sysproperty key="rhino.benchmark.report" value="../../build" />
      <classpath>
        <pathelement path="${classes}" />
        <pathelement path="${test.classes}" />
        <pathelement path="lib/junit.jar" />
        <pathelement path="lib/hamcrest.jar" />
        <pathelement location="${test.jstests.jar}"/>
      </classpath>
      <batchtest todir="build/test/benchmark">
        <fileset dir="${test.classes}" includes="**/benchmarks/V8Benchmark.class"/>
      </batchtest>
      <formatter type="xml"/>
      <assertions>
        <disable/>
      </assertions>
    </junit>

    <mkdir dir="build/test/benchmark/report"/>
    <junitreport todir="build/test/benchmark/report">
       <fileset dir="build/test/benchmark" includes="*.xml"/>
       <report todir="build/test/benchmark/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"/>
        <xml outfile="${coverage.report.dir}/emma.xml"/>
      </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">
        <include name="**/*.java"/>
        <include name="**/*.js"/>
        <include name="**/*.jstest"/>
        <include name="**/*.doctest"/>
        <include name="**/*.properties"/>
        <include name="**/*.xml"/>
        <include name="**/*.html"/>
        <include name="**/*.jar"/>
        <include name="*.skip"/>
        <include name="*.tests"/>
        <include name="tests.tar.gz"/>
      </fileset>
    </copy>
  </target>
</project>
