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

<project name="cobertura.examples.functionaltest1" basedir="." default="help" xmlns:cobertura="antlib:net.sourceforge.cobertura.ant">

	<description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner
	Copyright (C) 2006 John Lewis
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

	<!-- Import the Cobertura Ant Library -->
	<property name="cobertura.local.library" location="../../antLibrary" />
	<import file="${cobertura.local.library}/library.xml" />

	<property file="build.properties" />
	
	<path id="project.classpath">
		<path path="${java.class.path}" />
		<!-- 
		The next two should only come into play when running this script directly as
		opposed to being called by a functional test.
		-->
		<pathelement location="../../etc" />
		<pathelement location="../../target/classes" />
		
		<pathelement location="../../src/main/java" />
	</path>

	<target name="taskdef">
		<taskdef resource="tasks.properties" classpathref="project.classpath" />
	</target>
	
	<target name="taskdef-antlib">
		<taskdef 
			classpathref="project.classpath"
			resource="net/sourceforge/cobertura/ant/antlib.xml"
			uri="antlib:net.sourceforge.cobertura.ant" />
	</target>

	<target name="help">
		<echo>This example is only used for testing, and is not meant</echo>
		<echo>to be run from the command line.  It requires certain </echo>
		<echo>classes to be on the class path to work correctly.</echo>
	</target>

	<target name="compile">
		<mkdir dir="${classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" classpathref="project.classpath" />
		
		<rmic 
			base="${classes.dir}" 
			classpathref="project.classpath" 
			stubversion="1.2"
			includes="test/first/RemoteListener.class"
			debug="lines,source"
			verify="true"
		/>
	</target>

	<target name="instrument-includes-and-excludes" depends="compile,cobertura-groovy-init">
		<mkdir dir="${instrumented.dir}" />
		<cobertura:groovy><![CDATA[
			//load testUtil definitions
			evaluate(new File("${ant.project.baseDir}/../../testUtil.groovy"))

			ant."${instrumentTaskName}"(datafile:'${basedir}/cobertura.ser', todir:'${instrumented.dir}', classpathref:'project.classpath') {
				fileset(dir:'${classes.dir}') {
					include(name:'**/A.class')
					include(name:'test/first/RemoteListener*.class')
					exclude(name:'**/B.class')
					exclude(name:'**/*Test*')
				}
			}
		]]> </cobertura:groovy>

		<path id="test.classpath">
			<path location="${instrumented.dir}" />
		</path>
	</target>

	<target name="instrument-classpath" depends="compile,cobertura-groovy-init">
		<mkdir dir="${instrumented.dir}" />
		<cobertura:groovy><![CDATA[
			//load testUtil definitions
			evaluate(new File("${ant.project.baseDir}/../../testUtil.groovy"))

			ant."${instrumentTaskName}"(datafile:'${basedir}/cobertura.ser', todir:'${instrumented.dir}') {
				includeClasses(regex:'test.*')
				excludeClasses(regex:'test.*.B')
				excludeClasses(regex:'.*Test*')
				instrumentationClasspath(location:'${classes.dir}')
			}
		]]> </cobertura:groovy>

		<path id="test.classpath">
			<path location="${instrumented.dir}" />
		</path>
	</target>

	<target name="instrument-war" depends="compile,cobertura-groovy-init">
		<property name="tmp" value="tmp" />
		<property name="tmp.dir" value="${basedir}/${tmp}" />
		<property name="this.test.work.dir" location="${tmp.dir}/war" />
		<property name="this.test.wars" location="${tmp.dir}/wars" />
		<property name="this.test.extract" location="${tmp.dir}/extract" />
		<delete dir="${tmp.dir}" />
		<mkdir dir="${tmp.dir}" />

		<!-- Make a jar with a class from each package -->
		<zip destfile="${tmp.dir}/app.jar">
			<fileset dir="${classes.dir}">
				<include name="test/first/A.class" />
				<include name="test/second/B.class" />
				<include name="test/first/RemoteListener*.class" />
			</fileset>
		</zip>
		<!-- TODO: <signjar jar="${this.test.jars}/app.jar" alias="cobertura" storepass="secret" /> -->

		<!-- Make a war file -->
		<!-- This is required by the war task, but it can be empty -->
		<touch file="${tmp.dir}/web.xml" />
		<war destfile="${tmp.dir}/app.war" webxml="${tmp.dir}/web.xml">
			<lib dir="${tmp.dir}">
				<include name="app.jar" />
			</lib>
			<classes dir="${classes.dir}">
				<include name="test/first/B.class" />
				<include name="test/first/Test.class" />
				<include name="test/second/A.class" />
			</classes>
		</war>

		<!-- Instrument the war file -->
		<mkdir dir="${instrumented.dir}" />
		<cobertura:groovy><![CDATA[
			//load testUtil definitions
			evaluate(new File("${ant.project.baseDir}/../../testUtil.groovy"))

			ant."${instrumentTaskName}"(datafile:'${basedir}/cobertura.ser', todir:'${instrumented.dir}') {
				includeClasses(regex:'test.*')
				excludeClasses(regex:'test.*.B')
				excludeClasses(regex:'.*Test*')
				fileset(dir:'${tmp.dir}') {
					include(name:'app.war')
				}
			}
		]]> </cobertura:groovy>

		<!-- Unwar and set the classpath to the unwarred stuff -->
		<unwar src="${instrumented.dir}/app.war" dest="${instrumented.dir}" />

		<delete dir="${tmp.dir}" />

		<path id="test.classpath">
			<path location="${instrumented.dir}/WEB-INF/classes" />
			<fileset dir="${instrumented.dir}/WEB-INF/lib">
				<include name="*.jar" />
			</fileset>
		</path>
	</target>

	<target name="test" depends="compile">
		<junit fork="true" dir="${basedir}" haltonfailure="true">
			<classpath refid="test.classpath" />
			<classpath location="${classes.dir}" />
			<classpath path="${java.class.path}" />
			<classpath refid="project.classpath" />

			<formatter type="plain" usefile="false" />
			<test name="test.first.Test" />
		</junit>
	</target>

	<target name="coverage-reports-only" depends="cobertura-groovy-init">
		<cobertura:groovy><![CDATA[
			//load runReports definition
			evaluate(new File("${ant.project.baseDir}/functionalTest.groovy"))
			
			runReports {
				ant.fileset(dir:'${src.dir}', id:'mysrc') {
					include(name:'**/*.java')
				}
			}
		]]> </cobertura:groovy>
	</target>
	
	<target name="coverage-reports" depends="taskdef,coverage-reports-only" />
	
	<target name="coverage-reports-with-dirset" depends="cobertura-groovy-init">
		<cobertura:groovy><![CDATA[
			//load runReports definition
			evaluate(new File("${ant.project.baseDir}/functionalTest.groovy"))
			
			runReports {
				ant.dirset(dir:'${basedir}', id:'mysrc') {
					include(name:'**/src')
				}
			}
		]]> </cobertura:groovy>
	</target>

	<target name="coverage-reports-with-archive" depends="cobertura-groovy-init">
		<cobertura:groovy><![CDATA[
			//load runReports definition
			evaluate(new File("${ant.project.baseDir}/functionalTest.groovy"))
			
			//make a source zip file in the tmp.dir
			ant.sequential {
				delete(dir:'${tmp.dir}', failonerror:'false')
				mkdir(dir:'${tmp.dir}')
				zip(destfile:'${tmp.dir}/source.zip', basedir:'${src.dir}')
			}
	
			runReports {
				ant.dirset(dir:'${basedir}', id:'mysrc') {
					include(name:'${tmp}')
				}
			}
		]]> </cobertura:groovy>
	</target>

	<target name="coverage-check-only" depends="cobertura-groovy-init">
		<cobertura:groovy><![CDATA[
			evaluate(new File("${ant.project.baseDir}/functionalTest.groovy"))
			
			checkRates()
			
		]]> </cobertura:groovy>
	</target>
	
	<target name="coverage-check" depends="taskdef-antlib,coverage-check-only" />

	<target name="clean">
		<delete dir="${classes.dir}" />
		<delete dir="${instrumented.dir}" />
		<delete dir="${reports.dir}" failonerror="false"/>
		<delete file="cobertura.log" />
		<delete file="cobertura.ser" />
		<delete file="cobertura.ser.lock" />
	</target>

	<target name="test-dirset" depends="taskdef-antlib,clean,compile,instrument-includes-and-excludes,test,coverage-reports-with-dirset,coverage-check-only" />
	<target name="test-includes-and-excludes" depends="taskdef,clean,compile,instrument-includes-and-excludes,test,coverage-reports-only" />
	<target name="test-classpath" depends="taskdef,clean,compile,instrument-classpath,test,coverage-reports-only" />
	<target name="test-war" depends="taskdef,clean,compile,instrument-war,test,coverage-reports-with-archive" />
	<target name="all" depends="taskdef,test-includes-and-excludes,test-classpath" />

</project>
