diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-20 15:43:13 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-20 16:12:06 +0100 |
commit | 4434c2ae06e098bb3dbf1aa21edff2a6857699e0 (patch) | |
tree | 0ece0631b0bb9b27a3a38afcda2ae771b76ed653 /sonar-plugin-api/src/test | |
parent | 237bc2074a3a8641d9464289ddb5d06b9f7565ea (diff) | |
download | sonarqube-4434c2ae06e098bb3dbf1aa21edff2a6857699e0.tar.gz sonarqube-4434c2ae06e098bb3dbf1aa21edff2a6857699e0.zip |
SONAR-6175 Validate that offsets provided for highlighting and symbol reference are val
Diffstat (limited to 'sonar-plugin-api/src/test')
2 files changed, 42 insertions, 6 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java index 55900838bc6..4cf1225e27b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java @@ -45,7 +45,7 @@ public class DefaultHighlightingTest { public void setUpSampleRules() { DefaultHighlighting highlightingDataBuilder = new DefaultHighlighting() - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100)) .highlight(0, 10, COMMENT) .highlight(10, 12, KEYWORD) .highlight(24, 38, KEYWORD) @@ -71,7 +71,7 @@ public class DefaultHighlightingTest { @Test public void should_suport_overlapping() throws Exception { new DefaultHighlighting(mock(SensorStorage.class)) - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100)) .highlight(0, 15, KEYWORD) .highlight(8, 12, CPP_DOC) .save(); @@ -83,10 +83,46 @@ public class DefaultHighlightingTest { throwable.expectMessage("Cannot register highlighting rule for characters from 8 to 15 as it overlaps at least one existing rule"); new DefaultHighlighting(mock(SensorStorage.class)) - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100)) .highlight(0, 10, KEYWORD) .highlight(8, 15, KEYWORD) .save(); } + @Test + public void should_prevent_invalid_offset() throws Exception { + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Invalid endOffset 15. Should be >= 0 and <= 10 for file [moduleKey=foo, relative=src/Foo.java, basedir=null]"); + + new DefaultHighlighting(mock(SensorStorage.class)) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(10)) + .highlight(0, 10, KEYWORD) + .highlight(8, 15, KEYWORD) + .save(); + } + + @Test + public void positive_offset() throws Exception { + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Invalid startOffset -8. Should be >= 0 and <= 10 for file [moduleKey=foo, relative=src/Foo.java, basedir=null]"); + + new DefaultHighlighting(mock(SensorStorage.class)) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(10)) + .highlight(0, 10, KEYWORD) + .highlight(-8, 15, KEYWORD) + .save(); + } + + @Test + public void should_prevent_invalid_offset_order() throws Exception { + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("startOffset (18) should be < endOffset (15) for file [moduleKey=foo, relative=src/Foo.java, basedir=null]."); + + new DefaultHighlighting(mock(SensorStorage.class)) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(20)) + .highlight(0, 10, KEYWORD) + .highlight(18, 15, KEYWORD) + .save(); + } + } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 92a3e262fc1..0be267797b7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -87,12 +87,12 @@ public class SensorContextTesterTest { assertThat(tester.issues("foo:src/Foo.java")).isEmpty(); assertThat(tester.allIssues()).isEmpty(); tester.newIssue() - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLines(10)) .forRule(RuleKey.of("repo", "rule")) .atLine(1) .save(); tester.newIssue() - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLines(10)) .forRule(RuleKey.of("repo", "rule")) .atLine(3) .save(); @@ -144,7 +144,7 @@ public class SensorContextTesterTest { public void testHighlighting() { assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 3)).isEmpty(); tester.newHighlighting() - .onFile(new DefaultInputFile("foo", "src/Foo.java")) + .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100)) .highlight(0, 4, TypeOfText.ANNOTATION) .highlight(8, 10, TypeOfText.CONSTANT) .highlight(9, 10, TypeOfText.COMMENT) |