Browse Source

SONAR-5676 Improve validation of highlighting API

tags/5.3-RC1
Duarte Meneses 8 years ago
parent
commit
ff15786ca7

+ 5
- 0
it/it-projects/testing/xoo-sample-highlighting/sonar-project.properties View File

@@ -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

+ 16
- 0
it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo View File

@@ -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();
}
}
}

+ 3
- 0
it/it-projects/testing/xoo-sample-highlighting/src/main/xoo/sample/Sample.xoo.highlighting View File

@@ -0,0 +1,3 @@
# <startOffset>:<endOffset>:<typeOfText>

20:20:k

+ 48
- 1
sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java View File

@@ -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<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 {


+ 1
- 0
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlighting.java View 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);

+ 12
- 0
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java View 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);

Loading…
Cancel
Save