Search in sources :

Example 21 with Metadata

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

the class FileMetadataTest method non_ascii_utf_16.

@Test
public void non_ascii_utf_16() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_16, true);
    Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_16, 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".getBytes(StandardCharsets.UTF_8)));
    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 22 with Metadata

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

the class DefaultInputFileTest method selectLine.

@Test
public void selectLine() {
    Metadata metadata = new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new StringReader("bla bla a\nabcde\n\nabc"));
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.selectLine(1).start().line()).isOne();
    assertThat(file.selectLine(1).start().lineOffset()).isZero();
    assertThat(file.selectLine(1).end().line()).isOne();
    assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9);
    // Don't fail when selecting empty line
    assertThat(file.selectLine(3).start().line()).isEqualTo(3);
    assertThat(file.selectLine(3).start().lineOffset()).isZero();
    assertThat(file.selectLine(3).end().line()).isEqualTo(3);
    assertThat(file.selectLine(3).end().lineOffset()).isZero();
    try {
        file.selectLine(5);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)");
    }
}
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) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) StringReader(java.io.StringReader) IOException(java.io.IOException) Test(org.junit.Test)

Example 23 with Metadata

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

the class DefaultInputFileTest method checkValidPointer.

@Test
public void checkValidPointer() {
    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();
    // Don't fail
    file.newPointer(1, 9);
    file.newPointer(2, 0);
    file.newPointer(2, 5);
    try {
        file.newPointer(0, 1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("0 is not a valid line for a file");
    }
    try {
        file.newPointer(3, 1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)");
    }
    try {
        file.newPointer(1, -1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("-1 is not a valid line offset for a file");
    }
    try {
        file.newPointer(1, 10);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
    }
}
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) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with Metadata

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

the class DefaultInputFileTest method checkValidPointerUsingGlobalOffset.

@Test
public void checkValidPointerUsingGlobalOffset() {
    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();
    assertThat(file.newPointer(9).line()).isOne();
    // Ignore eol characters
    assertThat(file.newPointer(9).lineOffset()).isEqualTo(8);
    assertThat(file.newPointer(10).line()).isEqualTo(2);
    assertThat(file.newPointer(10).lineOffset()).isZero();
    assertThat(file.newPointer(15).line()).isEqualTo(2);
    assertThat(file.newPointer(15).lineOffset()).isEqualTo(5);
    assertThat(file.newPointer(16).line()).isEqualTo(2);
    // Ignore eol characters
    assertThat(file.newPointer(16).lineOffset()).isEqualTo(5);
    try {
        file.newPointer(-1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("-1 is not a valid offset for a file");
    }
    try {
        file.newPointer(17);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("17 is not a valid offset for file src/Foo.php. Max offset is 16");
    }
}
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) IOException(java.io.IOException) Test(org.junit.Test)

Example 25 with Metadata

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

the class DefaultInputFileTest method checkValidRange.

@Test
public void checkValidRange() {
    Metadata metadata = new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new StringReader("bla bla a\nabcde"));
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isOne();
    // Don't fail
    file.newRange(file.newPointer(1, 0), file.newPointer(1, 1));
    file.newRange(file.newPointer(1, 0), file.newPointer(1, 9));
    file.newRange(file.newPointer(1, 0), file.newPointer(2, 0));
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15));
    try {
        file.newRange(file.newPointer(1, 0), file.newPointer(1, 0));
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]");
    }
    try {
        file.newRange(file.newPointer(1, 0), file.newPointer(1, 10));
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
    }
}
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) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) StringReader(java.io.StringReader) IOException(java.io.IOException) 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