From 6da8ba421e7b51df1c5c48ca7fb904fad9c68eef Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 3 Aug 2018 11:58:28 +0200 Subject: [PATCH] SONAR-11077 log nb of duplications in CE LoadDuplicationsFromReportStep --- .../step/LoadDuplicationsFromReportStep.java | 72 +++++++++++-------- .../LoadDuplicationsFromReportStepTest.java | 21 +++++- 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java index 82702754de6..ee7ca258892 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java @@ -21,6 +21,7 @@ package org.sonar.server.computation.task.projectanalysis.step; 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; @@ -40,7 +41,6 @@ import org.sonar.server.computation.task.step.ComputationStep; 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}. @@ -58,40 +58,14 @@ public class LoadDuplicationsFromReportStep implements ComputationStep { @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 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 { @@ -112,5 +86,41 @@ public class LoadDuplicationsFromReportStep implements ComputationStep { } 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 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()); + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java index cc3ddb7b382..3f8a69c193f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java @@ -20,9 +20,12 @@ 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; @@ -62,12 +65,19 @@ public class LoadDuplicationsFromReportStepTest { 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 @@ -78,6 +88,7 @@ public class LoadDuplicationsFromReportStepTest { assertNoDuplication(FILE_1_REF); assertDuplications(FILE_2_REF, singleLineDetailedTextBlock(1, LINE), new InnerDuplicate(singleLineTextBlock(LINE + 1))); + assertStatisticsLog(1); } @Test @@ -88,6 +99,7 @@ public class LoadDuplicationsFromReportStepTest { assertDuplications(FILE_1_REF, singleLineDetailedTextBlock(1, LINE), new InProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1))); assertNoDuplication(FILE_2_REF); + assertStatisticsLog(1); } @Test @@ -118,6 +130,7 @@ public class LoadDuplicationsFromReportStepTest { duplication( singleLineDetailedTextBlock(3, OTHER_LINE + 80), new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10)))); + assertStatisticsLog(3); } @Test @@ -143,6 +156,7 @@ public class LoadDuplicationsFromReportStepTest { singleLineDetailedTextBlock(2, LINE), new InnerDuplicate(singleLineTextBlock(LINE + 2)), new InnerDuplicate(singleLineTextBlock(LINE + 3)), new InProjectDuplicate(file1Component, singleLineTextBlock(LINE + 2)))); + assertStatisticsLog(2); } @Test @@ -216,4 +230,9 @@ public class LoadDuplicationsFromReportStepTest { assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty(); } + private void assertStatisticsLog(int expectedDuplications) { + assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("duplications=" + expectedDuplications); + } + + } -- 2.39.5