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;
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();
+ }
}
*/
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;
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();
}
}