<project name="Automated Testing" default="all"  basedir="." >

	<!--properties file containing the plugin directory name including version number-->
	<property file="test.properties" />
	
	<!-- urls and such from the build -->
	<property file="${basedir}/../../../build.cfg" />

	<!--default directory where test-eclipse will be installed-->
	<property name="install" value="${basedir}/target" />
	
	<!--name that can be added to report name to identify which platform tests results come from-->
	<property name="platform" value="" />

	<!-- The root of the eclipse installation -->
	<property name="eclipse-home" value="${install}/eclipse" />

	<!-- The directory that will contain the xml and html results from the tests that are run -->
 	<property name="results" value="${basedir}/results" />

	<target name="setup" if="clean" description="Reinstall the test Eclipse installation if specified by user">
		<delete dir="${install}" /><mkdir dir="${install}" />
		
		<!-- TODO: if your project requires more dependencies, add them here -->
		<echo message="Download, then unpack: Eclipse, EMF ..." />
		<property name="common.releng.dir" value="${basedir}/../../../org.eclipse.modeling.common.releng"/>
		<property name="getTestDependencies.xml" value="${common.releng.dir}/scripts/getTestDependencies.xml"/>
		<ant target="getTestDependency" antfile="${getTestDependencies.xml}" dir="${common.releng.dir}">
			<property name="url" value="${eclipseURL}" />
			<property name="file" value="${eclipseFile}" />
			<property name="unpackDest" value="${install}"/>
		</ant>
		<ant target="getTestDependency" antfile="${getTestDependencies.xml}" dir="${common.releng.dir}">
			<property name="url" value="${emfURL}" />
			<property name="file" value="${emfFile}" />
			<property name="unpackDest" value="${install}"/>
		</ant>
		<ant target="getTestDependency" antfile="${getTestDependencies.xml}" dir="${common.releng.dir}">
			<property name="url" value="${orbitURL}" />
			<property name="file" value="${orbitFile}" />
			<property name="unpackDest" value="${install}"/>
		</ant>

		<property name="dir" location="."/>
		<echo message="Unpack SDK + JUnit Tests (${dir}/RSE-*.zip) ..."/>		
		<unzip dest="${install}" overwrite="true">
		    <fileset dir="${dir}">
		    	<include name="**/RSE-*.zip"/>
		    	<include name="**/TM-terminal-*.zip"/>
		    </fileset>
		</unzip>
	</target>

	<target name="runtests" depends="setup" description="Runs ant on the test.xml for a specified plugin.  Requires a property value setting for testPlugin only if test.properties is not available.  The property testPlugin represents a directory name made up of the plugin id and plugin version.  This directory must contain a valid test.xml.">
		<echo message="Running test.xml from jarfile ${eclipse-home}/plugins/${testPlugin} ... "/>
		<ant antfile="${eclipse-home}/plugins/${testPlugin}/test.xml" dir="${eclipse-home}"/>
		<copy file="${eclipse-home}/${report}.xml" tofile="${results}/xml/${report}_${platform}.xml" />
	</target>

	<target name="unjarPlugin" depends="setup">
		<echo message="Unpack ${eclipse-home}/plugins/${testPlugin}.jar to ${eclipse-home}/plugins/${testPlugin} ... "/>
		<move file="${eclipse-home}/plugins/${testPlugin}.jar" tofile="${eclipse-home}/plugins/${testPlugin}_.jar"/>
		<unjar src="${eclipse-home}/plugins/${testPlugin}_.jar" dest="${eclipse-home}/plugins/${testPlugin}"/>
		<delete file="${eclipse-home}/plugins/${testPlugin}_.jar"/>
	</target>

	<target name="dontUnjarPlugin" depends="setup">
		<echo message="${eclipse-home}/plugins/${testPlugin} is not a jarred plugin. No unpack required!"/>
	</target>

	<target name="rse" description="Runs the test.xml of the rse.tests plugin">
		<antcall target="dontUnjarPlugin">
			<param name="testPlugin" value="${org.eclipse.rse.tests}" />
		</antcall>	
		<antcall target="runtests">
			<param name="testPlugin" value="${org.eclipse.rse.tests}" />
			<param name="report" value="org.eclipse.rse.tests" />
		</antcall>

		<!-- test if chkpii script exists before proceeding -->
		<property name="common.releng.dir" value="${basedir}/../../../org.eclipse.modeling.common.releng"/>
		<property name="chkpii.xml" value="${common.releng.dir}/scripts/chkpii.xml"/>
		<available file="${chkpii.xml}" property="chkpiiXMLAvailable"/>
		<antcall target="runChkpii"/>
	</target>

	<target name="terminal" description="Runs the test.xml of the tm.terminal.test plugin">
		<antcall target="unjarPlugin">
			<param name="testPlugin" value="${org.eclipse.tm.terminal.test}" />
		</antcall>	
		<antcall target="runtests">
			<param name="testPlugin" value="${org.eclipse.tm.terminal.test}" />
			<param name="report" value="org.eclipse.tm.terminal.test" />
		</antcall>

		<!-- test if chkpii script exists before proceeding -->
		<property name="common.releng.dir" value="${basedir}/../../../org.eclipse.modeling.common.releng"/>
		<property name="chkpii.xml" value="${common.releng.dir}/scripts/chkpii.xml"/>
		<available file="${chkpii.xml}" property="chkpiiXMLAvailable"/>
		<antcall target="runChkpii"/>
	</target>

	<target name="runChkpii" if="chkpiiXMLAvailable">
		<ant target="chkpii" antfile="${chkpii.xml}" inheritall="true">
			<property name="zipPattern" value="RSE-*.zip"/>
		</ant>
	</target>

	<target name="all">
		<antcall target="rse" />
		<antcall target="terminal" />
		<antcall target="genHtml" />
	</target>
	
	<target name="genHtml" description="Generates HTML results with provided JUNIT.XSL provided">
		<style style="JUNIT.XSL" basedir="${results}/xml" destdir="${results}/html" />
	</target>
</project>
