use of org.sonar.java.xml.maven.PomCheckContext.Location in project sonar-java by SonarSource.
the class PomElementOrderCheck method checkPositions.
private static List<Location> checkPositions(LocatedTree... trees) {
List<LocatedTree> expectedOrder = Arrays.stream(trees).filter(Objects::nonNull).collect(Collectors.toList());
List<LocatedTree> observedOrder = expectedOrder.stream().sorted(LINE_COMPARATOR).collect(Collectors.toList());
int lastWrongPosition = -1;
int firstWrongPosition = -1;
for (int index = 0; index < expectedOrder.size(); index++) {
if (observedOrder.indexOf(expectedOrder.get(index)) != index) {
lastWrongPosition = index;
if (firstWrongPosition == -1) {
firstWrongPosition = index;
}
}
}
if (lastWrongPosition == -1) {
return Collections.emptyList();
}
List<Location> issues = new ArrayList<>();
// only reports between first and last wrong position
for (int index = firstWrongPosition; index <= lastWrongPosition; index++) {
issues.add(new Location("Expected position: " + (index + 1), expectedOrder.get(index).startLocation().line()));
}
return issues;
}
use of org.sonar.java.xml.maven.PomCheckContext.Location in project sonar-java by SonarSource.
the class PomCheckContextImplTest method should_report_issue_on_lines_of_all_locations.
@Test
public void should_report_issue_on_lines_of_all_locations() {
ArrayList<Location> secondaries = new ArrayList<>();
// secondary on located tree
int secondary1Line = 1;
int secondary1Column = 3;
int secondary1Size = 5;
String msg1 = "msg1";
secondaries.add(new Location(msg1, fakeLocatedTree(secondary1Line, secondary1Column, secondary1Size)));
// secondary on located tree with unknown column - will use the complete line
int secondary2Line = 2;
String msg2 = "msg2";
secondaries.add(new Location(msg2, fakeLocatedTreeWithUnknownColumn(secondary2Line)));
// secondary on a given line with unknown column
int secondary3Line = 3;
String msg3 = "msg3";
secondaries.add(new Location(msg3, secondary3Line));
String msg = "message";
context.reportIssue(CHECK, LINE, msg, secondaries);
String expected = "analyzerMessage:" + msg;
expected += ";" + msg1 + "[" + secondary1Line + ";" + (secondary1Column - 1) + "/" + secondary1Line + ";" + (secondary1Column + secondary1Size - 1) + "]";
expected += ";" + msg2 + "[" + secondary2Line + ";0/" + secondary2Line + ";6]";
expected += ";" + msg3 + "[" + secondary3Line + ";0/" + secondary3Line + ";16]";
assertThat(reportedMessage).isEqualTo(expected);
}
use of org.sonar.java.xml.maven.PomCheckContext.Location in project sonar-java by SonarSource.
the class PomElementOrderCheck method scanFile.
@Override
public void scanFile(PomCheckContext context) {
MavenProject project = context.getMavenProject();
List<Location> issues = checkPositions(project.getModelVersion(), project.getParent(), project.getGroupId(), project.getArtifactId(), project.getVersion(), project.getPackaging(), project.getName(), project.getDescription(), project.getUrl(), project.getInceptionYear(), project.getOrganization(), project.getLicenses(), project.getDevelopers(), project.getContributors(), project.getMailingLists(), project.getPrerequisites(), project.getModules(), project.getScm(), project.getIssueManagement(), project.getCiManagement(), project.getDistributionManagement(), project.getProperties(), project.getDependencyManagement(), project.getDependencies(), project.getRepositories(), project.getPluginRepositories(), project.getBuild(), project.getReporting(), project.getProfiles());
if (!issues.isEmpty()) {
context.reportIssue(this, project.startLocation().line(), "Reorder the elements of this pom to match the recommended order.", issues);
}
}
Aggregations