aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-02-20 15:43:13 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-02-20 16:12:06 +0100
commit4434c2ae06e098bb3dbf1aa21edff2a6857699e0 (patch)
tree0ece0631b0bb9b27a3a38afcda2ae771b76ed653 /sonar-plugin-api
parent237bc2074a3a8641d9464289ddb5d06b9f7565ea (diff)
downloadsonarqube-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')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java6
4 files changed, 62 insertions, 6 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 cebd7c410f8..c639559c60e 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
@@ -43,6 +43,7 @@ public class DefaultInputFile implements InputFile, Serializable {
private Status status;
private int lines;
private Charset charset;
+ private int lastValidOffset;
public DefaultInputFile(String moduleKey, String relativePath) {
this.moduleKey = moduleKey;
@@ -144,6 +145,15 @@ public class DefaultInputFile implements InputFile, Serializable {
return this;
}
+ public int lastValidOffset() {
+ return lastValidOffset;
+ }
+
+ public DefaultInputFile setLastValidOffset(int lastValidOffset) {
+ this.lastValidOffset = lastValidOffset;
+ return this;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java
index 9849c058335..ca1a0b4a355 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java
@@ -23,6 +23,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.internal.DefaultStorable;
@@ -91,12 +92,21 @@ public class DefaultHighlighting extends DefaultStorable implements NewHighlight
@Override
public DefaultHighlighting highlight(int startOffset, int endOffset, TypeOfText typeOfText) {
Preconditions.checkState(inputFile != null, "Call onFile() first");
+ int maxValidOffset = ((DefaultInputFile) inputFile).lastValidOffset();
+ checkOffset(startOffset, maxValidOffset, "startOffset");
+ checkOffset(endOffset, maxValidOffset, "endOffset");
+ Preconditions.checkArgument(startOffset < endOffset, "startOffset (" + startOffset + ") should be < endOffset (" + endOffset + ") for file " + inputFile + ".");
SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(startOffset, endOffset,
typeOfText);
this.syntaxHighlightingRuleSet.add(syntaxHighlightingRule);
return this;
}
+ private void checkOffset(int offset, int maxValidOffset, String label) {
+ Preconditions.checkArgument(offset >= 0 && offset <= maxValidOffset, "Invalid " + label + " " + offset + ". Should be >= 0 and <= " + maxValidOffset
+ + " for file " + inputFile);
+ }
+
@Override
protected void doSave() {
Preconditions.checkState(inputFile != null, "Call onFile() first");
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)