aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-06-29 16:33:48 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-07-04 23:47:46 +0200
commit4ad21e8bc6085cb13d16e5b7e68b475296833bd2 (patch)
treecca58ff1b5f42253b99512fa08dc9dd5a75079d6 /sonar-scanner-engine
parent875e23e29f4ed2d41146d8bba8b22448b23df113 (diff)
downloadsonarqube-4ad21e8bc6085cb13d16e5b7e68b475296833bd2.tar.gz
sonarqube-4ad21e8bc6085cb13d16e5b7e68b475296833bd2.zip
SONAR-8546 Improve SensorDescriptor API to give more flexibility on configuration requirements to execute Sensors
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageSensor.java50
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensor.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorOptimizer.java8
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java8
4 files changed, 31 insertions, 37 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageSensor.java
index cf0158cb718..94410acb9ea 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageSensor.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericCoverageSensor.java
@@ -25,7 +25,6 @@ import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
-import org.sonar.api.batch.Initializer;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
@@ -35,9 +34,10 @@ import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import static java.util.Arrays.asList;
import static org.sonar.api.CoreProperties.CATEGORY_CODE_COVERAGE;
-public class GenericCoverageSensor extends Initializer implements Sensor {
+public class GenericCoverageSensor implements Sensor {
private static final Logger LOG = Loggers.get(GenericCoverageSensor.class);
@@ -63,10 +63,10 @@ public class GenericCoverageSensor extends Initializer implements Sensor {
@Deprecated
static final String OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY = "sonar.genericcoverage.overallReportPaths";
- private final Configuration settings;
+ private final Configuration config;
- public GenericCoverageSensor(Configuration settings) {
- this.settings = settings;
+ public GenericCoverageSensor(Configuration config) {
+ this.config = config;
}
public static ImmutableList<PropertyDefinition> properties() {
@@ -82,39 +82,27 @@ public class GenericCoverageSensor extends Initializer implements Sensor {
}
- /**
- * Use an initializer to migrate old properties to the new one before Sensor phase so that
- * Sensor will not be executed if there is no report (thanks to SensorDescriptor.requireProperty(REPORT_PATH_PROPERTY_KEY))
- */
- @Override
- public void execute() {
- Set<String> reportPaths = new LinkedHashSet<>();
- reportPaths.addAll(Arrays.asList(settings.getStringArray(REPORT_PATHS_PROPERTY_KEY)));
- loadDeprecated(reportPaths, OLD_REPORT_PATH_PROPERTY_KEY);
- loadDeprecated(reportPaths, OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
- loadDeprecated(reportPaths, OLD_IT_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
- loadDeprecated(reportPaths, OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
- if (!reportPaths.isEmpty()) {
- // settings.setProperty(REPORT_PATHS_PROPERTY_KEY, reportPaths.stream().collect(Collectors.joining(",")));
- }
- }
-
private void loadDeprecated(Set<String> reportPaths, String propertyKey) {
- if (settings.hasKey(propertyKey)) {
+ if (config.hasKey(propertyKey)) {
LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", propertyKey, REPORT_PATHS_PROPERTY_KEY);
- reportPaths.addAll(Arrays.asList(settings.getStringArray(propertyKey)));
+ reportPaths.addAll(Arrays.asList(config.getStringArray(propertyKey)));
}
}
@Override
public void describe(SensorDescriptor descriptor) {
descriptor.name("Generic Coverage Report")
- .requireProperty(REPORT_PATHS_PROPERTY_KEY);
+ .onlyWhenConfiguration(c -> asList(REPORT_PATHS_PROPERTY_KEY, OLD_REPORT_PATH_PROPERTY_KEY, OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY,
+ OLD_IT_COVERAGE_REPORT_PATHS_PROPERTY_KEY, OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY)
+ .stream()
+ .anyMatch(c::hasKey));
}
@Override
public void execute(SensorContext context) {
- for (String reportPath : settings.getStringArray(REPORT_PATHS_PROPERTY_KEY)) {
+ Set<String> reportPaths = loadReportPaths();
+
+ for (String reportPath : reportPaths) {
File reportFile = context.fileSystem().resolvePath(reportPath);
LOG.info("Parsing {}", reportFile);
GenericCoverageReportParser parser = new GenericCoverageReportParser();
@@ -128,4 +116,14 @@ public class GenericCoverageSensor extends Initializer implements Sensor {
}
+ Set<String> loadReportPaths() {
+ Set<String> reportPaths = new LinkedHashSet<>();
+ reportPaths.addAll(Arrays.asList(config.getStringArray(REPORT_PATHS_PROPERTY_KEY)));
+ loadDeprecated(reportPaths, OLD_REPORT_PATH_PROPERTY_KEY);
+ loadDeprecated(reportPaths, OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
+ loadDeprecated(reportPaths, OLD_IT_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
+ loadDeprecated(reportPaths, OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY);
+ return reportPaths;
+ }
+
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensor.java
index 0f4bae6396d..a2f9fbffe17 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensor.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensor.java
@@ -66,7 +66,7 @@ public class GenericTestExecutionSensor implements Sensor {
@Override
public void describe(SensorDescriptor descriptor) {
descriptor.name("Generic Test Executions Report")
- .requireProperty(REPORT_PATHS_PROPERTY_KEY);
+ .onlyWhenConfiguration(conf -> conf.hasKey(REPORT_PATHS_PROPERTY_KEY));
}
@Override
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorOptimizer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorOptimizer.java
index 8f8907dbb9a..3f4c43cc8c9 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorOptimizer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorOptimizer.java
@@ -63,12 +63,8 @@ public class SensorOptimizer {
}
private boolean settingsCondition(DefaultSensorDescriptor descriptor) {
- if (!descriptor.properties().isEmpty()) {
- for (String propertyKey : descriptor.properties()) {
- if (!config.hasKey(propertyKey)) {
- return false;
- }
- }
+ if (descriptor.configurationPredicate() != null) {
+ return descriptor.configurationPredicate().test(config);
}
return true;
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
index a23a5d6499f..7fb9d002655 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
@@ -19,10 +19,10 @@
*/
package org.sonar.scanner.genericcoverage;
+import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
@@ -36,19 +36,19 @@ public class GenericCoverageSensorTest {
@Test
public void migrateOldProperties() {
- Settings settings = new MapSettings(new PropertyDefinitions(GenericCoverageSensor.properties()));
+ MapSettings settings = new MapSettings(new PropertyDefinitions(GenericCoverageSensor.properties()));
settings.setProperty(GenericCoverageSensor.OLD_REPORT_PATH_PROPERTY_KEY, "old.xml");
settings.setProperty(GenericCoverageSensor.OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old1.xml,old2.xml");
settings.setProperty(GenericCoverageSensor.OLD_IT_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old3.xml,old4.xml,old.xml");
settings.setProperty(GenericCoverageSensor.OLD_OVERALL_COVERAGE_REPORT_PATHS_PROPERTY_KEY, "old5.xml,old6.xml");
- new GenericCoverageSensor(settings).execute();
+ Set<String> reportPaths = new GenericCoverageSensor(settings.asConfig()).loadReportPaths();
assertThat(logTester.logs(LoggerLevel.WARN)).contains(
"Property 'sonar.genericcoverage.reportPath' is deprecated. Please use 'sonar.coverageReportPaths' instead.",
"Property 'sonar.genericcoverage.reportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead.",
"Property 'sonar.genericcoverage.itReportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead.",
"Property 'sonar.genericcoverage.overallReportPaths' is deprecated. Please use 'sonar.coverageReportPaths' instead.");
- assertThat(settings.getStringArray(GenericCoverageSensor.REPORT_PATHS_PROPERTY_KEY)).containsOnly(
+ assertThat(reportPaths).containsOnly(
"old.xml", "old1.xml", "old2.xml", "old3.xml", "old4.xml", "old5.xml", "old6.xml");
}