]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9904 fix author of issue to not depend on analyzer implementations
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 15 Aug 2018 22:20:01 +0000 (00:20 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 17 Aug 2018 18:21:31 +0000 (20:21 +0200)
The Xoo plugin used in QA tests declares the component on all the
issue locations. That is not the case with SonarJava for instance.
It assumes that the component is the one declared on the issue.

server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IssueLocations.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/IssueLocationsTest.java

index ed356f0242784ea8539476975f99dca2d4a0fde5..d5de3e02ab847e52ca77550600fee54b866f24f0 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.issue;
 import java.util.Objects;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
+import org.apache.commons.lang.StringUtils;
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.db.protobuf.DbCommons;
 import org.sonar.db.protobuf.DbIssues;
@@ -50,8 +51,15 @@ class IssueLocations {
       locations.hasTextRange() ? Stream.of(locations.getTextRange()) : Stream.empty(),
       locations.getFlowList().stream()
         .flatMap(f -> f.getLocationList().stream())
-        .filter(l -> Objects.equals(l.getComponentId(), componentId))
+        .filter(l -> Objects.equals(componentIdOf(issue, l), componentId))
         .map(DbIssues.Location::getTextRange));
     return textRanges.flatMapToInt(range -> IntStream.rangeClosed(range.getStartLine(), range.getEndLine()));
   }
+
+  private static String componentIdOf(DefaultIssue issue, DbIssues.Location location) {
+    if (location.hasComponentId()) {
+      return StringUtils.defaultIfEmpty(location.getComponentId(), issue.componentUuid());
+    }
+    return issue.componentUuid();
+  }
 }
index 4d23cf46457aec0768468127ce58844bfa8cc16c..4588e420d8ecb46a27d8828431ab16c75c4ff261 100644 (file)
  */
 package org.sonar.ce.task.projectanalysis.issue;
 
+import javax.annotation.Nullable;
 import org.junit.Test;
 import org.sonar.core.issue.DefaultIssue;
+import org.sonar.core.util.Protobuf;
 import org.sonar.db.protobuf.DbCommons;
 import org.sonar.db.protobuf.DbIssues;
 
@@ -80,10 +82,27 @@ public class IssueLocationsTest {
     assertThat(IssueLocations.allLinesFor(issue, "file1")).isEmpty();
   }
 
-  private static DbIssues.Location newLocation(String componentId, int startLine, int endLine) {
-    return DbIssues.Location.newBuilder()
-      .setComponentId(componentId)
-      .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(startLine).setEndLine(endLine).build())
+  @Test
+  public void allLinesFor_default_component_of_location_is_the_issue_component() {
+    DbIssues.Locations.Builder locations = DbIssues.Locations.newBuilder();
+    locations.addFlowBuilder()
+      .addLocation(newLocation("", 5, 5))
+      .addLocation(newLocation(null, 7, 7))
+      .addLocation(newLocation("file2", 9, 9))
       .build();
+    DefaultIssue issue = new DefaultIssue()
+      .setComponentUuid("file1")
+      .setLocations(locations.build());
+
+    assertThat(IssueLocations.allLinesFor(issue, "file1")).containsExactlyInAnyOrder(5, 7);
+    assertThat(IssueLocations.allLinesFor(issue, "file2")).containsExactlyInAnyOrder(9);
+    assertThat(IssueLocations.allLinesFor(issue, "file3")).isEmpty();
+  }
+
+  private static DbIssues.Location newLocation(@Nullable String componentId, int startLine, int endLine) {
+    DbIssues.Location.Builder builder = DbIssues.Location.newBuilder()
+      .setTextRange(DbCommons.TextRange.newBuilder().setStartLine(startLine).setEndLine(endLine).build());
+    Protobuf.setNullable(componentId, builder::setComponentId);
+    return builder.build();
   }
 }