AbstractCoverageTask.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Brock Janiczak - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.ant;
   14
   15import java.io.File;
   16import java.io.IOException;
   17
   18import org.apache.tools.ant.BuildException;
   19import org.apache.tools.ant.Task;
   20import org.jacoco.agent.AgentJar;
   21import org.jacoco.core.runtime.AgentOptions;
   22
   23/**
   24 * Base class for all coverage tasks that require agent options
   25 * 
   26 * @author Brock Janiczak
   27 * @version $Revision: $
   28 */
   29public class AbstractCoverageTask extends Task {
   30
   31    private final AgentOptions agentOptions;
   32
   33    private boolean enabled;
   34
   35    /**
   36     * Create default agent options
   37     */
   38    protected AbstractCoverageTask() {
   39        agentOptions = new AgentOptions();
   40        enabled = true;
   41    }
   42
   43    /**
   44     * @return Whether or not the current task is enabled
   45     */
   46    public boolean isEnabled() {
   47        return enabled;
   48    }
   49
   50    /**
   51     * Sets whether or not the current task is enabled
   52     * 
   53     * @param enabled
   54     *            Enablement state of the task
   55     */
   56    public void setEnabled(final boolean enabled) {
   57        this.enabled = enabled;
   58    }
   59
   60    /**
   61     * Gets the currently configured agent options for this task
   62     * 
   63     * @return Configured agent options
   64     */
   65    public AgentOptions getAgentOptions() {
   66        return agentOptions;
   67    }
   68
   69    /**
   70     * Sets the location to write coverage execution data
   71     * 
   72     * @ant.not-required Default is current working directory
   73     * @param file
   74     *            Location to write coverage execution data
   75     */
   76    public void setDestfile(final File file) {
   77        agentOptions.setDestfile(file.getAbsolutePath());
   78    }
   79
   80    /**
   81     * Append execution coverage data if a coverage file is already present
   82     * 
   83     * @ant.not-required Default is true
   84     * @param append
   85     *            <code>true</code> to append execution data to an existing file
   86     */
   87    public void setAppend(final boolean append) {
   88        agentOptions.setAppend(append);
   89    }
   90
   91    /**
   92     * List of wildcard patterns classes to include for instrumentation.
   93     * 
   94     * @ant.not-required Default is *
   95     * @param includes
   96     *            Wildcard pattern of included classes
   97     */
   98    public void setIncludes(final String includes) {
   99        agentOptions.setIncludes(includes);
  100    }
  101
  102    /**
  103     * List of wildcard patterns classes to exclude from instrumentation.
  104     * 
  105     * @ant.not-required Default is the empty string, no classes excluded
  106     * @param excludes
  107     *            Wildcard pattern of excluded classes
  108     */
  109    public void setExcludes(final String excludes) {
  110        agentOptions.setExcludes(excludes);
  111    }
  112
  113    /**
  114     * List of wildcard patterns for classloaders that JaCoCo will not
  115     * instrument classes from.
  116     * 
  117     * @ant.not-required Default is sun.reflect.DelegatingClassLoader
  118     * @param exclClassLoader
  119     *            Wildcard pattern of class loaders to exclude
  120     */
  121    public void setExclClassLoader(final String exclClassLoader) {
  122        agentOptions.setExclClassloader(exclClassLoader);
  123    }
  124
  125    /**
  126     * Creates JVM argument to launch with the specified JaCoCo agent jar and
  127     * the current options
  128     * 
  129     * @return JVM Argument to pass to new VM
  130     */
  131    protected String getLaunchingArgument() {
  132        return getAgentOptions().getVMArgument(getAgentFile());
  133    }
  134
  135    private File getAgentFile() {
  136        try {
  137            File agentFile = null;
  138            final String agentFileLocation = getProject().getProperty(
  139                    "_jacoco.agentFile");
  140            if (agentFileLocation != null) {
  141                agentFile = new File(agentFileLocation);
  142            } else {
  143                agentFile = AgentJar.extractToTempLocation();
  144                getProject().setProperty("_jacoco.agentFile",
  145                        agentFile.toString());
  146            }
  147
  148            return agentFile;
  149        } catch (final IOException e) {
  150            throw new BuildException("Unable to extract agent jar", e);
  151        }
  152    }
  153
  154}