From: Seigel, J. <Jam...@av...> - 2005-06-07 03:56:55
|
So here is a patch against the latest cvs repository. I hadn't updated = my local repository in a while so it took longer than expected. I will = post it on the sf site as well. Give 'er a rip. The test files are = posted on the sf.net site already so I won't repost them. If this gets = applied, I will try and clean up the code some more and add more test = cases. Here is the format to use in your ant file... <cobertura-report destdir=3D"${coverage.html.dir}"> <src path=3D"${src.dir.one}"/> <src path=3D"${src.dir.two}"/> <src path=3D"${src.dir.three}"/> </cobertura-report> ? test/net/sourceforge/cobertura/reporting/MainTest.java ? test/net/sourceforge/cobertura/reporting/html/HTMLReportTest.java Index: src/net/sourceforge/cobertura/ant/ReportTask.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /cvsroot/cobertura/cobertura/src/net/sourceforge/cobertura/ant/ReportTask= .java,v retrieving revision 1.4 diff -u -d -p -r1.4 ReportTask.java --- src/net/sourceforge/cobertura/ant/ReportTask.java 29 Apr 2005 = 20:20:16 -0000 1.4 +++ src/net/sourceforge/cobertura/ant/ReportTask.java 7 Jun 2005 = 03:44:12 -0000 @@ -59,6 +59,8 @@ package net.sourceforge.cobertura.ant; import java.io.File; import java.net.URL; import java.net.URLClassLoader; +import java.util.Iterator; +import java.util.Vector; =20 import net.sourceforge.cobertura.util.Header; import net.sourceforge.cobertura.util.StringUtil; @@ -70,6 +72,7 @@ import org.apache.tools.ant.taskdefs.Mat import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; =20 + /** * Generate a coverage report based on coverage data generated=20 * by instrumented classes. @@ -79,10 +82,10 @@ public class ReportTask extends Matching =20 private String dataFile =3D null; private String format =3D "html"; - private Path src; private File destDir; =20 private Java java =3D null; + private Vector sourcePaths =3D new Vector(); =20 public void setDataFile(String dataFile) { @@ -101,14 +104,8 @@ public class ReportTask extends Matching =20 public void setSrcDir(Path srcDir) { - if (src =3D=3D null) - { - src =3D srcDir; - } - else - { - src.append(srcDir); - } + // support legacy + createSrc().setPath(srcDir.toString()); } =20 public void execute() throws BuildException @@ -126,9 +123,13 @@ public class ReportTask extends Matching =20 getJava().createArg().setValue("--destination"); getJava().createArg().setValue(destDir.toString()); - - getJava().createArg().setValue("--source"); - getJava().createArg().setValue(src.toString()); + =09 + Iterator pathIterator =3D sourcePaths.iterator(); + while (pathIterator.hasNext()) { + Src path =3D (Src) pathIterator.next(); + getJava().createArg().setValue("--source"); + getJava().createArg().setValue(path.path()); + } =20 if (getJava().executeJava() !=3D 0) { @@ -182,5 +183,23 @@ public class ReportTask extends Matching { createClasspath().setRefid(r); } +=09 + public Src createSrc() { + Src sourcePath =3D new Src(); + sourcePaths.add(sourcePath); + return sourcePath; + } +=09 + public static class Src { + private String sourcePath; + + public String path() { + return sourcePath; + } + + public void setPath(String aPath) { + sourcePath =3D aPath; + } + } =20 } Index: src/net/sourceforge/cobertura/reporting/Main.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /cvsroot/cobertura/cobertura/src/net/sourceforge/cobertura/reporting/Main= .java,v retrieving revision 1.13 diff -u -d -p -r1.13 Main.java --- src/net/sourceforge/cobertura/reporting/Main.java 2 Jun 2005 = 17:40:50 -0000 1.13 +++ src/net/sourceforge/cobertura/reporting/Main.java 7 Jun 2005 = 03:44:12 -0000 @@ -26,6 +26,8 @@ import gnu.getopt.Getopt; import gnu.getopt.LongOpt; =20 import java.io.File; +import java.io.IOException; +import java.util.StringTokenizer; =20 import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler; import net.sourceforge.cobertura.coveragedata.ProjectData; @@ -39,18 +41,15 @@ public class Main =20 private static final Logger logger =3D Logger.getLogger(Main.class); =20 - // TODO: make these not static? - static String format =3D "html"; - static File dataFile =3D null; - static File destinationDir =3D null; - static File sourceDir =3D null; - - public static void main(String[] args) throws Exception - { + public static void main(String[] args) throws Exception { + String format =3D "html"; + File dataFile =3D null; + File destinationDir =3D null; + File sourceDir[] =3D null; long startTime =3D System.currentTimeMillis(); =20 LongOpt[] longOpts =3D new LongOpt[4]; - // TODO: Allow for multiple destination and multiple source = directories + =09 longOpts[0] =3D new LongOpt("format", LongOpt.REQUIRED_ARGUMENT, = null, 'f'); longOpts[1] =3D new LongOpt("datafile", LongOpt.REQUIRED_ARGUMENT, @@ -62,6 +61,7 @@ public class Main =20 Getopt g =3D new Getopt(Main.class.getName(), args, ":f:d:o:s:", longOpts); + StringBuffer sourceDirBuffer =3D null; int c; while ((c =3D g.getopt()) !=3D -1) { @@ -112,23 +112,44 @@ public class Main break; =20 case 's': - sourceDir =3D new File(g.getOptarg()); - if (!sourceDir.exists()) - { - System.err.println("Error: source directory " - + sourceDir + " does not exist"); - System.exit(1); - } - if (!sourceDir.isDirectory()) - { - System.err.println("Error: source directory " - + sourceDir + " must be a directory"); - System.exit(1); + if (sourceDirBuffer =3D=3D null) { + sourceDirBuffer =3D new StringBuffer(); + sourceDirBuffer.append(g.getOptarg()); + } else { + sourceDirBuffer.append(";"); + sourceDirBuffer.append(g.getOptarg()); } break; } } + sourceDir =3D parseSourceDirectory(sourceDirBuffer.toString()); + performCoverage(format, dataFile, destinationDir, sourceDir); + + long stopTime =3D System.currentTimeMillis(); + System.out.println("Reporting time: " + (stopTime - startTime) + = "ms"); + } + + protected static File[] parseSourceDirectory(String = sourceDirectoryString) { + File[] sourceDir; + StringTokenizer stringTokenizer =3D new = StringTokenizer(sourceDirectoryString, ";"); + sourceDir =3D new File[stringTokenizer.countTokens()]; + int i =3D 0; + while (stringTokenizer.hasMoreTokens()) { + sourceDir[i] =3D new File(stringTokenizer.nextToken()); + if (!sourceDir[i].exists()) { + System.err.println("Error: source directory " + sourceDir[i] + " = does not exist"); + System.exit(1); + } + if (!sourceDir[i].isDirectory()) { + System.err.println("Error: source directory " + sourceDir[i] + " = must be a directory"); + System.exit(1); + } + i++; + } + return sourceDir; + } =20 + protected static void performCoverage(String format, File dataFile, = File destinationDir, File[] sourceDir) throws Exception, IOException { if (dataFile =3D=3D null) dataFile =3D CoverageDataFileHandler.getDefaultDataFile(); =20 @@ -150,7 +171,7 @@ public class Main logger.debug("dataFile is " + dataFile.getAbsolutePath()); logger.debug("destinationDir is " + destinationDir.getAbsolutePath()); - logger.debug("sourceDir is " + sourceDir.getAbsolutePath()); + // logger.debug("sourceDir is " + sourceDir.getAbsolutePath()); } =20 ProjectData projectData =3D CoverageDataFileHandler @@ -170,10 +191,6 @@ public class Main { new XMLReport(projectData, destinationDir, sourceDir); } - - long stopTime =3D System.currentTimeMillis(); - System.out - .println("Reporting time: " + (stopTime - startTime) + "ms"); } =20 } Index: src/net/sourceforge/cobertura/reporting/html/HTMLReport.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /cvsroot/cobertura/cobertura/src/net/sourceforge/cobertura/reporting/html= /HTMLReport.java,v retrieving revision 1.25 diff -u -d -p -r1.25 HTMLReport.java --- src/net/sourceforge/cobertura/reporting/html/HTMLReport.java 31 May = 2005 19:18:59 -0000 1.25 +++ src/net/sourceforge/cobertura/reporting/html/HTMLReport.java 7 Jun = 2005 03:44:12 -0000 @@ -52,14 +52,14 @@ public class HTMLReport =20 private File destinationDir; =20 - private File sourceDir; + File sourceDir[]; =20 private ProjectData projectData; =20 /** * Create a coverage report */ - public HTMLReport(ProjectData projectData, File outputDir, File = sourceDir) + public HTMLReport(ProjectData projectData, File outputDir, File[] = sourceDir) throws Exception { this.destinationDir =3D outputDir; @@ -73,6 +73,9 @@ public class HTMLReport generateSourceFiles(); } =20 + protected HTMLReport() { + } + private String generatePackageName(PackageData packageData) { if (packageData.getName().equals("")) @@ -448,8 +451,7 @@ public class HTMLReport BufferedReader br =3D null; try { - File sourceFile =3D new File(sourceDir, sourceFileData - .getName()); + File sourceFile =3D sourceFileForName(sourceFileData.getName()); br =3D new BufferedReader(new FileReader(sourceFile)); String lineStr; JavaToHtml javaToHtml =3D new JavaToHtml(); @@ -525,6 +527,16 @@ public class HTMLReport } } =20 + protected File sourceFileForName(String sourceFileName) { + File file =3D null; + for (int i =3D 0; i < sourceDir.length; i++) { + file =3D new File(sourceDir[i], sourceFileName); + if (file.exists()) + return file; + } + return file; + } + private static String generateTableHeader(String title, boolean showColumnForNumberOfClasses) { @@ -640,7 +652,7 @@ public class HTMLReport StringBuffer ret =3D new StringBuffer(); double lineCoverage =3D -1; double branchCoverage =3D -1; - double ccn =3D Util.getCCN(sourceDir, true); + double ccn =3D averageCCN(); =20 if (projectData.getNumberOfValidLines() > 0) lineCoverage =3D projectData.getLineCoverageRate(); @@ -657,6 +669,14 @@ public class HTMLReport return ret.toString(); } =20 + private double averageCCN() { + double accumulator =3D 0; + for (int i =3D 0; i < sourceDir.length; i++) { + accumulator +=3D Util.getCCN(sourceDir[i], true); + } + return accumulator/sourceDir.length; + } + private String generateTableRowForPackage(PackageData packageData) { StringBuffer ret =3D new StringBuffer(); @@ -664,8 +684,7 @@ public class HTMLReport String url2 =3D "frame-sourcefiles-" + packageData.getName() + = ".html"; double lineCoverage =3D -1; double branchCoverage =3D -1; - double ccn =3D Util.getCCN(new File(sourceDir, packageData - .getSourceFileName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(packageData.getSourceFileName()), false); =20 if (packageData.getNumberOfValidLines() > 0) lineCoverage =3D packageData.getLineCoverageRate(); @@ -689,8 +708,7 @@ public class HTMLReport StringBuffer ret =3D new StringBuffer(); double lineCoverage =3D -1; double branchCoverage =3D -1; - double ccn =3D Util.getCCN( - new File(sourceDir, sourceFileData.getName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(sourceFileData.getName()), false); =20 if (sourceFileData.getNumberOfValidLines() > 0) lineCoverage =3D sourceFileData.getLineCoverageRate(); Index: src/net/sourceforge/cobertura/reporting/xml/XMLReport.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /cvsroot/cobertura/cobertura/src/net/sourceforge/cobertura/reporting/xml/= XMLReport.java,v retrieving revision 1.21 diff -u -d -p -r1.21 XMLReport.java --- src/net/sourceforge/cobertura/reporting/xml/XMLReport.java 25 May = 2005 14:31:37 -0000 1.21 +++ src/net/sourceforge/cobertura/reporting/xml/XMLReport.java 7 Jun = 2005 03:44:12 -0000 @@ -51,10 +51,10 @@ public class XMLReport =20 private int indent =3D 0; =20 - private File sourceDirectory; + private File sourceDirectory[]; =20 public XMLReport(ProjectData projectData, File destinationDir, - File sourceDirectory) throws IOException + File[] sourceDirectory) throws IOException { this.sourceDirectory =3D sourceDirectory; =20 @@ -134,8 +134,7 @@ public class XMLReport { logger.debug("Dumping package " + packageData.getName()); =20 - double ccn =3D Util.getCCN(new File(sourceDirectory, packageData - .getSourceFileName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(packageData.getSourceFileName()), false); println("<package name=3D\"" + packageData.getName() + "\" line-rate=3D\"" + packageData.getLineCoverageRate() + "\" branch-rate=3D\"" + packageData.getBranchCoverageRate() @@ -174,8 +173,7 @@ public class XMLReport { logger.debug("Dumping class " + classData.getName()); =20 - double ccn =3D Util.getCCN(new File(sourceDirectory, classData - .getSourceFileName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(classData.getSourceFileName()), false); println("<class name=3D\"" + classData.getName() + "\" filename=3D\"" + classData.getSourceFileName() + "\" line-rate=3D\"" + classData.getLineCoverageRate() + "\" branch-rate=3D\"" @@ -190,6 +188,16 @@ public class XMLReport println("</class>"); } =20 + protected File sourceFileForName(String sourceFileName) { + File file =3D null; + for (int i =3D 0; i < sourceDirectory.length; i++) { + file =3D new File(sourceDirectory[i], sourceFileName); + if (file.exists()) + return file; + } + return file; + } + private void dumpMethods(ClassData classData) { println("<methods>"); |