import com.google.common.base.Function;
import javax.annotation.Nonnull;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.core.util.CloseableIterator;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.FluentIterable.from;
-import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
/**
* Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
@Override
public String getDescription() {
- return "Load inner file and in project duplications";
+ return "Load duplications";
}
@Override
public void execute() {
- new DepthTraversalTypeAwareCrawler(
- new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, 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());
+ Loggers.get(getClass()).debug("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());
+ }
}
}
package org.sonar.server.computation.task.projectanalysis.step;
import java.util.Arrays;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public LogTester logTester = new LogTester();
private LoadDuplicationsFromReportStep underTest = new LoadDuplicationsFromReportStep(treeRootHolder, reportReader, duplicationRepository);
+ @Before
+ public void setUp() {
+ logTester.setLevel(LoggerLevel.DEBUG);
+ }
+
@Test
public void verify_description() {
- assertThat(underTest.getDescription()).isEqualTo("Load inner file and in project duplications");
+ assertThat(underTest.getDescription()).isEqualTo("Load duplications");
}
@Test
assertNoDuplication(FILE_1_REF);
assertDuplications(FILE_2_REF, singleLineDetailedTextBlock(1, LINE), new InnerDuplicate(singleLineTextBlock(LINE + 1)));
+ assertStatisticsLog(1);
}
@Test
assertDuplications(FILE_1_REF, singleLineDetailedTextBlock(1, LINE), new InProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1)));
assertNoDuplication(FILE_2_REF);
+ assertStatisticsLog(1);
}
@Test
duplication(
singleLineDetailedTextBlock(3, OTHER_LINE + 80),
new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10))));
+ assertStatisticsLog(3);
}
@Test
singleLineDetailedTextBlock(2, LINE),
new InnerDuplicate(singleLineTextBlock(LINE + 2)), new InnerDuplicate(singleLineTextBlock(LINE + 3)),
new InProjectDuplicate(file1Component, singleLineTextBlock(LINE + 2))));
+ assertStatisticsLog(2);
}
@Test
assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty();
}
+ private void assertStatisticsLog(int expectedDuplications) {
+ assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("duplications=" + expectedDuplications);
+ }
+
+
}