]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7876 Add SensorContext::isCancelled in the API 1106/head
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 18 Jul 2016 09:07:22 +0000 (11:07 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 18 Jul 2016 13:23:54 +0000 (15:23 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorContextTest.java

index c2852681f6066ac0774368de0edb828f80da426c..9612521b23f82e2435f56ceef149efe65479ad84 100644 (file)
@@ -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();
+
 }
index 1b274aaecf8abfd1c605e0479e93f8dc9cea0d93..a2c8cd2ac2696017d1ddec09686049df5e78a9bd 100644 (file)
@@ -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<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);
index 46fe16802a1a847829f493763f3ae9aa87322575..a4dad0ea510bdf05a8a5a1d28e82a8b1c093cdeb 100644 (file)
@@ -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();
+  }
 }
index a2e4ca8f9325460675950bea08dc0d18b1501809..d3cb867c509941f5bc4926318bdef4205201e758 100644 (file)
@@ -153,4 +153,9 @@ public class DefaultSensorContext implements SensorContext {
     return NO_OP_NEW_ANALYSIS_ERROR;
   }
 
+  @Override
+  public boolean isCancelled() {
+    return false;
+  }
+
 }
index a7c5e59efb559383f39d4842e9da9cdb42f79629..edcdaf576fbb87f6515a9de4b7a95d6b26948e38 100644 (file)
@@ -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();
   }
 
 }