Skip to content

Commit

Permalink
Added Line Number - Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fperezpa committed Apr 4, 2020
1 parent 6e707de commit 46f3cb2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Sonarqube plugin:

## Release Notes

#### 1.0.0
##### Changes
- Added Line Number processing in Coverage Sensor

#### 0.0.11
##### Changes
- Added Rule Template.
Expand All @@ -74,7 +78,7 @@ Sonarqube plugin:
##### Changes
- Added property sonar.mule.ruleset.categories. It allows to filter ruleset categories to apply in the project.
Value should be a string separated by commas.
For example run, `mvn sonar:sonar -Dsonar.mule.ruleset.categories=flows` to only run the flows category ruleset
For example run, `mvn sonar:sonar -Dsonar.mule.ruleset.categories=flows` to only apply the flows category ruleset

#### 0.0.8
##### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Scanner;

Expand All @@ -29,15 +28,15 @@
public class CoverageSensor implements Sensor {

private final Logger logger = Loggers.get(CoverageSensor.class);

private static final String LANGUAGE_PROPERTY = "sonar.property.language";

private static final String MUNIT_NAME_PROPERTY = "mule.munit.properties.name";
private static final String MUNIT_FLOWS_PROPERTY = "mule.munit.properties.flows";
private static final String MUNIT_FILES_PROPERTY = "mule.munit.properties.files";
private static final String MUNIT_LINES_PROPERTY = "mule.munit.properties.lines";
private static final String MUNIT_COVERAGE_PROPERTY = "mule.munit.properties.coverage";
private static final String MUNIT_PROCESSOR_COUNT = "mule.munit.properties.processorCount";
private static final String MUNIT_COVERED_PROCESSOR_COUNT = "mule.munit.properties.coveredProcessorCount";
private static final String MUNIT_LINE_NUMBER = "mule.munit.properties.lineNumber";
private static final String MUNIT_COVERED = "mule.munit.properties.covered";

ObjectMapper objectMapper = new ObjectMapper();

Expand Down Expand Up @@ -93,6 +92,21 @@ private Map<String, FlowCoverageCounter> loadResults(Properties props, File muni
int coveredProcessorCount = flowNode.get(props.getProperty(MUNIT_COVERED_PROCESSOR_COUNT)).asInt();
counter.addProcessors(messageProcessorCount);
counter.addCoveredProcessors(coveredProcessorCount);
ArrayNode lines = (ArrayNode) flowNode.get(props.getProperty(MUNIT_LINES_PROPERTY));
if (lines != null) {
counter.setHasLineNumbers(true);
for (Iterator<JsonNode> linesIterator = lines.iterator(); linesIterator.hasNext();) {
JsonNode linesNode = linesIterator.next();
int lineNumber = linesNode.get(props.getProperty(MUNIT_LINE_NUMBER)).asInt();
boolean covered = linesNode.get(props.getProperty(MUNIT_COVERED)).asBoolean();

if (covered) {
counter.getCoveredLines().add(lineNumber);
} else {
counter.getNotcoveredLines().add(lineNumber);
}
}
}
}
String[] fileParts = name.split(File.separator);
coverageMap.put(fileParts[fileParts.length - 1], counter);
Expand All @@ -113,14 +127,28 @@ private void saveCoverage(Map<String, FlowCoverageCounter> coverageMap, String f
FlowCoverageCounter coverage = coverageMap.get(fileName);
if (coverage != null) {
NewCoverage newCoverage = context.newCoverage().onFile(file);
int processors = coverage.getProcessors();
int covered = coverage.getCoveredProcessors();

for (int i = 0; i < processors; i++) {
newCoverage.lineHits(i + 1, covered > 0 ? 1 : 0);
covered--;
if (coverage.hasLineNumbers()) {
for (Iterator<Integer> iterator = coverage.getCoveredLines().iterator(); iterator.hasNext();) {
Integer lineNumber = iterator.next();
newCoverage.lineHits(lineNumber, 1);
}
for (Iterator<Integer> iterator = coverage.getNotcoveredLines().iterator(); iterator.hasNext();) {
Integer lineNumber = iterator.next();
newCoverage.lineHits(lineNumber, 0);
}
newCoverage.save();
} else {
int processors = coverage.getProcessors();
int covered = coverage.getCoveredProcessors();

for (int i = 0; i < processors; i++) {
newCoverage.lineHits(i + 1, covered > 0 ? 1 : 0);
covered--;
}

newCoverage.save();
}
newCoverage.save();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.mulesoft.services.tools.sonarqube.metrics;

import java.util.ArrayList;

public class FlowCoverageCounter {

private int processors = 0;
private int coveredProcessors = 0;
private boolean hasLineNumbers = false;
private ArrayList<Integer> coveredLines = new ArrayList<Integer>();
private ArrayList<Integer> notcoveredLines = new ArrayList<Integer>();

public void addProcessors(int processors) {
this.processors += processors;
Expand All @@ -20,4 +25,28 @@ public int getCoveredProcessors() {
public int getProcessors() {
return processors;
}

public void setCoveredLines(ArrayList<Integer> coveredLines) {
this.coveredLines = coveredLines;
}

public ArrayList<Integer> getCoveredLines() {
return coveredLines;
}

public ArrayList<Integer> getNotcoveredLines() {
return notcoveredLines;
}

public void setNotcoveredLines(ArrayList<Integer> notcoveredLines) {
this.notcoveredLines = notcoveredLines;
}

public void setHasLineNumbers(boolean hasLineNumbers) {
this.hasLineNumbers = hasLineNumbers;
}

public boolean hasLineNumbers() {
return hasLineNumbers;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/mule3.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mule.munit.properties.files=files
mule.munit.properties.coverage=coverage
mule.munit.properties.processorCount=messageProcessorCount
mule.munit.properties.coveredProcessorCount=coveredMessageProcessorCount
mule.munit.properties.lines=lines
mule.munit.properties.lineNumber=lineNumber
mule.munit.properties.covered=covered

## METRICS

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/mule4.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mule.munit.properties.files=files
mule.munit.properties.coverage=coverage
mule.munit.properties.processorCount=messageProcessorCount
mule.munit.properties.coveredProcessorCount=coveredProcessorCount
mule.munit.properties.lines=lines
mule.munit.properties.lineNumber=lineNumber
mule.munit.properties.covered=covered

## METRICS

Expand Down

0 comments on commit 46f3cb2

Please sign in to comment.