changeset 8:a929d697c161

2012-01-27 Pavel Tisnovsky <ptisnovs@redhat.com> * src/ReportGenerator.java: * templates/all_packages_template.html: Added new functionality: table with number of all classes and classes coveraged by the test are used in the page with all packages list.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Fri, 27 Jan 2012 17:28:22 +0100
parents 342d366654ce
children 5bc6733b29bd
files ChangeLog src/ReportGenerator.java templates/all_packages_template.html
diffstat 3 files changed, 91 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jan 27 16:35:26 2012 +0100
+++ b/ChangeLog	Fri Jan 27 17:28:22 2012 +0100
@@ -1,3 +1,11 @@
+2012-01-27  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/ReportGenerator.java:
+	* templates/all_packages_template.html:
+	Added new functionality: table with number of all classes
+	and classes coveraged by the test are used in the
+	page with all packages list.
+
 2012-01-27  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* templates/style.css:
--- a/src/ReportGenerator.java	Fri Jan 27 16:35:26 2012 +0100
+++ b/src/ReportGenerator.java	Fri Jan 27 17:28:22 2012 +0100
@@ -82,10 +82,13 @@
      *            directory where report is generated
      * @param allClasses
      *            set of all classes
+     * @param testedClasses
+     *            set of tested classes
      * @param packageNames
      *            set of package names
      */
