Search in sources :

Example 11 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class DefaultInputFileTest method test_content.

@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));
    assertThat(Files.readAllLines(testFile, StandardCharsets.ISO_8859_1).get(0)).hasSize(content.length());
    Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10);
    DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata)).setStatus(InputFile.Status.ADDED).setCharset(StandardCharsets.ISO_8859_1);
    assertThat(inputFile.contents()).isEqualTo(content);
    try (InputStream inputStream = inputFile.inputStream()) {
        String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining());
        assertThat(result).isEqualTo(content);
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) InputStream(java.io.InputStream) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 12 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class MetadataTest method testRoundtrip.

@Test
public void testRoundtrip() {
    Metadata metadata = new Metadata(10, 20, "hash", new int[] { 1, 3 }, new int[] { 2, 4 }, 5);
    assertThat(metadata.isEmpty()).isFalse();
    assertThat(metadata.lines()).isEqualTo(10);
    assertThat(metadata.nonBlankLines()).isEqualTo(20);
    assertThat(metadata.originalLineStartOffsets()).isEqualTo(new int[] { 1, 3 });
    assertThat(metadata.originalLineEndOffsets()).isEqualTo(new int[] { 2, 4 });
    assertThat(metadata.lastValidOffset()).isEqualTo(5);
    assertThat(metadata.hash()).isEqualTo("hash");
}
Also used : Metadata(org.sonar.api.batch.fs.internal.Metadata) Test(org.junit.Test)

Example 13 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class FileMetadataTest method non_ascii_utf_8.

@Test
public void non_ascii_utf_8() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_8, true);
    Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
    assertThat(metadata.lines()).isEqualTo(4);
    assertThat(metadata.nonBlankLines()).isEqualTo(3);
    assertThat(metadata.hash()).isEqualTo(md5Hex("föo\nbàr\n\u1D11Ebaßz\n"));
    assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 5, 10, 18);
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 14 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class FileMetadataTest method mac_without_latest_eol.

@Test
public void mac_without_latest_eol() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "foo\rbar\rbaz", StandardCharsets.UTF_8, true);
    Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
    assertThat(metadata.lines()).isEqualTo(3);
    assertThat(metadata.nonBlankLines()).isEqualTo(3);
    assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
    assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8);
    assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11);
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 15 with Metadata

use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.

the class FileMetadataTest method empty_file.

@Test
public void empty_file() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.touch(tempFile);
    Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
    assertThat(metadata.lines()).isOne();
    assertThat(metadata.nonBlankLines()).isZero();
    assertThat(metadata.hash()).isNotEmpty();
    assertThat(metadata.originalLineStartOffsets()).containsOnly(0);
    assertThat(metadata.originalLineEndOffsets()).containsOnly(0);
    assertThat(metadata.isEmpty()).isTrue();
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Metadata (org.sonar.api.batch.fs.internal.Metadata)26 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)23 File (java.io.File)15 FileInputStream (java.io.FileInputStream)14 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 Charset (java.nio.charset.Charset)2 Path (java.nio.file.Path)2 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TextRange (org.sonar.api.batch.fs.TextRange)1