<?xml version="1.0"?>
<project name="Greenstone3 Testing Extension" default="build-util-jar" basedir=".">
  <property name="pref.java.major.version" value="11"/><!--1.8-->
  
  <property environment="env"/> <!-- get properties from the environment, gives access to Windows env vars too like APPDATA and LOCALAPPDATA -->

  <!--Allow compiling up jar files for a particular source and target, specified in JAVACFLAGS
      e.g JAVACFLAGS="-source 1.8 -target 1.8"
      which says the source code syntax is to be compatible with 1.8 and
      target bytecode (class files) should be compatible with v1.8.
  -->
  <condition property="custom.javac.flags"
	     value="-source ${pref.java.major.version} -target ${pref.java.major.version}"
	     else="">
    <and>
      <isset property="pref.java.major.version"/>
      <not><equals arg1="${pref.java.major.version}" arg2=""/></not>
    </and>
  </condition>
  <condition property="compile.javac.flags" value="${env.JAVACFLAGS}" else="${custom.javac.flags}">
    <and>
      <isset property="env.JAVACFLAGS"/>
      <not><equals arg1="${env.JAVACFLAGS}" arg2=""/></not>
    </and>
  </condition>

  <property name="compile.includeantruntime" value="false"/> <!-- to get rid of annoying 'ant' warning -->

  <property name="gli.home" value="${env.GSDL3SRCHOME}/gli"/>
  <property name="gsi.home" value="${env.GSDL3SRCHOME}"/>

  <!-- define ext.test.folder so we can move some targets into toplevel GS3 ant build file -->
  <property name="ext.test.folder" value="${basedir}" />
  
  <!-- where the reports will go -->
  <property name="test.reports" value="${ext.test.folder}/reports" />
  
  <!-- https://stackoverflow.com/questions/453170/using-ant-to-detect-os-and-set-property -->
  <!--
  <condition property="gecko.exe" value=".exe" else="">
	<os family="windows" />
  </condition>
  <condition property="gecko.mac" value="_mac" else="">
	<os family="mac" />
  </condition>
  -->
  <!-- target to compile both testing harness and GUI testing helper functions in GSGUITestingUtil.java
  TODO: It will eventually probably need to also compile the Selenium-based browser testing helper functions in GSSeleniumUtil.java -->
  <target name="build-all-jars" depends="build-util-jar, compile-tutorials-tests">
    <echo>*** Building jar files using java version settings: |${compile.javac.flags}|
    (Note you have version ${java.version} installed.)
    To control this, adjust property pref.java.major.version in build.xml
    or override by setting the JAVACFLAGS env variable as follows:
    export JAVACFLAGS="-source &lt;java-version&gt; -target &lt;java-version&gt;"
    To not set java source/target version settings, comment out pref.java.major.version.</echo>
  </target>
  
  <target name="build-util-jar" depends="needs-gs3-setup, needs-make-gli-jar,
					 needs-gsi-server-jar">
    <mkdir dir="${ext.test.folder}/build"/>
    <javac srcdir="${ext.test.folder}/src"
	       destdir="${ext.test.folder}/build"
           encoding="UTF-8"
	   includeantruntime="${compile.includeantruntime}">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath>
	<!--
	<fileset dir="${ext.test.folder}/lib/java">
	  <include name="*.jar"/>
	</fileset>
	-->
	
	<!-- 1. Force the Selenium Server jar to load first -->
	<pathelement location="${ext.test.folder}/lib/java/selenium-server-4.11.0.jar" />	
	<!-- 2. Load the rest of the jars -->
	<fileset dir="${ext.test.folder}/lib/java">
          <include name="*.jar"/>
          <!-- Optional: exclude the selenium jar here since it's already added -->
          <exclude name="selenium-server-4.11.0.jar"/>
	  <exclude name="guava-*.jar" />
	</fileset>
	
	<fileset dir="${gli.home}">
	<include name="GLI.jar"/>
	</fileset>
	<fileset dir="${gsi.home}">
	<include name="server.jar"/>
      </fileset>
      </classpath>
      <include name="org/greenstone/gsdl3/testing/*.java"/>			
    </javac>
    <jar destfile="${ext.test.folder}/lib/java/GSTestingUtil.jar">
      <fileset dir="${ext.test.folder}/build">
	<include name="org/greenstone/gsdl3/testing/**"/>			  
      </fileset>
      <manifest>
	<attribute name="Built-By" value="Greenstone" />
      </manifest>
    </jar>
  </target>
  
<!-- +++++++++++++ TUTORIAL TESTING TARGETS ++++++++++++++ -->
<target name="needs-gsi-server-jar">  
  <condition property="gsi-jar-missing">
    <not>
      <available file="${gsi.home}/server.jar"/>
    </not>
    </condition>
    
    <fail if="gsi-jar-missing" message="Run ant compile-core in ${gsi.home} folder to generate the Greenstone Server Interface's server.jar."/>
