aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api-impl
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2022-03-14 11:28:22 -0500
committersonartech <sonartech@sonarsource.com>2022-03-18 20:02:57 +0000
commitecf844b39473fe21acaa1f832d244a4cc4c9f811 (patch)
treea03e87cb743b877be05c92774e6f009bad3932f5 /sonar-plugin-api-impl
parent1269984e8e09338c057d068d715ade7df5a0c354 (diff)
downloadsonarqube-ecf844b39473fe21acaa1f832d244a4cc4c9f811.tar.gz
sonarqube-ecf844b39473fe21acaa1f832d244a4cc4c9f811.zip
SONAR-16097 Add plugin cache to the Sensor API
Diffstat (limited to 'sonar-plugin-api-impl')
-rw-r--r--sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java34
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java21
2 files changed, 55 insertions, 0 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
index d5d0c27f8da..9cbe24f4eeb 100644
--- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
+++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
@@ -47,6 +47,8 @@ import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.cache.ReadCache;
+import org.sonar.api.batch.sensor.cache.WriteCache;
import org.sonar.api.batch.sensor.code.NewSignificantCode;
import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
@@ -110,8 +112,11 @@ public class SensorContextTester implements SensorContext {
private DefaultInputProject project;
private DefaultInputModule module;
private SonarRuntime runtime;
+ private ReadCache readCache;
+ private WriteCache writeCache;
private boolean canSkipUnchangedFiles;
private boolean cancelled;
+ private boolean cacheEnabled = false;
private SensorContextTester(Path moduleBaseDir) {
this.settings = new MapSettings();
@@ -409,6 +414,35 @@ public class SensorContextTester implements SensorContext {
}
@Override
+ public WriteCache nextCache() {
+ return writeCache;
+ }
+
+ public void setNextCache(WriteCache writeCache) {
+ this.writeCache = writeCache;
+ }
+
+ @Override
+ public ReadCache previousAnalysisCache() {
+ return readCache;
+ }
+
+
+ public void setPreviousAnalysisCache(ReadCache cache) {
+ this.readCache = cache;
+ }
+
+ @Override
+ public boolean isCacheEnabled() {
+ return cacheEnabled;
+ }
+
+
+ public void setCacheEnabled(boolean enabled) {
+ this.cacheEnabled = enabled;
+ }
+
+ @Override
public NewSignificantCode newSignificantCode() {
return new DefaultSignificantCode(sensorStorage);
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
index eb61b3a55d4..12e42d9b3ff 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
@@ -36,6 +36,8 @@ import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.NewActiveRule;
+import org.sonar.api.batch.sensor.cache.ReadCache;
+import org.sonar.api.batch.sensor.cache.WriteCache;
import org.sonar.api.batch.sensor.error.AnalysisError;
import org.sonar.api.batch.sensor.error.NewAnalysisError;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
@@ -51,6 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.data.MapEntry.entry;
+import static org.mockito.Mockito.mock;
public class SensorContextTesterTest {
@@ -82,6 +85,24 @@ public class SensorContextTesterTest {
}
@Test
+ public void testPluginCache() {
+ assertThat(tester.nextCache()).isNull();
+ assertThat(tester.previousAnalysisCache()).isNull();
+ assertThat(tester.isCacheEnabled()).isFalse();
+
+ ReadCache readCache = mock(ReadCache.class);
+ WriteCache writeCache = mock(WriteCache.class);
+
+ tester.setPreviousAnalysisCache(readCache);
+ tester.setNextCache(writeCache);
+ tester.setCacheEnabled(true);
+
+ assertThat(tester.nextCache()).isEqualTo(writeCache);
+ assertThat(tester.previousAnalysisCache()).isEqualTo(readCache);
+ assertThat(tester.isCacheEnabled()).isTrue();
+ }
+
+ @Test
public void testActiveRules() {
NewActiveRule activeRule = new NewActiveRule.Builder()
.setRuleKey(RuleKey.of("foo", "bar"))