From ff15786ca7aa029a1ae4a2ed67f5352a1b03853b Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 12 Nov 2015 16:07:45 +0100 Subject: [PATCH] SONAR-5676 Improve validation of highlighting API --- .../sonar-project.properties | 5 ++ .../src/main/xoo/sample/Sample.xoo | 16 ++++++ .../main/xoo/sample/Sample.xoo.highlighting | 3 ++ .../highlighting/HighlightingMediumTest.java | 49 ++++++++++++++++++- .../internal/DefaultHighlighting.java | 1 + .../internal/DefaultHighlightingTest.java | 12 +++++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties create mode 100644 it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo create mode 100644 it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting 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 @@ +# :: + +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") @@ -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() { + @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.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 { 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 @@ -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); -- 2.39.5