import javax.annotation.Nonnull;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
import org.sonar.ce.task.projectanalysis.component.Component;
-import org.sonar.ce.task.projectanalysis.component.ComponentVisitor;
import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
@Override
public String getDescription() {
- return "Load inner file and in project duplications";
+ return "Load duplications";
}
@Override
public void execute(ComputationStep.Context context) {
- new DepthTraversalTypeAwareCrawler(
- new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, ComponentVisitor.Order.POST_ORDER) {
- @Override
- public void visitFile(Component file) {
- try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
- int idGenerator = 1;
- while (duplications.hasNext()) {
- loadDuplications(file, duplications.next(), idGenerator);
- idGenerator++;
- }
- }
- }
- }).visit(treeRootHolder.getRoot());
- }
-
- private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
- duplicationRepository.add(file,
- new Duplication(
- convert(duplication.getOriginPosition(), id),
- from(duplication.getDuplicateList())
- .transform(new BatchDuplicateToCeDuplicate(file))));
- }
-
- private static TextBlock convert(ScannerReport.TextRange textRange) {
- return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
- }
-
- private static DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
- return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
+ DuplicationVisitor visitor = new DuplicationVisitor();
+ new DepthTraversalTypeAwareCrawler(visitor).visit(treeRootHolder.getRoot());
+ context.getStatistics().add("duplications", visitor.count);
}
private class BatchDuplicateToCeDuplicate implements Function<ScannerReport.Duplicate, Duplicate> {
}
return new InnerDuplicate(convert(input.getRange()));
}
+
+ private TextBlock convert(ScannerReport.TextRange textRange) {
+ return new TextBlock(textRange.getStartLine(), textRange.getEndLine());
+ }
+ }
+
+ private class DuplicationVisitor extends TypeAwareVisitorAdapter {
+ private int count = 0;
+
+ private DuplicationVisitor() {
+ super(CrawlerDepthLimit.FILE, Order.POST_ORDER);
+ }
+
+ @Override
+ public void visitFile(Component file) {
+ try (CloseableIterator<ScannerReport.Duplication> duplications = batchReportReader.readComponentDuplications(file.getReportAttributes().getRef())) {
+ int idGenerator = 1;
+ while (duplications.hasNext()) {
+ loadDuplications(file, duplications.next(), idGenerator);
+ idGenerator++;
+ count++;
+ }
+ }
+ }
+
+ private void loadDuplications(Component file, ScannerReport.Duplication duplication, int id) {
+ duplicationRepository.add(file,
+ new Duplication(
+ convert(duplication.getOriginPosition(), id),
+ from(duplication.getDuplicateList())
+ .transform(new BatchDuplicateToCeDuplicate(file))));
+ }
+
+ private DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
+ return new DetailedTextBlock(id, textRange.getStartLine(), textRange.getEndLine());
+ }
}
}
@Test
public void verify_description() {
- assertThat(underTest.getDescription()).isEqualTo("Load inner file and in project duplications");
+ assertThat(underTest.getDescription()).isEqualTo("Load duplications");
}
@Test
public void loads_duplication_without_otherFileRef_as_inner_duplication() {
reportReader.putDuplications(FILE_2_REF, createDuplication(singleLineTextRange(LINE), createInnerDuplicate(LINE + 1)));
- underTest.execute(new TestComputationStepContext());
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
assertNoDuplication(FILE_1_REF);
assertDuplications(FILE_2_REF, singleLineDetailedTextBlock(1, LINE), new InnerDuplicate(singleLineTextBlock(LINE + 1)));
+ assertNbOfDuplications(context, 1);
}
@Test
public void loads_duplication_with_otherFileRef_as_inProject_duplication() {
reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(LINE), createInProjectDuplicate(FILE_2_REF, LINE + 1)));
- underTest.execute(new TestComputationStepContext());
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
assertDuplications(FILE_1_REF, singleLineDetailedTextBlock(1, LINE), new InProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1)));
assertNoDuplication(FILE_2_REF);
+ assertNbOfDuplications(context, 1);
}
@Test
singleLineTextRange(OTHER_LINE + 80),
createInnerDuplicate(LINE), createInnerDuplicate(LINE + 10)));
- underTest.execute(new TestComputationStepContext());
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
Component file1Component = treeRootHolder.getComponentByRef(FILE_1_REF);
assertThat(duplicationRepository.getDuplications(FILE_2_REF)).containsOnly(
duplication(
singleLineDetailedTextBlock(3, OTHER_LINE + 80),
new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10))));
+ assertNbOfDuplications(context, 3);
}
@Test
singleLineTextRange(LINE),
createInnerDuplicate(LINE + 2), createInnerDuplicate(LINE + 3), createInProjectDuplicate(FILE_1_REF, LINE + 2)));
- underTest.execute(new TestComputationStepContext());
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
Component file1Component = treeRootHolder.getComponentByRef(FILE_1_REF);
assertThat(duplicationRepository.getDuplications(FILE_2_REF)).containsOnly(
singleLineDetailedTextBlock(2, LINE),
new InnerDuplicate(singleLineTextBlock(LINE + 2)), new InnerDuplicate(singleLineTextBlock(LINE + 3)),
new InProjectDuplicate(file1Component, singleLineTextBlock(LINE + 2))));
+
+ assertNbOfDuplications(context, 2);
}
@Test
assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty();
}
+ private static void assertNbOfDuplications(TestComputationStepContext context, int expected) {
+ context.getStatistics().assertValue("duplications", expected);
+ }
+
}