From eb67b4bc9e53af9d960eff8e539c5162ac6264b9 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 6 Jul 2016 09:46:14 +0200 Subject: [PATCH] SONAR-7664 Fail on empty TextRange (except when selecting empty line) --- .../batch/fs/internal/DefaultInputFile.java | 13 ++++---- .../fs/internal/DefaultInputFileTest.java | 32 +++++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) 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 b43e33d916e..031fa6de245 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 @@ -227,21 +227,21 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile public TextRange newRange(TextPointer start, TextPointer end) { checkValid(start, "start pointer"); checkValid(end, "end pointer"); - return newRangeValidPointers(start, end); + return newRangeValidPointers(start, end, false); } @Override public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) { TextPointer start = newPointer(startLine, startLineOffset); TextPointer end = newPointer(endLine, endLineOffset); - return newRangeValidPointers(start, end); + return newRangeValidPointers(start, end, false); } @Override public TextRange selectLine(int line) { TextPointer startPointer = newPointer(line, 0); TextPointer endPointer = newPointer(line, lineLength(line)); - return newRangeValidPointers(startPointer, endPointer); + return newRangeValidPointers(startPointer, endPointer, true); } public void validate(TextRange range) { @@ -249,8 +249,9 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile checkValid(range.end(), "end pointer"); } - private static TextRange newRangeValidPointers(TextPointer start, TextPointer end) { - Preconditions.checkArgument(start.compareTo(end) <= 0, "Start pointer %s should be before end pointer %s", start, end); + private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { + Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), + "Start pointer %s should be before end pointer %s", start, end); return new DefaultTextRange(start, end); } @@ -258,7 +259,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile * Create Range from global offsets. Used for backward compatibility with older API. */ public TextRange newRange(int startOffset, int endOffset) { - return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset)); + return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset), false); } public TextPointer newPointer(int globalOffset) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java index 1f51a777425..6b06eabe4b9 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java @@ -152,22 +152,19 @@ public class DefaultInputFileTest { @Test public void checkValidRange() { DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php"); - file.setLines(2); - file.setOriginalLineOffsets(new int[] {0, 10}); - file.setLastValidOffset(15); + file.initMetadata("bla bla a\nabcde"); assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1); // 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)); - file.newRange(file.newPointer(1, 0), file.newPointer(1, 0)); try { - file.newRange(file.newPointer(1, 1), file.newPointer(1, 0)); + file.newRange(file.newPointer(1, 0), file.newPointer(1, 0)); fail(); } catch (Exception e) { - assertThat(e).hasMessage("Start pointer [line=1, lineOffset=1] should be before end pointer [line=1, lineOffset=0]"); + 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)); @@ -177,6 +174,29 @@ public class DefaultInputFileTest { } } + @Test + public void selectLine() { + DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php"); + file.initMetadata("bla bla a\nabcde\n\nabc"); + assertThat(file.selectLine(1).start().line()).isEqualTo(1); + assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0); + assertThat(file.selectLine(1).end().line()).isEqualTo(1); + 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()).isEqualTo(0); + assertThat(file.selectLine(3).end().line()).isEqualTo(3); + assertThat(file.selectLine(3).end().lineOffset()).isEqualTo(0); + + try { + file.selectLine(5); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("5 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 4 line(s)"); + } + } + @Test public void checkValidRangeUsingGlobalOffset() { DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php"); -- 2.39.5