diff options
author | Benjamin Campomenosi <benjamin.campomenosi@sonarsource.com> | 2022-10-20 16:29:28 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-26 20:03:10 +0000 |
commit | b4216e6b0edef15492dd5d3d595e19fd33bb3459 (patch) | |
tree | ed48e865c3a5ead39f6c0bb7471beedd8b4d36c6 /sonar-plugin-api-impl | |
parent | 342f7a1caf70e329f92466f07424406fbc424ee9 (diff) | |
download | sonarqube-b4216e6b0edef15492dd5d3d595e19fd33bb3459.tar.gz sonarqube-b4216e6b0edef15492dd5d3d595e19fd33bb3459.zip |
SONAR-13579 Overall coverage improvements
Diffstat (limited to 'sonar-plugin-api-impl')
4 files changed, 36 insertions, 23 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java index be062dec668..8bc0255a286 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java @@ -25,7 +25,6 @@ import java.io.InputStream; import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -48,7 +47,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed private final Type type; private final Path absolutePath; private final SensorStrategy sensorStrategy; - private final String oldFilePath; + private final String oldRelativeFilePath; /** * Testing purposes only! @@ -64,7 +63,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed } public DefaultIndexedFile(Path absolutePath, String projectKey, String projectRelativePath, String moduleRelativePath, Type type, @Nullable String language, int batchId, - SensorStrategy sensorStrategy, @Nullable String oldFilePath) { + SensorStrategy sensorStrategy, @Nullable String oldRelativeFilePath) { super(batchId); this.projectKey = projectKey; this.projectRelativePath = PathUtils.sanitize(projectRelativePath); @@ -73,7 +72,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed this.language = language; this.sensorStrategy = sensorStrategy; this.absolutePath = absolutePath; - this.oldFilePath = oldFilePath; + this.oldRelativeFilePath = oldRelativeFilePath; } @Override @@ -105,12 +104,8 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed } @CheckForNull - public String oldPath() { - return oldFilePath; - } - - public boolean isMovedFile() { - return Objects.nonNull(this.oldPath()); + public String oldRelativePath() { + return oldRelativeFilePath; } @Override diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index fe4c116bebd..d6bc5786e48 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -178,12 +178,8 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile } @CheckForNull - public String oldPath() { - return indexedFile.oldPath(); - } - - public boolean isMovedFile() { - return indexedFile.isMovedFile(); + public String oldRelativePath() { + return indexedFile.oldRelativePath(); } @Override diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java index ec909d055c8..f7204de7670 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java @@ -57,6 +57,7 @@ public class TestInputFileBuilder { private final int id; private final String relativePath; + private String oldRelativePath; private final String projectKey; @CheckForNull private Path projectBaseDir; @@ -101,6 +102,14 @@ public class TestInputFileBuilder { this.id = id; } + public TestInputFileBuilder(String projectKey, String relativePath, String oldRelativePath, int id) { + this.projectKey = projectKey; + setModuleBaseDir(Paths.get(projectKey)); + this.relativePath = PathUtils.sanitize(relativePath); + this.oldRelativePath = oldRelativePath; + this.id = id; + } + public static TestInputFileBuilder create(String moduleKey, File moduleBaseDir, File filePath) { return new TestInputFileBuilder(moduleKey, moduleBaseDir, filePath); } @@ -218,7 +227,7 @@ public class TestInputFileBuilder { projectBaseDir = moduleBaseDir; } String projectRelativePath = projectBaseDir.relativize(absolutePath).toString(); - DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, projectKey, projectRelativePath, relativePath, type, language, id, new SensorStrategy()); + DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, projectKey, projectRelativePath, relativePath, type, language, id, new SensorStrategy(), oldRelativePath); DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(new Metadata(lines, nonBlankLines, hash, originalLineStartOffsets, originalLineEndOffsets, lastValidOffset)), contents); diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultInputFileTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultInputFileTest.java index 62333075385..4117f881c7e 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultInputFileTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultInputFileTest.java @@ -55,6 +55,7 @@ public class DefaultInputFileTest { private static final String PROJECT_RELATIVE_PATH = "module1/src/Foo.php"; private static final String MODULE_RELATIVE_PATH = "src/Foo.php"; + private static final String OLD_RELATIVE_PATH = "src/previous/Foo.php"; @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -102,11 +103,23 @@ public class DefaultInputFileTest { } @Test + public void test_moved_file() { + DefaultIndexedFile indexedFileForMovedFile = new DefaultIndexedFile(baseDir.resolve(PROJECT_RELATIVE_PATH), "ABCDE", PROJECT_RELATIVE_PATH, MODULE_RELATIVE_PATH, InputFile.Type.TEST, "php", 0, + sensorStrategy, OLD_RELATIVE_PATH); + Metadata metadata = new Metadata(42, 42, "", new int[0], new int[0], 10); + DefaultInputFile inputFile = new DefaultInputFile(indexedFileForMovedFile, (f) -> f.setMetadata(metadata)) + .setStatus(InputFile.Status.ADDED) + .setCharset(StandardCharsets.ISO_8859_1); + + assertThat(inputFile.oldRelativePath()).isEqualTo(OLD_RELATIVE_PATH); + } + + @Test public void test_content() throws IOException { Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH); Files.createDirectories(testFile.getParent()); String content = "test é string"; - Files.write(testFile, content.getBytes(StandardCharsets.ISO_8859_1)); + Files.writeString(testFile, content, StandardCharsets.ISO_8859_1); assertThat(Files.readAllLines(testFile, StandardCharsets.ISO_8859_1).get(0)).hasSize(content.length()); @@ -171,12 +184,12 @@ public class DefaultInputFileTest { @Test public void test_toString() { DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); - assertThat(file.toString()).isEqualTo(MODULE_RELATIVE_PATH); + assertThat(file).hasToString(MODULE_RELATIVE_PATH); } @Test public void checkValidPointer() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + Metadata metadata = new Metadata(2, 2, "", new int[]{0, 10}, new int[]{9, 15}, 16); DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); assertThat(file.newPointer(1, 0).line()).isOne(); assertThat(file.newPointer(1, 0).lineOffset()).isZero(); @@ -213,7 +226,7 @@ public class DefaultInputFileTest { @Test public void checkValidPointerUsingGlobalOffset() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {8, 15}, 16); + Metadata metadata = new Metadata(2, 2, "", new int[]{0, 10}, new int[]{8, 15}, 16); DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); assertThat(file.newPointer(0).line()).isOne(); assertThat(file.newPointer(0).lineOffset()).isZero(); @@ -299,7 +312,7 @@ public class DefaultInputFileTest { @Test public void checkValidRangeUsingGlobalOffset() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + Metadata metadata = new Metadata(2, 2, "", new int[]{0, 10}, new int[]{9, 15}, 16); DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); TextRange newRange = file.newRange(10, 13); assertThat(newRange.start().line()).isEqualTo(2); @@ -310,7 +323,7 @@ public class DefaultInputFileTest { @Test public void testRangeOverlap() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + Metadata metadata = new Metadata(2, 2, "", new int[]{0, 10}, new int[]{9, 15}, 16); DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); // Don't fail assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue(); |