aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerLogbackEncoderTest.java8
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueReportParserTest.java6
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java5
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java7
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java30
5 files changed, 35 insertions, 21 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerLogbackEncoderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerLogbackEncoderTest.java
index d752b67b4c5..ea0028bf90a 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerLogbackEncoderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerLogbackEncoderTest.java
@@ -70,9 +70,11 @@ class ScannerLogbackEncoderTest {
var bytes = underTest.encode(logEvent);
- assertThat(new String(bytes, StandardCharsets.UTF_8))
- .startsWith(
- "{\"level\":\"DEBUG\",\"message\":\"message\",\"stacktrace\":\"java.lang.IllegalArgumentException: foo\\n\\tat org.sonar.scanner.bootstrap.ScannerLogbackEncoderTest.should_encode_with_stacktrace");
+ String encodedLog = new String(bytes, StandardCharsets.UTF_8);
+ assertThat(encodedLog).contains("\"level\":\"DEBUG\"")
+ .contains("\"message\":\"message\"")
+ .contains("\"stacktrace\":\"java.lang.IllegalArgumentException: foo")
+ .contains("at org.sonar.scanner.bootstrap.ScannerLogbackEncoderTest.should_encode_with_stacktrace");
}
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueReportParserTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueReportParserTest.java
index ffefbd51751..a207bde260e 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueReportParserTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueReportParserTest.java
@@ -23,6 +23,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
+import static java.io.File.separator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
@@ -71,10 +72,11 @@ public class ExternalIssueReportParserTest {
public void parse_whenInvalidDeprecatedFormat_shouldFail() {
reportPath = Paths.get(DEPRECATED_REPORTS_LOCATION + "report_invalid_json.json");
+ String expectedReportPath = String.join(separator, "src", "test", "resources", "org", "sonar", "scanner", "externalissue", "report_invalid_json.json");
assertThatThrownBy(() -> externalIssueReportParser.parse(reportPath))
.isInstanceOf(IllegalStateException.class)
- .hasMessage("Failed to read external issues report 'src/test/resources/org/sonar/scanner/externalissue/report_invalid_json.json': " +
- "invalid JSON syntax");
+ .hasMessage(String.format("Failed to read external issues report '%s': " +
+ "invalid JSON syntax", expectedReportPath));
}
@Test
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
index 20b60cf0d26..48b77adf39b 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
@@ -44,7 +44,10 @@ public class ModuleCoverageAndDuplicationExclusionsTest {
@Before
public void prepare() throws Exception {
- baseDir = temp.newFolder();
+ // On Windows, 'temp.newFolder()' would return a 8.3 filename (or short filename).
+ // We need to convert it to long filename version, otherwise the path will be different and the resolution of the relative path will not work as intended.
+ // See https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14
+ baseDir = temp.newFolder().toPath().toRealPath().toFile();
}
@Test
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
index e81120bee32..1bf8599da5d 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
@@ -39,6 +39,7 @@ import org.sonar.api.utils.MessageException;
import org.sonar.scanner.mediumtest.FakeLanguagesLoader;
import org.sonar.scanner.repository.language.DefaultLanguagesRepository;
+import static java.io.File.separator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
@@ -193,8 +194,8 @@ public class LanguageDetectionTest {
assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java");
assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java");
- verify(languageCacheSpy, times(1)).put(endsWith("/Foo.java"), any(org.sonar.scanner.repository.language.Language.class));
- verify(languageCacheSpy, times(2)).get(endsWith("/Foo.java"));
+ verify(languageCacheSpy, times(1)).put(endsWith(separator + "Foo.java"), any(org.sonar.scanner.repository.language.Language.class));
+ verify(languageCacheSpy, times(2)).get(endsWith(separator + "Foo.java"));
}
private static Map<String, org.sonar.scanner.repository.language.Language> spyInternalLanguageCache(LanguageDetection detection) {
@@ -239,7 +240,7 @@ public class LanguageDetectionTest {
@Override
public String getName() {
- return key;
+ return getKey();
}
@Override
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 28f783a7452..a0d785bd163 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
@@ -146,11 +146,11 @@ class DefaultSensorStorageTest {
@Test
void shouldFailIfUnknownMetric() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
-
- assertThatThrownBy(() -> underTest.store(new DefaultMeasure()
+ DefaultMeasure defaultMeasure = new DefaultMeasure()
.on(file)
.forMetric(CoreMetrics.LINES)
- .withValue(10)))
+ .withValue(10);
+ assertThatThrownBy(() -> underTest.store(defaultMeasure))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Unknown metric: lines");
}
@@ -257,9 +257,11 @@ class DefaultSensorStorageTest {
.forMetric(CoreMetrics.NCLOC)
.withValue(10));
- ScannerReport.Measure m = reportReader.readComponentMeasures(file.scannerId()).next();
- assertThat(m.getIntValue().getValue()).isEqualTo(10);
- assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ try (CloseableIterator<ScannerReport.Measure> measureCloseableIterator = reportReader.readComponentMeasures(file.scannerId())) {
+ ScannerReport.Measure m = measureCloseableIterator.next();
+ assertThat(m.getIntValue().getValue()).isEqualTo(10);
+ assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ }
}
@Test
@@ -272,9 +274,11 @@ class DefaultSensorStorageTest {
.forMetric(CoreMetrics.NCLOC)
.withValue(10));
- ScannerReport.Measure m = reportReader.readComponentMeasures(file.scannerId()).next();
- assertThat(m.getIntValue().getValue()).isEqualTo(10);
- assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ try (CloseableIterator<ScannerReport.Measure> measureCloseableIterator = reportReader.readComponentMeasures(file.scannerId())) {
+ ScannerReport.Measure m = measureCloseableIterator.next();
+ assertThat(m.getIntValue().getValue()).isEqualTo(10);
+ assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ }
}
@Test
@@ -314,9 +318,11 @@ class DefaultSensorStorageTest {
.forMetric(CoreMetrics.NCLOC)
.withValue(10));
- ScannerReport.Measure m = reportReader.readComponentMeasures(module.scannerId()).next();
- assertThat(m.getIntValue().getValue()).isEqualTo(10);
- assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ try (CloseableIterator<ScannerReport.Measure> measureCloseableIterator = reportReader.readComponentMeasures(module.scannerId())) {
+ ScannerReport.Measure m = measureCloseableIterator.next();
+ assertThat(m.getIntValue().getValue()).isEqualTo(10);
+ assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
+ }
}
@Test