private void assertDuplication(Duplication d, int originStartLine, int originEndLine, @Nullable Integer otherFileRef, int rangeStartLine, int rangeEndLine) { assertThat(d.getOriginPosition().getStartLine()).isEqualTo(originStartLine); assertThat(d.getOriginPosition().getEndLine()).isEqualTo(originEndLine); assertThat(d.getDuplicateList()).hasSize(1); if (otherFileRef != null) { assertThat(d.getDuplicate(0).getOtherFileRef()).isEqualTo(otherFileRef); } else { assertThat(d.getDuplicate(0).getOtherFileRef()).isEqualTo(0); } assertThat(d.getDuplicate(0).getRange().getStartLine()).isEqualTo(rangeStartLine); assertThat(d.getDuplicate(0).getRange().getEndLine()).isEqualTo(rangeEndLine); }
@Test public void testIntraFileDuplications() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); String content = "Sample xoo\ncontent\nfoo\nbar\nSample xoo\ncontent\n"; File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, content); AnalysisResult result = tester.newAnalysis() .properties(builder .put("sonar.sources", "src") .put("sonar.cpd.xoo.minimumTokens", "2") .put("sonar.cpd.xoo.minimumLines", "2") .put("sonar.verbose", "true") .build()) .execute(); InputFile inputFile = result.inputFile("src/sample.xoo"); // One clone group List<ScannerReport.Duplication> duplicationGroups = result.duplicationsFor(inputFile); assertThat(duplicationGroups).hasSize(1); ScannerReport.Duplication cloneGroup = duplicationGroups.get(0); assertThat(cloneGroup.getOriginPosition().getStartLine()).isEqualTo(1); assertThat(cloneGroup.getOriginPosition().getEndLine()).isEqualTo(2); assertThat(cloneGroup.getDuplicateList()).hasSize(1); assertThat(cloneGroup.getDuplicate(0).getRange().getStartLine()).isEqualTo(5); assertThat(cloneGroup.getDuplicate(0).getRange().getEndLine()).isEqualTo(6); }
@Override @Nonnull public Duplicate apply(@Nonnull ScannerReport.Duplicate input) { if (input.getOtherFileRef() != 0) { checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same"); return new InProjectDuplicate( treeRootHolder.getComponentByRef(input.getOtherFileRef()), convert(input.getRange())); } return new InnerDuplicate(convert(input.getRange())); } }
@Override @Nonnull public Duplicate apply(@Nonnull ScannerReport.Duplicate input) { if (input.getOtherFileRef() != 0) { checkArgument(input.getOtherFileRef() != file.getReportAttributes().getRef(), "file and otherFile references can not be the same"); Component otherComponent = treeRootHolder.getReportTreeComponentByRef(input.getOtherFileRef()); if ((analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) && otherComponent.getStatus() == Component.Status.SAME) { return new InExtendedProjectDuplicate(otherComponent, convert(input.getRange())); } else { return new InProjectDuplicate(otherComponent, convert(input.getRange())); } } return new InnerDuplicate(convert(input.getRange())); }