aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-03-30 09:29:50 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-03-31 21:42:42 +0200
commitbe05689a5f84c433a2893ec1707bb40ecc37668d (patch)
tree8e3b673e21eaf4f68d13279ea50ebf26f953ce98 /sonar-plugin-api/src/test/java/org/sonar
parent22ae199fb6c1e162ccf5be8e45794ab00c85ddbb (diff)
downloadsonarqube-be05689a5f84c433a2893ec1707bb40ecc37668d.tar.gz
sonarqube-be05689a5f84c433a2893ec1707bb40ecc37668d.zip
SONAR-6319 SONAR-6321 Feed highlighting and symbols in compute report
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java107
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java273
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java65
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java10
4 files changed, 409 insertions, 46 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
index edf072dca79..6b8b6af9fea 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
@@ -27,6 +27,7 @@ import org.sonar.api.batch.fs.InputFile;
import java.io.File;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
public class DefaultInputFileTest {
@@ -73,4 +74,110 @@ public class DefaultInputFileTest {
DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
assertThat(file.toString()).isEqualTo("[moduleKey=ABCDE, relative=src/Foo.php, basedir=null]");
}
+
+ @Test
+ public void checkValidPointer() {
+ DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
+ file.setLines(2);
+ file.setOriginalLineOffsets(new int[] {0, 10});
+ file.setLastValidOffset(15);
+ assertThat(file.newPointer(1, 0).line()).isEqualTo(1);
+ assertThat(file.newPointer(1, 0).lineOffset()).isEqualTo(0);
+ // Don't fail
+ file.newPointer(1, 9);
+ file.newPointer(2, 0);
+ file.newPointer(2, 5);
+
+ try {
+ file.newPointer(0, 1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("0 is not a valid line for a file");
+ }
+ try {
+ file.newPointer(3, 1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("3 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 2 line(s)");
+ }
+ try {
+ file.newPointer(1, -1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("-1 is not a valid line offset for a file");
+ }
+ try {
+ file.newPointer(1, 10);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=null] has 9 character(s) at line 1");
+ }
+ }
+
+ @Test
+ public void checkValidPointerUsingGlobalOffset() {
+ DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
+ file.setLines(2);
+ file.setOriginalLineOffsets(new int[] {0, 10});
+ file.setLastValidOffset(15);
+ assertThat(file.newPointer(0).line()).isEqualTo(1);
+ assertThat(file.newPointer(0).lineOffset()).isEqualTo(0);
+
+ assertThat(file.newPointer(9).line()).isEqualTo(1);
+ assertThat(file.newPointer(9).lineOffset()).isEqualTo(9);
+
+ assertThat(file.newPointer(10).line()).isEqualTo(2);
+ assertThat(file.newPointer(10).lineOffset()).isEqualTo(0);
+
+ assertThat(file.newPointer(15).line()).isEqualTo(2);
+ assertThat(file.newPointer(15).lineOffset()).isEqualTo(5);
+
+ try {
+ file.newPointer(-1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("-1 is not a valid offset for a file");
+ }
+
+ try {
+ file.newPointer(16);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("16 is not a valid offset for file [moduleKey=ABCDE, relative=src/Foo.php, basedir=null]. Max offset is 15");
+ }
+ }
+
+ @Test
+ public void checkValidRange() {
+ DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
+ file.setLines(2);
+ file.setOriginalLineOffsets(new int[] {0, 10});
+ file.setLastValidOffset(15);
+ assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1);
+ // Don't fail
+ file.newRange(file.newPointer(1, 0), file.newPointer(1, 1));
+ file.newRange(file.newPointer(1, 0), file.newPointer(1, 9));
+ file.newRange(file.newPointer(1, 0), file.newPointer(2, 0));
+ assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15));
+
+ try {
+ file.newRange(file.newPointer(1, 0), file.newPointer(1, 0));
+ fail();
+ } catch (Exception e) {
+ assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]");
+ }
+ }
+
+ @Test
+ public void testRangeOverlap() {
+ DefaultInputFile file = new DefaultInputFile("ABCDE", "src/Foo.php");
+ file.setLines(2);
+ file.setOriginalLineOffsets(new int[] {0, 10});
+ file.setLastValidOffset(15);
+ // Don't fail
+ assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue();
+ assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue();
+ assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 1), file.newPointer(1, 2)))).isFalse();
+ assertThat(file.newRange(file.newPointer(1, 2), file.newPointer(1, 3)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isFalse();
+ }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java
new file mode 100644
index 00000000000..7696c1afcbf
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java
@@ -0,0 +1,273 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import com.google.common.base.Charsets;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.FileMetadata.LineHashConsumer;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.nio.charset.Charset;
+
+import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class FileMetadataTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public LogTester logTester = new LogTester();
+
+ @Test
+ public void empty_file() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.touch(tempFile);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(1);
+ assertThat(metadata.nonBlankLines).isEqualTo(0);
+ assertThat(metadata.hash).isNotEmpty();
+ assertThat(metadata.originalLineOffsets).containsOnly(0);
+ assertThat(metadata.lastValidOffset).isEqualTo(0);
+ assertThat(metadata.empty).isTrue();
+ }
+
+ @Test
+ public void windows_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10);
+ assertThat(metadata.lastValidOffset).isEqualTo(13);
+ assertThat(metadata.empty).isFalse();
+ }
+
+ @Test
+ public void read_with_wrong_encoding() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "marker´s\n", Charset.forName("cp1252"));
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(2);
+ assertThat(metadata.hash).isEqualTo(md5Hex("marker\ufffds\n"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 9);
+ }
+
+ @Test
+ public void non_ascii_utf_8() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbàr\n\u1D11Ebaßz\n"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18);
+ }
+
+ @Test
+ public void non_ascii_utf_16() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", Charsets.UTF_16, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_16);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbàr\n\u1D11Ebaßz\n"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18);
+ }
+
+ @Test
+ public void unix_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8);
+ assertThat(metadata.lastValidOffset).isEqualTo(11);
+ }
+
+ @Test
+ public void unix_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8, 12);
+ assertThat(metadata.lastValidOffset).isEqualTo(12);
+ }
+
+ @Test
+ public void mix_of_newlines_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9, 13);
+ }
+
+ @Test
+ public void several_new_lines() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\n\n\nbar", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(2);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\n\n\nbar"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 5, 6);
+ }
+
+ @Test
+ public void mix_of_newlines_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9);
+ }
+
+ @Test
+ public void start_with_newline() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("\nfoo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 1, 5, 10);
+ }
+
+ @Test
+ public void start_with_bom() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "\uFEFFfoo\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3);
+ assertThat(metadata.nonBlankLines).isEqualTo(3);
+ assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9);
+ }
+
+ @Test
+ public void ignore_whitespace_when_computing_line_hashes() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", Charsets.UTF_8, true);
+
+ DefaultInputFile f = new DefaultInputFile("foo", tempFile.getName());
+ f.setModuleBaseDir(tempFile.getParentFile().toPath());
+ f.setCharset(Charsets.UTF_8);
+ FileMetadata.computeLineHashesForIssueTracking(f, new LineHashConsumer() {
+
+ @Override
+ public void consume(int lineIdx, @Nullable byte[] hash) {
+ switch (lineIdx) {
+ case 1:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("foo"));
+ break;
+ case 2:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("bar"));
+ break;
+ case 3:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("baz"));
+ break;
+ }
+ }
+ });
+ }
+
+ @Test
+ public void should_throw_if_file_does_not_exist() throws Exception {
+ File tempFolder = temp.newFolder();
+ File file = new File(tempFolder, "doesNotExist.txt");
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Fail to read file '" + file.getAbsolutePath() + "' with encoding 'UTF-8'");
+
+ new FileMetadata().readMetadata(file, Charsets.UTF_8);
+ }
+
+ @Test
+ public void line_feed_is_included_into_hash() throws Exception {
+ File file1 = temp.newFile();
+ FileUtils.write(file1, "foo\nbar\n", Charsets.UTF_8, true);
+
+ // same as file1, except an additional return carriage
+ File file1a = temp.newFile();
+ FileUtils.write(file1a, "foo\r\nbar\n", Charsets.UTF_8, true);
+
+ File file2 = temp.newFile();
+ FileUtils.write(file2, "foo\nbar", Charsets.UTF_8, true);
+
+ String hash1 = new FileMetadata().readMetadata(file1, Charsets.UTF_8).hash;
+ String hash1a = new FileMetadata().readMetadata(file1a, Charsets.UTF_8).hash;
+ String hash2 = new FileMetadata().readMetadata(file2, Charsets.UTF_8).hash;
+ assertThat(hash1).isEqualTo(hash1a);
+ assertThat(hash1).isNotEqualTo(hash2);
+ }
+
+ @Test
+ public void binary_file_with_unmappable_character() throws Exception {
+ File woff = new File(this.getClass().getResource("glyphicons-halflings-regular.woff").toURI());
+
+ FileMetadata.Metadata metadata = new FileMetadata().readMetadata(woff, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(135);
+ assertThat(metadata.nonBlankLines).isEqualTo(134);
+ assertThat(metadata.hash).isNotEmpty();
+ assertThat(metadata.empty).isFalse();
+
+ assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file");
+ assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains(
+ "glyphicons-halflings-regular.woff at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.");
+ }
+
+}
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 4cf1225e27b..79f7db9e06b 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
@@ -23,7 +23,10 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.DefaultTextPointer;
+import org.sonar.api.batch.fs.internal.DefaultTextRange;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import java.util.Collection;
@@ -36,6 +39,11 @@ import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD;
public class DefaultHighlightingTest {
+ private static final DefaultInputFile INPUT_FILE = new DefaultInputFile("foo", "src/Foo.java")
+ .setLines(2)
+ .setOriginalLineOffsets(new int[] {0, 50})
+ .setLastValidOffset(100);
+
private Collection<SyntaxHighlightingRule> highlightingRules;
@Rule
@@ -45,7 +53,7 @@ public class DefaultHighlightingTest {
public void setUpSampleRules() {
DefaultHighlighting highlightingDataBuilder = new DefaultHighlighting()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100))
+ .onFile(INPUT_FILE)
.highlight(0, 10, COMMENT)
.highlight(10, 12, KEYWORD)
.highlight(24, 38, KEYWORD)
@@ -61,17 +69,25 @@ public class DefaultHighlightingTest {
assertThat(highlightingRules).hasSize(6);
}
+ private static TextRange rangeOf(int startLine, int startOffset, int endLine, int endOffset) {
+ return new DefaultTextRange(new DefaultTextPointer(startLine, startOffset), new DefaultTextPointer(endLine, endOffset));
+ }
+
@Test
public void should_order_by_start_then_end_offset() throws Exception {
- assertThat(highlightingRules).extracting("startPosition").containsOnly(0, 10, 12, 24, 24, 42);
- assertThat(highlightingRules).extracting("endPosition").containsOnly(10, 12, 20, 38, 65, 50);
+ assertThat(highlightingRules).extracting("range", TextRange.class).containsExactly(rangeOf(1, 0, 1, 10),
+ rangeOf(1, 10, 1, 12),
+ rangeOf(1, 12, 1, 20),
+ rangeOf(1, 24, 2, 15),
+ rangeOf(1, 24, 1, 38),
+ rangeOf(1, 42, 2, 0));
assertThat(highlightingRules).extracting("textType").containsOnly(COMMENT, KEYWORD, COMMENT, KEYWORD, CPP_DOC, KEYWORD);
}
@Test
public void should_suport_overlapping() throws Exception {
new DefaultHighlighting(mock(SensorStorage.class))
- .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100))
+ .onFile(INPUT_FILE)
.highlight(0, 15, KEYWORD)
.highlight(8, 12, CPP_DOC)
.save();
@@ -80,49 +96,14 @@ public class DefaultHighlightingTest {
@Test
public void should_prevent_boudaries_overlapping() throws Exception {
throwable.expect(IllegalStateException.class);
- throwable.expectMessage("Cannot register highlighting rule for characters from 8 to 15 as it overlaps at least one existing rule");
+ throwable
+ .expectMessage("Cannot register highlighting rule for characters at Range[from [line=1, lineOffset=8] to [line=1, lineOffset=15]] as it overlaps at least one existing rule");
new DefaultHighlighting(mock(SensorStorage.class))
- .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100))
+ .onFile(INPUT_FILE)
.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 0be267797b7..c1784a24dfc 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
@@ -26,6 +26,7 @@ import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
@@ -34,6 +35,7 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.RuleKey;
import java.io.File;
+import java.io.StringReader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -142,15 +144,15 @@ public class SensorContextTesterTest {
@Test
public void testHighlighting() {
- assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 3)).isEmpty();
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).isEmpty();
tester.newHighlighting()
- .onFile(new DefaultInputFile("foo", "src/Foo.java").setLastValidOffset(100))
+ .onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
.highlight(0, 4, TypeOfText.ANNOTATION)
.highlight(8, 10, TypeOfText.CONSTANT)
.highlight(9, 10, TypeOfText.COMMENT)
.save();
- assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 3)).containsExactly(TypeOfText.ANNOTATION);
- assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT);
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).containsExactly(TypeOfText.ANNOTATION);
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT);
}
@Test