<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; if it does not require all of these, remove those not required -->
		<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="${compareURL}" />
			<property name="file" value="${compareFile}" />
			<property name="unpackDest" value="${install}"/>
		</ant>	

		<!-- just unpack the jar(s) we need instead of a full unpack with getTestDependency --> 
		<unzip src="${downloadsDir}/${orbitFile}" dest="${install}" overwrite="true">
			<patternset><include name="**/org.antlr.runtime_3.0.0*.jar"/></patternset>
		</unzip>
				
		<property name="dir" location="."/>
		<echo message="Unpack SDK + JUnit Tests (${dir}/m2m-*.zip) ..."/>		
		<unzip dest="${install}" overwrite="true">
		    <fileset dir="${dir}"><include name="**/m2m-*.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.">
		<!-- TODO: if your test plugin is jarred, do nothing here
		     TODO: if your test plugin is NOT a jarred plugin, uncomment the next line and comment out the 4 lines thereafter -->
		<!-- <echo message="${eclipse-home}/plugins/${testPlugin} is not a jarred plugin. No unpack required!"/> -->
		<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"/>
		
		<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="m2m" description="Runs the test.xml of the tests plugin">
		<antcall target="runtests">
			<param name="testPlugin" value="${org.eclipse.m2m.atl.tests}" />
			<param name="report" value="org.eclipse.m2m.atl.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="runChkpii" if="chkpiiXMLAvailable">
		<ant target="chkpii" antfile="${chkpii.xml}" inheritall="true">
			<property name="zipPattern" value="m2m-*.zip"/>
		</ant>
	</target>

	<target name="all">
		<antcall target="m2m" />
		<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>
