From 504fef747275c8578f44bfc49483b63e79c4d741 Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Fri, 11 Aug 2017 18:02:17 +0300 Subject: [PATCH] Fixes bullseye conditions to be shown in SQ UI Refactors CoverageMeasure to not be divided to line and condition type --- .../cxx/sensors/coverage/CoverageMeasure.java | 21 ++----- .../sensors/coverage/CoverageMeasures.java | 57 +++++++------------ .../sensors/coverage/CxxCoverageSensor.java | 13 ++--- 3 files changed, 29 insertions(+), 62 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasure.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasure.java index c278d43ff8..b5d24a1811 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasure.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasure.java @@ -25,21 +25,12 @@ */ class CoverageMeasure { - private int hits; - - public enum CoverageType - { - LINE, - CONDITION - } - - private final CoverageType type; private final int line; + private int hits; private int conditions; private int coveredConditions; - CoverageMeasure(CoverageType coverageType, int line) { - this.type = coverageType; + CoverageMeasure(int line) { this.line = line; } @@ -55,8 +46,8 @@ int getCoveredConditions() { return this.coveredConditions; } - void setHits(int lineId, int i) { - this.hits += i; + void setHits(int hits) { + this.hits += hits; } void setConditions(int totalConditions, int coveredConditions) { @@ -64,10 +55,6 @@ void setConditions(int totalConditions, int coveredConditions) { this.conditions = totalConditions; } - CoverageType getType() { - return this.type; - } - int getLine() { return this.line; } diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasures.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasures.java index 790bf14e11..1301220364 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasures.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageMeasures.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -33,8 +32,7 @@ * @author jocs */ class CoverageMeasures { - private final Map measuresLines = new HashMap<>(); - private final Map measuresConditions = new HashMap<>(); + private final Map lineMeasures = new HashMap<>(); private CoverageMeasures() { // empty @@ -44,54 +42,41 @@ static CoverageMeasures create() { return new CoverageMeasures(); } - void setHits(int lineId, int i) { - if (measuresLines.containsKey(lineId)) { - CoverageMeasure existentData = measuresLines.get(lineId); - existentData.setHits(lineId, i); - } else { - CoverageMeasure newLineHit = new CoverageMeasure(CoverageMeasure.CoverageType.LINE, lineId); - newLineHit.setHits(lineId, i); - measuresLines.put(lineId, newLineHit); - } + void setHits(int lineId, int hits) { + lineMeasures.computeIfAbsent(lineId, v -> new CoverageMeasure(lineId)); + CoverageMeasure coverageMeasure = lineMeasures.get(lineId); + coverageMeasure.setHits(hits); } void setConditions(int lineId, int totalConditions, int coveredConditions) { - if (measuresConditions.containsKey(lineId)) { - CoverageMeasure existentData = measuresConditions.get(lineId); - existentData.setConditions(totalConditions, coveredConditions); - } else { - CoverageMeasure newLineHit = new CoverageMeasure(CoverageMeasure.CoverageType.CONDITION, lineId); - newLineHit.setConditions(totalConditions, coveredConditions); - measuresConditions.put(lineId, newLineHit); - } + lineMeasures.computeIfAbsent(lineId, v -> new CoverageMeasure(lineId)); + CoverageMeasure coverageMeasure = lineMeasures.get(lineId); + coverageMeasure.setConditions(totalConditions, coveredConditions); } Collection getCoverageMeasures() { Map measures = new HashMap<>(); - measures.putAll(measuresLines); - measures.putAll(measuresConditions); + measures.putAll(lineMeasures); return measures.values(); } - public Set getCoveredLines() { + Set getCoveredLines() { Set coveredLines = Sets.newHashSet(); - measuresLines.entrySet().forEach( - (Entry line) -> { - if (line.getValue().getHits()!=0) { - coveredLines.add(line.getValue().getLine()); - } - }); + lineMeasures.forEach((key, value) -> { + if (value.getHits() != 0) { + coveredLines.add(value.getLine()); + } + }); return ImmutableSet.copyOf(coveredLines); } - public Set getCoveredConditions() { + Set getCoveredConditions() { Set coveredConditionLines = Sets.newHashSet(); - measuresConditions.entrySet().forEach( - (Entry line) -> { - if (line.getValue().getCoveredConditions()!=0) { - coveredConditionLines.add(line.getValue().getLine()); - } - }); + lineMeasures.forEach((key, value) -> { + if (value.getCoveredConditions() != 0) { + coveredConditionLines.add(value.getLine()); + } + }); return ImmutableSet.copyOf(coveredConditionLines); } } diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CxxCoverageSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CxxCoverageSensor.java index 98848d0c8e..f837609118 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CxxCoverageSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CxxCoverageSensor.java @@ -307,7 +307,7 @@ private void saveMeasures(SensorContext context, LOG.debug("Saving '{}' coverage measures for file '{}'", measures.size(), filePath); } - measures.forEach((CoverageMeasure measure) -> checkCoverage(newCoverage, measure, measure.getType())); + measures.forEach((CoverageMeasure measure) -> checkCoverage(newCoverage, measure)); try { newCoverage.save(); @@ -334,15 +334,11 @@ private void saveMeasures(SensorContext context, /** * @param newCoverage * @param measure - * @param type */ - private void checkCoverage(NewCoverage newCoverage, CoverageMeasure measure, CoverageMeasure.CoverageType type) { + private void checkCoverage(NewCoverage newCoverage, CoverageMeasure measure) { try { - if(type == CoverageMeasure.CoverageType.CONDITION) { - newCoverage.conditions(measure.getLine(), measure.getConditions(), measure.getCoveredConditions()); - } else { - newCoverage.lineHits(measure.getLine(), measure.getHits()); - } + newCoverage.lineHits(measure.getLine(), measure.getHits()); + newCoverage.conditions(measure.getLine(), measure.getConditions(), measure.getCoveredConditions()); } catch(RuntimeException ex) { LOG.error("Cannot save Conditions Hits for Line '{}' , ignoring measure. ", measure.getLine(), ex); @@ -350,7 +346,6 @@ private void checkCoverage(NewCoverage newCoverage, CoverageMeasure measure, Cov } } - private void warnUsageOfDeprecatedProperty(Settings settings, String reportPathProperty) { if (isSQ_6_2_or_newer && !settings.hasKey(getReportPathKey())) { LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", reportPathProperty, getReportPathKey());