diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-10-27 12:10:31 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-10-27 16:06:49 +0200 |
commit | 0b6da814d228080096781457a6a4ba04d072c79d (patch) | |
tree | afc74ec3686822e08e57844b2a8b2e292061eb16 /sonar-plugin-api/src | |
parent | b34dc139c9ed67103e1e1bc7d6b65da87a041dd3 (diff) | |
download | sonarqube-0b6da814d228080096781457a6a4ba04d072c79d.tar.gz sonarqube-0b6da814d228080096781457a6a4ba04d072c79d.zip |
SONAR-8327 Support access input file's contents through VFS
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java | 22 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java | 29 |
2 files changed, 51 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index dafc0c8ad79..450dd704416 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -20,7 +20,10 @@ package org.sonar.api.batch.fs; import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; import javax.annotation.CheckForNull; import org.sonar.api.batch.fs.internal.DefaultInputFile; @@ -100,6 +103,25 @@ public interface InputFile extends InputPath { Type type(); /** + * Creates a stream of the file's contents. Depending on the runtime context, the source might be a file in a physical or virtual filesystem. + * Typically, it won't be buffered. <b>The stream must be closed by the caller</b>. + * Note that there is a default implementation. + * @since 6.2 + */ + default InputStream inputStream() throws IOException { + return Files.newInputStream(path()); + } + + /** + * Fetches the entire contents of the file, decoding with the {@link #charset}. + * Note that there is a default implementation. + * @since 6.2 + */ + default String contents() throws IOException { + return new String(Files.readAllBytes(path()), charset()); + } + + /** * Status regarding previous analysis */ Status status(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java index da9ead4cba7..5fd96320fec 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java @@ -19,9 +19,16 @@ */ package org.sonar.api.batch.fs.internal; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.stream.Collectors; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -59,6 +66,28 @@ public class DefaultInputFileTest { } @Test + public void test_content() throws IOException { + Path baseDir = temp.newFolder().toPath(); + Path testFile = baseDir.resolve("src").resolve("Foo.php"); + Files.createDirectories(testFile.getParent()); + Files.write(testFile, "test string".getBytes(StandardCharsets.UTF_8)); + DefaultInputFile inputFile = new DefaultInputFile("ABCDE", "src/Foo.php") + .setModuleBaseDir(baseDir) + .setLines(42) + .setLanguage("php") + .setStatus(InputFile.Status.ADDED) + .setType(InputFile.Type.TEST) + .setCharset(StandardCharsets.ISO_8859_1); + + assertThat(inputFile.contents()).isEqualTo("test string"); + try (InputStream inputStream = inputFile.inputStream()) { + String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining()); + assertThat(result).isEqualTo("test string"); + } + + } + + @Test public void test_equals_and_hashcode() throws Exception { DefaultInputFile f1 = new DefaultInputFile("ABCDE", "src/Foo.php"); DefaultInputFile f1a = new DefaultInputFile("ABCDE", "src/Foo.php"); |