<!--
use.xml
Copyright 2004 Christoph Csallner and Yannis Smaragdakis.
2004-11-09 started

====================================================================
Ant file for using Check 'n Crash: generate, compile, run, and archive tests.
This file must be called parameterized from the testee's Ant build file.
Note that properties of the calling project override properties defined here.
====================================================================
-->

<project name="CnC" default="test.archive">  
	<!--basedir overriden by calling target's dir-->
  <description>
		Check 'n Crash--generate, compile, run, and archive tests.
	</description>
	
	<taskdef
			resource="net/sf/antcontrib/antlib.xml" 
			classpath="${cnc.jar}"
	/>  <!--provides loop-->
	
  <path id="test.classpath">
  	<pathelement path="${classpath}"/>
  	<pathelement location="${bin}"/>
    <pathelement location="${cnc.jar}"/>
  </path>
	
	
	<!--
	The following targets implicitly depend on compiling the testee.
	<target name="compile" depends="init" ...>
	The calling Ant file executes the above target before any of the following.
	-->
		
	
	<!--test.init
	defines naming of generated files-->
  <target name="test.init">
		<tstamp>
			<format property="DSTAMP" pattern="yyyy-MM-dd"/>
		</tstamp>
  	<basename property="basedir.basename" file="${basedir}"/>
	  <property name="run.id" value="${basedir.basename}-cnc-${cnc.version}-${DSTAMP}-${TSTAMP}"/>	
	  <property name="cnc.out" value="${run.id}.cnc.txt"/>
	  <property name="junit.out" value="${run.id}.junit.txt"/>
		<property name="test.zip" value="${run.id}.zip"/>  	
	</target>
	
	
	
	<!--test.generate-->
	 <target name="test.generate" depends="test.init"
			description="run CnC to create JUnit test case sources in ${test}" >
		<java classname="edu.gatech.cc.cnc.Main" 
				output="${cnc.out}"
				fork="true"  
	 			dir="${src}"
			>
			<assertions>  <!--TODO we need assertions only for JABA, right?-->
			  <enable/>
			</assertions>
			<classpath refid="test.classpath"/>	
			<!--CnC switches that are not supported by ESC-->
			<arg value="-outdir"/>
			<arg file="${test}"/>
			<arg line="${cnc.params}"/>  <!--for example, "-NPE"-->
			<!--ESC switches-->
			<arg line="${esc.params}"/>  <!--for example, "-ea -source 1.4 -loop 1.5"-->
			<arg value="-simplify"/>
			<arg value="${simplify}"/>
			<arg value="-classpath"/>
			<arg pathref="esc.params.classpath"/>
			<arg value="-sourcepath"/>
			<arg pathref="esc.params.sourcepath"/>
			<arg value="-specs"/>
			<arg pathref="esc.params.specs"/>  <!--${cnc.jar} contains JDK specs-->
			<arg value="-f"/>
			<arg file="${testee.list.name}"/>
		</java>
	</target>
	
	
	
	<!--test.compile
		+ We want source line numbers in any exception trace,
			just like we would get by default when calling javac without arguments.
		+ Running CnC on big project may generate many test cases.
		  Compiling all test cases with a single Javac instance can lead to
		  an out of memory error. So we call Javac in an for loop by AntContrib.
	-->
	<target name="test.compile" depends="test.generate"
			description="compile the generated JUnit test case sources from ${test} to ${bin}" >
		<loadfile property="test.list" srcFile="${testee.list.name}">
			<filterchain> <!--convert line separation between entries to a comma-->
				<tokenfilter>
					<filetokenizer/>
					<trim/>
					<replaceregex pattern="\.java(\s)+(\S)" replace="Test\.java,\2" flags="g"/>
				</tokenfilter>
			</filterchain>
		</loadfile>
		<for param="suite.file" list="${test.list},JUnitAll.java">
		  <sequential>  <!--compile each element of list-->
				<javac srcdir="${test}" destdir="${bin}"
						classpathref="test.classpath"
						debug="true"
						debuglevel="source,lines"
						includes="@{suite.file}"/>
		  </sequential>
		</for>
	</target>
	
	
	
	<!--test.run-->
	<target name="test.run" depends="test.compile"
			description="run the generated JUnit test cases" >
		<java classname="edu.gatech.cc.junit.textui.RaGTestRunner" 
				output="${junit.out}"
				fork="true"
				dir="${bin}"
			>
			<assertions>  <!--assertions enabled for testing-->
			  <enable/>
			</assertions>
			<classpath refid="test.classpath"/>
			<arg value="-noreinit"/>
			<arg value="JUnitAll"/>
		</java>
	</target>
	
	
	
	<!--test.archive-->
	<target name="test.archive" depends="test.run"
			description="archieve this file, the generated test cases, and the results" >
		<!--convert A.java to test/ATest*.java and
		convert line separation between entries to a comma-->
		<loadfile property="tests.list" srcFile="${testee.list.name}">
			<filterchain>				
				<tokenfilter>
					<filetokenizer/>
					<trim/>
					<replaceregex pattern="^" replace="${test}/"/>
					<replaceregex pattern="(\s)+(\S)" replace=",${test}/\2" flags="g"/>
					<replaceregex pattern="\.java" replace="Test\*\.java" flags="g"/>
				</tokenfilter>
			</filterchain>
		</loadfile>
		<zip destfile="${test.zip}">
			<fileset file="build.xml"/>
			<fileset file="${testee.list.name}"/>
			<fileset dir="." includes="JUnitAll.java,${tests.list}/"/>
			<fileset file="${cnc.out}"/>
			<fileset file="${junit.out}"/>
		</zip>
	</target>	
</project>
