From: Julien HENRY Date: Mon, 18 Jul 2016 09:07:22 +0000 (+0200) Subject: SONAR-7876 Add SensorContext::isCancelled in the API X-Git-Tag: 6.0-RC1~16 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F1106%2Fhead;p=sonarqube.git SONAR-7876 Add SensorContext::isCancelled in the API --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index c2852681f60..9612521b23f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -82,6 +82,13 @@ public interface SensorContext { */ SonarProduct getRuntimeProduct(); + /** + * Test if a cancellation of the analysis was requested. Sensors should periodically test this flag + * and gracefully stop if value is true. For example it could be tested between each processed file. + * @since 6.0 + */ + boolean isCancelled(); + // ----------- MEASURES -------------- /** @@ -111,7 +118,7 @@ public interface SensorContext { */ NewSymbolTable newSymbolTable(); - // ------------ TESTS ------------ + // ------------ COVERAGE ------------ /** * Builder to define coverage in a file. @@ -128,10 +135,13 @@ public interface SensorContext { */ NewCpdTokens newCpdTokens(); + // ------------ ANALYSIS ERROR ------------ + /** * Builder to declare errors that happened while processing a source file. * Don't forget to call {@link NewAnalisisError#save()}. * @since 6.0 */ NewAnalysisError newAnalysisError(); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 1b274aaecf8..a2c8cd2ac26 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -93,6 +93,7 @@ public class SensorContextTester implements SensorContext { private InMemorySensorStorage sensorStorage; private InputModule module; private SonarQubeVersion sqVersion; + private boolean cancelled; private SensorContextTester(Path moduleBaseDir) { this.settings = new Settings(); @@ -165,6 +166,15 @@ public class SensorContextTester implements SensorContext { return this; } + @Override + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + @Override public InputModule module() { return module; @@ -195,7 +205,7 @@ public class SensorContextTester implements SensorContext { public Collection allIssues() { return sensorStorage.allIssues; } - + public Collection allAnalysisErrors() { return sensorStorage.allAnalysisErrors; } @@ -252,7 +262,7 @@ public class SensorContextTester implements SensorContext { public NewSymbolTable newSymbolTable() { return new DefaultSymbolTable(sensorStorage); } - + @Override public NewAnalysisError newAnalysisError() { return new DefaultAnalysisError(sensorStorage); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 46fe16802a1..a4dad0ea510 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -330,4 +330,11 @@ public class SensorContextTesterTest { .addToken(inputFile.newRange(0, 6), "public") .save(); } + + @Test + public void testCacnellation() { + assertThat(tester.isCancelled()).isFalse(); + tester.setCancelled(true); + assertThat(tester.isCancelled()).isTrue(); + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java index a2e4ca8f932..d3cb867c509 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java @@ -153,4 +153,9 @@ public class DefaultSensorContext implements SensorContext { return NO_OP_NEW_ANALYSIS_ERROR; } + @Override + public boolean isCancelled() { + return false; + } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java index a7c5e59efb5..edcdaf576fb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java @@ -37,7 +37,6 @@ import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.Version; -import org.sonar.scanner.sensor.DefaultSensorContext; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -83,6 +82,8 @@ public class DefaultSensorContextTest { assertThat(adaptor.newIssue()).isNotNull(); assertThat(adaptor.newMeasure()).isNotNull(); + + assertThat(adaptor.isCancelled()).isFalse(); } }