<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 -->


<project
  xmlns:j="jelly:core"
  xmlns:ant="jelly:ant"
  xmlns:log="jelly:log"
  xmlns:maven="jelly:maven"
  xmlns:define="jelly:define"
  xmlns:jcoverage="jcoverage"
  xmlns:u="jelly:util" 
  xmlns:doc="doc">

  <define:taglib uri="jcoverage">
    <define:jellybean
      name="report"
      className="org.apache.maven.jcoveragereport.CoverageReportGenerator"
      method="execute"
      />
  </define:taglib>

  <!-- global definitions -->
  <ant:path id="jcoverage.classpath">
    <ant:pathelement path="${plugin.getDependencyPath('bcel')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('urbanophile:java-getopt')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('log4j')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('jcoverage')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('oro')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('junit')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('xerces:xercesImpl')}"/>
    <ant:pathelement path="${plugin.getDependencyPath('xerces:xmlParserAPIs')}"/>
  </ant:path>

  <ant:taskdef classpath="${plugin.getDependencyPath('jcoverage')}" resource="tasks.properties" />

  <j:set var="maven.coverage.dir"
         value="${pom.getPluginContext('maven-xdoc-plugin').getVariable('maven.docs.dest')}/jcoverage" />

  <!-- ================================================================== -->
  <!-- J C O V E R A G E  R E P O R T  D E -/R E G I S T R A T I O N      -->
  <!-- ================================================================== -->
  <goal name="maven-jcoverage-plugin:register"
        description="Register the maven-jcoverage-plugin.">

    <j:if test="${sourcesPresent == 'true'}">
      <doc:registerReport
        name="JCoverage"
        pluginName="maven-jcoverage-plugin"
        link="jcoverage/index"
        description="JCoverage test coverage report." />
    </j:if>

  </goal>

  <goal name="maven-jcoverage-plugin:deregister"
        description="Deregister the jcoverage plugin">

    <j:if test="${sourcesPresent == 'true'}">
      <doc:deregisterReport name="JCoverage" />
    </j:if>

  </goal>


  <!-- ================================================================== -->
  <!-- D E F A U L T  R E P O R T                                         -->
  <!-- ================================================================== -->
  <goal name="maven-jcoverage-plugin:report"
        description="Run the default report (html).">

    <attainGoal name="jcoverage:html-report" />

  </goal>


  <!-- ================================================================== -->
  <!-- I N S T R U M E N T A T I O N                                      -->
  <!-- ================================================================== -->
  <goal name="jcoverage:on" description="perform the coverage analysis"
        prereqs="java:compile">

    <j:catch var="ex">

      <j:set var="instrumented" value="${maven.jcoverage.instrumentation}"/>
      <j:set var="oldBuildDest" value="${maven.build.dest}"/>
      <j:set var="fork"         value="${maven.jcoverage.junit.fork}"/>
      <j:set var="oldfork"         value="${pom.getPluginContext('maven-test-plugin').getVariable('maven.junit.fork')}"/>

      <!-- reset the maven.build.dest for the test plugin to find the instrumented classes -->
      ${pom.getPluginContext('maven-test-plugin').setVariable('maven.build.dest', instrumented)}

      <!--
        set the maven.junit.fork property
      -->
      ${pom.getPluginContext('maven-test-plugin').setVariable('maven.junit.fork', fork)}

      <ant:mkdir dir="${maven.coverage.dir}" />
      <ant:mkdir dir="${maven.jcoverage.database.dir}" />
      <ant:mkdir dir="${maven.jcoverage.instrumentation}" />

      <log:info>instrumenting the class-files...</log:info>

      <maven:addPath id="maven.dependency.classpath" refid="jcoverage.classpath"/>
      <maven:addPath id="maven.dependency.classpath" refid="${pom.getDependencyClasspath()}"/>

      <instrument todir="${maven.jcoverage.instrumentation}">
        <ant:fileset dir="${maven.build.dest}">
          <ant:include name="**/*.class" />
          <ant:exclude name="${maven.jcoverage.instrumentation.excludes}" />
        </ant:fileset>
        <ant:classpath>
          <ant:path refid="jcoverage.classpath"/>
        </ant:classpath>
      </instrument>

    </j:catch>
    <j:if test="${ex != null}">
      <log:error>${ex}</log:error>
      <ant:fail message="${ex}" />
    </j:if>

    <j:if test="${!pom.build.resources.isEmpty()}">
      <maven:copy-resources
        resources="${pom.build.resources}"
        todir="${maven.jcoverage.instrumentation}"/>
    </j:if>
  </goal>

  <!-- ================================================================== -->
  <!-- D E F A U L T  G O A L                                             -->
  <!-- ================================================================== -->
  <goal name="jcoverage"
        description="Generate HTML test coverage reports with JCoverage"
        prereqs="jcoverage:html-report"
  />

  <!-- ================================================================== -->
  <!-- R E P O R T   G O A L                                              -->
  <!-- ================================================================== -->
  <goal name="jcoverage:html-report"
        description="Generate HTML test coverage reports with JCoverage"
        prereqs="jcoverage:on,test:test">

    <j:catch var="ex">

      <j:set var="template" value="${maven.jcoverage.report.template}"/>
      <j:choose>
        <j:when test="${template == 'jcoverage'}">
          <report srcdir="${pom.build.sourceDirectory}" destdir="${maven.coverage.dir}">
            <ant:classpath>
              <ant:path refid="jcoverage.classpath"/>
            </ant:classpath>
          </report>
    
          <log:info>jcoverage reports have been generated.</log:info>
          <log:info>The HTML report is ${maven.build.coverage.dir}/index.html</log:info>
        </j:when>
        <j:otherwise>
          <report srcdir="${pom.build.sourceDirectory}" destdir="${maven.jcoverage.dir}"
                  format="xml">
            <ant:classpath>
              <ant:path refid="jcoverage.classpath"/>
            </ant:classpath>
          </report>
          <j:set var="stylesheet" value="${maven.jcoverage.report.stylesheet}"/> 
          <ant:copy file="${stylesheet}" tofile="${maven.coverage.dir}/style.css"/> 
          <jcoverage:report
              dataFile="${maven.jcoverage.dir}/coverage.xml"
              outputDir="${maven.coverage.dir}"/>
        </j:otherwise>
      </j:choose>

      <!--
        restore the maven.junit.fork property
      -->
      ${pom.getPluginContext('maven-test-plugin').setVariable('maven.junit.fork', oldfork)}
      <!--
        restore the maven.build.dest property
      -->
      ${pom.getPluginContext('maven-test-plugin').setVariable('maven.build.dest', oldBuildDest)}
    </j:catch>
    <j:if test="${ex != null}">
      <log:error>${ex}</log:error>
      <ant:fail message="${ex}" />
    </j:if>

  </goal>
  
  <!-- ================================================================== -->
  <!-- M E R G E   G O A L                                                -->
  <!-- ================================================================== -->
  <goal name="jcoverage:merge"
        description="Merge two or more instrumentation files into one">
  
    <java classname="com.jcoverage.tool.merge.Main">
      <u:tokenize var="listOfinput" delim=",">${maven.jcoverage.merge.instrumentedFiles}</u:tokenize>
      <j:forEach var="instrumentedFile" items="${listOfinput}">
        <arg line="-i ${instrumentedFile}"/>
      </j:forEach>
      <arg line="-o ${maven.jcoverage.merge.outputDir}"/>
      <classpath>
        <path refid="jcoverage.classpath"/>
      </classpath>
    </java>
  </goal>
  
  <postGoal name="clean:clean">
    <ant:delete file="${basedir}/jcoverage.ser" quiet="true" failonerror="false"/>
  </postGoal>

</project>
