changeset 18:6fb52e29ed29 draft

Fixed an issue which happens in the computation of test coverage in cases the test called some ancestor method(s).
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Fri, 01 Jun 2012 14:51:50 +0200
parents 8d326a855021
children 7bb9210fb192
files ChangeLog src/ReportGenerator.java
diffstat 2 files changed, 41 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu May 24 16:55:09 2012 +0200
+++ b/ChangeLog	Fri Jun 01 14:51:50 2012 +0200
@@ -1,3 +1,9 @@
+2012-06-01  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/ReportGenerator.java:
+	Fixed an issue which happens in the computation of test coverage in
+	cases the test called some ancestor method(s).
+
 2012-05-24  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/ReportGenerator.java:
--- a/src/ReportGenerator.java	Thu May 24 16:55:09 2012 +0200
+++ b/src/ReportGenerator.java	Fri Jun 01 14:51:50 2012 +0200
@@ -226,7 +226,7 @@
             // compute number of classes covered by tests
             final int testedClassesCnt = numberOfTestedClassesInPackage(packageName, testedClasses, classInfoMap);
             // -> in percent
-            final float classPercentage = allClassesCnt == 0 ? 0.0f : 100.0f * testedClassesCnt / allClassesCnt;
+            final float classPercentage = computePercentage(allClassesCnt, testedClassesCnt);
             // table row background color is based on percentual test coverage ration
             String backgroundColor1 = generateTableRowBackground(classPercentage);
 
@@ -242,7 +242,7 @@
                     coveragedMethodsCnt += classInfo.getTestedMethods().size();
                 }
             }
-            float methodsPercentage = allMethodsCnt == 0 ? 0.0f : 100.0f * coveragedMethodsCnt / allMethodsCnt;
+            final float methodsPercentage = computePercentage(allMethodsCnt, coveragedMethodsCnt);
             // table row background color is based on percentual test coverage ration
             String backgroundColor2 = generateTableRowBackground(methodsPercentage);
 
@@ -413,7 +413,7 @@
         // compute number of methods covered by tests
         final int testedMethodsCnt = testedMethods.size();
         // -> in percent
-        final float percentage = 100.0f*testedMethodsCnt / allMethodsCnt;
+        final float percentage = computePercentage(allMethodsCnt, testedMethodsCnt);
 
         // construct CSS class for given row based on a code coverage
         final long coverage = Math.round(Math.ceil(percentage / 10.0f) * 10);
@@ -430,6 +430,30 @@
     }
 
     /**
+     * Compute number of methods covered by tests
+     * 
+     * @param allMethodsCnt
+     *            number of all methods in a class
+     * @param testedMethodsCnt
+     *            number of tested methods
+     * @return test coverage in percents (0..100)
+     */
+    private static float computePercentage(final int allMethodsCnt, final int testedMethodsCnt)
+    {
+        // avoid division by zero
+        if (allMethodsCnt == 0)
+        {
+            return 0.0f;
+        }
+        // we tested some methods which are part of class ancestor(s)
+        if (testedMethodsCnt >= allMethodsCnt)
+        {
+            return 100.0f;
+        }
+        return 100.0f * testedMethodsCnt / allMethodsCnt;
+    }
+
+    /**
      * Create new HTML file containing report for one tested class.
      *
      * @param reportDirectory
@@ -668,7 +692,14 @@
     @SuppressWarnings("boxing")
     private static String calcRatio(int numberOfTestedItems, int numberOfAllItems)
     {
-        float ratio = 100.0f * numberOfTestedItems / numberOfAllItems;
+        float ratio = computePercentage(numberOfAllItems, numberOfTestedItems);
+        // sometimes the ratio could be greater than 100%
+        // one such case is a calling some method defined in the
+        // parent of tested class
+        if (ratio > 100.0f)
+        {
+            ratio = 100.0f;
+        }
         return String.format("%.2f%%", ratio);
     }