-    private static void printPackageListToFile(String reportDirectory, Set<String> allClasses, Set<String> packageNames)
+    private static void printPackageListToFile(String reportDirectory, Set<String> allClasses,
+                    Set<String> testedClasses, Set<String> packageNames)
     {
         List<String> template = FileUtils.readTextFile("templates/all_packages_template.html");
         List<String> out = new LinkedList<String>();
@@ -95,7 +98,7 @@
             // replace text in template where needed
             if ("${PACKAGE_LIST}".equals(templateLine))
             {
-                addPackageList(packageNames, out);
+                addPackageList(allClasses, testedClasses, packageNames, out);
             }
             // normal line
             else
@@ -119,7 +122,8 @@
      * @param testedClasses
      *            set of tested classes
      */
-    private static void printReportForPackageToFile(String reportDirectory, String packageName, Set<String> testedClasses)
+    private static void printReportForPackageToFile(String reportDirectory, String packageName,
+                    Set<String> testedClasses)
     {
         List<String> template = FileUtils.readTextFile("templates/package_template.html");
         List<String> out = new LinkedList<String>();
@@ -178,22 +182,82 @@
 
     /**
      * Add list of all packages to a list of string which represents generated
-     * report.
+     * report. Number of all classes and classes covered by tests are also
+     * calculated and printed. Background of table rows are changed according
+     * to test coverage ratio.
      * 
+     * @param allClasses
+     *            set of all classes
+     * @param testedClasses
+     *            set of tested classes
      * @param packageNames
      *            set of package names
      * @param out
      *            list of string which represents generated report
      */
-    private static void addPackageList(Set<String> packageNames, List<String> out)
+    @SuppressWarnings("boxing")
+    private static void addPackageList(Set<String> allClasses, Set<String> testedClasses, Set<String> packageNames,
+                    List<String> out)
     {
+        // iterate through all package names
         for (String packageName : packageNames)
         {
-            out.add("<a target='ClassesListFrame' href='" + packageName + ".html'>" + packageName + "</a><br />");
+            // compute number of all classes in a package
+            final int allClassesCnt = numberOfClassesInPackage(packageName, allClasses);
+            // compute number of classes coveraged by tests
+            final int testedClassesCnt = numberOfClassesInPackage(packageName, testedClasses);
+            // -> in percent
+            final float percentage = 100.0f*testedClassesCnt / allClassesCnt;
+            // table row background color is based on percentual test coverage ration
+            String backgroundColor = generateTableRowBackground(percentage); 
+            String str = String.format("<tr style='background-color:%s'><td><a target='ClassesListFrame' href='%s.html'>%s</a></td><td style='text-align:right'>%d</td><td style='text-align:right'>%d</td><td style='text-align:right'>%5.1f %%</td></tr>",
+                            backgroundColor, packageName, packageName, allClassesCnt, testedClassesCnt, percentage);
+            out.add(str);
         }
     }
 
     /**
+     * Generate table row background according to test coverage percentual
+     * ratio.
+     * 
+     * @param percentage
+     *            test coverage percentual ratio
+     * @return string representing HTML color
+     */
+    private static String generateTableRowBackground(final float percentage)
+    {
+        String backgroundColor = percentage < 10.0f ? "#ffc0c0" :
+                                 percentage == 100.0f ? "#c0ffc0" :
+                                 percentage >= 50.0f ? "#ffffc0" :
+                                 "#ffffff";
+        return backgroundColor;
+    }
+
+    /**
+     * Calculate number of classes in given package.
+     * 
+     * @param packageName
+     *            name of package
+     * @param classes
+     *            set of class names
+     * @return number of classes in given package
+     */
+    private static int numberOfClassesInPackage(String packageName, Set<String> classes)
+    {
+        int cnt = 0;
+        // iterate through all class names
+        for (String className : classes)
+        {
+            // count only classes in given package
+            if (className.startsWith(packageName))
+            {
+                cnt++;
+            }
+        }
+        return cnt;
+    }
+
+    /**
      * Add list of all classes to a list of string which represents generated
      * report.
      * 
@@ -259,7 +323,8 @@
      * @param testedMethods
      *            methods called from tests
      */
-    private static void createFileForClass(String reportDirectory, String testClass, Set<String> allMethods, Set<String> apiMethods, Set<String> testedMethods)
+    private static void createFileForClass(String reportDirectory, String testClass, Set<String> allMethods,
+                    Set<String> apiMethods, Set<String> testedMethods)
     {
         List<String> template = FileUtils.readTextFile("templates/class_template.html");
         List<String> out = new LinkedList<String>();
@@ -343,11 +408,9 @@
         String returnType = printedMethodName.substring(0, printedMethodName.indexOf(' '));
         String name = printedMethodName.substring(returnTypeEnds, parametersBegin);
         String params = printedMethodName.substring(parametersBegin);
-
-        return String.format(
-            "<span style='color:#000080'>%s</span>" +
-            "<span style='color:#008000'>%s</span>" +
-            "<span style='color:#804000'>%s</span>", returnType, name, params);
+        
+        return String.format("<span style='color:#000080'>%s</span>" + "<span style='color:#008000'>%s</span>"
+                        + "<span style='color:#804000'>%s</span>", returnType, name, params);
     }
 
     private static String printMethodCoverage(String methodName, Set<String> methodSet)
@@ -484,12 +547,14 @@
                 // replace text in template where needed
                 else if (templateLine.contains("${TESTED_PACKAGES_RATIO}"))
                 {
-                    out.add(templateLine.replace("${TESTED_PACKAGES_RATIO}", "" + calcRatio(numberOfUsedPackages, numberOfAllPackages)));
+                    out.add(templateLine.replace("${TESTED_PACKAGES_RATIO}", ""
+                                    + calcRatio(numberOfUsedPackages, numberOfAllPackages)));
                 }
                 // replace text in template where needed
                 else if (templateLine.contains("${TESTED_CLASSES_RATIO}"))
                 {
-                    out.add(templateLine.replace("${TESTED_CLASSES_RATIO}", "" + calcRatio(numberOfTestedClasses, numberOfAllClasses)));
+                    out.add(templateLine.replace("${TESTED_CLASSES_RATIO}", ""
+                                    + calcRatio(numberOfTestedClasses, numberOfAllClasses)));
                 }
                 // normal output
                 else
@@ -537,7 +602,7 @@
 
         System.out.println("Report directory:  " + reportDirectory);
 
-        printPackageListToFile(reportDirectory, allClasses, usedPackageNames);
+        printPackageListToFile(reportDirectory, allClasses, testedClasses, usedPackageNames);
         printReportForAllClassesInOneFile(reportDirectory, usedPackageNames, testedClasses);
         printReportForAllPackages(reportDirectory, usedPackageNames, testedClasses);
         printReportForAllClasses(reportDirectory, testedClasses);
--- a/templates/all_packages_template.html	Fri Jan 27 16:35:26 2012 +0100
+++ b/templates/all_packages_template.html	Fri Jan 27 17:28:22 2012 +0100
@@ -10,7 +10,10 @@
     <body>
         <h1>Package list</h1>
 <a target='ClassesListFrame' href='all_classes.html'>all classes</a><br /><br />
+<table class='package_list'>
+<tr><th>Package</th><th>Classes</th><th>Covered</th><th>Ratio</th></tr>
 ${PACKAGE_LIST}
+</table>
     </body>
 </html>