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 *******************************************************************************/
   12package org.jacoco.ant;
   13
   14import java.io.File;
   15import java.io.IOException;
   16
   17import org.apache.tools.ant.BuildException;
   18import org.apache.tools.ant.Task;
   19import org.jacoco.agent.AgentJar;
   20import org.jacoco.core.runtime.AgentOptions;
   21
   22/**
   23 * Base class for all coverage tasks that require agent options
   24 * 
   25 * @author Brock Janiczak
   26 * @version 0.4.1.20101007204400
   27 */
   28public class AbstractCoverageTask extends Task {
   29
   30    private final AgentOptions agentOptions;
   31
   32    private boolean enabled;
   33
   34    /**
   35     * Create default agent options
   36     */
   37    protected AbstractCoverageTask() {
   38        agentOptions = new AgentOptions();
   39        enabled = true;
   40    }
   41
   42    /**
   43     * @return Whether or not the current task is enabled
   44     */
   45    public boolean isEnabled() {
   46        return enabled;
   47    }
   48
   49    /**
   50     * Sets whether or not the current task is enabled
   51     * 
   52     * @param enabled
   53     *            Enablement state of the task
   54     */
   55    public void setEnabled(final boolean enabled) {
   56        this.enabled = enabled;
   57    }
   58
   59    /**
   60     * Gets the currently configured agent options for this task
   61     * 
   62     * @return Configured agent options
   63     */
   64    public AgentOptions getAgentOptions() {
   65        return agentOptions;
   66    }
   67
   68    /**
   69     * Sets the location to write coverage execution data. Default is current
   70     * working directory
   71     * 
   72     * @param file
   73     *            Location to write coverage execution data
   74     */
   75    public void setDestfile(final File file) {
   76        agentOptions.setDestfile(file.getAbsolutePath());
   77    }
   78
   79    /**
   80     * Append execution coverage data if a coverage file is already present.
   81     * Default is <code>true</code>
   82     * 
   83     * @param append
   84     *            <code>true</code> to append execution data to an existing file
   85     */
   86    public void setAppend(final boolean append) {
   87        agentOptions.setAppend(append);
   88    }
   89
   90    /**
   91     * List of wildcard patterns classes to include for instrumentation. Default
   92     * is <code>*</code>
   93     * 
   94     * @param includes
   95     *            Wildcard pattern of included classes
   96     */
   97    public void setIncludes(final String includes) {
   98        agentOptions.setIncludes(includes);
   99    }
  100
  101    /**
  102     * List of wildcard patterns classes to exclude from instrumentation.
  103     * Default is the empty string, no classes excluded
  104     * 
  105     * @param excludes
  106     *            Wildcard pattern of excluded classes
  107     */
  108    public void setExcludes(final String excludes) {
  109        agentOptions.setExcludes(excludes);
  110    }
  111
  112    /**
  113     * List of wildcard patterns for classloaders that JaCoCo will not
  114     * instrument classes from. Default is
  115     * <code>sun.reflect.DelegatingClassLoader</code>
  116     * 
  117     * @param exclClassLoader
  118     *            Wildcard pattern of class loaders to exclude
  119     */
  120    public void setExclClassLoader(final String exclClassLoader) {
  121        agentOptions.setExclClassloader(exclClassLoader);
  122    }
  123
  124    /**
  125     * Sets the session identifier. Default is a auto-generated id
  126     * 
  127     * @param id
  128     *            session identifier
  129     */
  130    public void setSessionId(final String id) {
  131        agentOptions.setSessionId(id);
  132    }
  133
  134    /**
  135     * Dump coverage data on VM termination. Default is <code>true</code>
  136     * 
  137     * @param dumpOnExit
  138     *            <code>true</code> to write coverage data on VM termination
  139     */
  140    public void setDumpOnExit(final boolean dumpOnExit) {
  141        agentOptions.setDumpOnExit(dumpOnExit);
  142    }
  143
  144    /**
  145     * Sets the output method. Default is <code>file</code>
  146     * 
  147     * @param output
  148     *            Output method
  149     */
  150    public void setOutput(final String output) {
  151        agentOptions.setOutput(output);
  152    }
  153
  154    /**
  155     * Sets the IP address or hostname to bind to when output method is tcp
  156     * server or connect to when the output method is tcp client. Default is
  157     * <code>localhost</code>
  158     * 
  159     * @param address
  160     *            Address to bind or connect to
  161     */
  162    public void setAddress(final String address) {
  163        agentOptions.setAddress(address);
  164    }
  165
  166    /**
  167     * Sets the Port to bind to when the output method is tcp server or connect
  168     * to when the output method is tcp client. Default is <code>6300</code>
  169     * 
  170     * @param port
  171     */
  172    public void setPort(final int port) {
  173        agentOptions.setPort(port);
  174    }
  175
  176    /**
  177     * Creates JVM argument to launch with the specified JaCoCo agent jar and
  178     * the current options
  179     * 
  180     * @return JVM Argument to pass to new VM
  181     */
  182    protected String getLaunchingArgument() {
  183        return getAgentOptions().getVMArgument(getAgentFile());
  184    }
  185
  186    private File getAgentFile() {
  187        try {
  188            File agentFile = null;
  189            final String agentFileLocation = getProject().getProperty(
  190                    "_jacoco.agentFile");
  191            if (agentFileLocation != null) {
  192                agentFile = new File(agentFileLocation);
  193            } else {
  194                agentFile = AgentJar.extractToTempLocation();
  195                getProject().setProperty("_jacoco.agentFile",
  196                        agentFile.toString());
  197            }
  198
  199            return agentFile;
  200        } catch (final IOException e) {
  201            throw new BuildException("Unable to extract agent jar", e);
  202        }
  203    }
  204
  205}