Coverage Report - net.sourceforge.cobertura.ant.InstrumentTask
 
Classes in this File Line Coverage Branch Coverage Complexity
InstrumentTask
79%
75/95
62%
20/32
2.357
 
 1  
 /*
 2  
  * The Apache Software License, Version 1.1
 3  
  *
 4  
  * Copyright (C) 2000-2002 The Apache Software Foundation.  All rights
 5  
  * reserved.
 6  
  * Copyright (C) 2003 jcoverage ltd.
 7  
  * Copyright (C) 2005 Mark Doliner
 8  
  * Copyright (C) 2005 Joakim Erdfelt
 9  
  * Copyright (C) 2005 Grzegorz Lukasik
 10  
  * Copyright (C) 2005 Alexei Yudichev
 11  
  * Copyright (C) 2006 John Lewis
 12  
  * Copyright (C) 2006 Jiri Mares 
 13  
  *
 14  
  * Redistribution and use in source and binary forms, with or without
 15  
  * modification, are permitted provided that the following conditions
 16  
  * are met:
 17  
  *
 18  
  * 1. Redistributions of source code must retain the above copyright
 19  
  *    notice, this list of conditions and the following disclaimer.
 20  
  *
 21  
  * 2. Redistributions in binary form must reproduce the above copyright
 22  
  *    notice, this list of conditions and the following disclaimer in
 23  
  *    the documentation and/or other materials provided with the
 24  
  *    distribution.
 25  
  *
 26  
  * 3. The end-user documentation included with the redistribution, if
 27  
  *    any, must include the following acknowlegement:
 28  
  *       "This product includes software developed by the
 29  
  *        Apache Software Foundation (http://www.apache.org/)."
 30  
  *    Alternately, this acknowlegement may appear in the software itself,
 31  
  *    if and wherever such third-party acknowlegements normally appear.
 32  
  *
 33  
  * 4. The names "Ant" and "Apache Software
 34  
  *    Foundation" must not be used to endorse or promote products derived
 35  
  *    from this software without prior written permission. For written
 36  
  *    permission, please contact apache@apache.org.
 37  
  *
 38  
  * 5. Products derived from this software may not be called "Apache"
 39  
  *    nor may "Apache" appear in their names without prior written
 40  
  *    permission of the Apache Group.
 41  
  *
 42  
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 43  
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 44  
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 45  
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 46  
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 47  
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 48  
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 49  
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 50  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 51  
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 52  
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 53  
  * SUCH DAMAGE.
 54  
  * ====================================================================
 55  
  *
 56  
  * This software consists of voluntary contributions made by many
 57  
  * individuals on behalf of the Apache Software Foundation.  For more
 58  
  * information on the Apache Software Foundation, please see
 59  
  * <http://www.apache.org/>.
 60  
  */
 61  
 
 62  
 package net.sourceforge.cobertura.ant;
 63  
 
 64  
 import java.io.File;
 65  
 import java.io.IOException;
 66  
 import java.util.ArrayList;
 67  
 import java.util.HashMap;
 68  
 import java.util.List;
 69  
 
 70  
 import net.sourceforge.cobertura.util.CommandLineBuilder;
 71  
 
 72  
 import org.apache.tools.ant.BuildException;
 73  
 import org.apache.tools.ant.Project;
 74  
 import org.apache.tools.ant.types.FileSet;
 75  
 import org.apache.tools.ant.types.Path;
 76  
 
 77  
 public class InstrumentTask extends CommonMatchingTask
 78  
 {
 79  
 
 80  4
         private String dataFile = null;
 81  
 
 82  4
         private File toDir = null;
 83  
 
 84  4
         List ignoreRegexs = new ArrayList();
 85  
 
 86  4
         List ignoreBranchesRegexs = new ArrayList();
 87  
         
 88  4
         List includeClassesRegexs = new ArrayList();
 89  
 
 90  4
         List excludeClassesRegexs = new ArrayList();
 91  
 
 92  
         private Integer forkedJVMDebugPort;
 93  
         
 94  4
         private Path instrumentationClasspath = null;
 95  
 
 96  4
         private HashMap fileSetMap = new HashMap();
 97  
 
 98  
         public InstrumentTask()
 99  
         {
 100  4
                 super("net.sourceforge.cobertura.instrument.Main");
 101  4
         }
 102  
 
 103  
         public Ignore createIgnore()
 104  
         {
 105  1
                 Ignore ignoreRegex = new Ignore();
 106  1
                 ignoreRegexs.add(ignoreRegex);
 107  1
                 return ignoreRegex;
 108  
         }
 109  
 
 110  
         public IgnoreBranches createIgnoreBranches()
 111  
         {
 112  0
                 IgnoreBranches ignoreBranchesRegex = new IgnoreBranches();
 113  0
                 ignoreBranchesRegexs.add(ignoreBranchesRegex);
 114  0
                 return ignoreBranchesRegex;
 115  
         }
 116  
 
 117  
         public IncludeClasses createIncludeClasses()
 118  
         {
 119  2
                 IncludeClasses includeClassesRegex = new IncludeClasses();
 120  2
                 includeClassesRegexs.add(includeClassesRegex);
 121  2
                 return includeClassesRegex;
 122  
         }
 123  
 
 124  
         public ExcludeClasses createExcludeClasses()
 125  
         {
 126  4
                 ExcludeClasses excludeClassesRegex = new ExcludeClasses();
 127  4
                 excludeClassesRegexs.add(excludeClassesRegex);
 128  4
                 return excludeClassesRegex;
 129  
         }
 130  
 
 131  
         public Path createInstrumentationClasspath()
 132  
         {
 133  1
                 if (instrumentationClasspath == null) {
 134  1
                         instrumentationClasspath = new Path(getProject());
 135  
                 }
 136  1
                 return instrumentationClasspath.createPath();
 137  
         }
 138  
 
 139  
         /*
 140  
          * TODO: Is the following method needed to use a classpath ref?  If so,
 141  
          *       test it and uncomment it.
 142  
          */
 143  
         /*
 144  
         public void setInstrumentationClasspathRef(Reference r)
 145  
         {
 146  
                 createInstrumentationClasspath().setRefid(r);
 147  
         }
 148  
         */
 149  
 
 150  
         public void execute() throws BuildException
 151  
         {
 152  4
                 CommandLineBuilder builder = null;
 153  
                 try {
 154  4
                         builder = new CommandLineBuilder();
 155  4
                         if (dataFile != null)
 156  4
                                 builder.addArg("--datafile", dataFile);
 157  4
                         if (toDir != null)
 158  4
                                 builder.addArg("--destination", toDir.getAbsolutePath());
 159  
 
 160  5
                         for (int i = 0; i < ignoreRegexs.size(); i++) {
 161  1
                                 Ignore ignoreRegex = (Ignore)ignoreRegexs.get(i);
 162  1
                                 builder.addArg("--ignore", ignoreRegex.getRegex());
 163  
                         }
 164  
 
 165  4
                         for (int i = 0; i < ignoreBranchesRegexs.size(); i++) {
 166  0
                                 IgnoreBranches ignoreBranchesRegex = (IgnoreBranches)ignoreBranchesRegexs.get(i);
 167  0
                                 builder.addArg("--ignoreBranches", ignoreBranchesRegex.getRegex());
 168  
                         }
 169  
 
 170  6
                         for (int i = 0; i < includeClassesRegexs.size(); i++) {
 171  2
                                 IncludeClasses includeClassesRegex = (IncludeClasses)includeClassesRegexs.get(i);
 172  2
                                 builder.addArg("--includeClasses", includeClassesRegex.getRegex());
 173  
                         }
 174  
 
 175  8
                         for (int i = 0; i < excludeClassesRegexs.size(); i++) {
 176  4
                                 ExcludeClasses excludeClassesRegex = (ExcludeClasses)excludeClassesRegexs.get(i);
 177  4
                                 builder.addArg("--excludeClasses", excludeClassesRegex.getRegex());
 178  
                         }
 179  
 
 180  4
                         if (instrumentationClasspath != null) {
 181  1
                                 processInstrumentationClasspath();
 182  
                         }
 183  4
                         createArgumentsForFilesets(builder);
 184  
 
 185  4
                         builder.saveArgs();
 186  0
                 } catch (IOException ioe) {
 187  0
                         getProject().log("Error creating commands file.", Project.MSG_ERR);
 188  0
                         throw new BuildException("Unable to create the commands file.", ioe);
 189  4
                 }
 190  
 
 191  
                 // Execute GPL licensed code in separate virtual machine
 192  4
                 getJava().createArg().setValue("--commandsfile");
 193  4
                 getJava().createArg().setValue(builder.getCommandLineFile());
 194  4
                 if (forkedJVMDebugPort != null && forkedJVMDebugPort.intValue() > 0) {
 195  0
                         getJava().createJvmarg().setValue("-Xdebug");
 196  0
                         getJava().createJvmarg().setValue("-Xrunjdwp:transport=dt_socket,address=" + forkedJVMDebugPort + ",server=y,suspend=y");
 197  
                 }
 198  4
                 AntUtil.transferCoberturaDataFileProperty(getJava());
 199  4
                 if (getJava().executeJava() != 0) {
 200  0
                         throw new BuildException(
 201  
                                         "Error instrumenting classes. See messages above.");
 202  
                 }
 203  
 
 204  4
                 builder.dispose();
 205  4
         }
 206  
 
 207  
         private void processInstrumentationClasspath()
 208  
         {
 209  1
                 if (includeClassesRegexs.size() == 0)
 210  
                 {
 211  0
                         throw new BuildException("'includeClasses' is required when 'instrumentationClasspath' is used");
 212  
                 }
 213  
 
 214  1
                 String[] sources = instrumentationClasspath.list();
 215  2
                 for (int i = 0; i < sources.length; i++) {
 216  1
                         File fileOrDir = new File(sources[i]);
 217  1
                         if (fileOrDir.exists())
 218  
                         {
 219  1
                                 if (fileOrDir.isDirectory()) {
 220  1
                                         createFilesetForDirectory(fileOrDir);
 221  1
                                 } else {
 222  0
                                         addFileToFilesets(fileOrDir);
 223  
                                 }
 224  
                         }
 225  
                 }
 226  1
         }
 227  
 
 228  
         private void addFileToFilesets(File file)
 229  
         {
 230  0
                 File dir = file.getParentFile();
 231  0
                 String filename = file.getName();
 232  0
                 FileSet fileSet = getFileSet(dir);
 233  0
                 fileSet.createInclude().setName(filename);
 234  0
         }
 235  
 
 236  
         private FileSet getFileSet(File dir)
 237  
         {
 238  1
                 String key = dir.getAbsolutePath();
 239  1
                 FileSet fileSet = (FileSet)fileSetMap.get(key);
 240  1
                 if (fileSet == null)
 241  
                 {
 242  1
                 fileSet = new FileSet();
 243  1
                 fileSet.setProject(getProject());
 244  1
                 fileSet.setDir(dir);
 245  
 
 246  
                 // Now add the new fileset to the map and to the fileSets list 
 247  1
                 fileSetMap.put(key, fileSet);
 248  1
                 addFileset(fileSet);
 249  
                 }
 250  1
                 return fileSet;
 251  
         }
 252  
 
 253  
         private void createFilesetForDirectory(File dir)
 254  
         {
 255  1
                 FileSet fileSet = getFileSet(dir);
 256  1
                 fileSet.createInclude().setName("**/*.class");
 257  1
         }
 258  
 
 259  
         public void setDataFile(String dataFile)
 260  
         {
 261  4
                 this.dataFile = dataFile;
 262  4
         }
 263  
 
 264  
         public void setToDir(File toDir)
 265  
         {
 266  4
                 this.toDir = toDir;
 267  4
         }
 268  
 
 269  
         public void setForkedJVMDebugPort(Integer forkedJVMDebugPort)
 270  
         {
 271  0
                 this.forkedJVMDebugPort = forkedJVMDebugPort;
 272  0
         }
 273  
 
 274  
 }