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