changeset 12:5030a59cd546

2012-03-02 Pavel Tisnovsky <ptisnovs@redhat.com> * src/ReportGenerator.java: * templates/package_template.html: Added new functionality of report generator: table with basic statistic is shown for each tested package.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Fri, 02 Mar 2012 16:51:17 +0100
parents f010a4361c96
children 8c9c61f17fd1
files ChangeLog src/ReportGenerator.java templates/package_template.html
diffstat 3 files changed, 71 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Feb 21 17:16:21 2012 +0100
+++ b/ChangeLog	Fri Mar 02 16:51:17 2012 +0100
@@ -1,3 +1,11 @@
+2012-03-02  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/ReportGenerator.java:
+	* templates/package_template.html:
+	Added new functionality of report generator:
+	table with basic statistic is shown for
+	each tested package.
+
 2012-02-21  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/FileUtils.java:
--- a/src/ReportGenerator.java	Tue Feb 21 17:16:21 2012 +0100
+++ b/src/ReportGenerator.java	Fri Mar 02 16:51:17 2012 +0100
@@ -136,7 +136,9 @@
     private static void printReportForPackageToFile(String reportDirectory, String packageName,
                     Set<String> testedClasses)
     {
+        // read HTML template
         List<String> template = FileUtils.readTextFile("templates/package_template.html");
+        // list containing all lines of generated HTML file
         List<String> out = new LinkedList<String>();
         // iterate through whole template
         for (String templateLine : template)
@@ -144,7 +146,11 @@
             // replace text in template where needed
             if ("${CLASS_LIST}".equals(templateLine))
             {
-                addClassList(packageName, testedClasses, out);
+                addClassList(reportDirectory, packageName, testedClasses, out);
+            }
+            else if ("${PACKAGE_NAME}".equals(templateLine))
+            {
+                out.add("Package " + packageName);
             }
             // normal line
             else
@@ -282,7 +288,7 @@
      * @param out
      *            list of string which represents generated report
      */
-    private static void addClassList(String packageName, Set<String> testedClasses, List<String> out)
+    private static void addClassList(String reportDirectory, String packageName, Set<String> testedClasses, List<String> out)
     {
         // iterate through all class names
         for (String className : testedClasses)
@@ -290,7 +296,8 @@
             // list only classes from given package
             if (className.startsWith(packageName))
             {
-                out.add("<a target='ResultsFrame' href='" + className + ".html'>" + className + "</a><br>");
+                //out.add("<a target='ResultsFrame' href='" + className + ".html'>" + className + "</a><br>");
+                out.add(addOneRowToResultsTable(reportDirectory, className));
             }
         }
     }
@@ -321,22 +328,7 @@
             {
                 if (className.startsWith(packageName))
                 {
-                    Set<String> apiMethods = readApiMethods(reportDirectory, className);
-                    Set<String> testedMethods = readTestedMethods(reportDirectory, className);
-                    // compute number of all methods in a class
-                    final int allMethodsCnt = apiMethods.size();
-                    // compute number of methods covered by tests
-                    final int testedMethodsCnt = testedMethods.size();
-                    // -> in percent
-                    final float percentage = 100.0f*testedMethodsCnt / allMethodsCnt;
-                    // table row background color is based on percentual test coverage ration
-                    String backgroundColor = generateTableRowBackground(percentage);
-                    String doc = DOC_BASE + "/" + className.replace('.', '/') + ".html";
-                    // format output string
-                    String outStr = String.format("<tr style='background-color:%s'><td><a target='ResultsFrame' 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><td style='text-align:right'><a href='%s' target='_blank'>ext</a></td></tr>",
-                            backgroundColor, className, className,
-                            allMethodsCnt, testedMethodsCnt, percentage, doc);
-                    out.add(outStr);
+                    out.add(addOneRowToResultsTable(reportDirectory, className));
                 }
             }
             out.add("</table>");
@@ -344,6 +336,34 @@
     }
 
     /**
+     * Add one row to a table containing test coverage for given class.
+     *
+     * @param reportDirectory
+     *            directory where report is generated
+     * @param className
+     *            name of tested class
+     */
+    private static String addOneRowToResultsTable(String reportDirectory, String className)
+    {
+        Set<String> apiMethods = readApiMethods(reportDirectory, className);
+        Set<String> testedMethods = readTestedMethods(reportDirectory, className);
+        // compute number of all methods in a class
+        final int allMethodsCnt = apiMethods.size();
+        // compute number of methods covered by tests
+        final int testedMethodsCnt = testedMethods.size();
+        // -> in percent
+        final float percentage = 100.0f*testedMethodsCnt / allMethodsCnt;
+        // table row background color is based on percentual test coverage ration
+        String backgroundColor = generateTableRowBackground(percentage);
+        String doc = DOC_BASE + "/" + className.replace('.', '/') + ".html";
+        // format output string
+        String outStr = String.format("<tr style='background-color:%s'><td><a target='ResultsFrame' 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><td style='text-align:right'><a href='%s' target='_blank'>ext</a></td></tr>",
+                backgroundColor, className, className,
+                Integer.valueOf(allMethodsCnt), Integer.valueOf(testedMethodsCnt), Float.valueOf(percentage), doc);
+        return outStr;
+    }
+
+    /**
      * Create new HTML file containing report for one tested class.
      *
      * @param reportDirectory
@@ -479,6 +499,17 @@
         return out;
     }
 
+    /**
+     * Create report for all packages. For each package, one package-name.html
+     * file is created containing table with basic coverage report.
+     * 
+     * @param reportDirectory
+     *            directory where report is generated
+     * @param usedPackageNames
+     *            packages for which the report is generated
+     * @param testedClasses
+     *            set of tested classes
+     */
     private static void printReportForAllPackages(String reportDirectory, Set<String> usedPackageNames,
                     Set<String> testedClasses)
     {
@@ -625,6 +656,13 @@
         return String.format("%.2f%%", ratio);
     }
 
+    /**
+     * Prepare whole report - HTML and txt one.
+     *
+     * @param allClassListFileName
+     * @param testedClassListFileName
+     * @param reportDirectory
+     */
     private static void prepareReport(String allClassListFileName, String testedClassListFileName,
                     String reportDirectory)
     {
--- a/templates/package_template.html	Tue Feb 21 17:16:21 2012 +0100
+++ b/templates/package_template.html	Fri Mar 02 16:51:17 2012 +0100
@@ -9,7 +9,13 @@
     </head>
     <body>
         <h1>Class list</h1>
+<h2>
+${PACKAGE_NAME}
+</h2>
+        <table class='classes_list'>
+            <tr><th>class</th><th>methods</th><th>covered</th><th>ratio</th><th>doc</th></tr>
 ${CLASS_LIST}
+        </table>
     </body>
 </html>