aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-11-12 16:07:45 +0100
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-11-16 15:12:13 +0100
commitff15786ca7aa029a1ae4a2ed67f5352a1b03853b (patch)
tree8092e240908b8514da9ce847d50c7be27983e60d
parent9db3683485161ca1a5b786ccbed3144d4daf6610 (diff)
downloadsonarqube-ff15786ca7aa029a1ae4a2ed67f5352a1b03853b.tar.gz
sonarqube-ff15786ca7aa029a1ae4a2ed67f5352a1b03853b.zip
SONAR-5676 Improve validation of highlighting API
-rw-r--r--it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties5
-rw-r--r--it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo16
-rw-r--r--it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java49
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java1
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java12
6 files changed, 85 insertions, 1 deletions
diff --git a/it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties b/it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties
new file mode 100644
index 00000000000..86ba98aee61
--- /dev/null
+++ b/it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties
@@ -0,0 +1,5 @@
+sonar.projectKey=sample-highlighting
+sonar.projectName=Sample Highlighting
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.sources=src/main/xoo
+sonar.language=xoo
diff --git a/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo b/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo
new file mode 100644
index 00000000000..41871e123a3
--- /dev/null
+++ b/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo
@@ -0,0 +1,16 @@
+package sample;
+
+public class Sample {
+
+ public Sample(int i) {
+ int j = i++;
+ }
+
+ private String myMethod() {
+ if (foo == bar) {
+ return "hello";
+ } else {
+ throw new IllegalStateException();
+ }
+ }
+}
diff --git a/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting b/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting
new file mode 100644
index 00000000000..4530b0c572b
--- /dev/null
+++ b/it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting
@@ -0,0 +1,3 @@
+# <startOffset>:<endOffset>:<typeOfText>
+
+20:20:k
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java
index bb0ad02c659..9672d2a4b5d 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java
@@ -19,9 +19,16 @@
*/
package org.sonar.batch.mediumtest.highlighting;
+import org.hamcrest.TypeSafeMatcher;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.junit.rules.ExpectedException;
import com.google.common.collect.ImmutableMap;
+
import java.io.File;
import java.io.IOException;
+
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.After;
@@ -34,7 +41,6 @@ import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.batch.mediumtest.BatchMediumTester;
import org.sonar.batch.mediumtest.TaskResult;
import org.sonar.xoo.XooPlugin;
-
import static org.assertj.core.api.Assertions.assertThat;
public class HighlightingMediumTest {
@@ -42,6 +48,9 @@ public class HighlightingMediumTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
public BatchMediumTester tester = BatchMediumTester.builder()
.registerPlugin("xoo", new XooPlugin())
.addDefaultQProfile("xoo", "Sonar Way")
@@ -88,6 +97,44 @@ public class HighlightingMediumTest {
}
@Test
+ public void computeInvalidOffsets() throws IOException {
+
+ File baseDir = temp.newFolder();
+ File srcDir = new File(baseDir, "src");
+ srcDir.mkdir();
+
+ File xooFile = new File(srcDir, "sample.xoo");
+ File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
+ FileUtils.write(xooFile, "Sample xoo\ncontent plop");
+ FileUtils.write(xoohighlightingFile, "0:10:s\n18:18:k");
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Error processing line 2");
+ exception.expectCause(new TypeSafeMatcher<IllegalArgumentException>() {
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Invalid cause");
+ }
+
+ @Override
+ protected boolean matchesSafely(IllegalArgumentException e) {
+ return e.getMessage().contains("Unable to highlight file");
+ }
+ });
+
+ TaskResult result = tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .build())
+ .start();
+ }
+
+ @Test
public void computeSyntaxHighlightingOnBigFile() throws IOException {
File baseDir = temp.newFolder();
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 faf3f7536ec..5dc58102622 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
@@ -79,6 +79,7 @@ public class DefaultHighlighting extends DefaultStorable implements NewHighlight
Preconditions.checkState(inputFile != null, "Call onFile() first");
TextRange newRange;
try {
+ Preconditions.checkArgument(startOffset < endOffset, "start offset should be strictly before end offset");
newRange = inputFile.newRange(startOffset, endOffset);
} catch (Exception e) {
throw new IllegalArgumentException("Unable to highlight file " + inputFile + " from offset " + startOffset + " to offset " + endOffset, e);
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 76378512ff5..dd5ef7bee1b 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
@@ -95,6 +95,18 @@ public class DefaultHighlightingTest {
}
@Test
+ public void should_prevent_start_equal_end() {
+ throwable.expect(IllegalArgumentException.class);
+ throwable
+ .expectMessage("Unable to highlight file");
+
+ new DefaultHighlighting(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .highlight(10, 10, KEYWORD)
+ .save();
+ }
+
+ @Test
public void should_prevent_boudaries_overlapping() {
throwable.expect(IllegalStateException.class);
throwable