<project default="all" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
	<!-- 
	Run code coverage for unit tests on the luaj vm and libraries.
	-->

	<property name="classes.dir" value="build/classes-debug" />
	<property name="instrumented.dir" value="build/instrumented" />
	<property name="reports.xml.dir" value="build/reports-junit-xml" />
	<property name="reports.html.dir" value="build/reports-junit-html" />
	<property name="coverage.xml.dir" value="build/reports-coverage-xml" />
	<property name="coverage.html.dir" value="build/reports-coverage-html" />
	<property name="cobertura.serfile" value="cobertura.ser" />
	<property name="cobertura.logfile" value="cobertura.log" />

	<artifact:dependencies filesetId="cobutura.fileset">
		<dependency groupId="net.sourceforge.cobertura" artifactId="cobertura" version="1.9.4.1"/>
		<dependency groupId="junit" artifactId="junit" version="3.8.1"/>
	</artifact:dependencies>
	
	<path id="cobertura.classpath">
		<fileset refid="cobutura.fileset" />
	</path>
	
	<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
	
	<import file="wtk.xml"/>
	
	<property environment="env"/>

	<target name="clean" description="Remove all files created by the build/test process.">
		<delete dir="${classes.dir}" failonerror="yes"/>
		<delete dir="${instrumented.dir}" failonerror="yes"/>
		<delete file="${cobertura.logfile}" />
		<delete file="${cobertura.serfile}" />
	</target>

	<target name="init">
		<ant antfile="build.xml" target="bcel-lib"/>
		<ant antfile="build.xml" target="luaj1-lib"/>
		<mkdir dir="${classes.dir}" />
		<mkdir dir="${instrumented.dir}" />
		<mkdir dir="${reports.xml.dir}" />
		<mkdir dir="${reports.html.dir}" />
		<mkdir dir="${coverage.xml.dir}" />
		<mkdir dir="${coverage.html.dir}" />
	</target>

	<target name="compile" depends="init,wtk-or-fail">
		<javac destdir="${classes.dir}" debug="yes" target="1.5">
			<classpath refid="cobertura.classpath" />
		    <classpath refid="wtk-libs" />
		    <classpath path="lib/bcel-5.2.jar" />
		    <src path="src/core"/>
		    <src path="src/jme"/>
		    <src path="src/jse"/>
		    <src path="test/junit"/>
		</javac>
	</target>

	<target name="instrument" depends="compile">
		<delete file="${cobertura.serfile}"/>
		<delete dir="${instrumented.dir}" failonerror="no"/>
		<cobertura-instrument datafile="${cobertura.serfile}" todir="${instrumented.dir}">
			<fileset dir="${classes.dir}">
				<include name="org/luaj/vm2/*.class" />
				<include name="org/luaj/vm2/lib/*.class" />
				<include name="org/luaj/vm2/lib/jse/*.class" />
				<include name="org/luaj/vm2/lib/jme/*.class" />
				<include name="org/luaj/vm2/compiler/*.class" />
				<include name="org/luaj/vm2/luajc/*.class" />
				<include name="org/luaj/vm2/lua2java/*.class" />
				<include name="org/luaj/vm2/parser/*.class" />
				<include name="org/luaj/vm2/ast/*.class" />
				<exclude name="**/*Test*.class" />
			</fileset>
		</cobertura-instrument>
	</target>

	<target name="test">
		<junit fork="yes" dir="${basedir}" showoutput="yes">
     		<sysproperty key="net.sourceforge.cobertura.serfile" 
     			file="${basedir}/${cobertura.serfile}" />		    
			<classpath location="${instrumented.dir}" />
			<classpath location="${classes.dir}" />
			<classpath refid="cobertura.classpath" />
			<classpath location="test/lua" />
			<classpath location="test/junit/org/luaj/vm2/compiler" />
			<classpath location="test/junit/org/luaj/vm2/vm1" />
		    <classpath path="lib/bcel-5.2.jar" />
			<formatter type="xml" />
			<batchtest todir="${reports.xml.dir}">
				<fileset dir="test/junit">
					<include name="org/luaj/vm2/AllTests.java" />
				</fileset>
			</batchtest>
		</junit>
		
		<junitreport todir="${reports.xml.dir}">
			<fileset dir="${reports.xml.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${reports.html.dir}" />
		</junitreport>
	</target>

	<target name="report">
		<cobertura-report datafile="${cobertura.serfile}" destdir="${coverage.xml.dir}" format="xml" /> 
		<cobertura-report datafile="${cobertura.serfile}" destdir="${coverage.html.dir}">
			<fileset dir="src/core"/>
			<fileset dir="src/jse"/>
			<fileset dir="src/jme"/>
		</cobertura-report>
	</target>

	<target name="coverage" depends="clean,init,compile,instrument,test,report"/>

	<target name="all" depends="coverage" />
	
</project>