</target>

<target name="needs-make-gli-jar">  
  <condition property="gli-jar-missing">
    <not>
      <available file="${gli.home}/GLI.jar"/>
    </not>
    </condition>
    
    <fail if="gli-jar-missing" message="Run the makegli followed by makejar scripts in the gli folder before starting the Greenstone server."/>
</target>

<target name="needs-gs3-setup">
    <!-- has the gs3-setup script been run?? -->
    <condition property="gs3-setup-not-done">
      <not>
	<isset property="env.GSDL3HOME"/>
      </not>
    </condition>

    <fail if="gs3-setup-not-done" message="Run .\setenv.bat on Windows or source ./setenv.sh on Linux (alternatively run 'gs3-setup' or 'source gs3-setup.sh' from the toplevle GS3 dir) before starting the Greenstone server."/>

    <echo>GLI HOME: ${gli.home}</echo>
</target>

<!-- Removed from depends list: needs-gsi-server-jar -->
<target name="compile-tutorials-tests" depends="needs-gs3-setup, needs-make-gli-jar, needs-gsi-server-jar">
  <mkdir dir="${ext.test.folder}/build"/>
  <javac
      srcdir="${ext.test.folder}/src"
      destdir="${ext.test.folder}/build"
      encoding="UTF-8"
      includeantruntime="${compile.includeantruntime}">
    <compilerarg line="${compile.javac.flags}"/>
    <classpath>
	<!--
	<fileset dir="${ext.test.folder}/lib/java">
		<include name="*.jar"/>
	</fileset>
	-->

	<!-- 1. Force the Selenium Server jar to load first -->
	<pathelement location="${ext.test.folder}/lib/java/selenium-server-4.11.0.jar" />	
	<!-- 2. Load the rest of the jars -->
	<fileset dir="${ext.test.folder}/lib/java">
          <include name="*.jar"/>
          <!-- Optional: exclude the selenium jar here since it's already added -->
          <exclude name="selenium-server-4.11.0.jar"/>
	  <exclude name="guava-*.jar" />
	</fileset>	
	
      <fileset dir="${gli.home}">
	<include name="GLI.jar"/>
      </fileset>
      <fileset dir="${gsi.home}">
	<include name="server.jar"/>
      </fileset>
    </classpath>
    <include name="gstests/tutorials/*.java"/>
  </javac>
  <jar destfile="${ext.test.folder}/tutorial-tests.jar">
    <fileset dir="${ext.test.folder}/build">
      <include name="gstests/tutorials/**"/>
    </fileset>
    <manifest>
      <attribute name="Built-By" value="greenstone" />
    </manifest>
  </jar>
</target>

<!-- https://stackoverflow.com/questions/10704324/how-to-add-to-classpath-all-classes-from-set-of-directories-in-ant -->
<path id="tutorials.path">
  
  <!-- Classpath for gs3-server:
       $GSDL3SRCHOME/cp.jar:$GSDL3SRCHOME/resources/java:$GSDL3SRCHOME/web/WEB-INF/classes:.
       where final . is $GSDL3SRCHOME (for accessing cp.jar and server.jar)
       
       Then can run as
       $GSDL3SRCHOME>java org.greenstone.server.Server3 "$GSDL3SRCHOME/" "en"
  -->
  <fileset dir="${gsi.home}">
    <include name="server.jar"/>
    <include name="cp.jar"/>
  </fileset>

  <!-- Now we also need log4j.jar on CLASSPATH (Mar 2026) -->
  <fileset dir="${gsi.home}/web/WEB-INF/lib">
    <include name="log4j-1.2.16.jar"/>
  </fileset>

  <!-- path to servericon.png image, to log4j jars and props, and to server.jar itself -->
  <pathelement path="${gsi.home}/resources/java"/>
  <pathelement path="${gsi.home}/web/WEB-INF"/> <!-- for web.xml to be able to find server.xml -->
  <pathelement path="${gsi.home}/web/WEB-INF/classes"/>
  <!--<pathelement path="${gsi.home}"/> in place of including server.jar above-->
  
  <!-- Path to properties file -->
  <pathelement path="${ext.test.folder}/lib"/>
  
  <!-- Includes ant-nodeps-1.8.1 from 2010 that replaces 2005's optional.jar of ant
       for generating reports -->
  <!-- 
       <fileset dir="${ext.test.folder}/lib/java">
       <include name="*.jar"/>
       </fileset>
  -->
  
  <!-- 1. Force the Selenium Server jar to load first -->
  <pathelement location="${ext.test.folder}/lib/java/selenium-server-4.11.0.jar" />	
  <!-- 2. Load the rest of the jars -->
  <fileset dir="${ext.test.folder}/lib/java">
    <include name="*.jar"/>
    <!-- Optional: exclude the selenium jar here since it's already added -->
    <exclude name="selenium-server-4.11.0.jar"/>
    <exclude name="guava-*.jar" />
  </fileset>
	
  <!--<fileset dir="${ext.test.folder}/build">	    
    <include name="gstests/tutorials/**/*.class"/>
  </fileset>-->
  
  <fileset dir="${ext.test.folder}">
    <include name="tutorial-tests.jar"/>
  </fileset>
  <!-- The files XML element with the includes attribute is insufficient for adding a jar file to the
	classpath in the case where we've installed GS3 in a path with spaces, so we use above fileset way -->
  <!--<files includes="${ext.test.folder}/tutorial-tests.jar"/>-->

  <!-- To run GLI, need GLI.jar and its help jars and folder: classes folder, apache.jar, jna.jar, jna-platform.jar, qfslib.jar, rsyntaxtextarea.jar -->
  <fileset dir="${gli.home}">
    <include name="GLI.jar"/>
  </fileset>
  <pathelement path="${gli.home}/classes"/>
  <fileset dir="${gli.home}/lib">
    <include name="*.jar"/>
  </fileset>
