]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11077 log nb of duplications in CE LoadDuplicationsFromReportStep
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 3 Aug 2018 09:58:28 +0000 (11:58 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 5 Aug 2018 21:17:31 +0000 (23:17 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java

index 82702754de644eaeeb3e6f81ab633867fbd10f9e..ee7ca2588929afcfaee96e3cf7dbd416c485f115 100644 (file)
@@ -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<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> {
@@ -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<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());
+    }
   }
 }
index cc3ddb7b3822939035c30b0ddb3e3f23051351c1..3f8a69c193f18f65bbf072ac52501665a3db814a 100644 (file)
 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);
+  }
+
+
 }