Search in sources :

Example 6 with Metadata

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

the class FileMetadataTest method mix_of_newlines_without_latest_eol.

@Test
public void mix_of_newlines_without_latest_eol() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "foo\nbar\r\nbaz", 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, 9);
    assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 12);
}
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 7 with Metadata

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

the class DefaultInputFileTest method test_content_exclude_bom.

@Test
public void test_content_exclude_bom() throws IOException {
    Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH);
    Files.createDirectories(testFile.getParent());
    try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.toFile()), StandardCharsets.UTF_8))) {
        out.write('\ufeff');
    }
    String content = "test é string €";
    Files.write(testFile, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
    assertThat(Files.readAllLines(testFile, StandardCharsets.UTF_8).get(0)).hasSize(content.length() + 1);
    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.UTF_8);
    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) FileOutputStream(java.io.FileOutputStream) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 8 with Metadata

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

the class DefaultInputFileTest method test.

@Test
public void test() {
    Metadata metadata = new Metadata(42, 42, "", 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.absolutePath()).endsWith("Foo.php");
    assertThat(inputFile.filename()).isEqualTo("Foo.php");
    assertThat(inputFile.uri()).hasPath(baseDir.resolve(PROJECT_RELATIVE_PATH).toUri().getPath());
    assertThat(new File(inputFile.absolutePath())).isAbsolute();
    assertThat(inputFile.language()).isEqualTo("php");
    assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED);
    assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST);
    assertThat(inputFile.lines()).isEqualTo(42);
    assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1);
    assertThat(inputFile.getModuleRelativePath()).isEqualTo(MODULE_RELATIVE_PATH);
    assertThat(inputFile.getProjectRelativePath()).isEqualTo(PROJECT_RELATIVE_PATH);
    sensorStrategy.setGlobal(false);
    assertThat(inputFile.relativePath()).isEqualTo(MODULE_RELATIVE_PATH);
    assertThat(new File(inputFile.relativePath())).isRelative();
    sensorStrategy.setGlobal(true);
    assertThat(inputFile.relativePath()).isEqualTo(PROJECT_RELATIVE_PATH);
    assertThat(new File(inputFile.relativePath())).isRelative();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Example 9 with Metadata

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

the class DefaultInputFileTest method checkValidRangeUsingGlobalOffset.

@Test
public void checkValidRangeUsingGlobalOffset() {
    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);
    assertThat(newRange.start().lineOffset()).isZero();
    assertThat(newRange.end().line()).isEqualTo(2);
    assertThat(newRange.end().lineOffset()).isEqualTo(3);
}
Also used : DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) TextRange(org.sonar.api.batch.fs.TextRange) Test(org.junit.Test)

Example 10 with Metadata

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

the class DefaultInputFileTest method testRangeOverlap.

@Test
public void testRangeOverlap() {
    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();
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue();
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 1), file.newPointer(1, 2)))).isFalse();
    assertThat(file.newRange(file.newPointer(1, 2), file.newPointer(1, 3)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isFalse();
}
Also used : DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) 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