2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.sensor.highlighting.internal;
22 import java.util.Collection;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.batch.fs.TextRange;
28 import org.sonar.api.batch.fs.internal.DefaultInputFile;
29 import org.sonar.api.batch.fs.internal.DefaultTextPointer;
30 import org.sonar.api.batch.fs.internal.DefaultTextRange;
31 import org.sonar.api.batch.sensor.internal.SensorStorage;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.sonar.api.batch.sensor.highlighting.TypeOfText.COMMENT;
36 import static org.sonar.api.batch.sensor.highlighting.TypeOfText.CPP_DOC;
37 import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD;
39 public class DefaultHighlightingTest {
41 private static final DefaultInputFile INPUT_FILE = new DefaultInputFile("foo", "src/Foo.java")
43 .setOriginalLineOffsets(new int[] {0, 50})
44 .setLastValidOffset(100);
46 private Collection<SyntaxHighlightingRule> highlightingRules;
49 public ExpectedException throwable = ExpectedException.none();
52 public void setUpSampleRules() {
54 DefaultHighlighting highlightingDataBuilder = new DefaultHighlighting(mock(SensorStorage.class))
56 .highlight(0, 10, COMMENT)
57 .highlight(10, 12, KEYWORD)
58 .highlight(24, 38, KEYWORD)
59 .highlight(42, 50, KEYWORD)
60 .highlight(24, 65, CPP_DOC)
61 .highlight(12, 20, COMMENT);
63 highlightingDataBuilder.save();
65 highlightingRules = highlightingDataBuilder.getSyntaxHighlightingRuleSet();
69 public void should_register_highlighting_rule() {
70 assertThat(highlightingRules).hasSize(6);
73 private static TextRange rangeOf(int startLine, int startOffset, int endLine, int endOffset) {
74 return new DefaultTextRange(new DefaultTextPointer(startLine, startOffset), new DefaultTextPointer(endLine, endOffset));
78 public void should_order_by_start_then_end_offset() {
79 assertThat(highlightingRules).extracting("range", TextRange.class).containsExactly(rangeOf(1, 0, 1, 10),
80 rangeOf(1, 10, 1, 12),
81 rangeOf(1, 12, 1, 20),
82 rangeOf(1, 24, 2, 15),
83 rangeOf(1, 24, 1, 38),
84 rangeOf(1, 42, 2, 0));
85 assertThat(highlightingRules).extracting("textType").containsOnly(COMMENT, KEYWORD, COMMENT, KEYWORD, CPP_DOC, KEYWORD);
89 public void should_suport_overlapping() {
90 new DefaultHighlighting(mock(SensorStorage.class))
92 .highlight(0, 15, KEYWORD)
93 .highlight(8, 12, CPP_DOC)
98 public void should_prevent_boudaries_overlapping() {
99 throwable.expect(IllegalStateException.class);
101 .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");
103 new DefaultHighlighting(mock(SensorStorage.class))
105 .highlight(0, 10, KEYWORD)
106 .highlight(8, 15, KEYWORD)