<?xml version="1.0" encoding="UTF-8"?>
<!--
    File:    nbtargets-suiterunner.xml
    Version: 0.1

    Enable NetBeans to use Artima SuiteRunner to run and debug project tests
    via "standard project" buildfile.

    Audience: developers who wish to use NetBeans to debug project test suites
    written using Artima SuiteRunner, on projects with the standard project
    build.xml file.  (May be adapted for a free-form project custom build.xml
    file.)

    Status: useful workaround

    Effects:
    - Test Commands run SuiteRunner tests
      * Test Project (project context menu):
        If test/recipe.srj exists, runs its tests with SuiteRunner, not JUnit.
        Otherwise runs tests in test classes **/Test.class, **/Tests.class,
        and **/*Suite.class using SuiteRunner, not JUnit.
      * Run File (project/test/file context menu):
        Runs tests in test class with SuiteRunner, not JUnit.
      * Debug File (project/test/file context menu):
        Runs tests in test class with SuiteRunner, not JUnit.
        (Tip: breakpoint on org.suiterunner.TestFailedException to debug test
        failure.)

    To install:
    0. Download SuiteRunner 1.0beta6 (site listed below).
    1. Add SuiteRunner jar to libraries (Tools/Library Manager)
    2. Add library to project test libraries.
    3. As of Netbeans 6.5, project "Compile on Save" must be off (issue#145772)
    4. Import this file in the build.xml of a NetBeans project.
       (Import it *before* build-impl.xml: among imports, first def prevails.)
    Example:

    <project name="MyProject" default="default" basedir=".">
      <description>Builds, tests, and runs the project MyProject.</description>
      <import file="nbtargets-suiterunner.xml" /> 
      <import file="nbproject/build-impl.xml" />
    </project>

    Tested using
    - NetBeans 5.5 - 6.5beta
      (www.netbeans.org)
    - Artima SuiteRunner 1.0beta6 (not beta7)
      (http://sourceforge.net/project/showfiles.php?group_id=72137,
       http://www.artima.com/suiterunner/index.html)
      (Beware, version 1.0beta7 has broken config parser.)

    Caveats of this draft
    - This file, nbtargets-suiterunner.xml, does not work with
      the NetBeans codecoverage module (2.2.1, 2007.02) (the classes
      are not instrumented until after the tests are run, so no code
      coverage data is recorded).
    - In contrast, nbtargets-testng-cvg.xml works with the NetBeans
      codecoverage module (2.2.1, 2007.02), though antcall is slower
      than using depends.
    - SuiteRunner-1.0beta6 does not set its exit status differently if
      there were failed tests, so NetBeans reports "BUILD SUCCESSFUL"
      even if some tests failed.
    - These Ant targets refer to the default directory property names,
      but adding a source or test directory more than once per project
      may cause NetBeans to use different property names for the
      directories.

    For more info, see "NetBeans and Alternative Test Runners"
    http://www.geocities.com/nimarukan/netbeans/nbtargets/nbtargets-alt-test-runners.html
-->
<project name="nbtargets-suiterunner">

  <!-- OVERRIDES -->

  <!-- override -do-test-run to use Artima SuiteRunner, not JUnit -->
  <target name="-do-test-run" 
          depends="init,compile-test,-pre-test-run,-suiterunner-run"
          if="have.tests" />

  <!-- override -do-test-run-single to use Artima SuiteRunner, not JUnit -->
  <target name="-do-test-run-single" 
          depends="init,compile-test-single,-pre-test-run-single,-suiterunner-run-single"
          if="have.tests"/>

  <!-- override -debug-start-debuggee-test to use Artima SuiteRunner, not JUnit -->
  <target name="-debug-start-debuggee-test" 
          depends="init,compile-test,-suiterunner-debug-single"
          if="have.tests" />


  <!-- SUITERUNNER-DEBUG-SINGLE -->

  <!-- Run selected test.class with SuiteRunner in the debugger. -->
  <target name="-suiterunner-debug-single">
    <fail unless="test.class">Must select one file in the IDE or set test.class</fail>

    <j3:debug classname="org.suiterunner.Runner"
              classpath="${debug.test.classpath}"
              xmlns:j3="http://www.netbeans.org/ns/j2se-project/3">
      <customize>
        <arg line="-oFBAR"/> <!-- report failures and aborts to System.out -->
        <arg line="-s ${test.class}"/>
      </customize>
    </j3:debug>
  
  </target>

  <!-- SUITERUNNER-RUN-SINGLE -->

  <!-- Run selected test.includes file with SuiteRunner -->
  <target name="-suiterunner-run-single">
    <fail unless="test.includes">Must select some files in the IDE or set test.includes</fail>

    <!-- convert test.includes "pkg/ClassTests.java" to "-s pkg.ClassTests" -->
    <pathconvert property="suiterunner.classnames.args" pathsep=" " dirsep=".">
      <path><filelist dir="." files="${test.includes}"/></path>
      <mapper type="glob" from="*.java" to="*"/>
      <map from="${basedir}${file.separator}" to="-s "/>
    </pathconvert>

    <java classname="org.suiterunner.Runner"
          classpath="${run.test.classpath}"
          fork="true">
      <arg line="-oFBAR"/> <!-- report failures and aborts to System.out -->
      <arg line="${suiterunner.classnames.args}"/>
    </java>
  </target>

  <!-- SUITERUNNER-RUN -->

  <!-- Run tests specified in test/recipe.srj or if no such file, then
       run all classes ending with *Test, *Tests, or *Suite in test dir.
       (One of -suiterunner-run-repipe or -suiterunner-run-classes
       runs, depending on existence of test/recipe.srj) -->
  <target name="-suiterunner-run"
          depends="-suiterunner-run-recipe,-suiterunner-run-classes"/>

  <target name="-check-suiterunner-recipe">
    <available property="suiterunner.recipe"
               file="${test.src.dir}${file.separator}recipe.srj"
               value="${test.src.dir}${file.separator}recipe.srj"/>
  </target>

  <target name="-suiterunner-run-recipe"
          depends="-check-suiterunner-recipe"
          if="suiterunner.recipe">
    <echo level="info">Running ${suiterunner.recipe}</echo>

    <java classname="org.suiterunner.Runner"
          classpath="${run.test.classpath}"
          fork="true">
      <arg line="${suiterunner.recipe}"/> <!-- first (rest ignored) -->
    </java>
  </target>

  <target name="-suiterunner-run-classes"
          depends="-check-suiterunner-recipe"
          unless="suiterunner.recipe">
    <!-- convert "pkg/ClassTests.java" to "-s pkg.ClassTests" -->
    <pathconvert property="suiterunner.classnames.args" pathsep=" " dirsep=".">
      <path>
        <fileset dir="${test.src.dir}" includes="**/*Test.java"/>
        <fileset dir="${test.src.dir}" includes="**/*Tests.java"/>
        <fileset dir="${test.src.dir}" includes="**/*Suite.java"/>
      </path>
      <mapper type="glob" from="*.java" to="*"/>
      <!-- map for relative test.src.dir -->
      <map from="${basedir}${file.separator}${test.src.dir}${file.separator}"
           to="-s "/>
      <!-- map for absolute test.src.dir -->
      <map from="${test.src.dir}${file.separator}"
           to="-s "/>
    </pathconvert>
    <echo level="info">Running -oFBAR ${suiterunner.classnames.args}</echo>
    <java classname="org.suiterunner.Runner"
          classpath="${run.test.classpath}"
          fork="true">
      <arg line="-oFBAR"/> <!-- report failures and aborts to System.out -->
      <arg line="${suiterunner.classnames.args}"/>
    </java>
  </target>

</project>
