diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-07-18 11:07:22 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-07-18 15:23:54 +0200 |
commit | 5fddece10c608fa2acbe8d1de1d6c9fa2eab6e56 (patch) | |
tree | 6ed88112d7558c3ea796d9c9f081bfdb1be68c08 | |
parent | 7cdbfc9e015093f8f2c58b43a106e6b347728bcf (diff) | |
download | sonarqube-5fddece10c608fa2acbe8d1de1d6c9fa2eab6e56.tar.gz sonarqube-5fddece10c608fa2acbe8d1de1d6c9fa2eab6e56.zip |
SONAR-7876 Add SensorContext::isCancelled in the API
5 files changed, 37 insertions, 4 deletions
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(); @@ -166,6 +167,15 @@ public class SensorContextTester implements SensorContext { } @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<Issue> allIssues() { return sensorStorage.allIssues; } - + public Collection<AnalysisError> 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(); } } |