Search in sources :

Example 1 with Location

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;
}
Also used : ArrayList(java.util.ArrayList) LocatedTree(org.sonar.maven.model.LocatedTree) Location(org.sonar.java.xml.maven.PomCheckContext.Location)

Example 2 with Location

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);
}
Also used : ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) XmlLocation(org.sonar.maven.model.XmlLocation) Location(org.sonar.java.xml.maven.PomCheckContext.Location) Test(org.junit.Test)

Example 3 with Location

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);
    }
}
Also used : MavenProject(org.sonar.maven.model.maven2.MavenProject) Location(org.sonar.java.xml.maven.PomCheckContext.Location)

Aggregations

Location (org.sonar.java.xml.maven.PomCheckContext.Location)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 LocatedTree (org.sonar.maven.model.LocatedTree)1 XmlLocation (org.sonar.maven.model.XmlLocation)1 MavenProject (org.sonar.maven.model.maven2.MavenProject)1