You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShortBranchOrPullRequestTrackerExecutionTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.ce.task.projectanalysis.issue;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Optional;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.mockito.Mock;
  31. import org.mockito.MockitoAnnotations;
  32. import org.sonar.api.rule.RuleKey;
  33. import org.sonar.ce.task.projectanalysis.component.Component;
  34. import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
  35. import org.sonar.core.issue.DefaultIssue;
  36. import org.sonar.core.issue.tracking.BlockHashSequence;
  37. import org.sonar.core.issue.tracking.Input;
  38. import org.sonar.core.issue.tracking.LineHashSequence;
  39. import org.sonar.core.issue.tracking.Tracker;
  40. import org.sonar.core.issue.tracking.Tracking;
  41. import org.sonar.db.protobuf.DbCommons;
  42. import org.sonar.db.protobuf.DbIssues;
  43. import org.sonar.db.rule.RuleTesting;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.mockito.Mockito.when;
  46. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  47. public class ShortBranchOrPullRequestTrackerExecutionTest {
  48. static final String FILE_UUID = "FILE_UUID";
  49. static final String FILE_KEY = "FILE_KEY";
  50. static final int FILE_REF = 2;
  51. static final Component FILE = builder(Component.Type.FILE, FILE_REF)
  52. .setKey(FILE_KEY)
  53. .setUuid(FILE_UUID)
  54. .build();
  55. @Mock
  56. private TrackerRawInputFactory rawFactory;
  57. @Mock
  58. private TrackerBaseInputFactory baseFactory;
  59. @Mock
  60. private TrackerMergeBranchInputFactory mergeFactory;
  61. @Mock
  62. private NewLinesRepository newLinesRepository;
  63. private ShortBranchOrPullRequestTrackerExecution underTest;
  64. private List<DefaultIssue> rawIssues = new ArrayList<>();
  65. private List<DefaultIssue> baseIssues = new ArrayList<>();
  66. private List<DefaultIssue> mergeBranchIssues = new ArrayList<>();
  67. @Before
  68. public void setUp() throws Exception {
  69. MockitoAnnotations.initMocks(this);
  70. when(rawFactory.create(FILE)).thenReturn(createInput(rawIssues));
  71. when(baseFactory.create(FILE)).thenReturn(createInput(baseIssues));
  72. when(mergeFactory.create(FILE)).thenReturn(createInput(mergeBranchIssues));
  73. Tracker<DefaultIssue, DefaultIssue> tracker = new Tracker<>();
  74. underTest = new ShortBranchOrPullRequestTrackerExecution(baseFactory, rawFactory, mergeFactory, tracker, newLinesRepository);
  75. }
  76. @Test
  77. public void simple_tracking_keep_only_issues_having_location_on_changed_lines() {
  78. final DefaultIssue issue1 = createIssue(2, RuleTesting.XOO_X1);
  79. issue1.setLocations(DbIssues.Locations.newBuilder()
  80. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(2).setEndLine(3)).build());
  81. rawIssues.add(issue1);
  82. final DefaultIssue issue2 = createIssue(2, RuleTesting.XOO_X1);
  83. issue2.setLocations(DbIssues.Locations.newBuilder().setTextRange(DbCommons.TextRange.newBuilder().setStartLine(2).setEndLine(2)).build());
  84. rawIssues.add(issue2);
  85. when(mergeFactory.hasMergeBranchAnalysis()).thenReturn(false);
  86. when(newLinesRepository.getNewLines(FILE)).thenReturn(Optional.of(new HashSet<>(Arrays.asList(1, 3))));
  87. Tracking<DefaultIssue, DefaultIssue> tracking = underTest.track(FILE);
  88. assertThat(tracking.getUnmatchedBases()).isEmpty();
  89. assertThat(tracking.getMatchedRaws()).isEmpty();
  90. assertThat(tracking.getUnmatchedRaws()).containsOnly(issue1);
  91. }
  92. @Test
  93. public void simple_tracking_keep_also_issues_having_secondary_locations_on_changed_lines() {
  94. final DefaultIssue issueWithSecondaryLocationOnAChangedLine = createIssue(2, RuleTesting.XOO_X1);
  95. issueWithSecondaryLocationOnAChangedLine.setLocations(DbIssues.Locations.newBuilder()
  96. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(2).setEndLine(3))
  97. .addFlow(DbIssues.Flow.newBuilder().addLocation(DbIssues.Location.newBuilder()
  98. .setComponentId(FILE.getUuid())
  99. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(6).setEndLine(8)).build()).build())
  100. .build());
  101. rawIssues.add(issueWithSecondaryLocationOnAChangedLine);
  102. final DefaultIssue issueWithNoLocationsOnChangedLines = createIssue(2, RuleTesting.XOO_X1);
  103. issueWithNoLocationsOnChangedLines.setLocations(DbIssues.Locations.newBuilder()
  104. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(2).setEndLine(2))
  105. .addFlow(DbIssues.Flow.newBuilder().addLocation(DbIssues.Location.newBuilder()
  106. .setComponentId(FILE.getUuid())
  107. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(11).setEndLine(12)).build()).build())
  108. .build());
  109. rawIssues.add(issueWithNoLocationsOnChangedLines);
  110. final DefaultIssue issueWithALocationOnADifferentFile = createIssue(2, RuleTesting.XOO_X1);
  111. issueWithALocationOnADifferentFile.setLocations(DbIssues.Locations.newBuilder()
  112. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(2).setEndLine(3))
  113. .addFlow(DbIssues.Flow.newBuilder().addLocation(DbIssues.Location.newBuilder()
  114. .setComponentId("anotherUuid")
  115. .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(6).setEndLine(8)).build()).build())
  116. .build());
  117. rawIssues.add(issueWithALocationOnADifferentFile);
  118. when(mergeFactory.hasMergeBranchAnalysis()).thenReturn(false);
  119. when(newLinesRepository.getNewLines(FILE)).thenReturn(Optional.of(new HashSet<>(Arrays.asList(7, 10))));
  120. Tracking<DefaultIssue, DefaultIssue> tracking = underTest.track(FILE);
  121. assertThat(tracking.getUnmatchedBases()).isEmpty();
  122. assertThat(tracking.getMatchedRaws()).isEmpty();
  123. assertThat(tracking.getUnmatchedRaws()).containsOnly(issueWithSecondaryLocationOnAChangedLine);
  124. }
  125. @Test
  126. public void tracking_with_all_results() {
  127. rawIssues.add(createIssue(1, RuleTesting.XOO_X1));
  128. rawIssues.add(createIssue(2, RuleTesting.XOO_X2));
  129. rawIssues.add(createIssue(3, RuleTesting.XOO_X3));
  130. when(mergeFactory.hasMergeBranchAnalysis()).thenReturn(true);
  131. mergeBranchIssues.add(rawIssues.get(0));
  132. baseIssues.add(rawIssues.get(0));
  133. baseIssues.add(rawIssues.get(1));
  134. Tracking<DefaultIssue, DefaultIssue> tracking = underTest.track(FILE);
  135. assertThat(tracking.getMatchedRaws()).isEqualTo(Collections.singletonMap(rawIssues.get(1), rawIssues.get(1)));
  136. assertThat(tracking.getUnmatchedRaws()).containsOnly(rawIssues.get(2));
  137. }
  138. private DefaultIssue createIssue(int line, RuleKey ruleKey) {
  139. DefaultIssue issue = new DefaultIssue()
  140. .setRuleKey(ruleKey)
  141. .setLine(line)
  142. .setMessage("msg" + line);
  143. return issue;
  144. }
  145. private Input<DefaultIssue> createInput(Collection<DefaultIssue> issues) {
  146. return new Input<DefaultIssue>() {
  147. @Override
  148. public LineHashSequence getLineHashSequence() {
  149. return LineHashSequence.createForLines(Arrays.asList("line1", "line2", "line3"));
  150. }
  151. @Override
  152. public BlockHashSequence getBlockHashSequence() {
  153. return BlockHashSequence.create(getLineHashSequence());
  154. }
  155. @Override
  156. public Collection<DefaultIssue> getIssues() {
  157. return issues;
  158. }
  159. };
  160. }
  161. }