3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.scanner.mediumtest.highlighting;
22 import com.google.common.collect.ImmutableMap;
24 import java.io.IOException;
25 import org.apache.commons.io.FileUtils;
26 import org.hamcrest.Description;
27 import org.hamcrest.TypeSafeMatcher;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.rules.TemporaryFolder;
34 import org.sonar.api.batch.fs.InputFile;
35 import org.sonar.api.batch.sensor.highlighting.TypeOfText;
36 import org.sonar.scanner.mediumtest.ScannerMediumTester;
37 import org.sonar.scanner.mediumtest.TaskResult;
38 import org.sonar.xoo.XooPlugin;
40 import static org.assertj.core.api.Assertions.assertThat;
42 public class HighlightingMediumTest {
45 public TemporaryFolder temp = new TemporaryFolder();
48 public ExpectedException exception = ExpectedException.none();
50 public ScannerMediumTester tester = ScannerMediumTester.builder()
51 .registerPlugin("xoo", new XooPlugin())
52 .addDefaultQProfile("xoo", "Sonar Way")
56 public void prepare() {
66 public void computeSyntaxHighlightingOnTempProject() throws IOException {
68 File baseDir = temp.newFolder();
69 File srcDir = new File(baseDir, "src");
72 File xooFile = new File(srcDir, "sample.xoo");
73 File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
74 FileUtils.write(xooFile, "Sample xoo\ncontent plop");
75 FileUtils.write(xoohighlightingFile, "0:10:s\n11:18:k");
77 TaskResult result = tester.newTask()
78 .properties(ImmutableMap.<String, String>builder()
79 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
80 .put("sonar.projectKey", "com.foo.project")
81 .put("sonar.projectName", "Foo Project")
82 .put("sonar.projectVersion", "1.0-SNAPSHOT")
83 .put("sonar.projectDescription", "Description of Foo Project")
84 .put("sonar.sources", "src")
88 InputFile file = result.inputFile("src/sample.xoo");
89 assertThat(result.highlightingTypeFor(file, 1, 0)).containsExactly(TypeOfText.STRING);
90 assertThat(result.highlightingTypeFor(file, 1, 9)).containsExactly(TypeOfText.STRING);
91 assertThat(result.highlightingTypeFor(file, 2, 0)).containsExactly(TypeOfText.KEYWORD);
92 assertThat(result.highlightingTypeFor(file, 2, 8)).isEmpty();
96 public void saveTwice() throws IOException {
97 File baseDir = temp.newFolder();
98 File srcDir = new File(baseDir, "src");
101 File xooFile = new File(srcDir, "sample.xoo");
102 FileUtils.write(xooFile, "Sample xoo\ncontent plop");
104 exception.expect(UnsupportedOperationException.class);
105 exception.expectMessage("Trying to save highlighting twice for the same file is not supported");
107 .properties(ImmutableMap.<String, String>builder()
108 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
109 .put("sonar.projectKey", "com.foo.project")
110 .put("sonar.projectName", "Foo Project")
111 .put("sonar.projectVersion", "1.0-SNAPSHOT")
112 .put("sonar.projectDescription", "Description of Foo Project")
113 .put("sonar.sources", "src")
114 .put("sonar.it.savedatatwice", "true")
121 public void computeInvalidOffsets() throws IOException {
123 File baseDir = temp.newFolder();
124 File srcDir = new File(baseDir, "src");
127 File xooFile = new File(srcDir, "sample.xoo");
128 File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting");
129 FileUtils.write(xooFile, "Sample xoo\ncontent plop");
130 FileUtils.write(xoohighlightingFile, "0:10:s\n18:18:k");
132 exception.expect(IllegalStateException.class);
133 exception.expectMessage("Error processing line 2");
134 exception.expectCause(new TypeSafeMatcher<IllegalArgumentException>() {
136 public void describeTo(Description description) {
137 description.appendText("Invalid cause");
141 protected boolean matchesSafely(IllegalArgumentException e) {
142 return e.getMessage().contains("Unable to highlight file");
147 .properties(ImmutableMap.<String, String>builder()
148 .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
149 .put("sonar.projectKey", "com.foo.project")
150 .put("sonar.projectName", "Foo Project")
151 .put("sonar.projectVersion", "1.0-SNAPSHOT")
152 .put("sonar.projectDescription", "Description of Foo Project")
153 .put("sonar.sources", "src")