diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-04-13 11:59:54 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-04-13 11:59:54 +0200 |
commit | 1434abc6dcf716a06267a109908c7dc920fad9c8 (patch) | |
tree | d64988b92b74bc4372fd1836f4296d9796b676e3 /sonar-plugin-api | |
parent | b80b6a014a747f42f589cc9c7cc59b5a42b21e9a (diff) | |
download | sonarqube-1434abc6dcf716a06267a109908c7dc920fad9c8.tar.gz sonarqube-1434abc6dcf716a06267a109908c7dc920fad9c8.zip |
Restore old behavior for InputFile::lines() -> returns 1 for empty files
Diffstat (limited to 'sonar-plugin-api')
4 files changed, 25 insertions, 16 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index 4e0af79ccc7..d98180bf417 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -105,12 +105,18 @@ public interface InputFile extends InputPath { Status status(); /** - * Number of physical lines. This method supports all end-of-line characters. Returns - * zero if the file is empty. + * Number of physical lines. This method supports all end-of-line characters. Formula is (number of line break + 1). Returns + * 1 if the file is empty.</br> Returns 2 for <tt>foo\nbar</tt>. Returns 3 for <tt>foo\nbar\n</tt>. */ int lines(); /** + * Check if the file content is empty (ignore potential BOM). + * @since 5.2 + */ + boolean isEmpty(); + + /** * Return a {@link TextPointer} in the given file. * @param line Line of the pointer. Start at 1. * @param lineOffset Offset in the line. Start at 0. diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index 4f89a1c0dd3..5190bb1a0f1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -104,6 +104,11 @@ public class DefaultInputFile implements InputFile { return lines; } + @Override + public boolean isEmpty() { + return lastValidOffset == 0; + } + /** * Component key. */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java index cb5ea5660af..fa894077be4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java @@ -66,7 +66,7 @@ public class FileMetadata implements BatchComponent { } private static class LineCounter extends CharHandler { - private int lines = 0; + private int lines = 1; private int nonBlankLines = 0; private boolean blankLine = true; boolean alreadyLoggedInvalidCharacter = false; @@ -80,9 +80,6 @@ public class FileMetadata implements BatchComponent { @Override protected void handleAll(char c) { - if (this.lines == 0) { - this.lines = 1; - } if (!alreadyLoggedInvalidCharacter && c == '\ufffd') { LOG.warn("Invalid character encountered in file {} at line {} for encoding {}. Please fix file content or configure the encoding to be used using property '{}'.", file, lines, encoding, CoreProperties.ENCODING_PROPERTY); @@ -156,20 +153,13 @@ public class FileMetadata implements BatchComponent { private final MessageDigest lineMd5Digest = DigestUtils.getMd5Digest(); private final StringBuilder sb = new StringBuilder(); private final LineHashConsumer consumer; - private int line = 0; + private int line = 1; public LineHashComputer(LineHashConsumer consumer) { this.consumer = consumer; } @Override - protected void handleAll(char c) { - if (this.line == 0) { - this.line = 1; - } - } - - @Override protected void handleIgnoreEoL(char c) { if (!Character.isWhitespace(c)) { sb.append(c); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java index 9e06a3e9e5c..e26451cf82d 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java @@ -56,7 +56,7 @@ public class FileMetadataTest { FileUtils.touch(tempFile); FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8); - assertThat(metadata.lines).isEqualTo(0); + assertThat(metadata.lines).isEqualTo(1); assertThat(metadata.nonBlankLines).isEqualTo(0); assertThat(metadata.hash).isNotEmpty(); assertThat(metadata.originalLineOffsets).containsOnly(0); @@ -219,6 +219,8 @@ public class FileMetadataTest { case 3: assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("baz")); break; + default: + fail("Invalid line"); } } }); @@ -236,7 +238,13 @@ public class FileMetadataTest { @Override public void consume(int lineIdx, @Nullable byte[] hash) { - fail("File is empty and should not report any line hash"); + switch (lineIdx) { + case 1: + assertThat(hash).isNull(); + break; + default: + fail("Invalid line"); + } } }); } |