From 6e5e545113a0aa7d8c6296e5fa8edfef65dec639 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 2 Oct 2015 17:08:40 +0200 Subject: [PATCH] SONAR-6827 now formula init. on other than FILE and PROJECT_VIEW they now will be initialized on Component which have no child, usually FILE and PROJECT_VIEW, but also empty VIEW or SUB_VIEW --- .../FormulaExecutorComponentVisitor.java | 22 +- .../ElementsAndCoveredElementsCounter.java | 2 +- ...ntsAndCoveredElementsVariationCounter.java | 3 +- ...rtFormulaExecutorComponentVisitorTest.java | 191 ++++++++++-------- ...wsFormulaExecutorComponentVisitorTest.java | 93 ++++----- 5 files changed, 167 insertions(+), 144 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/FormulaExecutorComponentVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/FormulaExecutorComponentVisitor.java index 4cc72415667..df7c3a87b96 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/FormulaExecutorComponentVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/FormulaExecutorComponentVisitor.java @@ -104,37 +104,45 @@ public class FormulaExecutorComponentVisitor extends PathAwareVisitorAdapter path) { - processNotLeaf(project, path); + process(project, path); } @Override public void visitModule(Component module, Path path) { - processNotLeaf(module, path); + process(module, path); } @Override public void visitDirectory(Component directory, Path path) { - processNotLeaf(directory, path); + process(directory, path); } @Override public void visitFile(Component file, Path path) { - processLeaf(file, path); + process(file, path); } @Override public void visitView(Component view, Path path) { - processNotLeaf(view, path); + process(view, path); } @Override public void visitSubView(Component subView, Path path) { - processNotLeaf(subView, path); + process(subView, path); } @Override public void visitProjectView(Component projectView, Path path) { - processLeaf(projectView, path); + process(projectView, path); + } + + private void process(Component component, Path path) { + if (component.getChildren().isEmpty()) { + processLeaf(component, path); + } else { + processNotLeaf(component, path); + } } private void processNotLeaf(Component component, Path path) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsCounter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsCounter.java index 9bf7c3ebec2..cefc025147a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsCounter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsCounter.java @@ -39,7 +39,7 @@ public abstract class ElementsAndCoveredElementsCounter implements Counterof(new FakeFormula(), new FakeVariationFormula())); - @Test public void verify_aggregation_on_value() throws Exception { treeRootHolder.setRoot(BALANCED_COMPONENT_TREE); - measureRepository.addRawMeasure(1111, LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(1112, LINES_KEY, newMeasureBuilder().create(8)); - measureRepository.addRawMeasure(1211, LINES_KEY, newMeasureBuilder().create(2)); - - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); - - assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(20))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(11))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(18))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(18))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1111))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(10))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1112))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(8))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(12))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(2))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(2))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1211))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(2))); + measureRepository.addRawMeasure(FILE_1_REF, LINES_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, LINES_KEY, newMeasureBuilder().create(8)); + measureRepository.addRawMeasure(FILE_3_REF, LINES_KEY, newMeasureBuilder().create(2)); + + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(BALANCED_COMPONENT_TREE); + + assertAddedRawMeasure(ROOT_REF, 20); + assertAddedRawMeasure(MODULE_1_REF, 18); + assertAddedRawMeasure(111, 18); + assertAddedRawMeasure(FILE_1_REF, 10); + assertAddedRawMeasure(FILE_2_REF, 8); + assertAddedRawMeasure(MODULE_2_REF, 2); + assertAddedRawMeasure(DIRECTORY_2_REF, 2); + assertAddedRawMeasure(FILE_3_REF, 2); } @Test public void verify_multi_metric_formula_support_and_aggregation() throws Exception { treeRootHolder.setRoot(BALANCED_COMPONENT_TREE); - measureRepository.addRawMeasure(1111, LINES_KEY, newMeasureBuilder().create(10)); - measureRepository.addRawMeasure(1112, LINES_KEY, newMeasureBuilder().create(8)); - measureRepository.addRawMeasure(1211, LINES_KEY, newMeasureBuilder().create(2)); + measureRepository.addRawMeasure(FILE_1_REF, LINES_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, LINES_KEY, newMeasureBuilder().create(8)); + measureRepository.addRawMeasure(FILE_3_REF, LINES_KEY, newMeasureBuilder().create(2)); - FormulaExecutorComponentVisitor underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) - .withVariationSupport(periodsHolder) - .buildFor(ImmutableList.of(new FakeMultiMetricFormula())); - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeMultiMetricFormula())) + .visit(BALANCED_COMPONENT_TREE); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly( + assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).containsOnly( entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(30)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(120))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(11))).containsOnly( + assertThat(toEntries(measureRepository.getAddedRawMeasures(MODULE_1_REF))).containsOnly( entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(28)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(118))); assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly( entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(28)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(118))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1111))).containsOnly( + assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_1_REF))).containsOnly( entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(20)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(110))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1112))).containsOnly( + assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_2_REF))).containsOnly( entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(18)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(108))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(12))).containsOnly( - entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(12)), + assertThat(toEntries(measureRepository.getAddedRawMeasures(MODULE_2_REF))).containsOnly( + entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(MODULE_2_REF)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(102))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly( - entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(12)), + assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_2_REF))).containsOnly( + entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(MODULE_2_REF)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(102))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1211))).containsOnly( - entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(12)), + assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_3_REF))).containsOnly( + entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(MODULE_2_REF)), entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(102))); } @@ -152,64 +156,81 @@ public class ReportFormulaExecutorComponentVisitorTest { public void verify_aggregation_on_variations() throws Exception { treeRootHolder.setRoot(BALANCED_COMPONENT_TREE); - measureRepository.addRawMeasure(1111, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(10, 20)); - measureRepository.addRawMeasure(1112, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(8, 16)); - measureRepository.addRawMeasure(1211, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(2, 4)); - - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); - - assertThat(toEntries(measureRepository.getAddedRawMeasures(1))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(20, 40))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(11))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(18, 36))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(111))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(18, 36))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1111))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(10, 20))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1112))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(8, 16))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(12))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(2, 4))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(121))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(2, 4))); - assertThat(toEntries(measureRepository.getAddedRawMeasures(1211))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(2, 4))); - } - - private static Measure createMeasureWithVariation(double variation2Value, double variation5Value) { - return newMeasureBuilder().setVariations(new MeasureVariations(null, variation2Value, null, null, variation5Value)).createNoValue(); + measureRepository.addRawMeasure(FILE_1_REF, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(10, 20)); + measureRepository.addRawMeasure(FILE_2_REF, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(8, 16)); + measureRepository.addRawMeasure(FILE_3_REF, NEW_LINES_TO_COVER_KEY, createMeasureWithVariation(2, 4)); + + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeVariationFormula())) + .visit(BALANCED_COMPONENT_TREE); + + assertAddedRawMeasure(ROOT_REF, 20, 40); + assertAddedRawMeasure(MODULE_1_REF, 18, 36); + assertAddedRawMeasure(DIRECTORY_1_REF, 18, 36); + assertAddedRawMeasure(FILE_1_REF, 10, 20); + assertAddedRawMeasure(FILE_2_REF, 8, 16); + assertAddedRawMeasure(MODULE_2_REF, 2, 4); + assertAddedRawMeasure(DIRECTORY_2_REF, 2, 4); + assertAddedRawMeasure(FILE_3_REF, 2, 4); } @Test - public void add_no_measure() throws Exception { - ReportComponent project = ReportComponent.builder(PROJECT, 1) + public void measures_are_0_when_there_is_no_input_measure() throws Exception { + ReportComponent project = ReportComponent.builder(PROJECT, ROOT_REF) .addChildren( - ReportComponent.builder(MODULE, 11) + ReportComponent.builder(MODULE, MODULE_1_REF) .addChildren( - ReportComponent.builder(DIRECTORY, 111) + ReportComponent.builder(DIRECTORY, DIRECTORY_1_REF) .addChildren( - builder(Component.Type.FILE, 1111).build()) + builder(Component.Type.FILE, FILE_1_REF).build()) .build()) .build()) .build(); treeRootHolder.setRoot(project); - new PathAwareCrawler<>(underTest).visit(project); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(project); - assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(11)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(1111)).isEmpty(); + assertAddedRawMeasure(ROOT_REF, 0); + assertAddedRawMeasure(MODULE_1_REF, 0); + assertAddedRawMeasure(DIRECTORY_1_REF, 0); + assertAddedRawMeasure(FILE_1_REF, 0); } @Test - public void add_no_measure_when_no_file() throws Exception { - ReportComponent project = ReportComponent.builder(PROJECT, 1) + public void add_measure_even_when_leaf_is_not_FILE() throws Exception { + ReportComponent project = ReportComponent.builder(PROJECT, ROOT_REF) .addChildren( - ReportComponent.builder(MODULE, 11) + ReportComponent.builder(MODULE, MODULE_1_REF) .addChildren( ReportComponent.builder(DIRECTORY, 111).build()) .build()) .build(); treeRootHolder.setRoot(project); - new PathAwareCrawler<>(underTest).visit(project); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(project); + + assertAddedRawMeasure(MODULE_1_REF, 0); + assertAddedRawMeasure(DIRECTORY_1_REF, 0); + } + + private FormulaExecutorComponentVisitor formulaExecutorComponentVisitor(Formula formula) { + return FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) + .withVariationSupport(periodsHolder) + .buildFor(ImmutableList.of(formula)); + } + + private static Measure createMeasureWithVariation(double variation2Value, double variation5Value) { + return newMeasureBuilder().setVariations(new MeasureVariations(null, variation2Value, null, null, variation5Value)).createNoValue(); + } + + private void assertAddedRawMeasure(int componentRef, int expectedValue) { + assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(NCLOC_KEY, newMeasureBuilder().create(expectedValue))); + } - assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(11)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(111)).isEmpty(); + private void assertAddedRawMeasure(int componentRef, int variation2Value, int variation5Value) { + assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) + .containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(variation2Value, variation5Value))); } private class FakeFormula implements Formula { @@ -226,10 +247,6 @@ public class ReportFormulaExecutorComponentVisitorTest { assertThat(context.getComponent()).isNotNull(); assertThat(context.getMetric()).isSameAs(metricRepository.getByKey(NCLOC_KEY)); - // simplest computation - if (counter.value <= 0) { - return Optional.absent(); - } return Optional.of(Measure.newMeasureBuilder().create(counter.value)); } @@ -254,10 +271,6 @@ public class ReportFormulaExecutorComponentVisitorTest { assertThat(context.getMetric()) .isIn(metricRepository.getByKey(NEW_LINES_TO_COVER_KEY), metricRepository.getByKey(NEW_IT_COVERAGE_KEY)); - // simplest computation - if (counter.value <= 0) { - return Optional.absent(); - } return Optional.of(Measure.newMeasureBuilder().create(counter.value + metricOffset(context.getMetric()))); } @@ -288,7 +301,7 @@ public class ReportFormulaExecutorComponentVisitorTest { @Override public void initialize(CounterInitializationContext context) { // verify the context which is passed to the method - assertThat(context.getLeaf().getReportAttributes().getRef()).isIn(1111, 1112, 1211); + assertThat(context.getLeaf().getChildren()).isEmpty(); assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods()); Optional measureOptional = context.getMeasure(LINES_KEY); @@ -339,7 +352,7 @@ public class ReportFormulaExecutorComponentVisitorTest { @Override public void initialize(CounterInitializationContext context) { // verify the context which is passed to the method - assertThat(context.getLeaf().getReportAttributes().getRef()).isIn(1111, 1112, 1211); + assertThat(context.getLeaf().getChildren()).isEmpty(); assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods()); Optional measureOptional = context.getMeasure(NEW_LINES_TO_COVER_KEY); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ViewsFormulaExecutorComponentVisitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ViewsFormulaExecutorComponentVisitorTest.java index 6103e0e8d80..a77ec3a8d31 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ViewsFormulaExecutorComponentVisitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ViewsFormulaExecutorComponentVisitorTest.java @@ -67,15 +67,15 @@ public class ViewsFormulaExecutorComponentVisitorTest { .addChildren( ViewsComponent.builder(SUBVIEW, SUBVIEW_1_REF) .addChildren( - ViewsComponent.builder(SUBVIEW, SUB_SUBVIEW_REF) - .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(), - builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build()) - .build()) + ViewsComponent.builder(SUBVIEW, SUB_SUBVIEW_REF) + .addChildren( + builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(), + builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build()) + .build()) .build(), ViewsComponent.builder(SUBVIEW, SUBVIEW_2_REF) .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) + builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) .build(), builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build()) .build(); @@ -94,10 +94,6 @@ public class ViewsFormulaExecutorComponentVisitorTest { public PeriodsHolderRule periodsHolder = new PeriodsHolderRule() .setPeriods(new Period(2, "some mode", null, 95l, 756l), new Period(5, "some other mode", null, 756L, 956L)); - FormulaExecutorComponentVisitor underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) - .withVariationSupport(periodsHolder) - .buildFor(ImmutableList.of(new FakeFormula(), new FakeVariationFormula())); - @Test public void verify_aggregation_on_value() throws Exception { treeRootHolder.setRoot(BALANCED_COMPONENT_TREE); @@ -106,7 +102,8 @@ public class ViewsFormulaExecutorComponentVisitorTest { addRawMeasure(PROJECT_VIEW_3_REF, 3, LINES_KEY); addRawMeasure(PROJECT_VIEW_4_REF, 4, LINES_KEY); - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(BALANCED_COMPONENT_TREE); verifyProjectViewsHasNoAddedRawMeasures(); verifySingleMetricValue(SUB_SUBVIEW_REF, 3); @@ -127,10 +124,8 @@ public class ViewsFormulaExecutorComponentVisitorTest { addRawMeasure(PROJECT_VIEW_3_REF, 5, LINES_KEY); addRawMeasure(PROJECT_VIEW_4_REF, 4, LINES_KEY); - FormulaExecutorComponentVisitor underTest = FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) - .withVariationSupport(periodsHolder) - .buildFor(ImmutableList.of(new FakeMultiMetricFormula())); - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeMultiMetricFormula())) + .visit(BALANCED_COMPONENT_TREE); verifyProjectViewsHasNoAddedRawMeasures(); verifyMultiMetricValues(SUB_SUBVIEW_REF, 13, 103); @@ -148,7 +143,8 @@ public class ViewsFormulaExecutorComponentVisitorTest { addRawMeasureWithVariation(PROJECT_VIEW_3_REF, NEW_LINES_TO_COVER_KEY, 2, 4); addRawMeasureWithVariation(PROJECT_VIEW_4_REF, NEW_LINES_TO_COVER_KEY, 3, 7); - new PathAwareCrawler<>(underTest).visit(BALANCED_COMPONENT_TREE); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeVariationFormula())) + .visit(BALANCED_COMPONENT_TREE); verifyProjectViewsHasNoAddedRawMeasures(); verifySingleMetricWithVariations(SUB_SUBVIEW_REF, 18, 36); @@ -157,8 +153,10 @@ public class ViewsFormulaExecutorComponentVisitorTest { verifySingleMetricWithVariations(ROOT_REF, 23, 47); } - private AbstractIterableAssert, MeasureRepoEntry> verifySingleMetricWithVariations(int componentRef, int variation2Value, int variation5Value) { - return assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(variation2Value, variation5Value))); + private AbstractIterableAssert, MeasureRepoEntry> verifySingleMetricWithVariations(int componentRef, int variation2Value, + int variation5Value) { + return assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) + .containsOnly(entryOf(NEW_IT_COVERAGE_KEY, createMeasureWithVariation(variation2Value, variation5Value))); } private MeasureRepositoryRule addRawMeasureWithVariation(int componentRef, String metricKey, int variation2Value, int variation5Value) { @@ -170,7 +168,7 @@ public class ViewsFormulaExecutorComponentVisitorTest { } @Test - public void add_no_measure() throws Exception { + public void verify_no_measure_added_on_projectView() throws Exception { ViewsComponent project = ViewsComponent.builder(VIEW, ROOT_REF) .addChildren( ViewsComponent.builder(SUBVIEW, SUBVIEW_1_REF) @@ -183,16 +181,17 @@ public class ViewsFormulaExecutorComponentVisitorTest { .build(); treeRootHolder.setRoot(project); - new PathAwareCrawler<>(underTest).visit(project); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(project); - assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(SUBVIEW_1_REF)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_REF)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(PROJECT_VIEW_1_REF)).isEmpty(); + assertNoAddedRawMeasure(PROJECT_VIEW_1_REF); + verifySingleMetricValue(SUB_SUBVIEW_REF, 0); + verifySingleMetricValue(SUBVIEW_1_REF, 0); + verifySingleMetricValue(ROOT_REF, 0); } @Test - public void add_no_measure_when_no_file() throws Exception { + public void add_measure_even_if_leaf_is_not_a_PROJECT_VIEW() throws Exception { ViewsComponent project = ViewsComponent.builder(VIEW, ROOT_REF) .addChildren( ViewsComponent.builder(SUBVIEW, SUBVIEW_1_REF) @@ -202,11 +201,12 @@ public class ViewsFormulaExecutorComponentVisitorTest { .build(); treeRootHolder.setRoot(project); - new PathAwareCrawler<>(underTest).visit(project); + new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) + .visit(project); - assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(SUBVIEW_1_REF)).isEmpty(); - assertThat(measureRepository.getAddedRawMeasures(SUB_SUBVIEW_REF)).isEmpty(); + verifySingleMetricValue(SUB_SUBVIEW_REF, 0); + verifySingleMetricValue(SUBVIEW_1_REF, 0); + verifySingleMetricValue(ROOT_REF, 0); } private class FakeFormula implements Formula { @@ -223,10 +223,6 @@ public class ViewsFormulaExecutorComponentVisitorTest { assertThat(context.getComponent()).isNotNull(); assertThat(context.getMetric()).isSameAs(metricRepository.getByKey(NCLOC_KEY)); - // simplest computation - if (counter.value <= 0) { - return Optional.absent(); - } return Optional.of(Measure.newMeasureBuilder().create(counter.value)); } @@ -251,10 +247,6 @@ public class ViewsFormulaExecutorComponentVisitorTest { assertThat(context.getMetric()) .isIn(metricRepository.getByKey(NEW_LINES_TO_COVER_KEY), metricRepository.getByKey(NEW_IT_COVERAGE_KEY)); - // simplest computation - if (counter.value <= 0) { - return Optional.absent(); - } return Optional.of(Measure.newMeasureBuilder().create(counter.value + metricOffset(context.getMetric()))); } @@ -347,11 +339,21 @@ public class ViewsFormulaExecutorComponentVisitorTest { } } + private FormulaExecutorComponentVisitor formulaExecutorComponentVisitor(Formula formula) { + return FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) + .withVariationSupport(periodsHolder) + .buildFor(ImmutableList.of(formula)); + } + private void verifyProjectViewsHasNoAddedRawMeasures() { - assertThat(toEntries(measureRepository.getAddedRawMeasures(PROJECT_VIEW_1_REF))).isEmpty(); - assertThat(toEntries(measureRepository.getAddedRawMeasures(PROJECT_VIEW_2_REF))).isEmpty(); - assertThat(toEntries(measureRepository.getAddedRawMeasures(PROJECT_VIEW_3_REF))).isEmpty(); - assertThat(toEntries(measureRepository.getAddedRawMeasures(PROJECT_VIEW_4_REF))).isEmpty(); + assertNoAddedRawMeasure(PROJECT_VIEW_1_REF); + assertNoAddedRawMeasure(PROJECT_VIEW_2_REF); + assertNoAddedRawMeasure(PROJECT_VIEW_3_REF); + assertNoAddedRawMeasure(PROJECT_VIEW_4_REF); + } + + private void assertNoAddedRawMeasure(int componentRef) { + assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty(); } private void verifySingleMetricValue(int componentRef, int measureValue) { @@ -361,14 +363,13 @@ public class ViewsFormulaExecutorComponentVisitorTest { private void verifyMultiMetricValues(int componentRef, int valueLinesToCover, int valueItCoverage) { assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))) - .containsOnly( - entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(valueLinesToCover)), - entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(valueItCoverage))); + .containsOnly( + entryOf(NEW_LINES_TO_COVER_KEY, newMeasureBuilder().create(valueLinesToCover)), + entryOf(NEW_IT_COVERAGE_KEY, newMeasureBuilder().create(valueItCoverage))); } private void verifyLeafContext(CounterInitializationContext context) { - assertThat(context.getLeaf().getKey()).isIn(String.valueOf(PROJECT_VIEW_1_REF), String.valueOf(PROJECT_VIEW_2_REF), String.valueOf(PROJECT_VIEW_3_REF), - String.valueOf(PROJECT_VIEW_4_REF)); + assertThat(context.getLeaf().getChildren()).isEmpty(); assertThat(context.getPeriods()).isEqualTo(periodsHolder.getPeriods()); } -- 2.39.5