From: Seigel, J. <Jam...@av...> - 2005-05-20 22:49:22
|
I have taken a stab at modifying the code to allow it to support = multiple source directories separated by commas. I haven't added as = many unit tests I would have liked as it was a little challenging at = times to wedge them in. =20 Unfortunately my eclipse template is very different from yours and = unfortunately I fixed some of the todos while I was poking around..... If there are too many changes I can work on isolating them and resubmit = and dot it a bit more cleanly. One note, for calculating the complexity of "all packages" I averaged = the results for each source tree. this might be wrong but easily = changed. anyway have a look Cheers James. 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.11 diff -u -r1.11 Main.java --- src/net/sourceforge/cobertura/reporting/Main.java 3 May 2005 = 13:20:02 -0000 1.11 +++ src/net/sourceforge/cobertura/reporting/Main.java 20 May 2005 = 22:41:26 -0000 @@ -26,6 +26,8 @@ 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; @@ -34,140 +36,115 @@ =20 import org.apache.log4j.Logger; =20 -public class Main -{ - +public class Main { 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 - longOpts[0] =3D new LongOpt("format", LongOpt.REQUIRED_ARGUMENT, = null, - 'f'); - longOpts[1] =3D new LongOpt("datafile", LongOpt.REQUIRED_ARGUMENT, - null, 'd'); - longOpts[2] =3D new LongOpt("destination", LongOpt.REQUIRED_ARGUMENT, - null, 'o'); - longOpts[3] =3D new LongOpt("source", LongOpt.REQUIRED_ARGUMENT, = null, - 's'); + longOpts[0] =3D new LongOpt("format", LongOpt.REQUIRED_ARGUMENT, = null, 'f'); + longOpts[1] =3D new LongOpt("datafile", LongOpt.REQUIRED_ARGUMENT, = null, 'd'); + longOpts[2] =3D new LongOpt("destination", LongOpt.REQUIRED_ARGUMENT, = null, 'o'); + longOpts[3] =3D new LongOpt("source", LongOpt.REQUIRED_ARGUMENT, = null, 's'); =20 - Getopt g =3D new Getopt(Main.class.getName(), args, ":f:d:o:s:", - longOpts); + Getopt g =3D new Getopt(Main.class.getName(), args, ":f:d:o:s:", = longOpts); int c; - while ((c =3D g.getopt()) !=3D -1) - { - switch (c) - { - case 'f': - format =3D g.getOptarg(); - if (!format.equalsIgnoreCase("html") - && !format.equalsIgnoreCase("xml")) - { - System.err - .println("Error: format \"" - + format - + "\" is invalid. Must be either html or xml"); - System.exit(1); - } - break; - - case 'd': - dataFile =3D new File(g.getOptarg()); - if (!dataFile.exists()) - { - System.err.println("Error: data file " - + dataFile.getAbsolutePath() - + " does not exist"); - System.exit(1); - } - if (!dataFile.isFile()) - { - System.err.println("Error: data file " - + dataFile.getAbsolutePath() - + " must be a regular file"); - System.exit(1); - } - break; - - case 'o': - destinationDir =3D new File(g.getOptarg()); - if (destinationDir.exists() - && !destinationDir.isDirectory()) - { - System.err.println("Error: destination directory " - + destinationDir - + " already exists but is not a directory"); - System.exit(1); - } - destinationDir.mkdirs(); - break; - - 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); - } - break; + while ((c =3D g.getopt()) !=3D -1) { + switch (c) { + case 'f': + format =3D g.getOptarg(); + if (!format.equalsIgnoreCase("html") && = !format.equalsIgnoreCase("xml")) { + System.err.println("Error: format \"" + format + "\" is invalid. = Must be either html or xml"); + System.exit(1); + } + break; + + case 'd': + dataFile =3D new File(g.getOptarg()); + if (!dataFile.exists()) { + System.err.println("Error: data file " + = dataFile.getAbsolutePath() + " does not exist"); + System.exit(1); + } + if (!dataFile.isFile()) { + System.err.println("Error: data file " + = dataFile.getAbsolutePath() + " must be a regular file"); + System.exit(1); + } + break; + + case 'o': + destinationDir =3D new File(g.getOptarg()); + if (destinationDir.exists() && !destinationDir.isDirectory()) { + System.err.println("Error: destination directory " + = destinationDir + " already exists but is not a directory"); + System.exit(1); + } + destinationDir.mkdirs(); + break; + + case 's': + sourceDir =3D parseSourceDirectory(g.getOptarg()); + break; + } + } + + 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 - if (destinationDir =3D=3D null) - { + if (destinationDir =3D=3D null) { System.err.println("Error: destination directory must be set"); System.exit(1); } =20 - if (sourceDir =3D=3D null) - { + if (sourceDir =3D=3D null) { System.err.println("Error: source directory must be set"); System.exit(1); } =20 - if (logger.isDebugEnabled()) - { + if (logger.isDebugEnabled()) { logger.debug("format is " + format); logger.debug("dataFile is " + dataFile.getAbsolutePath()); - logger.debug("destinationDir is " - + destinationDir.getAbsolutePath()); - logger.debug("sourceDir is " + sourceDir.getAbsolutePath()); + logger.debug("destinationDir is " + = destinationDir.getAbsolutePath()); } =20 - ProjectData projectData =3D CoverageDataFileHandler - .loadCoverageData(dataFile); + ProjectData projectData =3D = CoverageDataFileHandler.loadCoverageData(dataFile); =20 - if (format.equalsIgnoreCase("html")) - { + if (format.equalsIgnoreCase("html")) { new HTMLReport(projectData, destinationDir, sourceDir); - } - else if (format.equalsIgnoreCase("xml")) - { + } else if (format.equalsIgnoreCase("xml")) { 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.15 diff -u -r1.15 HTMLReport.java --- src/net/sourceforge/cobertura/reporting/html/HTMLReport.java 18 May = 2005 05:26:20 -0000 1.15 +++ src/net/sourceforge/cobertura/reporting/html/HTMLReport.java 20 May = 2005 22:41:27 -0000 @@ -52,14 +52,14 @@ =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 @@ generateSourceFiles(); } =20 + protected HTMLReport() { + } + private String generatePackageName(PackageData packageData) { if (packageData.getName().equals("")) @@ -301,15 +304,10 @@ } =20 // Output a line for each package or subpackage - // TODO: Do we need this extra "package.size() > 0" check? - if (packages.size() > 0) - { - Iterator iter =3D packages.iterator(); - while (iter.hasNext()) - { - PackageData subPackageData =3D (PackageData)iter.next(); - out.println(generateTableRowForPackage(subPackageData)); - } + Iterator iter =3D packages.iterator(); + while (iter.hasNext()) { + PackageData subPackageData =3D (PackageData) iter.next(); + out.println(generateTableRowForPackage(subPackageData)); } =20 out.println("</tbody>"); @@ -328,9 +326,7 @@ if (packageData =3D=3D null) { classes =3D new TreeSet(); - if (projectData.getNumberOfClasses() > 0) - { - Iterator iter =3D projectData.getClasses().iterator(); + iter =3D projectData.getClasses().iterator(); while (iter.hasNext()) { ClassData classData =3D (ClassData)iter.next(); @@ -339,7 +335,6 @@ classes.add(classData); } } - } } else { @@ -353,11 +348,10 @@ out.println("<table class=3D\"report\" id=3D\"classResults\">"); out.println(generateTableHeaderForClasses()); out.println("<tbody>"); - - Iterator iter =3D classes.iterator(); - while (iter.hasNext()) - { - ClassData classData =3D (ClassData)iter.next(); + =09 + iter =3D classes.iterator(); + while (iter.hasNext()) { + ClassData classData =3D (ClassData) iter.next(); out.println(generateTableRowForClass(classData)); } =20 @@ -451,8 +445,7 @@ BufferedReader br =3D null; try { - File sourceFile =3D new File(sourceDir, classData - .getSourceFileName()); + File sourceFile =3D = sourceFileForName(classData.getSourceFileName()); br =3D new BufferedReader(new FileReader(sourceFile)); String lineStr; JavaToHtml javaToHtml =3D new JavaToHtml(); @@ -527,6 +520,16 @@ } } =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 generateHelpURL(String text, String description) { StringBuffer ret =3D new StringBuffer(); @@ -627,7 +630,7 @@ StringBuffer ret =3D new StringBuffer(); double lineCoverage =3D projectData.getLineCoverageRate(); double branchCoverage =3D projectData.getBranchCoverageRate(); - double ccn =3D Util.getCCN(sourceDir, true); + double ccn =3D averageCCN(); ret.append(" <tr>"); ret.append("<td class=3D\"text\"><b>All Packages</b></td>"); ret.append("<td class=3D\"value\">" + = projectData.getNumberOfClasses() @@ -638,15 +641,22 @@ return ret.toString(); } =20 - private String generateTableRowForPackage(PackageData packageData) + 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; + } + + protected String generateTableRowForPackage(PackageData packageData) { StringBuffer ret =3D new StringBuffer(); String url1 =3D "frame-summary-" + packageData.getName() + ".html"; String url2 =3D "frame-classes-" + packageData.getName() + ".html"; double lineCoverage =3D packageData.getLineCoverageRate(); double branchCoverage =3D packageData.getBranchCoverageRate(); - double ccn =3D Util.getCCN(new File(sourceDir, packageData - .getSourceFileName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(packageData.getSourceFileName()), false); ret.append(" <tr>"); ret.append("<td class=3D\"text\"><a href=3D\"" + url1 + "\" onClick=3D'parent.classList.location.href=3D\"" + url2 @@ -664,8 +674,7 @@ StringBuffer ret =3D new StringBuffer(); double lineCoverage =3D classData.getLineCoverageRate(); double branchCoverage =3D classData.getBranchCoverageRate(); - double ccn =3D Util.getCCN(new File(sourceDir, classData - .getSourceFileName()), false); + double ccn =3D = Util.getCCN(sourceFileForName(classData.getSourceFileName()), false); ret.append(" <tr>"); ret.append("<td class=3D\"text\"><a href=3D\"" + classData.getName() + ".html\">" + classData.getName() + "</a></td>"); 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.19 diff -u -r1.19 XMLReport.java --- src/net/sourceforge/cobertura/reporting/xml/XMLReport.java 29 Apr = 2005 20:06:53 -0000 1.19 +++ src/net/sourceforge/cobertura/reporting/xml/XMLReport.java 20 May = 2005 22:41:27 -0000 @@ -50,10 +50,10 @@ =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 @@ -133,8 +133,7 @@ { 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() @@ -164,8 +163,7 @@ { 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\"" @@ -180,6 +178,16 @@ 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>"); Index: test/net/sourceforge/cobertura/reporting/MainTest.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: test/net/sourceforge/cobertura/reporting/MainTest.java diff -N test/net/sourceforge/cobertura/reporting/MainTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/net/sourceforge/cobertura/reporting/MainTest.java 1 Jan 1970 = 00:00:00 -0000 @@ -0,0 +1,34 @@ +package net.sourceforge.cobertura.reporting; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +public class MainTest extends TestCase { +=09 + private File srcDirectory1; + private File srcDirectory2; + + protected void setUp() throws Exception { + super.setUp(); + File file =3D File.createTempFile("test", ".tmp"); + file.deleteOnExit(); + srcDirectory1 =3D new File(file.getParent(),"src1"); + srcDirectory1.mkdir(); + srcDirectory2 =3D new File(file.getParent(),"src2"); + srcDirectory2.mkdir(); + } + + protected void tearDown() throws Exception { + srcDirectory1.delete(); + srcDirectory2.delete(); + super.tearDown(); + } + + public void testParseSourceDirectory() throws Exception { + File[] files =3D = Main.parseSourceDirectory(srcDirectory1.getCanonicalPath()+","+srcDirecto= ry2.getCanonicalPath()); + assertEquals(2, files.length); + } + +} Index: test/net/sourceforge/cobertura/reporting/html/HTMLReportTest.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: = test/net/sourceforge/cobertura/reporting/html/HTMLReportTest.java diff -N = test/net/sourceforge/cobertura/reporting/html/HTMLReportTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/net/sourceforge/cobertura/reporting/html/HTMLReportTest.java 1 = Jan 1970 00:00:00 -0000 @@ -0,0 +1,63 @@ +package net.sourceforge.cobertura.reporting.html; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import junit.framework.TestCase; + +public class HTMLReportTest extends TestCase { +=09 + private File[] srcDirectory; + + protected void setUp() throws Exception { + super.setUp(); + File tempFile =3D File.createTempFile("temp", ".tmp"); + tempFile.deleteOnExit(); + srcDirectory =3D new File[2]; + srcDirectory[1] =3D new File(tempFile.getParent(),"src1"); + srcDirectory[1].mkdir(); + filesUnderSourceDir(srcDirectory[1],1); + srcDirectory[0] =3D new File(tempFile.getParent(),"src0"); + srcDirectory[0].mkdir(); + filesUnderSourceDir(srcDirectory[0],2); + } + + private void filesUnderSourceDir(File srcDirectory, int number) throws = IOException, FileNotFoundException { + File sub =3D new File(srcDirectory,"com"); + sub.mkdir(); + File temp =3D new File(sub,"example"); + temp.mkdir(); + File sample1 =3D new File(temp,"Sample"+number); + sample1.createNewFile(); + File sample2 =3D new File(temp,"Sample" + (number+1) ); + sample2.createNewFile(); + } +=09 + protected void tearDown() throws Exception { + deleteTree(srcDirectory[0]); + deleteTree(srcDirectory[1]); + super.tearDown(); + } +=09 + protected void deleteTree(File fileRoot) { + if(fileRoot.isFile()){ + fileRoot.delete(); + return; + } else {=20 + File[] files =3D fileRoot.listFiles(); + for (int i =3D 0; i < files.length; i++) { + deleteTree(files[i]); + } + fileRoot.delete(); + } =09 + =09 + } +=09 + public void testSourceFileForName() throws Exception { + HTMLReport report =3D new HTMLReport(); + report.sourceDir =3D srcDirectory; + File sourceFileForName =3D = report.sourceFileForName("com/example/Sample2"); + assertTrue(sourceFileForName.exists()); + assertTrue(sourceFileForName.getCanonicalPath().endsWith("Sample2")); + } +} |