aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJulien HENRY <henryju@yahoo.fr>2016-12-14 17:45:59 +0100
committerJulien HENRY <henryju@yahoo.fr>2016-12-15 11:01:40 +0100
commit4377c049eab9efbf8ecb56caf1fd3c0c779a0731 (patch)
treef34e2df27ee7d859441b82c35bb174a23705233d /sonar-scanner-engine
parentda66f798788cb21053032004f9439b80ca5f13aa (diff)
downloadsonarqube-4377c049eab9efbf8ecb56caf1fd3c0c779a0731.tar.gz
sonarqube-4377c049eab9efbf8ecb56caf1fd3c0c779a0731.zip
SONAR-8540 Fix saving of raw IT/Overall coverage measures
and improve coverage
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java21
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java115
2 files changed, 126 insertions, 10 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
index 1a14f1882e8..908c5320e55 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
@@ -24,8 +24,8 @@ import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
-import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -33,8 +33,6 @@ import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
@@ -54,6 +52,8 @@ import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.KeyValueFormat;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.core.metric.ScannerMetrics;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.internal.pmd.PmdBlockChunker;
@@ -118,7 +118,7 @@ import static org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES;
public class DefaultSensorStorage implements SensorStorage {
- private static final Logger LOG = LoggerFactory.getLogger(DefaultSensorStorage.class);
+ private static final Logger LOG = Loggers.get(DefaultSensorStorage.class);
private static final List<String> DEPRECATED_METRICS_KEYS = Arrays.asList(
DEPENDENCY_MATRIX_KEY,
@@ -153,7 +153,7 @@ public class DefaultSensorStorage implements SensorStorage {
private final ContextPropertiesCache contextPropertiesCache;
private final Settings settings;
private final ScannerMetrics scannerMetrics;
- private final Map<Metric<?>, Metric<?>> deprecatedCoverageMetricMapping = new IdentityHashMap<>();
+ private final Map<Metric<?>, Metric<?>> deprecatedCoverageMetricMapping = new HashMap<>();
private final Set<Metric<?>> coverageMetrics = new HashSet<>();
private final Set<Metric<?>> byLineMetrics = new HashSet<>();
private Set<String> alreadyLogged = new HashSet<>();
@@ -233,8 +233,15 @@ public class DefaultSensorStorage implements SensorStorage {
logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube. Provided value is ignored.", measure.metric().key());
return;
}
+ DefaultMeasure measureToSave;
if (deprecatedCoverageMetricMapping.containsKey(metric)) {
metric = deprecatedCoverageMetricMapping.get(metric);
+ measureToSave = new DefaultMeasure<>()
+ .forMetric((Metric) metric)
+ .on(measure.inputComponent())
+ .withValue(measure.value());
+ } else {
+ measureToSave = measure;
}
if (!scannerMetrics.getMetrics().contains(metric)) {
throw new UnsupportedOperationException("Metric '" + metric.key() + "' should not be computed by a Sensor");
@@ -249,12 +256,12 @@ public class DefaultSensorStorage implements SensorStorage {
if (coverageExclusions.isExcluded((InputFile) component)) {
return;
}
- saveCoverageMetricInternal((InputFile) component, metric, measure);
+ saveCoverageMetricInternal((InputFile) component, metric, measureToSave);
} else {
if (measureCache.contains(component.key(), metric.key())) {
throw new UnsupportedOperationException("Can not add the same measure twice on " + component + ": " + measure);
}
- measureCache.put(component.key(), metric.key(), measure);
+ measureCache.put(component.key(), metric.key(), measureToSave);
}
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java
index c2f6158a808..11da3d60ee2 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java
@@ -27,9 +27,12 @@ import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.scanner.mediumtest.BatchMediumTester;
import org.sonar.scanner.mediumtest.TaskResult;
import org.sonar.scanner.protocol.output.ScannerReport.Measure;
@@ -37,10 +40,14 @@ import org.sonar.xoo.XooPlugin;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
+import static org.junit.Assert.fail;
public class MeasuresMediumTest {
- @org.junit.Rule
+ @Rule
+ public LogTester logTester = new LogTester();
+
+ @Rule
public TemporaryFolder temp = new TemporaryFolder();
private File baseDir;
@@ -62,8 +69,8 @@ public class MeasuresMediumTest {
}
@Before
- public void setUp() {
- baseDir = temp.getRoot();
+ public void setUp() throws Exception {
+ baseDir = temp.newFolder();
srcDir = new File(baseDir, "src");
srcDir.mkdir();
}
@@ -126,6 +133,108 @@ public class MeasuresMediumTest {
}
@Test
+ public void applyExclusionsOnCoverageMeasures() throws IOException {
+ File xooFile = new File(srcDir, "sample.xoo");
+ FileUtils.write(xooFile, "Sample xoo\n\ncontent");
+
+ File measures = new File(srcDir, "sample.xoo.measures");
+ FileUtils.write(measures, "lines_to_cover:2");
+
+ TaskResult result = tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.task", "scan")
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+
+ Map<String, List<Measure>> allMeasures = result.allMeasures();
+
+ assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue.value")
+ .containsOnly(tuple("lines", 3),
+ tuple("lines_to_cover", 2));
+
+ result = tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.task", "scan")
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .put("sonar.coverage.exclusions", "src/sample.xoo")
+ .build())
+ .start();
+
+ allMeasures = result.allMeasures();
+ assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue.value")
+ .containsOnly(tuple("lines", 3));
+ }
+
+ @Test
+ public void deprecatedCoverageMeasuresAreConverted() throws IOException {
+ File xooFile = new File(srcDir, "sample.xoo");
+ FileUtils.write(xooFile, "Sample xoo\n\ncontent");
+
+ File measures = new File(srcDir, "sample.xoo.measures");
+ FileUtils.write(measures, "it_lines_to_cover:2");
+
+ TaskResult result = tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.task", "scan")
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+
+ Map<String, List<Measure>> allMeasures = result.allMeasures();
+
+ assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue.value")
+ .containsOnly(tuple("lines", 3),
+ tuple("lines_to_cover", 2));
+
+ assertThat(logTester.logs(LoggerLevel.WARN))
+ .contains("Coverage measure for metric 'lines_to_cover' should not be saved directly by a Sensor. Plugin should be updated to use SensorContext::newCoverage instead.");
+ }
+
+ @Test
+ public void failIfTryingToSaveServerSideMeasure() throws IOException {
+ File xooFile = new File(srcDir, "sample.xoo");
+ FileUtils.write(xooFile, "Sample xoo\n\ncontent");
+
+ File measures = new File(srcDir, "sample.xoo.measures");
+ FileUtils.write(measures, "new_lines:2");
+
+ try {
+ tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.task", "scan")
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+ fail("Expected exception");
+ } catch (Exception e) {
+ assertThat(e)
+ .hasCauseInstanceOf(UnsupportedOperationException.class)
+ .hasStackTraceContaining("Metric 'new_lines' should not be computed by a Sensor");
+ }
+ }
+
+ @Test
public void lineMeasures() throws IOException {
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\n\n\ncontent");