]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5676 Improve validation of highlighting API
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 12 Nov 2015 15:07:45 +0000 (16:07 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 16 Nov 2015 14:12:13 +0000 (15:12 +0100)
it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties [new file with mode: 0644]
it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo [new file with mode: 0644]
it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java

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 (file)
index 0000000..86ba98a
--- /dev/null
@@ -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 (file)
index 0000000..41871e1
--- /dev/null
@@ -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 (file)
index 0000000..4530b0c
--- /dev/null
@@ -0,0 +1,3 @@
+# <startOffset>:<endOffset>:<typeOfText>
+
+20:20:k
index bb0ad02c6590632ec2b5c2d060181a1d66a3925c..9672d2a4b5dcf24bc782fdec7c5a44c352942bd2 100644 (file)
  */
 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")
@@ -87,6 +96,44 @@ public class HighlightingMediumTest {
     assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty();
   }
 
+  @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 {
 
index faf3f7536ec7f34347458f18fb1312bbb9847936..5dc5810262270b5321567be8075afe113a4308c6 100644 (file)
@@ -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);
index 76378512ff5b670a6266c2263b695f36cec36671..dd5ef7bee1bf6c78bd2e3956fb87380069e98b7d 100644 (file)
@@ -94,6 +94,18 @@ public class DefaultHighlightingTest {
       .save();
   }
 
+  @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);