Coverage Report - net.sourceforge.cobertura.ant.CommonMatchingTask
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonMatchingTask
88%
45/51
81%
13/16
1.467
 
 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) 2006 Srivathsan Varadarajan
 11  
  * Copyright (C) 2006 Matt Cordes
 12  
  *
 13  
  * Redistribution and use in source and binary forms, with or without
 14  
  * modification, are permitted provided that the following conditions
 15  
  * are met:
 16  
  *
 17  
  * 1. Redistributions of source code must retain the above copyright
 18  
  *    notice, this list of conditions and the following disclaimer.
 19  
  *
 20  
  * 2. Redistributions in binary form must reproduce the above copyright
 21  
  *    notice, this list of conditions and the following disclaimer in
 22  
  *    the documentation and/or other materials provided with the
 23  
  *    distribution.
 24  
  *
 25  
  * 3. The end-user documentation included with the redistribution, if
 26  
  *    any, must include the following acknowlegement:
 27  
  *       "This product includes software developed by the
 28  
  *        Apache Software Foundation (http://www.apache.org/)."
 29  
  *    Alternately, this acknowlegement may appear in the software itself,
 30  
  *    if and wherever such third-party acknowlegements normally appear.
 31  
  *
 32  
  * 4. The names "Ant" and "Apache Software
 33  
  *    Foundation" must not be used to endorse or promote products derived
 34  
  *    from this software without prior written permission. For written
 35  
  *    permission, please contact apache@apache.org.
 36  
  *
 37  
  * 5. Products derived from this software may not be called "Apache"
 38  
  *    nor may "Apache" appear in their names without prior written
 39  
  *    permission of the Apache Group.
 40  
  *
 41  
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 42  
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 43  
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 44  
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 45  
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 46  
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 47  
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 48  
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 49  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 50  
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 51  
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 52  
  * SUCH DAMAGE.
 53  
  * ====================================================================
 54  
  *
 55  
  * This software consists of voluntary contributions made by many
 56  
  * individuals on behalf of the Apache Software Foundation.  For more
 57  
  * information on the Apache Software Foundation, please see
 58  
  * <http://www.apache.org/>.
 59  
  */
 60  
 
 61  
 package net.sourceforge.cobertura.ant;
 62  
 
 63  
 import java.io.File;
 64  
 import java.io.IOException;
 65  
 import java.net.URL;
 66  
 import java.net.URLClassLoader;
 67  
 import java.util.Iterator;
 68  
 import java.util.LinkedList;
 69  
 import java.util.List;
 70  
 
 71  
 import net.sourceforge.cobertura.util.CommandLineBuilder;
 72  
 import net.sourceforge.cobertura.util.StringUtil;
 73  
 
 74  
 import org.apache.tools.ant.AntClassLoader;
 75  
 import org.apache.tools.ant.DirectoryScanner;
 76  
 import org.apache.tools.ant.Project;
 77  
 import org.apache.tools.ant.taskdefs.Java;
 78  
 import org.apache.tools.ant.taskdefs.MatchingTask;
 79  
 import org.apache.tools.ant.types.FileSet;
 80  
 import org.apache.tools.ant.types.Path;
 81  
 import org.apache.tools.ant.types.Reference;
 82  
 
 83  
 public abstract class CommonMatchingTask extends MatchingTask
 84  
 {
 85  
 
 86  
         final String className;
 87  12
         final List fileSets = new LinkedList();
 88  
 
 89  12
         private Java java = null;
 90  12
         private String maxMemory = null;
 91  
 
 92  
         public CommonMatchingTask(String className)
 93  12
         {
 94  12
                 this.className = className;
 95  12
         }
 96  
 
 97  
         private String getClassName()
 98  
         {
 99  12
                 return className;
 100  
         }
 101  
 
 102  
         protected Java getJava()
 103  
         {
 104  301
                 if (java == null)
 105  
                 {
 106  12
                         java = (Java)getProject().createTask("java");
 107  12
                         java.setTaskName(getTaskName());
 108  12
                         java.setClassname(getClassName());
 109  12
                         java.setFork(true);
 110  12
                         java.setDir(getProject().getBaseDir());
 111  12
                         if (maxMemory != null)
 112  4
                                 java.setJvmargs("-Xmx" + maxMemory);
 113  
 
 114  
                         /**
 115  
                          * We replace %20 with a space character because, for some
 116  
                          * reason, when we call Cobertura from within CruiseControl,
 117  
                          * the classpath here contains %20's instead of spaces.  I
 118  
                          * don't know if this is our problem, or CruiseControl, or
 119  
                          * ant, but this seems to fix it.  --Mark
 120  
                          */
 121  12
                         if (getClass().getClassLoader() instanceof AntClassLoader)
 122  
                         {
 123  0
                                 String classpath = ((AntClassLoader)getClass()
 124  
                                                 .getClassLoader()).getClasspath();
 125  0
                                 createClasspath().setPath(
 126  
                                                 StringUtil.replaceAll(classpath, "%20", " "));
 127  0
                         }
 128  12
                         else if (getClass().getClassLoader() instanceof URLClassLoader)
 129  
                         {
 130  12
                                 URL[] earls = ((URLClassLoader)getClass().getClassLoader())
 131  
                                                 .getURLs();
 132  264
                                 for (int i = 0; i < earls.length; i++)
 133  
                                 {
 134  252
                                         String classpath = (new File(earls[i].getFile())).getAbsolutePath();
 135  252
                                         createClasspath().setPath(
 136  
                                                         StringUtil.replaceAll(classpath, "%20", " "));
 137  
                                 }
 138  
                         }
 139  
                 }
 140  
 
 141  301
                 return java;
 142  
         }
 143  
 
 144  
         protected void createArgumentsForFilesets( CommandLineBuilder builder) throws IOException {
 145  12
                 Iterator iter = fileSets.iterator();
 146  20
                 while (iter.hasNext())
 147  
                 {
 148  8
                         FileSet fileSet = (FileSet)iter.next();
 149  
 
 150  8
                         builder.addArg("--basedir", baseDir(fileSet));
 151  8
                         createArgumentsForFilenames( builder, getFilenames(fileSet));
 152  8
                 }
 153  12
         }
 154  
 
 155  
         private void createArgumentsForFilenames( CommandLineBuilder builder, String[] filenames) throws IOException
 156  
         {
 157  45
                 for (int i = 0; i < filenames.length; i++)
 158  
                 {
 159  37
                         getProject().log("Adding " + filenames[i] + " to list",
 160  
                                         Project.MSG_VERBOSE);
 161  37
                         builder.addArg(filenames[i]);
 162  
                 }
 163  8
         }
 164  
 
 165  
         public Path createClasspath()
 166  
         {
 167  253
                 return getJava().createClasspath().createPath();
 168  
         }
 169  
 
 170  
         public void setClasspath(Path classpath)
 171  
         {
 172  0
                 createClasspath().append(classpath);
 173  0
         }
 174  
 
 175  
         public void setClasspathRef(Reference r)
 176  
         {
 177  1
                 createClasspath().setRefid(r);
 178  1
         }
 179  
 
 180  
         DirectoryScanner getDirectoryScanner(FileSet fileSet)
 181  
         {
 182  8
                 return fileSet.getDirectoryScanner(getProject());
 183  
         }
 184  
 
 185  
         String[] getIncludedFiles(FileSet fileSet)
 186  
         {
 187  8
                 return getDirectoryScanner(fileSet).getIncludedFiles();
 188  
         }
 189  
 
 190  
         String[] getExcludedFiles(FileSet fileSet)
 191  
         {
 192  0
                 return getDirectoryScanner(fileSet).getExcludedFiles();
 193  
         }
 194  
 
 195  
         String[] getFilenames(FileSet fileSet)
 196  
         {
 197  8
                 String[] filesToReturn = getIncludedFiles(fileSet);
 198  
 
 199  8
                 return filesToReturn;
 200  
         }
 201  
 
 202  
         String baseDir(FileSet fileSet)
 203  
         {
 204  8
                 return fileSet.getDirectoryScanner(getProject()).getBasedir()
 205  
                                 .toString();
 206  
         }
 207  
 
 208  
         public void addFileset(FileSet fileSet)
 209  
         {
 210  8
                 fileSets.add(fileSet);
 211  8
         }
 212  
 
 213  
         /**
 214  
          * @param maxMemory Assumed to be something along the lines of
 215  
          *        100M or 50K or 1G.
 216  
          */
 217  
         public void setMaxMemory(String maxMemory)
 218  
         {
 219  4
                 this.maxMemory = maxMemory != null ? maxMemory.trim() : null;
 220  4
         }
 221  
 }