# HG changeset patch # User Pavel Tisnovsky # Date 1338555110 -7200 # Node ID 6fb52e29ed2941b4d6e698dd4f6ab1d14483ae75 # Parent 8d326a855021a4a843fa92863687ccdfcd811d0d Fixed an issue which happens in the computation of test coverage in cases the test called some ancestor method(s). diff -r 8d326a855021 -r 6fb52e29ed29 ChangeLog --- 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 + + * 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 * src/ReportGenerator.java: diff -r 8d326a855021 -r 6fb52e29ed29 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); }