From 46f3cb2c4b3d0b31f2d0b33d8de9248941f34090 Mon Sep 17 00:00:00 2001 From: Franco Perez Date: Sat, 4 Apr 2020 16:59:17 -0300 Subject: [PATCH] Added Line Number - Coverage --- README.md | 6 ++- .../sonarqube/metrics/CoverageSensor.java | 48 +++++++++++++++---- .../metrics/FlowCoverageCounter.java | 29 +++++++++++ src/main/resources/mule3.properties | 3 ++ src/main/resources/mule4.properties | 3 ++ 5 files changed, 78 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 952b6b4..f33e311 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/CoverageSensor.java b/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/CoverageSensor.java index 96962ac..9cdcafc 100644 --- a/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/CoverageSensor.java +++ b/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/CoverageSensor.java @@ -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; @@ -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(); @@ -93,6 +92,21 @@ private Map 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 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); @@ -113,14 +127,28 @@ private void saveCoverage(Map 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 iterator = coverage.getCoveredLines().iterator(); iterator.hasNext();) { + Integer lineNumber = iterator.next(); + newCoverage.lineHits(lineNumber, 1); + } + for (Iterator 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(); } } diff --git a/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/FlowCoverageCounter.java b/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/FlowCoverageCounter.java index de0b9c6..b613ef2 100644 --- a/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/FlowCoverageCounter.java +++ b/src/main/java/com/mulesoft/services/tools/sonarqube/metrics/FlowCoverageCounter.java @@ -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 coveredLines = new ArrayList(); + private ArrayList notcoveredLines = new ArrayList(); public void addProcessors(int processors) { this.processors += processors; @@ -20,4 +25,28 @@ public int getCoveredProcessors() { public int getProcessors() { return processors; } + + public void setCoveredLines(ArrayList coveredLines) { + this.coveredLines = coveredLines; + } + + public ArrayList getCoveredLines() { + return coveredLines; + } + + public ArrayList getNotcoveredLines() { + return notcoveredLines; + } + + public void setNotcoveredLines(ArrayList notcoveredLines) { + this.notcoveredLines = notcoveredLines; + } + + public void setHasLineNumbers(boolean hasLineNumbers) { + this.hasLineNumbers = hasLineNumbers; + } + + public boolean hasLineNumbers() { + return hasLineNumbers; + } } diff --git a/src/main/resources/mule3.properties b/src/main/resources/mule3.properties index 5be20f5..5dab45f 100644 --- a/src/main/resources/mule3.properties +++ b/src/main/resources/mule3.properties @@ -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 diff --git a/src/main/resources/mule4.properties b/src/main/resources/mule4.properties index 02cfce0..3b76f62 100644 --- a/src/main/resources/mule4.properties +++ b/src/main/resources/mule4.properties @@ -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