<?xml version="1.0" encoding="UTF-8"?>

<project name="JOGLTest" basedir="." default="all">

    <description>JUNIT Tests JOGL</description>

    <import file="build-common.xml"/>

    <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath> <pathelement location="${ant-contrib.jar}"/> </classpath>
    </taskdef>

    <!-- ================================================================== -->
    <!-- 
       - Declare all paths and user defined variables.
      -->
    <target name="declare.common" description="Declare properties" depends="common.init">
        <property name="rootrel.src.test"     value="src/test" />
        <property name="src.test"             value="${project.root}/${rootrel.src.test}" />

        <property name="classes"              value="${build.test}/classes" />
        <property name="classes.path"         location="${classes}"/> <!-- absolute path -->

        <property name="java.part.test"       value="com/jogamp/** jogamp/**"/>
        <property name="java.dir.test"        value="com/jogamp/opengl/test"/>
        <property name="java.dir.junit"       value="${java.dir.test}/junit"/>
        <property name="java.dir.bugs"        value="${java.dir.test}/bugs"/>

        <property name="test.archive.name"    value="${archive.name}-test-results-${build.node.name}"/>
        <condition property="jvmarg.mainthrd" value="-XstartOnFirstThread"><isset property="isOSX"/></condition>
        <condition property="jvmarg.mainthrd" value="-Ddummy"><not><isset property="isOSX"/></not></condition>
        <condition property="jvmarg.headless" value="-XstartOnFirstThread -Djava.awt.headless=true"><isset property="isOSX"/></condition>
        <condition property="jvmarg.headless" value="-Djava.awt.headless=true"><not><isset property="isOSX"/></not></condition>

        <property name="batchtest.timeout"    value="1800000"/> <!-- 30 min -->

        <property name="java.part.test.shadercode" value="com/jogamp/opengl/test/junit/jogl/demos/es2/shader/*"/>
    </target>
    
    <!-- ================================================================== -->
    <!--
       - Clean up all that is built.
      -->
    <target name="clean" description="Remove all build products" depends="declare.common">
        <delete includeEmptyDirs="true" quiet="true">
            <fileset dir="${build.test}" />
            <fileset dir="." includes="*.tga" />
            <fileset file="${jogl.test.jar}" />
        </delete>
    </target>

    <!-- ================================================================== -->
    <!--
       - Build/run tests/junit.
      -->
    <target name="test.compile.check" depends="declare.common">
      <!-- Create the required output directories. -->
      <mkdir dir="${obj.test}" />
      <mkdir dir="${classes}" />

      <!-- Clean the results -->
      <delete quiet="true">
        <fileset dir="${results.test}" includes="**"/>
        <fileset file="${build}/${test.archive.name}.7z"/>
        <fileset dir="." includes="*.tga" />
      </delete>
      <mkdir dir="${results.test}" />

      <property name="jogl.test.jar.path" location="${jogl.test.jar}"/> <!-- absolute path -->
      <echo message="jogl.test.jar ${jogl.test.jar.path}"/>
      <uptodate property="test.compile.skip">
        <srcfiles dir= "."                 includes="*.xml"/>
        <srcfiles dir= "${src.test}"       includes="**"/>
        <srcfiles                          file="${nativewindow.all.jar}" />
        <srcfiles                          file="${jogl.all.jar}" />
        <srcfiles                          file="${newt.all.jar}" />
        <srcfiles                          file="${gluegen.jar}" />
        <mapper type="merge" to="${jogl.test.jar.path}"/>
      </uptodate>
    </target>

    <target name="test.compile" depends="test.compile.check" unless="test.compile.skip">
        <!-- Perform the junit pass Java compile -->
        <javac destdir="${classes}"
               source="${host.sourcelevel}"
               fork="yes"
               memoryMaximumSize="${javac.memorymax}"
               includeAntRuntime="false"
               debug="${javacdebug}" debuglevel="${javacdebuglevel}">
            <classpath refid="junit_jogl_newt.compile.classpath"/>
            <src path="${src.test}" />
        </javac>
        <jar destfile="${jogl.test.jar}" filesonly="true">
            <!-- get all class files, but skip any resource files that external tools
                 might have copied into the class directory (otherwise, it's possible
                 to get the same resource file twice in the jar) -->
            <fileset dir="${classes}"
                includes="${java.part.test}"/>
            <!-- include any resource files that tests may requre -->
            <fileset dir="${src.test}">
                <exclude name="**/*.java"/>
            </fileset>
            <fileset dir="${src.test}" includes="${java.part.test.shadercode}"/>
        </jar>
        <copy todir="${classes}">
            <fileset dir="${src.test}" includes="${java.part.test.shadercode}"/>
        </copy>
    </target>

    <target name="test.manual.run" depends="test.compile">
        <for param="test.class.path.m" keepgoing="true">
            <!-- results in absolute path -->
            <fileset dir="${classes}">
                <include name="${java.dir.bugs}/**/*Test*"/>
                <exclude name="**/*$$*"/>
            </fileset>
          <sequential>
            <var name="test.class.path" unset="true"/>
            <property name="test.class.path" basedir="${classes}" relative="true" location="@{test.class.path.m}"/>
            <var name="test.class.fqn" unset="true"/>
            <pathconvert property="test.class.fqn">
              <fileset file="${classes}${file.separator}${test.class.path}"/>
              <chainedmapper>
                  <globmapper    from="${classes.path}${file.separator}*" to="*"/> <!-- rel. -->
                  <packagemapper from="*.class"           to="*"/> <!-- FQCN -->
              </chainedmapper>
            </pathconvert>
            <var name="test.class.result.file" value="${results.test}/TEST-${test.class.fqn}.log"/>
            <echo message="Testing ${test.class.fqn} -- ${test.class.result.file}"/>
            <apply dir="." executable="${java.home}/bin/java" 
                 parallel="false" 
                 timeout="${batchtest.timeout}"
                 vmlauncher="false"
                 relative="true"
                 failonerror="false"
                 output="${test.class.result.file}">
                <env key="${system.env.library.path}" path="${obj.all.paths}"/>
                <env key="CLASSPATH" value="${junit_jogl_newt_awt.run.jars}"/>
                <arg value="-Djava.library.path=${obj.all.paths}"/>
                <!--
                <arg line="-Dnewt.debug.EDT"/>
                -->
                <srcfile/>
                <mappedresources>
                    <fileset dir="${classes}" includes="${test.class.path}"/>
                    <packagemapper from="*.class" to="*"/>
                </mappedresources>
            </apply>
          </sequential>
        </for>
        <antcall target="test-zip-archive" inheritRefs="true" inheritAll="true"/>
    </target>

    <target name="junit.run.noui" depends="test.compile">
        <!-- Test*NOUI* -->
        <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${obj.all.paths}"/>
            <jvmarg value="-Djava.library.path=${obj.all.paths}"/>

            <!--
            <jvmarg value="-Djogl.debug=all"/>
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-Djogl.debug.GLSLState"/>
            <jvmarg value="-Dnativewindow.debug=all"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->

            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit_jogl_awt.run.classpath"/>

            <batchtest todir="${results.test}">
              <fileset dir="${classes}">
                  <include name="${java.dir.junit}/**/Test*NOUI*"/>
                  <exclude name="**/*$$*"/>
              </fileset>
              <formatter usefile="false" type="brief"/>
              <formatter usefile="true" type="xml"/>
            </batchtest>
        </junit>
    </target>

    <!-- NEWT is currently not supported on OSX -->
    <target name="junit.run.newt.headless" depends="test.compile" unless="isOSX">
        <!-- Test*NEWT* 

             Emulation of junit task,
             due to the fact that we have to place invoke our MainThread class first (-> MacOSX).

             Utilizing Ant-1.8.0 and ant-contrib-1.0b3 (loops, mutable properties).
          --> 
        <for param="test.class.path.m" keepgoing="true">
            <!-- results in absolute path -->
            <fileset dir="${classes}">
                <include name="${java.dir.junit}/**/Test*NEWT*"/>
                <exclude name="**/*$$*"/>
            </fileset>
          <sequential>
            <var name="test.class.path" unset="true"/>
            <property name="test.class.path" basedir="${classes}" relative="true" location="@{test.class.path.m}"/>
            <var name="test.class.fqn" unset="true"/>
            <pathconvert property="test.class.fqn">
              <fileset file="${classes}${file.separator}${test.class.path}"/>
              <chainedmapper>
                  <globmapper    from="${classes.path}${file.separator}*" to="*"/> <!-- rel. -->
                  <packagemapper from="*.class"           to="*"/> <!-- FQCN -->
              </chainedmapper>
            </pathconvert>
            <var name="test.class.result.file" value="${results.test}/TEST-${test.class.fqn}.xml"/>
            <echo message="Testing ${test.class.fqn} -- ${test.class.result.file}"/>
            <apply dir="." executable="${java.home}/bin/java" 
                 parallel="false" 
                 timeout="${batchtest.timeout}"
                 vmlauncher="false"
                 relative="true"
                 failonerror="false">
                <env key="${system.env.library.path}" path="${obj.all.paths}"/>
                <env key="CLASSPATH" value="${junit_jogl_newt.run.jars}"/>
                <arg value="-Djava.library.path=${obj.all.paths}"/>
                <arg line="${jvmarg.headless}"/>
                <arg line="-Dnativewindow.x11.mt-bug=true"/> <!-- bug 502 workaround -->
                <!--
                <arg line="-Dnewt.debug.EDT"/>
                -->
                <arg line="com.jogamp.newt.util.MainThread"/>
                <arg line="org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"/>
                <srcfile/>
                <arg line="filtertrace=true"/>
                <arg line="haltOnError=false"/>
                <arg line="haltOnFailure=false"/>
                <arg line="showoutput=true"/>
                <arg line="outputtoformatters=true"/>
                <arg line="logfailedtests=true"/>
                <arg line="logtestlistenerevents=true"/>
                <arg line="formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter"/>
                <arg line="formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,${test.class.result.file}"/>
                <mappedresources>
                    <fileset dir="${classes}" includes="${test.class.path}"/>
                    <packagemapper from="*.class" to="*"/>
                </mappedresources>
            </apply>
          </sequential>
        </for>
    </target>

    <!-- junit.run.newt is covered by junit.run.newt.headless, disable it for now, but may be checked manually.
         This test target would also overwrite the test result XML files, we would also need a solution here for hudson,
         if run in parallel.
         NEWT is currently not supported on OSX 
      -->
    <target name="junit.run.newt" depends="test.compile" unless="isOSX">
        <!-- Test*NEWT* -->
        <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${obj.all.paths}"/>
            <jvmarg value="-Djava.library.path=${obj.all.paths}"/>
            <jvmarg value="-Dnativewindow.x11.mt-bug=true"/> <!-- bug 502 workaround -->

            <!--
            <jvmarg value="-Dnewt.debug.EDT"/>
            <jvmarg value="-Djogl.debug=all"/>
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-Djogl.debug.GLSLState"/>
            <jvmarg value="-Dnativewindow.debug=all"/>
            <jvmarg value="-Dnewt.debug=all"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->

            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit_jogl_newt.run.classpath"/>

            <batchtest todir="${results.test}">
              <fileset dir="${classes}">
                  <include name="${java.dir.junit}/**/Test*NEWT*"/>
                  <exclude name="**/*$$*"/>
              </fileset>
              <formatter usefile="false" type="brief"/>
              <formatter usefile="true" type="xml"/>
            </batchtest>
        </junit>
    </target>

    <target name="junit.run.awt" depends="test.compile">
        <!-- Test*AWT* -->
        <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${obj.all.paths}"/>
            <jvmarg value="-Djava.library.path=${obj.all.paths}"/>

            <!--
            <jvmarg value="-Djogl.debug=all"/>
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-Djogl.debug.GLSLState"/>
            <jvmarg value="-Dnativewindow.debug=all"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->

            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit_jogl_awt.run.classpath"/>

            <batchtest todir="${results.test}">
              <fileset dir="${classes}">
                  <include name="${java.dir.junit}/**/Test*AWT*"/>
                  <exclude name="**/*$$*"/>
                  <exclude name="**/*SWT*"/>
                  <exclude name="**/newt/**"/>
              </fileset>
              <formatter usefile="false" type="brief"/>
              <formatter usefile="true" type="xml"/>
            </batchtest>
        </junit>
    </target>

    <target name="junit.run.awt.singletest" depends="test.compile">
        <!-- Test*AWT* -->
        <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${obj.all.paths}"/>
            <jvmarg value="-Djava.library.path=${obj.all.paths}"/>

            <!--
            <jvmarg value="-Djogl.debug=all"/>
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-Djogl.debug.GLSLState"/>
            <jvmarg value="-Dnativewindow.debug=all"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->

            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit_jogl_newt_awt.run.classpath"/>

            <test name="${testclass}"/>
        </junit>
    </target>

    <target name="junit.run.swt.headless" depends="test.compile" description="Runs all pure SWT tests.">
        <!-- Test*SWT* 

             Emulation of junit task,
             due to the fact that we have to place invoke our MainThread class first (-> MacOSX).

             Utilizing Ant-1.8.0 and ant-contrib-1.0b3 (loops, mutable properties).
          --> 
        <for param="test.class.path.m" keepgoing="true">
            <!-- results in absolute path -->
            <fileset dir="${classes}">
                <include name="${java.dir.junit}/**/Test*SWT*"/>
                <exclude name="**/*$$*"/>
                <exclude name="**/*AWT*"/>
                <exclude name="**/newt/**"/>
            </fileset>
          <sequential>
            <var name="test.class.path" unset="true"/>
            <property name="test.class.path" basedir="${classes}" relative="true" location="@{test.class.path.m}"/>
            <var name="test.class.fqn" unset="true"/>
            <pathconvert property="test.class.fqn">
              <fileset file="${classes}${file.separator}${test.class.path}"/>
              <chainedmapper>
                  <globmapper    from="${classes.path}${file.separator}*" to="*"/> <!-- rel. -->
                  <packagemapper from="*.class"           to="*"/> <!-- FQCN -->
              </chainedmapper>
            </pathconvert>
            <var name="test.class.result.file" value="${results.test}/TEST-${test.class.fqn}.xml"/>
            <echo message="Testing ${test.class.fqn} -- ${test.class.result.file}"/>
            <apply dir="." executable="${java.home}/bin/java" 
                 parallel="false" 
                 timeout="${batchtest.timeout}"
                 vmlauncher="false"
                 relative="true"
                 failonerror="false">
                <env key="${system.env.library.path}" path="${obj.all.paths}"/>
                <env key="CLASSPATH" value="${junit_jogl_swt.run.jars}"/>
                <arg value="-Djava.library.path=${obj.all.paths}"/>
                <arg line="${jvmarg.headless}"/>
                <!--
                <arg line="-Dnewt.debug.EDT"/>
                -->
                <arg line="com.jogamp.newt.util.MainThread"/>
                <arg line="org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"/>
                <srcfile/>
                <arg line="filtertrace=true"/>
                <arg line="haltOnError=false"/>
                <arg line="haltOnFailure=false"/>
                <arg line="showoutput=true"/>
                <arg line="outputtoformatters=true"/>
                <arg line="logfailedtests=true"/>
                <arg line="logtestlistenerevents=true"/>
                <arg line="formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter"/>
                <arg line="formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,${test.class.result.file}"/>
                <mappedresources>
                    <fileset dir="${classes}" includes="${test.class.path}"/>
                    <packagemapper from="*.class" to="*"/>
                </mappedresources>
            </apply>
          </sequential>
        </for>
    </target>

    <target name="junit.run.swt.awt" depends="test.compile" description="Runs all SWT_AWT tests.">
      <!-- Test*SWT* -->
      <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
          <env key="${system.env.library.path}" path="${obj.all.paths}"/>
          <jvmarg value="-Djava.library.path=${obj.all.paths}"/>
          <jvmarg value="${jvmarg.mainthrd}"/>

          <!--
          <jvmarg value="-Djogl.debug=all"/>
          <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
          <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
          <jvmarg value="-Djogl.debug.GLSLState"/>
          <jvmarg value="-Dnativewindow.debug=all"/>
          <jvmarg value="-verbose:jni"/> 
          <jvmarg value="-client"/>
          <jvmarg value="-d32"/>
          -->

          <formatter usefile="false" type="plain"/>
          <formatter usefile="true" type="xml"/>
          <classpath refid="junit_jogl_swt.run.classpath"/>

          <batchtest todir="${results.test}">
            <fileset dir="${classes}">
                <include name="${java.dir.junit}/**/Test*SWT*AWT*"/>
                <exclude name="**/*$$*"/>
                <exclude name="**/newt/**"/>
            </fileset>
            <formatter usefile="false" type="brief"/>
            <formatter usefile="true" type="xml"/>
          </batchtest>
      </junit>
    </target>

    <!-- NEWT is currently not supported on OSX -->
    <target name="junit.run.newt.awt" depends="test.compile" unless="isOSX">
        <!-- Test*AWT* -->
        <junit forkmode="perTest" showoutput="true" fork="true" haltonerror="off" timeout="${batchtest.timeout}">
            <env key="${system.env.library.path}" path="${obj.all.paths}"/>
            <jvmarg value="-Djava.library.path=${obj.all.paths}"/>
            <jvmarg value="-Dnativewindow.x11.mt-bug=true"/> <!-- bug 502 workaround -->

            <!--
            <jvmarg value="-Dnewt.debug.EDT"/>
            <jvmarg value="-Djogl.debug=all"/>
            <jvmarg value="-Dgluegen.debug.NativeLibrary=true"/>
            <jvmarg value="-Dgluegen.debug.ProcAddressHelper=true"/>
            <jvmarg value="-Djogl.debug.GLSLState"/>
            <jvmarg value="-Dnativewindow.debug=all"/>
            <jvmarg value="-Dnewt.debug=all"/>
            <jvmarg value="-verbose:jni"/> 
            <jvmarg value="-client"/>
            <jvmarg value="-d32"/>
            -->

            <formatter usefile="false" type="plain"/>
            <formatter usefile="true" type="xml"/>
            <classpath refid="junit_jogl_newt_awt.run.classpath"/>

            <batchtest todir="${results.test}">
              <fileset dir="${classes}">
                  <include name="${java.dir.junit}/**/newt/**/Test*AWT*"/>
                  <exclude name="**/*$$*"/>
              </fileset>
              <formatter usefile="false" type="brief"/>
              <formatter usefile="true" type="xml"/>
            </batchtest>
        </junit>
    </target>

    <target name="test.auto.run" depends="junit.run"/>
    <target name="junit.run" depends="junit.run.noui,junit.run.newt.headless,junit.run.awt,junit.run.newt.awt,junit.run.swt.headless,junit.run.swt.awt">
        <antcall target="test-zip-archive" inheritRefs="true" inheritAll="true"/>
    </target>

    <!-- updates / create the test results zip file -->
    <target name="test-zip-archive" depends="declare.common">
        <move todir="${results.test}">
            <fileset dir="." includes="*.tga" />
        </move>
        <mkdir dir="${results.test}" />
        <archive.7z destfile="${build}/${test.archive.name}.7z"
                    basedir="${results.test}" 
                    includes="**" />
    </target>

    <!-- ================================================================== -->
    <!--
       - Build everything.
      -->
    <target name="all" description="Build JOGL JUNIT tests" depends="test.compile" />

</project>