<project name="grails-unit-test">
                 
  <condition property="_skipTests_">
    <istrue value="${skipTests}"/>
  </condition>

	<target name="test-with-coverage" depends="build-test, instrument-for-coverage, run-test,test-report,coverage-report" description="Compile and Run the unit tests while measuring coverage">
		<fail if="grails.test.failure">The tests failed. Have a look at ${grails.test.reports}</fail>
	</target>
	<target name="test" depends="clean-instrumented, run-test,test-report" unless="_skipTests_" description="Compile and Run the unit tests plainly">
		<fail if="grails.test.failure">The tests failed. Have a look at ${grails.test.reports}</fail>
	</target>

	<taskdef resource="tasks.properties">
		<classpath>
			<pathelement path="${grails.build}" />
			<fileset dir="cruise/coverage/lib" includes="*.jar" />
		</classpath>
		<classpath refid="classpath" />
	</taskdef>
	<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
		<classpath refid="classpath" />
	</taskdef>

	<property name="coverage.build" value="cruise/coverage/build/instrumented-classes" />
	<property name="coverage.report" value="cruise/coverage/report" />

	<target name="clean-instrumented">
		<delete quiet="true" dir="${coverage.build}" />
		<mkdir dir="${coverage.build}" />
	</target>

	<target name="instrument-for-coverage" depends="clean-instrumented">
		<delete file="cobertura.ser" quiet="true" />
		<cobertura-instrument todir="${coverage.build}">
			<fileset dir="${grails.build}" includes="**/*.class" />
		</cobertura-instrument>
	</target>

	<target name="coverage-report">
		<delete quiet="true">
			<fileset dir="${coverage.report}" />
		</delete>
		<mkdir dir="${coverage.report}" />
		<cobertura-report format="xml" destdir="cruise/coverage/report">
			<fileset dir="${grails.src}" />
		</cobertura-report>
	</target>

	<target name="build-test" depends="build,build-scripts">

		<groovyc srcdir="${grails.test}" destdir="${grails.test.build}" encoding="UTF-8">
            <javac debug="on" deprecation="on" encoding="UTF-8"/>
            <classpath refid="classpath" />
			<classpath location="${grails.test.build}" />
			<classpath location="${grails.build}" />
		</groovyc>

        <copy todir="${grails.test.build}">
            <fileset dir="${grails.src.jar}">
                <include name="**/*.handlers" />
                <include name="**/*.schemas" />
            </fileset>
        </copy>


    </target>


    <target name="defineTestingScope">
		<!--Sets the test value to all tests ('*') if no value is provided-->
		<condition property="test" value="*">
			<not>
				<isset property="test" />
			</not>
		</condition>
		<condition property="test.isolated" value="true">
			<equals arg1="${test}" arg2="*" />
		</condition>
	</target>

	<target name="run-test" depends="defineTestingScope, build-test,-jar-app-files" unless="_skipTests_">
		<delete dir="${grails.test.reports}" />
        <mkdir dir="${grails.test.reports}" />
        <echo>Test being run ${test} from ${grails.test.build}</echo>
        <junit fork="true" forkmode="perBatch" clonevm="true" printsummary="on">
			<!-- only fork VMs as needed -->
            <sysproperty key="grails.cli.testing" value="true"/>
            <jvmarg value="-server" />
			<jvmarg value="-Xmx1G" />
			<jvmarg value="-Xms256m" />
			<jvmarg value="-XX:MaxPermSize=256m"/>
			<jvmarg value="-Dgroovy.grails.joint=${groovy.grails.joint}" />			
            <!--<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"/>-->
            <classpath>
                <pathelement location="${coverage.build}" />
                <!-- must be first in cp!    -->
                <pathelement location="cruise/coverage/lib/cobertura.jar" />
                <!-- only needed for coverage -->
                <path refid="classpath" />
                <pathelement location="${grails.test.build}" />
                <pathelement location="${grails.test}" />
                <!-- needed to load codecs -->
                <pathelement location="${grails.src.codecs}" />
                <!-- CLI tests require build.properties on the classpath -->
                <pathelement location="." />
                <pathelement location="${grails.target}" />
            </classpath>

            <formatter type="xml" />

            <!-- This test is run separately because of http://jira.codehaus.org/browse/GROOVY-3299 -->
            <test todir="${grails.test.reports}" name="org.codehaus.groovy.grails.web.mime.ContentFormatControllerTests" fork="true" if="test.isolated" />

            <test todir="${grails.test.reports}" name="org.codehaus.groovy.grails.reload.TransactionalServiceReloadTests" fork="true" if="test.isolated" />

            <batchtest todir="${grails.test.reports}">
                  <fileset dir="${grails.test.build}">
                    <include name="org.codehaus.groovy.grails.cli.PackagePluginTests" />
                  </fileset>
            </batchtest>
            <batchtest todir="${grails.test.reports}">
                  <fileset dir="${grails.test.build}">
                    <include name="org.codehaus.groovy.grails.cli.InstallTemplatesTests" />
                  </fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}">
                  <fileset dir="${grails.test.build}">
                    <include name="org.codehaus.groovy.grails.cli.GenerateAllTests" />
                  </fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}">
                  <fileset dir="${grails.test.build}">
                    <include name="org.codehaus.groovy.grails.cli.**Tests" />
                     <exclude name="**GenerateAllTests" />
                      <exclude name="**PackagePluginTests" />
                      <exclude name="**InstallTemplatesTests" />
                  </fileset>
            </batchtest>


            <batchtest todir="${grails.test.reports}" failureproperty="grails.test.failure" fork="true" >
			    <fileset dir="${grails.test.build}">
                    <include name="**/orm/hibernate/${test}Tests.class" />
                    <include name="**/orm/hibernate/binding/${test}Tests.class" />
                    <include name="**/orm/hibernate/cfg/${test}Tests.class" />
                    <include name="**/orm/hibernate/validation/${test}Tests.class" />
                    <exclude name="**/Abstract*" />
                    <exclude name="**/UniqueConstraint*" />
				</fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}" failureproperty="grails.test.failure" fork="true" >
			    <fileset dir="${grails.test.build}">
                    <include name="**/UniqueConstraintTests.class" />
				</fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}" failureproperty="grails.test.failure" fork="true" >
			    <fileset dir="${grails.test.build}">
                    <include name="**/webflow/**${test}Tests.class" />                    
                    <exclude name="**/Abstract*" />
				</fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}" failureproperty="grails.test.failure" fork="true" >
			    <fileset dir="${grails.test.build}">
                    <include name="**/web/taglib/**${test}Tests.class" />                    
                    <exclude name="**/Abstract*" />
				</fileset>
            </batchtest>

            <batchtest todir="${grails.test.reports}" failureproperty="grails.test.failure" fork="true" >
				<fileset dir="${grails.test.build}">
					<include name="**/${test}Tests.class" />
					<exclude name="**/Abstract*" />
                    <exclude name="**/TransactionalServiceReloadTests*" />
                    <exclude name="**/ContentFormatControllerTests*" />
                    <exclude name="**/orm/hibernate/**" />
                    <exclude name="**/webflow/**" />
                    <exclude name="**/web/taglib/**" />
                    <exclude name="**/cli/**Tests.class" />
				</fileset>
			</batchtest>




		</junit>
	</target>

	<target name="test-report" depends="clean-instrumented, run-test" description="Runs unit tests and generates HTML report">
		<mkdir dir="${grails.test.html.reports}" />
		<junitreport todir="${grails.test.html.reports}">
			<fileset dir="${grails.test.reports}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${grails.test.html.reports}" />
		</junitreport>
		<fail if="grails.test.failure">The tests failed. Have a look at ${grails.test.html.reports}/index.html</fail>
	</target>
</project>
