diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-10-02 17:08:40 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-10-06 09:46:04 +0200 |
commit | 6e5e545113a0aa7d8c6296e5fa8edfef65dec639 (patch) | |
tree | 6183345469a5a6ebe7372d832997a5fd54ba6d32 /server/sonar-server | |
parent | c19116e2b378845ec91805ad60a880e8032fb586 (diff) | |
download | sonarqube-6e5e545113a0aa7d8c6296e5fa8edfef65dec639.tar.gz sonarqube-6e5e545113a0aa7d8c6296e5fa8edfef65dec639.zip |
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
Diffstat (limited to 'server/sonar-server')
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<For @Override public void visitProject(Component project, Path<FormulaExecutorComponentVisitor.Counters> path) { - processNotLeaf(project, path); + process(project, path); } @Override public void visitModule(Component module, Path<FormulaExecutorComponentVisitor.Counters> path) { - processNotLeaf(module, path); + process(module, path); } @Override public void visitDirectory(Component directory, Path<FormulaExecutorComponentVisitor.Counters> path) { - processNotLeaf(directory, path); + process(directory, path); } @Override public void visitFile(Component file, Path<FormulaExecutorComponentVisitor.Counters> path) { - processLeaf(file, path); + process(file, path); } @Override public void visitView(Component view, Path<Counters> path) { - processNotLeaf(view, path); + process(view, path); } @Override public void visitSubView(Component subView, Path<Counters> path) { - processNotLeaf(subView, path); + process(subView, path); } @Override public void visitProjectView(Component projectView, Path<Counters> path) { - processLeaf(projectView, path); + process(projectView, path); + } + + private void process(Component component, Path<FormulaExecutorComponentVisitor.Counters> path) { + if (component.getChildren().isEmpty()) { + processLeaf(component, path); + } else { + processNotLeaf(component, path); + } } private void processNotLeaf(Component component, Path<FormulaExecutorComponentVisitor.Counters> 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 Counter<Eleme @Override public void initialize(CounterInitializationContext context) { Component component = context.getLeaf(); - if (component.getType().isReportType() && component.getFileAttributes().isUnitTest()) { + if (component.getType() == Component.Type.FILE && component.getFileAttributes().isUnitTest()) { return; } initializeForSupportedLeaf(context); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsVariationCounter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsVariationCounter.java index 6b5eee72a5d..c39fb596b53 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsVariationCounter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/ElementsAndCoveredElementsVariationCounter.java @@ -19,6 +19,7 @@ */ package org.sonar.server.computation.formula.coverage; +import org.sonar.server.computation.component.Component; import org.sonar.server.computation.formula.Counter; import org.sonar.server.computation.formula.CounterInitializationContext; import org.sonar.server.computation.formula.counter.LongVariationValue; @@ -38,7 +39,7 @@ public abstract class ElementsAndCoveredElementsVariationCounter implements Coun @Override public void initialize(CounterInitializationContext context) { - if (context.getLeaf().getType().isReportType() && context.getLeaf().getFileAttributes().isUnitTest()) { + if (context.getLeaf().getType() == Component.Type.FILE && context.getLeaf().getFileAttributes().isUnitTest()) { return; } initializeForSupportedLeaf(context); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ReportFormulaExecutorComponentVisitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ReportFormulaExecutorComponentVisitorTest.java index b9f42a59ba5..c2ea835f42a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ReportFormulaExecutorComponentVisitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/formula/ReportFormulaExecutorComponentVisitorTest.java @@ -39,7 +39,6 @@ import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.PeriodsHolderRule; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.guava.api.Assertions.assertThat; import static org.sonar.api.measures.CoreMetrics.LINES_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_IT_COVERAGE_KEY; @@ -53,24 +52,34 @@ import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf; import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries; public class ReportFormulaExecutorComponentVisitorTest { - private static final ReportComponent BALANCED_COMPONENT_TREE = ReportComponent.builder(PROJECT, 1) + private static final int ROOT_REF = 1; + private static final int MODULE_1_REF = 11; + private static final int DIRECTORY_1_REF = 111; + private static final int FILE_1_REF = 1111; + private static final int FILE_2_REF = 1112; + private static final int MODULE_2_REF = 12; + private static final int DIRECTORY_2_REF = 121; + private static final int FILE_3_REF = 1211; + + private static final ReportComponent BALANCED_COMPONENT_TREE = 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, 1112).build()) + builder(Component.Type.FILE, FILE_1_REF).build(), + builder(Component.Type.FILE, FILE_2_REF).build()) .build()) .build(), - ReportComponent.builder(MODULE, 12) + ReportComponent.builder(MODULE, MODULE_2_REF) .addChildren( - ReportComponent.builder(DIRECTORY, 121) + ReportComponent.builder(DIRECTORY, DIRECTORY_2_REF) .addChildren( - builder(Component.Type.FILE, 1211).build()) + builder(Component.Type.FILE, FILE_3_REF).build()) .build()) .build()) .build(); + @Rule public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); @Rule @@ -85,66 +94,61 @@ public class ReportFormulaExecutorComponentVisitorTest { 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.<Formula>of(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.<Formula>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<FakeCounter> { @@ -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<Measure> 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<Measure> 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.<Formula>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.<Formula>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<?, ? extends Iterable<? extends MeasureRepoEntry>, 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<?, ? extends Iterable<? extends MeasureRepoEntry>, 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<FakeCounter> { @@ -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()); } |