<project name="tdd" default="tests" basedir="." xmlns:sonar="antlib:org.sonar.ant" xmlns:jacoco="antlib:org.jacoco.ant">
	<property name="project.java.version" value="1.8" />
	<property name="encoding" value="UTF-8" />
	<property name="sonar.login" value="202fbf7d57aa6517e5b81dff5a9a15ae213212ac" />
	<!-- LE RESTE DOIT ETRE BON -->
	<property name="build" value="build" />
	<property name="testreports" value="testreports" />
	<property name="src" value="src" />
	<!--property name="etudiant" value="arthur_brandao" /-->

	<!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) -->
	<property name="sonar.host.url" value="https://forge.univ-artois.fr/sonar67" />
	<property name="sonar.projectKey" value="${etudiant}:2019:MyLisp" />
	<property name="sonar.projectName" value="${etudiant} Lisp 19" />
	<property name="sonar.projectVersion" value="1.0" />
	<property name="sonar.java.source" value="${project.java.version}" />
	<property name="sonar.java.binaries" value="build" />
	<property name="sonar.java.libraries" value="lib/*.jar" />
	<property name="sonar.sources" value="src" />
	<property name="sonar.exclusions" value="**/*Test*" />
	<property name="sonar.tests" value="TDD2019TESTS/src,TDD2019OWNTESTS/src" />
	<property name="sonar.junit.reportPaths" value="${testreports}" />
	<property name="sonar.jacoco.reportPaths" value="${testreports}/jacoco.exec" />
    <property name="sonar.pitest.reportsDirectory" value="${testreports}" />
	<property name="sonar.web.javaAdditionalOpts" value="-Dhttps.proxyHost=cache-etu.univ-artois.fr -Dhttps.proxyPort=3128" />
	<target name="prepare" description="Cree les repertoires et initialise certaines valeurs">
		<!-- Create the time stamp -->
		<tstamp />
		<mkdir dir="${build}" />
		<mkdir dir="${testreports}" />
	</target>

	<path id="mypath">
		<pathelement location="${build}" />
		<fileset dir="lib">
			<include name="*.jar" />
		</fileset>
	</path>

	<target name="build" description="Compile les fichiers Java" depends="prepare">
		<javac includeantruntime="true" srcdir="${src}" destdir="${build}" source="${project.java.version}" debug="true" encoding="${encoding}">
			<classpath refid="mypath" />
		</javac>
	</target>

	<target name="clean" description="Efface tous les fichiers generes">
		<!-- Delete directory trees -->
		<delete dir="${build}" />
		<delete dir="${testreports}" />
		<delete dir="${target}" />
	</target>

	<target name="compilepublictests">
		<echo>Compilation des tests publics</echo>
		<javac includeantruntime="true" srcdir="TDD2019TESTS/src" destdir="${build}" source="${project.java.version}" debug="true" encoding="${encoding}">
			<classpath refid="mypath" />
		</javac>
		<copy todir="${build}">
			<fileset dir="TDD2019TESTS/src">
				<include name="**/*.story" />
			</fileset>
		</copy>
	</target>
	<target name="compileprivatetests" if="private.tests.found">
		<echo>Compilation des tests privés</echo>
		<javac includeantruntime="true" srcdir="TDD2019HIDDENTESTS/src" destdir="${build}" source="${project.java.version}" debug="true" encoding="${encoding}">
			<classpath refid="mypath" />
		</javac>

		<copy todir="${build}">
			<fileset dir="TDD2019HIDDENTESTS/src">
				<include name="**/*.story" />
			</fileset>
		</copy>
	</target>
	<target name="compileowntests">
		<echo>Compilation des tests personnels</echo>
		<javac includeantruntime="true" srcdir="TDD2019OWNTESTS/src" destdir="${build}" source="${project.java.version}" debug="true" encoding="${encoding}">
			<classpath refid="mypath" />
		</javac>

		<copy todir="${build}">
			<fileset dir="TDD2019OWNTESTS/src">
				<include name="**/*.story" />
			</fileset>
		</copy>
	</target>

	<target name="tests" description="test de l'application web à l'aide de JUnit" depends="build">
		<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
			<classpath path="lib/jacocoant.jar" />
		</taskdef>
		<antcall target="compilepublictests" />
		<available property="private.tests.found" file="TDD2019HIDDENTESTS/src" type="dir" />
		<antcall target="compileprivatetests" />
		<antcall target="compileowntests" />
		<jacoco:coverage destfile="${sonar.jacoco.reportPaths}">
			<junit fork="yes" forkmode="once" printsummary="false" haltonfailure="no" failureproperty="tests.failure">
				<classpath refid="mypath" />
				<formatter type="plain" usefile="false" />
				<formatter type="xml" />
				<batchtest fork="yes" todir="${testreports}">
					<fileset dir="${build}">
						<include name="**/TDD*.class" />
						<include name="**/*Test*.class" />
						<include name="**/PeterNorvig*.class" />
						<exclude name="**/AllTests.class" />
						<exclude name="**/Abstract*.class" />
					</fileset>
				</batchtest>
			</junit>
		</jacoco:coverage>
	</target>

	<!--target name="m1" description="Verification des projets de TDD2019" depends="clean,build,tests,mutationCoverage" / ligne prof-->
	<target name="m1" description="Verification des projets de TDD2019" depends="clean,build,tests">
		<subant failonerror="false" target="mutationCoverage">
			<fileset dir="." includes="build.xml" />
		</subant>
	</target>

	<!-- Define the SonarQube target -->
	<target name="sonar" depends="m1" description="Analyse le code avec SonarQube">
		<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
			<classpath path="lib/sonarqube-ant-task-2.5.jar" />
		</taskdef>
		<!-- To avoid analyzing the tests -->
		<delete dir="${build}" />
		<mkdir dir="${build}" />
		<antcall target="build" />
		<!-- Execute the SonarQube analysis -->
		<sonar:sonar />
		<fail message="Il reste des tests qui ne passent pas !" if="tests.failure" />
	</target>

	<taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="mypath" />
    <target name="mutationCoverage">
        <pitest
              pitClasspath="mypath"
              classPath="mypath"
              targetClasses="migl.*"
              excludedMethods="*hashCode()"
              excludedClasses="*Test*,*Eval*"
              targetTests="migl.*"
              reportDir="${testreports}"
              outputFormats="XML,HTML"
              sourceDir="${src}"/>
    </target>
</project>
