diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2022-11-14 14:26:19 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-11-16 20:03:06 +0000 |
commit | 0adba24b6d1eae24e43c8e53bbb08588969d24c9 (patch) | |
tree | 73f911873afebb5a4714a8649c1395c144129b00 /sonar-plugin-api-impl | |
parent | a9f83b6f7a9a8b0bb4a2d67a40ccbf61a9395ba5 (diff) | |
download | sonarqube-0adba24b6d1eae24e43c8e53bbb08588969d24c9.tar.gz sonarqube-0adba24b6d1eae24e43c8e53bbb08588969d24c9.zip |
SONAR-17564 Add ITs for SARIF import.
Diffstat (limited to 'sonar-plugin-api-impl')
3 files changed, 53 insertions, 5 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(); |