diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-20 11:02:00 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-07-21 17:31:54 +0200 |
commit | 1066a59abeb3e3cafda581b84b87fada2a5947a2 (patch) | |
tree | bd975f6c5827c94b0ea1a6d58b40b8bb95a349f3 /server | |
parent | 245d9e1719ef6b3cc4d848d7c993f27c79b138fe (diff) | |
download | sonarqube-1066a59abeb3e3cafda581b84b87fada2a5947a2.tar.gz sonarqube-1066a59abeb3e3cafda581b84b87fada2a5947a2.zip |
SONAR-6681 Move computation of Unit test related measures to CE
Diffstat (limited to 'server')
4 files changed, 436 insertions, 1 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java index 943746e11e5..c38bc358f96 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputationSteps.java @@ -61,6 +61,7 @@ public class ComputationSteps { IntegrateIssuesStep.class, CoreMetricFormulaExecutorStep.class, LanguageDistributionMeasuresStep.class, + UnitTestMeasuresStep.class, // SQALE measures depend on issues SqaleMeasuresStep.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/UnitTestMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/UnitTestMeasuresStep.java new file mode 100644 index 00000000000..becb1c0bb4f --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/UnitTestMeasuresStep.java @@ -0,0 +1,153 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.computation.step; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; +import org.sonar.server.computation.component.Component; +import org.sonar.server.computation.component.TreeRootHolder; +import org.sonar.server.computation.formula.Counter; +import org.sonar.server.computation.formula.CreateMeasureContext; +import org.sonar.server.computation.formula.FileAggregateContext; +import org.sonar.server.computation.formula.Formula; +import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor; +import org.sonar.server.computation.formula.SumCounter; +import org.sonar.server.computation.formula.SumFormula; +import org.sonar.server.computation.measure.Measure; +import org.sonar.server.computation.measure.MeasureRepository; +import org.sonar.server.computation.metric.MetricRepository; + +import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY; +import static org.sonar.api.measures.CoreMetrics.TESTS_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY; + +/** + * Computes unit test measures on files and then aggregates them on higher components. + */ +public class UnitTestMeasuresStep implements ComputationStep { + + private static final String[] METRICS = new String[] {TESTS_KEY, TEST_ERRORS_KEY, TEST_FAILURES_KEY, TEST_SUCCESS_DENSITY_KEY}; + + private static final ImmutableList<Formula> FORMULAS = ImmutableList.<Formula>of( + new SumFormula(TEST_EXECUTION_TIME_KEY), + new SumFormula(SKIPPED_TESTS_KEY), + new UnitTestsFormula() + ); + + private final TreeRootHolder treeRootHolder; + private final MetricRepository metricRepository; + private final MeasureRepository measureRepository; + + public UnitTestMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) { + this.treeRootHolder = treeRootHolder; + this.metricRepository = metricRepository; + this.measureRepository = measureRepository; + } + + @Override + public void execute() { + FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository) + .buildFor(FORMULAS) + .visit(treeRootHolder.getRoot()); + } + + private static class UnitTestsFormula implements Formula<UnitTestsCounter> { + + @Override + public UnitTestsCounter createNewCounter() { + return new UnitTestsCounter(); + } + + @Override + public Optional<Measure> createMeasure(UnitTestsCounter counter, CreateMeasureContext context) { + String metricKey = context.getMetric().getKey(); + switch (metricKey) { + case TESTS_KEY: + return createMeasure(context.getComponent().getType(), counter.testsCounter.getValue()); + case TEST_ERRORS_KEY: + return createMeasure(context.getComponent().getType(), counter.testsErrorsCounter.getValue()); + case TEST_FAILURES_KEY: + return createMeasure(context.getComponent().getType(), counter.testsFailuresCounter.getValue()); + case TEST_SUCCESS_DENSITY_KEY: + return createDensityMeasure(counter); + default: + throw new IllegalStateException(String.format("Metric '%s' is not supported", metricKey)); + } + } + + private static Optional<Measure> createMeasure(Component.Type componentType, Optional<Integer> metricValue) { + return (componentType.isHigherThan(Component.Type.FILE) && metricValue.isPresent()) ? + Optional.of(Measure.newMeasureBuilder().create(metricValue.get())) : + Optional.<Measure>absent(); + } + + private static Optional<Measure> createDensityMeasure(UnitTestsCounter counter) { + if (isPositive(counter.testsCounter.getValue(), true) + && isPositive(counter.testsErrorsCounter.getValue(), false) + && isPositive(counter.testsFailuresCounter.getValue(), false)) { + int tests = counter.testsCounter.getValue().get(); + int errors = counter.testsErrorsCounter.getValue().get(); + int failures = counter.testsFailuresCounter.getValue().get(); + double density = (errors + failures) * 100d / tests; + return Optional.of(Measure.newMeasureBuilder().create(100d - density)); + } + return Optional.absent(); + } + + private static boolean isPositive(Optional<Integer> value, boolean isStrictComparison) { + return value.isPresent() && (isStrictComparison ? (value.get() > 0) : (value.get() >= 0)); + } + + @Override + public String[] getOutputMetricKeys() { + return METRICS; + } + } + + private static class UnitTestsCounter implements Counter<UnitTestsCounter> { + + private final SumCounter testsCounter = new SumCounter(TESTS_KEY); + private final SumCounter testsErrorsCounter = new SumCounter(TEST_ERRORS_KEY); + private final SumCounter testsFailuresCounter = new SumCounter(TEST_FAILURES_KEY); + + @Override + public void aggregate(UnitTestsCounter counter) { + testsCounter.aggregate(counter.testsCounter); + testsErrorsCounter.aggregate(counter.testsErrorsCounter); + testsFailuresCounter.aggregate(counter.testsFailuresCounter); + } + + @Override + public void aggregate(FileAggregateContext context) { + testsCounter.aggregate(context); + testsErrorsCounter.aggregate(context); + testsFailuresCounter.aggregate(context); + } + } + + @Override + public String getDescription() { + return "Aggregation of comment measures"; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java index f5ecf8e1c74..9de3ddb8f15 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/CommentMeasuresStepTest.java @@ -106,7 +106,6 @@ public class CommentMeasuresStepTest { assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, COMMENT_LINES_KEY).get().getIntValue()).isEqualTo(500); assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, COMMENT_LINES_KEY).get().getIntValue()).isEqualTo(500); assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, COMMENT_LINES_KEY).get().getIntValue()).isEqualTo(500); - assertThat(measureRepository.getAddedRawMeasures(ROOT_REF).get(COMMENT_LINES_KEY)).containsOnly(newMeasureBuilder().create(500)); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/UnitTestMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/UnitTestMeasuresStepTest.java new file mode 100644 index 00000000000..b01fd23593f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/UnitTestMeasuresStepTest.java @@ -0,0 +1,282 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.computation.step; + +import org.assertj.core.data.Offset; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.server.computation.batch.TreeRootHolderRule; +import org.sonar.server.computation.component.FileAttributes; +import org.sonar.server.computation.measure.MeasureRepositoryRule; +import org.sonar.server.computation.metric.MetricRepositoryRule; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.guava.api.Assertions.assertThat; +import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS; +import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY; +import static org.sonar.api.measures.CoreMetrics.TESTS; +import static org.sonar.api.measures.CoreMetrics.TESTS_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS; +import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME; +import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES; +import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY; +import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY; +import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY; +import static org.sonar.server.computation.component.Component.Type.DIRECTORY; +import static org.sonar.server.computation.component.Component.Type.FILE; +import static org.sonar.server.computation.component.Component.Type.MODULE; +import static org.sonar.server.computation.component.Component.Type.PROJECT; +import static org.sonar.server.computation.component.DumbComponent.builder; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; +import static org.sonar.server.computation.measure.MeasureRepoEntry.entryOf; +import static org.sonar.server.computation.measure.MeasureRepoEntry.toEntries; + +public class UnitTestMeasuresStepTest { + + private static final Offset<Double> DEFAULT_OFFSET = Offset.offset(0.01d); + + private static final int ROOT_REF = 1; + private static final int MODULE_REF = 12; + private static final int SUB_MODULE_REF = 123; + private static final int DIRECTORY_REF = 1234; + private static final int FILE_1_REF = 12341; + private static final int FILE_2_REF = 12342; + + @Rule + public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); + + @Rule + public MetricRepositoryRule metricRepository = new MetricRepositoryRule() + .add(TESTS) + .add(TEST_ERRORS) + .add(TEST_FAILURES) + .add(TEST_EXECUTION_TIME) + .add(SKIPPED_TESTS) + .add(TEST_SUCCESS_DENSITY); + + @Rule + public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); + + ComputationStep underTest = new UnitTestMeasuresStep(treeRootHolder, metricRepository, measureRepository); + + @Before + public void setUp() throws Exception { + treeRootHolder.setRoot( + builder(PROJECT, ROOT_REF) + .addChildren( + builder(MODULE, MODULE_REF) + .addChildren( + builder(MODULE, SUB_MODULE_REF) + .addChildren( + builder(DIRECTORY, DIRECTORY_REF) + .addChildren( + builder(FILE, FILE_1_REF).setFileAttributes(new FileAttributes(true, null)).build(), + builder(FILE, FILE_2_REF).setFileAttributes(new FileAttributes(true, null)).build() + ).build() + ).build() + ).build() + ).build()); + } + + @Test + public void aggregate_tests() { + checkMeasuresAggregation(TESTS_KEY, 100, 400, 500); + } + + @Test + public void aggregate_tests_in_errors() { + checkMeasuresAggregation(TEST_ERRORS_KEY, 100, 400, 500); + } + + @Test + public void aggregate_tests_in_failures() { + checkMeasuresAggregation(TEST_FAILURES_KEY, 100, 400, 500); + } + + @Test + public void aggregate_tests_execution_time() { + checkMeasuresAggregation(TEST_EXECUTION_TIME_KEY, 100, 400, 500); + } + + @Test + public void aggregate_skipped_tests_time() { + checkMeasuresAggregation(SKIPPED_TESTS_KEY, 100, 400, 500); + } + + @Test + public void compute_test_success_density() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(2)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(5)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(4)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(1)); + + underTest.execute(); + + assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_1_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(40d))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(FILE_2_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(70d))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(60d))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_MODULE_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(60d))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(MODULE_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(60d))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).contains(entryOf(TEST_SUCCESS_DENSITY_KEY, newMeasureBuilder().create(60d))); + } + + @Test + public void compute_test_success_density_when_zero_tests_in_errors() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(4)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(1)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(60d); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(95d); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(83.3d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(83.3d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(83.3d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(83.3d, DEFAULT_OFFSET); + } + + @Test + public void compute_test_success_density_when_zero_tests_in_failures() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(2)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(5)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(0)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(80d); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(75d); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(76.7d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(76.7d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(76.7d, DEFAULT_OFFSET); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(76.7d, DEFAULT_OFFSET); + } + + @Test + public void compute_100_percent_test_success_density_when_no_tests_in_errors_or_failures() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(0)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(100d); + } + + @Test + public void compute_0_percent_test_success_density() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(8)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(15)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(2)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(5)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY).get().getDoubleValue()).isEqualTo(0d); + } + + @Test + public void do_not_compute_test_success_density_when_no_tests_in_errors() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(4)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_FAILURES_KEY, newMeasureBuilder().create(1)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + } + + @Test + public void do_not_compute_test_success_density_when_no_tests_in_failure() { + measureRepository.addRawMeasure(FILE_1_REF, TESTS_KEY, newMeasureBuilder().create(10)); + measureRepository.addRawMeasure(FILE_2_REF, TESTS_KEY, newMeasureBuilder().create(20)); + + measureRepository.addRawMeasure(FILE_1_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + measureRepository.addRawMeasure(FILE_2_REF, TEST_ERRORS_KEY, newMeasureBuilder().create(0)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(SUB_MODULE_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(MODULE_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, TEST_SUCCESS_DENSITY_KEY)).isAbsent(); + } + + private void checkMeasuresAggregation(String metricKey, int file1Value, int file2Value, int expectedValue) { + measureRepository.addRawMeasure(FILE_1_REF, metricKey, newMeasureBuilder().create(file1Value)); + measureRepository.addRawMeasure(FILE_2_REF, metricKey, newMeasureBuilder().create(file2Value)); + + underTest.execute(); + + assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, metricKey)).isAbsent(); + assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, metricKey)).isAbsent(); + assertThat(toEntries(measureRepository.getAddedRawMeasures(DIRECTORY_REF))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(expectedValue))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(SUB_MODULE_REF))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(expectedValue))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(MODULE_REF))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(expectedValue))); + assertThat(toEntries(measureRepository.getAddedRawMeasures(ROOT_REF))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(expectedValue))); + } + +} |