]> source.dussan.org Git - sonarqube.git/blob
644599bc0dd3613a08e9016873900e27c46dc564
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.scanner.mediumtest.issues;
21
22 import java.io.File;
23 import java.util.List;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.sonar.scanner.mediumtest.ScannerMediumTester;
30 import org.sonar.scanner.mediumtest.TaskResult;
31 import org.sonar.scanner.protocol.output.ScannerReport.Flow;
32 import org.sonar.scanner.protocol.output.ScannerReport.Issue;
33 import org.sonar.scanner.protocol.output.ScannerReport.IssueLocation;
34 import org.sonar.xoo.XooPlugin;
35 import org.sonar.xoo.rule.XooRulesDefinition;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38
39 public class MultilineIssuesMediumTest {
40
41   @org.junit.Rule
42   public TemporaryFolder temp = new TemporaryFolder();
43
44   public ScannerMediumTester tester = ScannerMediumTester.builder()
45     .registerPlugin("xoo", new XooPlugin())
46     .addRules(new XooRulesDefinition())
47     .addDefaultQProfile("xoo", "Sonar Way")
48     .addActiveRule("xoo", "MultilineIssue", null, "Multinile Issue", "MAJOR", null, "xoo")
49     .build();
50
51   private TaskResult result;
52
53   @Before
54   public void prepare() throws Exception {
55     tester.start();
56
57     File projectDir = new File(MultilineIssuesMediumTest.class.getResource("/mediumtest/xoo/sample-multiline").toURI());
58     File tmpDir = temp.getRoot();
59     FileUtils.copyDirectory(projectDir, tmpDir);
60
61     result = tester
62       .newScanTask(new File(tmpDir, "sonar-project.properties"))
63       .start();
64   }
65
66   @After
67   public void stop() {
68     tester.stop();
69   }
70
71   @Test
72   public void testIssueRange() throws Exception {
73     List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Single.xoo"));
74     assertThat(issues).hasSize(1);
75     Issue issue = issues.get(0);
76     assertThat(issue.getMsg()).isEqualTo("Primary location");
77     assertThat(issue.getTextRange().getStartLine()).isEqualTo(6);
78     assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23);
79     assertThat(issue.getTextRange().getEndLine()).isEqualTo(6);
80     assertThat(issue.getTextRange().getEndOffset()).isEqualTo(50);
81   }
82
83   @Test
84   public void testMultilineIssueRange() throws Exception {
85     List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Multiline.xoo"));
86     assertThat(issues).hasSize(1);
87     Issue issue = issues.get(0);
88     assertThat(issue.getMsg()).isEqualTo("Primary location");
89     assertThat(issue.getTextRange().getStartLine()).isEqualTo(6);
90     assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23);
91     assertThat(issue.getTextRange().getEndLine()).isEqualTo(7);
92     assertThat(issue.getTextRange().getEndOffset()).isEqualTo(23);
93   }
94
95   @Test
96   public void testFlowWithSingleLocation() throws Exception {
97     List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Multiple.xoo"));
98     assertThat(issues).hasSize(1);
99     Issue issue = issues.get(0);
100     assertThat(issue.getMsg()).isEqualTo("Primary location");
101     assertThat(issue.getTextRange().getStartLine()).isEqualTo(6);
102     assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23);
103     assertThat(issue.getTextRange().getEndLine()).isEqualTo(6);
104     assertThat(issue.getTextRange().getEndOffset()).isEqualTo(50);
105
106     assertThat(issue.getFlowList()).hasSize(1);
107     Flow flow = issue.getFlow(0);
108     assertThat(flow.getLocationList()).hasSize(1);
109     IssueLocation additionalLocation = flow.getLocation(0);
110     assertThat(additionalLocation.getMsg()).isEqualTo("Flow step #1");
111     assertThat(additionalLocation.getTextRange().getStartLine()).isEqualTo(7);
112     assertThat(additionalLocation.getTextRange().getStartOffset()).isEqualTo(26);
113     assertThat(additionalLocation.getTextRange().getEndLine()).isEqualTo(7);
114     assertThat(additionalLocation.getTextRange().getEndOffset()).isEqualTo(53);
115   }
116
117   @Test
118   public void testFlowsWithMultipleElements() throws Exception {
119     List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/WithFlow.xoo"));
120     assertThat(issues).hasSize(1);
121     Issue issue = issues.get(0);
122     assertThat(issue.getFlowList()).hasSize(1);
123
124     Flow flow = issue.getFlow(0);
125     assertThat(flow.getLocationList()).hasSize(2);
126     // TODO more assertions
127   }
128 }