]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11077 log nb of duplications in CE LoadDuplicationsFromReportStep
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 30 Jul 2018 19:33:48 +0000 (21:33 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 2 Aug 2018 18:21:36 +0000 (20:21 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java

index d5df8cebbf54202c188cb2107ec2d44436e50ab1..be7b456c4e0837d4a0e84a7bb4811c4e81801341 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.base.Function;
 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;
@@ -58,40 +57,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(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> {
@@ -112,5 +85,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 0f6cc3cca507639d60f0580efdbadfe10da33acf..cae4d04fb6e8196a7a42e8c053ea02fab1f367db 100644 (file)
@@ -68,27 +68,31 @@ public class LoadDuplicationsFromReportStepTest {
 
   @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
@@ -105,7 +109,8 @@ public class LoadDuplicationsFromReportStepTest {
         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(
@@ -119,6 +124,7 @@ public class LoadDuplicationsFromReportStepTest {
       duplication(
         singleLineDetailedTextBlock(3, OTHER_LINE + 80),
         new InnerDuplicate(singleLineTextBlock(LINE)), new InnerDuplicate(singleLineTextBlock(LINE + 10))));
+    assertNbOfDuplications(context, 3);
   }
 
   @Test
@@ -132,7 +138,8 @@ public class LoadDuplicationsFromReportStepTest {
         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(
@@ -144,6 +151,8 @@ public class LoadDuplicationsFromReportStepTest {
         singleLineDetailedTextBlock(2, LINE),
         new InnerDuplicate(singleLineTextBlock(LINE + 2)), new InnerDuplicate(singleLineTextBlock(LINE + 3)),
         new InProjectDuplicate(file1Component, singleLineTextBlock(LINE + 2))));
+
+    assertNbOfDuplications(context, 2);
   }
 
   @Test
@@ -217,4 +226,8 @@ public class LoadDuplicationsFromReportStepTest {
     assertThat(duplicationRepository.getDuplications(fileRef)).isEmpty();
   }
 
+  private static void assertNbOfDuplications(TestComputationStepContext context, int expected) {
+    context.getStatistics().assertValue("duplications", expected);
+  }
+
 }