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);
}
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)");
}
}
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");
}
}
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");
}
}
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");
}
}
Aggregations