aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>2022-11-14 14:26:19 +0100
committersonartech <sonartech@sonarsource.com>2022-11-16 20:03:06 +0000
commit0adba24b6d1eae24e43c8e53bbb08588969d24c9 (patch)
tree73f911873afebb5a4714a8649c1395c144129b00
parenta9f83b6f7a9a8b0bb4a2d67a40ccbf61a9395ba5 (diff)
downloadsonarqube-0adba24b6d1eae24e43c8e53bbb08588969d24c9.tar.gz
sonarqube-0adba24b6d1eae24e43c8e53bbb08588969d24c9.zip
SONAR-17564 Add ITs for SARIF import.
-rw-r--r--sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java5
-rw-r--r--sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java43
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java10
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java26
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java5
5 files changed, 73 insertions, 16 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java
index d64ff6f0320..17a4be4dbdb 100644
--- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java
+++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicates.java
@@ -133,10 +133,7 @@ public class DefaultFilePredicates implements FilePredicates {
@Override
public FilePredicate is(File ioFile) {
- if (ioFile.isAbsolute()) {
- return hasAbsolutePath(ioFile.getAbsolutePath());
- }
- return hasRelativePath(ioFile.getPath());
+ return new IsPredicate(ioFile.toPath());
}
@Override
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java
new file mode 100644
index 00000000000..63b85610b8d
--- /dev/null
+++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/predicates/IsPredicate.java
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.api.batch.fs.internal.predicates;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.sonar.api.batch.fs.InputFile;
+
+public class IsPredicate extends AbstractFilePredicate {
+
+ private final Path path;
+
+ public IsPredicate(Path path) {
+ this.path = path;
+ }
+
+ @Override
+ public boolean apply(InputFile inputFile) {
+ try {
+ return Files.isSameFile(path, inputFile.path());
+ } catch (IOException e) {
+ return false;
+ }
+ }
+}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java
index 792333a1100..bfb243bf995 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/predicates/DefaultFilePredicatesTest.java
@@ -19,10 +19,12 @@
*/
package org.sonar.api.batch.fs.internal.predicates;
+import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Before;
@@ -146,8 +148,14 @@ public class DefaultFilePredicatesTest {
@Test
public void is_file() throws Exception {
+
+ Files.createParentDirs(javaFile.file());
+ Files.touch(javaFile.file());
+
// relative file
- assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue();
+ Path workingDir = Paths.get(System.getProperty("user.dir"));
+ Path relativePath = workingDir.relativize(javaFile.path());
+ assertThat(predicates.is(relativePath.toFile()).apply(javaFile)).isTrue();
// absolute file
assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue();
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
index 235a35d2791..c0f63a753a9 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
@@ -19,13 +19,17 @@
*/
package org.sonar.scanner.externalissue.sarif;
+import java.io.File;
import java.net.URI;
+import java.util.Optional;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.scanner.ScannerSide;
+import org.sonar.core.sarif.ArtifactLocation;
import org.sonar.core.sarif.Location;
import org.sonar.core.sarif.PhysicalLocation;
import org.sonar.core.sarif.Result;
@@ -72,18 +76,26 @@ public class LocationMapper {
private static String getFileUriOrThrow(Location location) {
PhysicalLocation physicalLocation = location.getPhysicalLocation();
- checkArgument(physicalLocation != null
- && physicalLocation.getArtifactLocation() != null
- && physicalLocation.getArtifactLocation().getUri() != null,
- "The field location.physicalLocation.artifactLocation.uri is not set.");
+ checkArgument(hasUriFieldPopulated(physicalLocation), "The field location.physicalLocation.artifactLocation.uri is not set.");
return physicalLocation.getArtifactLocation().getUri();
}
+ private static boolean hasUriFieldPopulated(@Nullable PhysicalLocation location) {
+ return Optional.ofNullable(location).map(PhysicalLocation::getArtifactLocation).map(ArtifactLocation::getUri).isPresent();
+ }
+
@CheckForNull
private static InputFile findFile(SensorContext context, String filePath) {
FilePredicates predicates = context.fileSystem().predicates();
- return context.fileSystem().inputFile(predicates.or(
- predicates.hasURI(URI.create(filePath)), predicates.hasPath(filePath)
- ));
+ return context.fileSystem().inputFile(predicates.is(getFileFromAbsoluteUriOrPath(filePath)));
+ }
+
+ private static File getFileFromAbsoluteUriOrPath(String filePath) {
+ URI uri = URI.create(filePath);
+ if (uri.isAbsolute()) {
+ return new File(uri);
+ } else {
+ return new File(filePath);
+ }
}
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java
index be6f703643a..fdffbf2feea 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/LocationMapperTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.scanner.externalissue.sarif;
-import java.net.URI;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
@@ -86,9 +85,7 @@ public class LocationMapperTest {
FilePredicate filePredicate = mock(FilePredicate.class);
FilePredicates predicates = sensorContext.fileSystem().predicates();
- when(predicates.or(
- predicates.hasURI(URI.create(URI_TEST)), predicates.hasPath(URI_TEST)
- )).thenReturn(filePredicate);
+ when(predicates.is(any())).thenReturn(filePredicate);
when(sensorContext.fileSystem().inputFile(filePredicate)).thenReturn(inputFile);