aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2024-10-09 15:09:39 +0200
committersonartech <sonartech@sonarsource.com>2024-10-11 20:02:42 +0000
commit3f0a34901c55eb1e204fe4028d2773f8484b513f (patch)
treee79565fd2126baf3711a545619470dd2f44d2598 /sonar-scanner-engine/src/test/java/org
parent8859ed6887ec1e2eb3e86a11b98b73b5b8606710 (diff)
downloadsonarqube-3f0a34901c55eb1e204fe4028d2773f8484b513f.tar.gz
sonarqube-3f0a34901c55eb1e204fe4028d2773f8484b513f.zip
SONAR-23327 Implement addTelemetryProperty in the scanner engine
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/TelemetryPublisherTest.java66
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/TelemetryCacheTest.java83
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java107
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java7
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ProjectSensorContextTest.java92
5 files changed, 305 insertions, 50 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/TelemetryPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/TelemetryPublisherTest.java
new file mode 100644
index 00000000000..f71e9a8f8c5
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/TelemetryPublisherTest.java
@@ -0,0 +1,66 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.report;
+
+import com.google.common.collect.Lists;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.sonar.scanner.protocol.output.ScannerReport;
+import org.sonar.scanner.protocol.output.ScannerReportWriter;
+import org.sonar.scanner.repository.TelemetryCache;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+class TelemetryPublisherTest {
+ private final ScannerReportWriter writer = mock(ScannerReportWriter.class);
+ private final TelemetryCache telemetryCache = new TelemetryCache();
+ private final TelemetryPublisher underTest = new TelemetryPublisher(telemetryCache);
+
+ @Test
+ void publish_writes_telemetry_to_report() {
+ telemetryCache.put("key1", "value1");
+ telemetryCache.put("key2", "value2");
+
+ underTest.publish(writer);
+
+ List<ScannerReport.TelemetryEntry> expected = Arrays.asList(
+ newTelemetryEntry("key1", "value1"),
+ newTelemetryEntry("key2", "value2"));
+ expectWritten(expected);
+ }
+
+ private void expectWritten(List<ScannerReport.TelemetryEntry> expected) {
+ verify(writer).writeTelemetry(argThat(entries -> {
+ List<ScannerReport.TelemetryEntry> copy = Lists.newArrayList(entries);
+ copy.removeAll(expected);
+ return copy.isEmpty();
+ }));
+ }
+
+ private static ScannerReport.TelemetryEntry newTelemetryEntry(String key, String value) {
+ return ScannerReport.TelemetryEntry.newBuilder()
+ .setKey(key)
+ .setValue(value)
+ .build();
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/TelemetryCacheTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/TelemetryCacheTest.java
new file mode 100644
index 00000000000..cbdb518db7f
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/TelemetryCacheTest.java
@@ -0,0 +1,83 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.repository;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.data.MapEntry.entry;
+
+class TelemetryCacheTest {
+
+ TelemetryCache underTest = new TelemetryCache();
+
+ @Test
+ void put_EntryIsAddedToCache() {
+ assertThat(underTest.getAll()).isEmpty();
+
+ underTest.put("key", "value");
+ assertThat(underTest.getAll()).containsOnly(entry("key", "value"));
+ }
+
+ @Test
+ void put_whenKeyIsAlreadyThere_EntryOverridesPreviousValue() {
+ underTest.put("key", "value");
+ underTest.put("key", "newValue");
+ assertThat(underTest.getAll()).containsOnly(entry("key", "newValue"));
+ }
+
+ @Test
+ void put_whenCacheIsAlreadyFull_newEntryIsNotAdded() {
+ for (int i = 0; i < 1000; i++) {
+ underTest.put("key" + i, "value" + i);
+ }
+ underTest.put("key", "value");
+ assertThat(underTest.getAll()).hasSize(1000);
+ assertThat(underTest.getAll()).doesNotContain(entry("key", "value"));
+ }
+
+ @Test
+ void put_whenCacheIsAlreadyFull_newEntryIsAddedIfKeyAlreadyThere() {
+ for (int i = 0; i < 1000; i++) {
+ underTest.put("key" + i, "value" + i);
+ }
+ underTest.put("key1", "newValue");
+ underTest.put("key", "newValue");
+
+ assertThat(underTest.getAll()).hasSize(1000);
+ assertThat(underTest.getAll()).contains(entry("key1", "newValue"));
+ }
+
+ @Test
+ void put_whenKeyIsNull_IAEIsThrown() {
+ assertThatThrownBy(() -> underTest.put(null, "value"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Key of the telemetry entry must not be null");
+ }
+
+ @Test
+ void put_whenValueIsNull_IAEIsThrown() {
+ assertThatThrownBy(() -> underTest.put("key", null))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Value of the telemetry entry must not be null");
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
index 1eeea7ede12..41680ce3a1d 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
@@ -20,14 +20,12 @@
package org.sonar.scanner.sensor;
import java.io.File;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.groups.Tuple;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
@@ -66,34 +64,37 @@ import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.repository.ContextPropertiesCache;
+import org.sonar.scanner.repository.TelemetryCache;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.MapEntry.entry;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
-public class DefaultSensorStorageTest {
+class DefaultSensorStorageTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
+ @TempDir
+ public File temp;
private DefaultSensorStorage underTest;
private MapSettings settings;
private IssuePublisher moduleIssues;
private ScannerReportWriter reportWriter;
private ContextPropertiesCache contextPropertiesCache = new ContextPropertiesCache();
+ private TelemetryCache telemetryCache = new TelemetryCache();
private BranchConfiguration branchConfiguration;
private DefaultInputProject project;
private ScannerReportReader reportReader;
private ReportPublisher reportPublisher;
- @Before
- public void prepare() throws Exception {
+ @BeforeEach
+ public void prepare() {
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
@@ -103,8 +104,7 @@ public class DefaultSensorStorageTest {
moduleIssues = mock(IssuePublisher.class);
reportPublisher = mock(ReportPublisher.class);
- final File reportDir = temp.newFolder();
- FileStructure fileStructure = new FileStructure(reportDir);
+ FileStructure fileStructure = new FileStructure(temp);
reportWriter = new ScannerReportWriter(fileStructure);
reportReader = new ScannerReportReader(fileStructure);
when(reportPublisher.getWriter()).thenReturn(reportWriter);
@@ -113,16 +113,16 @@ public class DefaultSensorStorageTest {
branchConfiguration = mock(BranchConfiguration.class);
underTest = new DefaultSensorStorage(metricFinder,
- moduleIssues, settings.asConfig(), reportPublisher, mock(SonarCpdBlockIndex.class), contextPropertiesCache, new ScannerMetrics(), branchConfiguration);
+ moduleIssues, settings.asConfig(), reportPublisher, mock(SonarCpdBlockIndex.class), contextPropertiesCache, telemetryCache, new ScannerMetrics(), branchConfiguration);
project = new DefaultInputProject(ProjectDefinition.create()
.setKey("foo")
- .setBaseDir(temp.newFolder())
- .setWorkDir(temp.newFolder()));
+ .setBaseDir(temp)
+ .setWorkDir(temp));
}
@Test
- public void should_merge_coverage() {
+ void should_merge_coverage() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setLines(5).build();
DefaultCoverage coverage = new DefaultCoverage(underTest);
@@ -144,7 +144,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void shouldFailIfUnknownMetric() {
+ void shouldFailIfUnknownMetric() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
assertThatThrownBy(() -> underTest.store(new DefaultMeasure()
@@ -156,7 +156,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void shouldIgnoreMeasuresOnFolders() {
+ void shouldIgnoreMeasuresOnFolders() {
underTest.store(new DefaultMeasure()
.on(new DefaultInputDir("foo", "bar"))
.forMetric(CoreMetrics.LINES)
@@ -166,9 +166,9 @@ public class DefaultSensorStorageTest {
}
@Test
- public void shouldIgnoreMeasuresOnModules() throws IOException {
- ProjectDefinition module = ProjectDefinition.create().setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder());
- ProjectDefinition root = ProjectDefinition.create().addSubProject(module);
+ void shouldIgnoreMeasuresOnModules() {
+ ProjectDefinition module = ProjectDefinition.create().setBaseDir(temp).setWorkDir(temp);
+ ProjectDefinition.create().addSubProject(module);
underTest.store(new DefaultMeasure()
.on(new DefaultInputModule(module))
@@ -179,7 +179,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_issue() {
+ void should_save_issue() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
DefaultIssue issue = new DefaultIssue(project).at(new DefaultIssueLocation().on(file));
@@ -191,7 +191,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_external_issue() {
+ void should_save_external_issue() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
DefaultExternalIssue externalIssue = new DefaultExternalIssue(project).at(new DefaultIssueLocation().on(file));
@@ -203,7 +203,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_skip_issue_on_pr_when_file_status_is_SAME() {
+ void should_skip_issue_on_pr_when_file_status_is_SAME() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isPullRequest()).thenReturn(true);
@@ -214,7 +214,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void has_issues_delegates_to_report_publisher() {
+ void has_issues_delegates_to_report_publisher() {
DefaultInputFile file1 = new TestInputFileBuilder("foo", "src/Foo1.php").setStatus(InputFile.Status.SAME).build();
DefaultInputFile file2 = new TestInputFileBuilder("foo", "src/Foo2.php").setStatus(InputFile.Status.SAME).build();
@@ -224,7 +224,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_highlighting() {
+ void should_save_highlighting() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("// comment").build();
@@ -235,7 +235,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_skip_highlighting_on_pr_when_file_status_is_SAME() {
+ void should_skip_highlighting_on_pr_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("// comment")
.setStatus(InputFile.Status.SAME).build();
@@ -248,7 +248,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_file_measure() {
+ void should_save_file_measure() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.build();
@@ -263,7 +263,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_not_skip_file_measures_on_pull_request_when_file_status_is_SAME() {
+ void should_not_skip_file_measures_on_pull_request_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isPullRequest()).thenReturn(true);
@@ -278,7 +278,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_skip_significant_code_on_pull_request_when_file_status_is_SAME() {
+ void should_skip_significant_code_on_pull_request_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setStatus(InputFile.Status.SAME)
.setContents("foo")
@@ -293,7 +293,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_significant_code() {
+ void should_save_significant_code() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("foo")
.build();
@@ -305,9 +305,9 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_save_project_measure() throws IOException {
+ void should_save_project_measure() {
String projectKey = "myProject";
- DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create().setKey(projectKey).setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
+ DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create().setKey(projectKey).setBaseDir(temp).setWorkDir(temp));
underTest.store(new DefaultMeasure()
.on(module)
@@ -319,45 +319,56 @@ public class DefaultSensorStorageTest {
assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
}
- @Test(expected = UnsupportedOperationException.class)
- public void duplicateHighlighting() throws Exception {
+ @Test
+ void duplicateHighlighting() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
- .setModuleBaseDir(temp.newFolder().toPath()).build();
+ .setModuleBaseDir(temp.toPath()).build();
DefaultHighlighting h = new DefaultHighlighting(null)
.onFile(inputFile);
underTest.store(h);
- underTest.store(h);
+ assertThrows(UnsupportedOperationException.class, () -> {
+ underTest.store(h);
+ });
}
- @Test(expected = UnsupportedOperationException.class)
- public void duplicateSignificantCode() throws Exception {
+ @Test
+ void duplicateSignificantCode() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
- .setModuleBaseDir(temp.newFolder().toPath()).build();
+ .setModuleBaseDir(temp.toPath()).build();
DefaultSignificantCode h = new DefaultSignificantCode(null)
.onFile(inputFile);
underTest.store(h);
- underTest.store(h);
+ assertThrows(UnsupportedOperationException.class, () -> {
+ underTest.store(h);
+ });
}
- @Test(expected = UnsupportedOperationException.class)
- public void duplicateSymbolTable() throws Exception {
+ @Test
+ void duplicateSymbolTable() {
InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
- .setModuleBaseDir(temp.newFolder().toPath()).build();
+ .setModuleBaseDir(temp.toPath()).build();
DefaultSymbolTable st = new DefaultSymbolTable(null)
.onFile(inputFile);
underTest.store(st);
- underTest.store(st);
+ assertThrows(UnsupportedOperationException.class, () -> {
+ underTest.store(st);
+ });
}
@Test
- public void shouldStoreContextProperty() {
+ void shouldStoreContextProperty() {
underTest.storeProperty("foo", "bar");
assertThat(contextPropertiesCache.getAll()).containsOnly(entry("foo", "bar"));
}
@Test
- public void store_whenAdhocRuleIsSpecified_shouldWriteAdhocRuleToReport() {
+ void shouldStoreTelemetryEntries() {
+ underTest.storeTelemetry("key", "value");
+ assertThat(telemetryCache.getAll()).containsOnly(entry("key", "value"));
+ }
+ @Test
+ void store_whenAdhocRuleIsSpecified_shouldWriteAdhocRuleToReport() {
underTest.store(new DefaultAdHocRule().ruleId("ruleId").engineId("engineId")
.name("name")
.addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH)
@@ -383,7 +394,7 @@ public class DefaultSensorStorageTest {
}
@Test
- public void store_whenAdhocRuleIsSpecifiedWithOptionalFieldEmpty_shouldWriteAdhocRuleWithDefaultImpactsToReport() {
+ void store_whenAdhocRuleIsSpecifiedWithOptionalFieldEmpty_shouldWriteAdhocRuleWithDefaultImpactsToReport() {
underTest.store(new DefaultAdHocRule().ruleId("ruleId").engineId("engineId")
.name("name")
.description("description"));
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
index 94d7a2ce282..4ca7d33d642 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
@@ -35,6 +35,7 @@ import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.Version;
+import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cache.AnalysisCacheEnabled;
import org.sonar.scanner.cache.ReadCacheImpl;
import org.sonar.scanner.cache.WriteCacheImpl;
@@ -61,12 +62,14 @@ public class ModuleSensorContextTest {
private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
private DefaultFileSystem fs;
private ModuleSensorContext underTest;
+ private ExecutingSensorContext executingSensorContext = mock(ExecutingSensorContext.class);
+ private ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class);
@Before
public void prepare() throws Exception {
fs = new DefaultFileSystem(temp.newFolder().toPath());
underTest = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime,
- branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler);
+ branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler, executingSensorContext, pluginRepository);
}
@Test
@@ -102,7 +105,7 @@ public class ModuleSensorContextTest {
public void pull_request_can_skip_unchanged_files() {
when(branchConfiguration.isPullRequest()).thenReturn(true);
underTest = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime,
- branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler);
+ branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler, executingSensorContext, pluginRepository);
assertThat(underTest.canSkipUnchangedFiles()).isTrue();
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ProjectSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ProjectSensorContextTest.java
new file mode 100644
index 00000000000..5d25250e8bb
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ProjectSensorContextTest.java
@@ -0,0 +1,92 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.sensor;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.sonar.api.SonarEdition;
+import org.sonar.api.SonarQubeSide;
+import org.sonar.api.SonarRuntime;
+import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.internal.SonarRuntimeImpl;
+import org.sonar.api.utils.Version;
+import org.sonar.core.platform.PluginInfo;
+import org.sonar.scanner.bootstrap.ScannerPluginRepository;
+import org.sonar.scanner.cache.AnalysisCacheEnabled;
+import org.sonar.scanner.cache.ReadCacheImpl;
+import org.sonar.scanner.cache.WriteCacheImpl;
+import org.sonar.scanner.scan.branch.BranchConfiguration;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+class ProjectSensorContextTest {
+
+ private final ActiveRules activeRules = new ActiveRulesBuilder().build();
+ private final MapSettings settings = new MapSettings();
+ private final DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class);
+ private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
+ private final WriteCacheImpl writeCache = mock(WriteCacheImpl.class);
+ private final ReadCacheImpl readCache = mock(ReadCacheImpl.class);
+ private final AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class);
+ private final UnchangedFilesHandler unchangedFilesHandler = mock(UnchangedFilesHandler.class);
+ private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
+ private DefaultFileSystem fs = mock(DefaultFileSystem.class);
+ private ExecutingSensorContext executingSensorContext = mock(ExecutingSensorContext.class);
+ private ScannerPluginRepository pluginRepository = mock(ScannerPluginRepository.class);
+
+ private ProjectSensorContext underTest = new ProjectSensorContext(mock(DefaultInputProject.class), settings.asConfig(), settings, fs, activeRules, sensorStorage, runtime,
+ branchConfiguration, writeCache, readCache, analysisCacheEnabled, unchangedFilesHandler, executingSensorContext, pluginRepository);
+
+ private static final String PLUGIN_KEY = "org.sonarsource.pluginKey";
+
+ @BeforeEach
+ void prepare() {
+ when(executingSensorContext.getSensorExecuting()).thenReturn(new SensorId(PLUGIN_KEY, "sensorName"));
+ }
+
+
+ @Test
+ void addTelemetryProperty_whenTheOrganizationIsSonarSource_mustStoreTheTelemetry() {
+
+ when(pluginRepository.getPluginInfo(PLUGIN_KEY)).thenReturn(new PluginInfo(PLUGIN_KEY).setOrganizationName("sonarsource"));
+
+ underTest.addTelemetryProperty("key", "value");
+
+ //then verify that the defaultStorage is called with the telemetry property once
+ verify(sensorStorage).storeTelemetry("key", "value");
+ }
+
+ @Test
+ void addTelemetryProperty_whenTheOrganizationIsNotSonarSource_mustThrowExcaption() {
+ when(pluginRepository.getPluginInfo(PLUGIN_KEY)).thenReturn(new PluginInfo(PLUGIN_KEY).setOrganizationName("notSonarsource"));
+
+ assertThrows(IllegalStateException.class, () -> underTest.addTelemetryProperty("key", "value"));
+
+ verifyNoInteractions(sensorStorage);
+ }
+}