]> source.dussan.org Git - sonarqube.git/blob
240c547dc0566420d765bbae5a8ca62905c93ec7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.issue;
21
22 import com.google.common.collect.Iterators;
23 import java.util.Collection;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.issue.Issue;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.tracking.Input;
31 import org.sonar.scanner.protocol.Constants;
32 import org.sonar.scanner.protocol.output.ScannerReport;
33 import org.sonar.scanner.protocol.output.ScannerReport.TextRange;
34 import org.sonar.server.computation.batch.BatchReportReaderRule;
35 import org.sonar.server.computation.batch.TreeRootHolderRule;
36 import org.sonar.server.computation.component.Component;
37 import org.sonar.server.computation.component.ReportComponent;
38 import org.sonar.server.computation.issue.commonrule.CommonRuleEngine;
39 import org.sonar.server.computation.source.SourceLinesRepositoryRule;
40 import org.sonar.server.rule.CommonRuleKeys;
41
42 import static java.util.Arrays.asList;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
47 public class TrackerRawInputFactoryTest {
48
49   static int FILE_REF = 2;
50
51   static ReportComponent PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1).setKey("PROJECT_KEY_2").setUuid("PROJECT_UUID_1").build();
52   static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY_2").setUuid("FILE_UUID_2").build();
53
54   @Rule
55   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
56
57   @Rule
58   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
59
60   @Rule
61   public SourceLinesRepositoryRule fileSourceRepository = new SourceLinesRepositoryRule();
62
63   CommonRuleEngine commonRuleEngine = mock(CommonRuleEngine.class);
64
65   TrackerRawInputFactory underTest = new TrackerRawInputFactory(treeRootHolder, reportReader, fileSourceRepository, commonRuleEngine);
66
67   @Test
68   public void load_source_hash_sequences() throws Exception {
69     fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
70     Input<DefaultIssue> input = underTest.create(FILE);
71
72     assertThat(input.getLineHashSequence()).isNotNull();
73     assertThat(input.getLineHashSequence().getHashForLine(1)).isNotEmpty();
74     assertThat(input.getLineHashSequence().getHashForLine(2)).isNotEmpty();
75     assertThat(input.getLineHashSequence().getHashForLine(3)).isEmpty();
76
77     assertThat(input.getBlockHashSequence()).isNotNull();
78   }
79
80   @Test
81   public void load_source_hash_sequences_only_on_files() throws Exception {
82     Input<DefaultIssue> input = underTest.create(PROJECT);
83
84     assertThat(input.getLineHashSequence()).isNotNull();
85     assertThat(input.getBlockHashSequence()).isNotNull();
86   }
87
88   @Test
89   public void load_issues() throws Exception {
90     fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
91     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
92       .setTextRange(TextRange.newBuilder().setStartLine(2).build())
93       .setMsg("the message")
94       .setRuleRepository("java")
95       .setRuleKey("S001")
96       .setSeverity(Constants.Severity.BLOCKER)
97       .setGap(3.14)
98       .build();
99     reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
100     Input<DefaultIssue> input = underTest.create(FILE);
101
102     Collection<DefaultIssue> issues = input.getIssues();
103     assertThat(issues).hasSize(1);
104     DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());
105
106     // fields set by analysis report
107     assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("java", "S001"));
108     assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
109     assertThat(issue.line()).isEqualTo(2);
110     assertThat(issue.effortToFix()).isEqualTo(3.14);
111     assertThat(issue.gap()).isEqualTo(3.14);
112     assertThat(issue.message()).isEqualTo("the message");
113
114     // fields set by compute engine
115     assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
116     assertThat(issue.tags()).isEmpty();
117     assertInitializedIssue(issue);
118   }
119
120   @Test
121   public void ignore_report_issues_on_common_rules() throws Exception {
122     fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
123     ScannerReport.Issue reportIssue = ScannerReport.Issue.newBuilder()
124       .setMsg("the message")
125       .setRuleRepository(CommonRuleKeys.commonRepositoryForLang("java"))
126       .setRuleKey("S001")
127       .setSeverity(Constants.Severity.BLOCKER)
128       .build();
129     reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
130
131     Input<DefaultIssue> input = underTest.create(FILE);
132
133     assertThat(input.getIssues()).isEmpty();
134   }
135
136   @Test
137   public void load_issues_of_compute_engine_common_rules() throws Exception {
138     fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
139     DefaultIssue ceIssue = new DefaultIssue()
140       .setRuleKey(RuleKey.of(CommonRuleKeys.commonRepositoryForLang("java"), "InsufficientCoverage"))
141       .setMessage("not enough coverage")
142       .setGap(10.0);
143     when(commonRuleEngine.process(FILE)).thenReturn(asList(ceIssue));
144
145     Input<DefaultIssue> input = underTest.create(FILE);
146
147     assertThat(input.getIssues()).containsOnly(ceIssue);
148     assertInitializedIssue(input.getIssues().iterator().next());
149   }
150
151   private void assertInitializedIssue(DefaultIssue issue) {
152     assertThat(issue.componentKey()).isEqualTo(FILE.getKey());
153     assertThat(issue.componentUuid()).isEqualTo(FILE.getUuid());
154     assertThat(issue.resolution()).isNull();
155     assertThat(issue.status()).isEqualTo(Issue.STATUS_OPEN);
156     assertThat(issue.key()).isNull();
157     assertThat(issue.authorLogin()).isNull();
158     assertThat(issue.debt()).isNull();
159   }
160 }