3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.issue;
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;
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;
47 public class TrackerRawInputFactoryTest {
49 static int FILE_REF = 2;
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();
55 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
58 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
61 public SourceLinesRepositoryRule fileSourceRepository = new SourceLinesRepositoryRule();
63 CommonRuleEngine commonRuleEngine = mock(CommonRuleEngine.class);
65 TrackerRawInputFactory underTest = new TrackerRawInputFactory(treeRootHolder, reportReader, fileSourceRepository, commonRuleEngine);
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);
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();
77 assertThat(input.getBlockHashSequence()).isNotNull();
81 public void load_source_hash_sequences_only_on_files() throws Exception {
82 Input<DefaultIssue> input = underTest.create(PROJECT);
84 assertThat(input.getLineHashSequence()).isNotNull();
85 assertThat(input.getBlockHashSequence()).isNotNull();
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")
96 .setSeverity(Constants.Severity.BLOCKER)
99 reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
100 Input<DefaultIssue> input = underTest.create(FILE);
102 Collection<DefaultIssue> issues = input.getIssues();
103 assertThat(issues).hasSize(1);
104 DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());
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");
114 // fields set by compute engine
115 assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
116 assertThat(issue.tags()).isEmpty();
117 assertInitializedIssue(issue);
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"))
127 .setSeverity(Constants.Severity.BLOCKER)
129 reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
131 Input<DefaultIssue> input = underTest.create(FILE);
133 assertThat(input.getIssues()).isEmpty();
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")
143 when(commonRuleEngine.process(FILE)).thenReturn(asList(ceIssue));
145 Input<DefaultIssue> input = underTest.create(FILE);
147 assertThat(input.getIssues()).containsOnly(ceIssue);
148 assertInitializedIssue(input.getIssues().iterator().next());
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();