Skip to content

Commit

Permalink
Fixes bullseye conditions to be shown in SQ UI
Browse files Browse the repository at this point in the history
Refactors CoverageMeasure to not be divided to line and condition type
  • Loading branch information
Pavel Pavlov committed Sep 25, 2017
1 parent ad4d183 commit 504fef7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -55,19 +46,15 @@ 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) {
this.coveredConditions = coveredConditions;
this.conditions = totalConditions;
}

CoverageType getType() {
return this.type;
}

int getLine() {
return this.line;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,8 +32,7 @@
* @author jocs
*/
class CoverageMeasures {
private final Map<Integer, CoverageMeasure> measuresLines = new HashMap<>();
private final Map<Integer, CoverageMeasure> measuresConditions = new HashMap<>();
private final Map<Integer, CoverageMeasure> lineMeasures = new HashMap<>();

private CoverageMeasures() {
// empty
Expand All @@ -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<CoverageMeasure> getCoverageMeasures() {
Map<Integer, CoverageMeasure> measures = new HashMap<>();
measures.putAll(measuresLines);
measures.putAll(measuresConditions);
measures.putAll(lineMeasures);
return measures.values();
}

public Set<Integer> getCoveredLines() {
Set<Integer> getCoveredLines() {
Set<Integer> coveredLines = Sets.newHashSet();
measuresLines.entrySet().forEach(
(Entry<Integer, CoverageMeasure> 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<Integer> getCoveredConditions() {
Set<Integer> getCoveredConditions() {
Set<Integer> coveredConditionLines = Sets.newHashSet();
measuresConditions.entrySet().forEach(
(Entry<Integer, CoverageMeasure> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -334,23 +334,18 @@ 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);
CxxUtils.validateRecovery(ex, this.language);
}
}


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());
Expand Down

0 comments on commit 504fef7

Please sign in to comment.