<project xmlns:au="antlib:org.apache.ant.antunit">
	<import file="macros.ant" />

	<property name="binjarfile" value="${antdistdir}/${test.jarfile}" />
	<property name="srcjarfile" value="${antdistdir}/${test.srcjarfile}" />
	
	<!-- MACROS -->
	
	<macrodef name="sizeOfDir">
		<attribute name="property.name" />
		<sequential>
			<length property="@{property.name}">
				<fileset dir="." includes="**/*" />
			</length>
		</sequential>
	</macrodef>

	<macrodef name="dirSizeChanges">
		<attribute name="property.name" />
		<element implicit="yes" name="operation" />

		<sequential>
			<sizeOfDir property.name="oldlength" />
			<operation />
			<sizeOfDir property.name="newlength" />
			<condition property="@{property.name}">
				<not>
					<equals arg1="${oldlength}" arg2="${newlength}" />
				</not>
			</condition>
		</sequential>
	</macrodef>

	<macrodef name="unzip.exttestjavadoczip">
		<sequential>
			<unzip dest="${tempdir}" src="${exttestjavadoczip}" />
		</sequential>
	</macrodef>
	
	<!-- TEST FIXTURE TESTS -->
	
	<target name="testTearDown">
		<mkdir dir="${antdistdir}" />
		<mkdir dir="${tempdir}" />
		<touch file="${exttestjavadoczip}" />

		<antcall target="tearDown" />

		<au:assertFileDoesntExist file="${antdistdir}" />
		<au:assertFileDoesntExist file="${tempdir}" />
		<au:assertFileDoesntExist file="${exttestjavadoczip}" />
	</target>

	<target name="testCharacterize_DirSizeChanges">
		<dirSizeChanges property.name="changed">
			<mkdir dir="${tempdir}" />
			<echo message="h" file="${tempdir}/whatever.txt" />
		</dirSizeChanges>
		<au:assertPropertySet name="changed" />
	</target>
	
	<target name="testUnzipOnlyCreatesDirectoryCleanedByTeardown">
		<dirSizeChanges property.name="changed">
			<junitbuild target="javadoczip" />
			<unzip.exttestjavadoczip />
			<antcall target="tearDown" />
		</dirSizeChanges>
		<au:assertFalse>
			<isset property="changed" />
		</au:assertFalse>
	</target>

	<!-- CHARACTERIZATION TESTS DURING REFACTORING -->
	
	<target name="testCharacterize_JavadocGeneratesJavadoc">
		<junitbuild target="javadoc" />
		<au:assertFileExists file="${antdistdir}/ant-unit-java-docs" />
	</target>

	<target name="testCharacterize_PopulateGeneratesJUnitJar">
		<junitbuild target="populate-dist" />
		<au:assertFileExists file="${binjarfile}" />
	</target>

	<target name="testCharacterize_PopulateCopiesSamplesAndTests">
		<junitbuild target="samples-and-tests" />
		<au:assertFileExists file="${antdistdir}/junit/samples" />
		<au:assertFileExists file="${antdistdir}/junit/tests" />
		<au:assertFileExists file="${antdistdir}/org/junit/samples" />
		<au:assertFileExists file="${antdistdir}/org/junit/tests" />
	</target>

	<!-- TODO: shotgun to pass in hamcrestsrc -->
	<target name="testCharacterize_UnjarHamcrestGeneratesSource">
		<junitbuild target="unjar.hamcrest" />
		
		<!-- TODO: confused about where this directory is -->
		<au:assertFileExists file="${junitdir}/${test.hamcrestsrc}/org/hamcrest" />
	</target>
	
	<!-- DRIVING TESTS -->

	<target name="testJavaDocZipCreatesOrgDir">
		<junitbuild target="javadoczip" />
		<au:assertFileExists file="${exttestjavadoczip}" />
		<unzip.exttestjavadoczip />
		<au:assertFileExists file="${tempdir}/org" />
	</target>
	
	<!-- TODO: what hamcrest packages do I need? -->

	<!-- TODO DUP above -->
	<target name="testJavaDocZipCreatesHamcrestDir">
		<junitbuild target="javadoczip" javadocpackages="org.junit, org.hamcrest.core" />
		<au:assertFileExists file="${exttestjavadoczip}" />
		<unzip.exttestjavadoczip />
		<au:assertFileExists file="${tempdir}/org/hamcrest" />
	</target>

	<target name="testJavaDocHasNoErrors">
		<junitbuild target="javadoc" />
		<au:assertLogDoesntContain text="does not exist" />
	</target>
	
	<target name="testJavaDocZipDeletedOnClean">
		<touch file="${exttestjavadoczip}" />
		<junitbuild target="clean" />
		<au:assertFileDoesntExist file="${exttestjavadoczip}" />
	</target>

	<!-- [Junit-trackers] [ junit-Bugs-1686931 ] JUnit 4.3 contains samples and tests in junit-4.3[-src].jar -->
	<target name="testJarsDoesntIncludeSamplesInJUnitJar">
		<junitbuild target="jars" />
		<au:assertFalse>
			<available classname="org.junit.tests.AllTests"
			           classpath="${binjarfile}"
			/>
		</au:assertFalse>
	</target>
</project>