<?xml version="1.0"?>

<project 
  xmlns:j="jelly:core" 
  xmlns:ant="jelly:ant" 
  xmlns:maven="jelly:maven"
  xmlns:java="java"
  xmlns:test="test"
  xmlns:doc="doc">

  <java:dependency-handle/>
  <j:set var="javaPlugin" value="${pom.getPluginContext('maven-java-plugin')}"/>
  
  <test:dependency-handle/>
  <j:set var="testPlugin" value="${pom.getPluginContext('maven-test-plugin')}"/>
  
  <!--ant:echo>Java Plugin: ${javaPlugin}</ant:echo>
  <ant:echo>Test Plugin: ${testPlugin}</ant:echo-->
  
  <ant:path id="clover.classpath">
    <ant:pathelement path="${plugin.getDependencyPath('clover')}"/>
  </ant:path>

  <!--j:set var="tmp.clover.excludes">
    <j:forEach var="pat" items="${pom.build.unitTest.includes}"> ${pat} </j:forEach>
  </j:set>

  <ant:property name="clover.excludes" value="${tmp.clover.excludes}"/-->


  <goal name="maven-clover-plugin:register">
    <j:if test="${sourcesPresent == 'true'}">
      <doc:registerReport 
        name="Clover" 
        pluginName="maven-clover-plugin" 
        link="clover/index"
        description="Clover test coverage report."/>
    </j:if>
  </goal>
  
  <goal name="maven-clover-plugin:deregister">
    <j:if test="${sourcesPresent == 'true'}">
      <doc:deregisterReport name="Clover"/>
    </j:if>
  </goal>
  
  <goal name="maven-clover-plugin:report">
    <attainGoal name="clover:html-report"/>
  </goal>
  
  
  <!-- =================================================================== -->
  <!-- Prepare for clover                                                  -->
  <!-- =================================================================== -->

  <goal
    name="clover:on"
    description="Create the needed directory structure">
	
	<ant:taskdef resource="clovertasks"/>
    <ant:typedef resource="clovertypes"/>
    
    <preGoal name="java:compile">
      <echo>Setting Clover compiler</echo>
      <ant:clover-setup 
      	initstring="${maven.clover.database.dir}/clover_coverage.db"
      	flushpolicy="interval"
      	flushinterval="500"
      	/>
      <echo>Now using primary build.compiler : ${build.compiler}</echo>
	  <!--${pom.getContext().setVariable('build.compiler',"org.apache.tools.ant.taskdefs.CloverCompilerAdapter")}-->
    </preGoal>

    <postGoal name="java:compile">
      ${pom.getContext().removeVariable('build.compiler')}
    </postGoal>

	<clover-setup initstring="${maven.clover.database.dir}/clover_coverage.db"/>
	
    <j:set 
      var="cloverReportDirectory"
      value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.dest')}/clover"/>

    <maven:addPath id="maven.dependency.classpath" refid="clover.classpath"/>

    <ant:mkdir dir="${maven.build.clover}"/>
    <ant:mkdir dir="${maven.build.clover.classes}"/>
    <ant:mkdir dir="${maven.clover.database.dir}"/>
    <ant:mkdir dir="${cloverReportDirectory}"/>
    
    
    <j:set var="tmp" value="${maven.build.clover.classes}"/>
    ${pom.getPluginContext('maven-java-plugin').setVariable('maven.build.dest',tmp)}
    <!--echo>Found maven-test-plugin: ${pom.getPluginContext('maven-test-plugin')}</echo-->
    ${pom.getPluginContext('maven-test-plugin').setVariable('maven.build.dest',tmp)}
  </goal>

  <!-- =================================================================== -->
  <!-- C L O V E R  R E P O R T S                                          -->
  <!-- =================================================================== -->

  <goal name="clover"
        description="Generate HTML test coverage reports with Clover"
        prereqs="clover:html-report"/>

  <goal name="clover:test" prereqs="clover:on">
  	<!-- Make sure that the report is generated whether the tests pass or not -->
    <j:set var="ignoreTestFailureOld" value="${maven.test.failure.ignore}"/>
    <j:set var="junitForkOld" value="${maven.junit.fork}"/>
    
    <j:set var="maven.test.failure.ignore" scope="parent" value="true"/>
    <j:set var="maven.junit.fork" scope="parent" value="true"/>
    
    <attainGoal name="test:test"/>
    
    <j:set var="maven.test.failure.ignore" scope="parent" value="${ignoreTestFailureOld}"/>
    <j:set var="maven.junit.fork" scope="parent" value="${junitForkOld}"/>
  </goal>
  
  
  <goal 
    name="clover:html-report" 
    description="Generate HTML test coverage reports with Clover">
    <attainGoal name="clover:test"/>
	<ant:clover-report>
      <current 
      	outfile="${cloverReportDirectory}" 
      	title="${pom.name} - ${pom.currentVersion}">
        <format type="html" orderBy="ElementsCoveredAsc"/>
      </current>
    </ant:clover-report>
  </goal>

  <goal
    name="clover:swing-report"
    prereqs="clover:test"
    description="Generate Swing test coverage reports with Clover">

    <ant:mkdir dir="${cloverReportDirectory}"/>

    <ant:java classname="com.cortexeb.tools.clover.reporters.jfc.Viewer"
        fork="yes">
        <ant:arg line="${clover.initstring}"/>
        <ant:classpath>
          <ant:path refid="maven-classpath"/>
          <ant:pathelement path="${plugin.getDependencyPath('clover')}"/>
        </ant:classpath>
    </ant:java>

  </goal>

</project>
