Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #27 from Tamini/develop
Browse files Browse the repository at this point in the history
[JENKINS-22412] : Portlet view has missing and wrongly aligned columns when a metric has no data.
  • Loading branch information
Tamini committed Apr 12, 2014
2 parents 1355378 + 0ca769d commit 7d97703
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
50 changes: 46 additions & 4 deletions src/main/java/hudson/plugins/cobertura/targets/CoverageResult.java
Expand Up @@ -15,11 +15,13 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -327,18 +329,58 @@ public CoverageResult getChild(String name) {
}

public Ratio getCoverage(CoverageMetric metric) {

return aggregateResults.get(metric);
}


public Ratio getCoverageWithEmpty(CoverageMetric metric) {
if (aggregateResults.containsKey(metric))
return aggregateResults.get(metric);
Map<CoverageMetric, Ratio> currMetricSet = new EnumMap<CoverageMetric, Ratio>(CoverageMetric.class);
currMetricSet.putAll(aggregateResults);
if (!currMetricSet.containsKey(metric))
{
return null;
}
return currMetricSet.get(metric);
}

/**
* Getter for property 'metrics'.
*
* @return Value for property 'metrics'.
*/
public Set<CoverageMetric> getMetrics() {
return Collections.unmodifiableSet(EnumSet.copyOf(aggregateResults.keySet()));
}

return Collections.unmodifiableSet(EnumSet.copyOf(aggregateResults.keySet()));
}

public Set<CoverageMetric> getMetricsWithEmpty() {
Map<CoverageMetric, Ratio> currMetricSet = new EnumMap<CoverageMetric, Ratio>(CoverageMetric.class);
currMetricSet.putAll(aggregateResults);
fixEmptyMetrics(findEmptyMetrics(currMetricSet), currMetricSet);
return Collections.unmodifiableSet(EnumSet.copyOf(currMetricSet.keySet()));
}

private List<CoverageMetric> findEmptyMetrics(Map<CoverageMetric, Ratio> currMetricSet){
List<CoverageMetric> allMetrics = new LinkedList<CoverageMetric>(Arrays.asList(CoverageMetric.PACKAGES, CoverageMetric.FILES, CoverageMetric.CLASSES, CoverageMetric.METHOD, CoverageMetric.LINE, CoverageMetric.CONDITIONAL));
List<CoverageMetric> missingMetrics = new LinkedList<CoverageMetric>();
for (CoverageMetric currMetric : allMetrics)
{
if (!currMetricSet.containsKey(currMetric.getName()))
{
missingMetrics.add(currMetric);
}
}
return missingMetrics;
}

private void fixEmptyMetrics(List<CoverageMetric> missingMetrics, Map<CoverageMetric, Ratio> currMetricSet) {
for (CoverageMetric missing : missingMetrics)
{
currMetricSet.put(missing, Ratio.create(1, 1));
}
}

public void updateMetric(CoverageMetric metric, Ratio additionalResult) {
if (localResults.containsKey(metric)) {
Ratio existingResult = localResults.get(metric);
Expand Down
Expand Up @@ -45,7 +45,7 @@ THE SOFTWARE.
<tbody>
<j:forEach var="r" items="${coverageRuns}">
<j:set var="result" value="${it.getCoverageResult(r)}"/>
<j:set var="metrics" value="${result.metrics}"/>
<j:set var="metrics" value="${result.metricsWithEmpty}"/>
<tr style="border-top: 0px; border: 1px #bbb solid;">
<td><t:buildLink jobName="${r.parent.name}" job="${r.parent}" number="${r.number}" href="${r.url}" /></td>
<j:choose>
Expand All @@ -54,18 +54,32 @@ THE SOFTWARE.
</j:when>
<j:otherwise>
<j:forEach var="metric" items="${metrics}">
<td class="pane" style="text-align: right;" data="${result.getCoverage(metric)}"> ${result.getCoverage(metric).percentage}%</td>
<j:choose>
<j:when test="${empty(result.getCoverageWithEmpty(metric).percentage)}">
<td class="pane" style="text-align: right;" data="${result.getCoverageWithEmpty(metric)}">${%N/A}</td>
</j:when>
<j:otherwise>
<td class="pane" style="text-align: right;" data="${result.getCoverageWithEmpty(metric)}"> ${result.getCoverageWithEmpty(metric).percentage}%</td>
</j:otherwise>
</j:choose>
</j:forEach>
</j:otherwise>
</j:choose>
</tr>
</j:forEach>
<tr class="sortbottom">
<j:set var="result" value="${it.getCoverageResult(coverageRuns[0])}"/>
<j:set var="metrics" value="${result.metrics}"/>
<j:set var="metrics" value="${result.metricsWithEmpty}"/>
<td class="pane-header"><b>${%Total}:</b></td>
<j:forEach var="metric" items="${metrics}">
<td class="pane-header" style="text-align: right;"> ${it.getTotalCoverageRatio().get(metric).percentage}%</td>
<j:choose>
<j:when test="${empty(it.getTotalCoverageRatio().get(metric).percentage)}">
<td class="pane-header" style="text-align: right;">${%N/A}</td>
</j:when>
<j:otherwise>
<td class="pane-header" style="text-align: right;">${it.getTotalCoverageRatio().get(metric).percentage}%</td>
</j:otherwise>
</j:choose>
</j:forEach>
</tr>
</tbody>
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/hudson/plugins/cobertura/CoverageResultTest.java
Expand Up @@ -5,7 +5,9 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.easymock.classextension.EasyMock;
import org.easymock.classextension.IMocksControl;
Expand Down Expand Up @@ -79,6 +81,18 @@ public void testGetResults() throws Exception {
ctl.verify();
}

/**
* Test behavior of {@link CoverageResult#getMetricsWithEmpty()}.
*/
public void testGetMetricsWithEmpty() throws Exception {
ctl.replay();
CoverageResult result = loadResults("coverage-no-data.xml");
Set<CoverageMetric> metrics = result.getMetricsWithEmpty();
List<CoverageMetric> allMetrics = new LinkedList<CoverageMetric>(Arrays.asList(CoverageMetric.PACKAGES, CoverageMetric.FILES, CoverageMetric.CLASSES, CoverageMetric.METHOD, CoverageMetric.LINE, CoverageMetric.CONDITIONAL));
assertEquals(metrics.size(), allMetrics.size());
ctl.verify();
}

/**
* Tests the behavior of {@link CoverageResult#getParent()}.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/hudson/plugins/cobertura/coverage-no-data.xml
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-03.dtd"-->

<coverage line-rate="0.0" branch-rate="0.0" version="1.9" timestamp="1187353747005">
<sources>
<source>C:/local/mvn-coverage-example/src/main/java</source>
</sources>
<packages/>
<classes/>
<methods/>
<lines/>
</coverage>

0 comments on commit 7d97703

Please sign in to comment.