</path>

<!-- TODO: for darwin, need to set custom_vm_args, see gli.sh
TODO: Launching GLI for GS2 is also different -->



<!-- JUnit must run in GLI folder to mimic how GLI is run.
     One reason why this is necessary is that GLI then won't display parsing issues with GLI class HelpFrame -->
<!-- http://ant.1045680.n5.nabble.com/How-to-increase-memory-used-by-JVM-in-Ant-td1355370.html -->
<target name="run-tutorials-tests" depends="needs-gs3-setup, needs-make-gli-jar">
  <echo>Tutorial tests</echo>
  <echo>GSDLHOME: ${env.GSDLHOME}</echo>
  <!-- GS on Windows supports spaces in installation filepath, but testing doesn't work.
  The following prints out the classpath, and running ant -verbose run-tutorials-tests will print out the
  java command that's run-->
  <pathconvert property="runtime.classpath.for.echo" refid="tutorials.path"/>
  <!--<echo>classpath: ${runtime.classpath.for.echo}</echo>-->
  <java classname="org.junit.runner.JUnitCore" dir="${gli.home}" fork="true" maxmemory="256m" classpathref="tutorials.path">
    <!-- https://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr -->
    
    <!--
    <sysproperty key="webdriver.gecko.driver" path="${ext.test.folder}/geckodriver${gecko.exe}${gecko.mac}"/>
    <sysproperty key="webdriver.edge.driver" path="${ext.test.folder}/msedgedriver.exe"/>
    <sysproperty key="webdriver.chrome.driver" path="${ext.test.folder}/chromedriver-linux64/chromedriver"/>
    -->
    
    <!-- Set the environment variable here -->
    <env key="MSEDGEDRIVER_TELEMETRY_OPTOUT" value="1"/> <!-- don't need to send EdgeDriver's diagnostic data to Microsoft -->
	
    <!-- The Browser (Firefox) -->
    <!--<sysproperty key="webdriver.firefox.bin" value="${env.LOCALAPPDATA}\Mozilla Firefox\firefox.exe" />-->
       <!-- Always passed, but if empty, Selenium ignores it and uses defaults -->
       <!--<sysproperty key="webdriver.firefox.bin" value="${firefox.path}" />-->
       <!--<sysproperty key="webdriver.${test.browser}.bin" value="${test.browser.path}" />-->
				 
    <arg value="gstests.tutorials.RunGLITest"/>
    <!-- TODO: for now hardcoding the GS3 URL. Toplevel GS3 build.xml can properly construct this
	 URL, but hard to do so quickly here -->
    <jvmarg value="-DSERVERURL=http://localhost:8383/greenstone3/library "/>
  </java>

  <!-- create reports dir if it doesn't exist -->
  <mkdir dir="${test.reports}"/>
  <!--
      https://junit.org/junit4/faq.html#running_6
      How do I use Ant to create HTML test reports?
  -->
  <junitreport todir="${test.reports}">
    <fileset dir="${test.reports}">
      <include name="TEST-*.xml" />
    </fileset>
    <report todir="${test.reports}" />
  </junitreport>
</target>

<target name="clean">
  <delete file="${basedir}/tutorial-tests.jar"/>
  <delete file="${basedir}/lib/java/GSTestingUtil.jar"/>
    <!-- Use filesets to delete all classfiles from the given dir
	 https://ant.apache.org/manual/Tasks/delete.html -->
  <delete>
    <fileset dir="${basedir}/build/gstests/tutorials/" includes="**/*.class"/>
    <fileset dir="${basedir}/build/org/greenstone/gsdl3/testing/" includes="**/*.class"/>
  </delete>
</target>

<!-- TODO: check if after testing GLI we may need to ensure tomcat stopped? -->

</project>

