3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import java.util.List;
23 import java.util.Optional;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
27 import org.sonar.ce.task.projectanalysis.formula.Counter;
28 import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext;
29 import org.sonar.ce.task.projectanalysis.formula.CreateMeasureContext;
30 import org.sonar.ce.task.projectanalysis.formula.Formula;
31 import org.sonar.ce.task.projectanalysis.formula.FormulaExecutorComponentVisitor;
32 import org.sonar.ce.task.projectanalysis.formula.counter.IntSumCounter;
33 import org.sonar.ce.task.projectanalysis.formula.counter.LongSumCounter;
34 import org.sonar.ce.task.projectanalysis.measure.Measure;
35 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
36 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
37 import org.sonar.ce.task.step.ComputationStep;
39 import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY;
40 import static org.sonar.api.measures.CoreMetrics.TESTS_KEY;
41 import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY;
42 import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY;
43 import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY;
44 import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY;
47 * Computes unit test measures on files and then aggregates them on higher components.
49 public class UnitTestMeasuresStep implements ComputationStep {
50 private static final String[] METRICS = new String[] {TESTS_KEY, TEST_ERRORS_KEY, TEST_FAILURES_KEY, SKIPPED_TESTS_KEY, TEST_SUCCESS_DENSITY_KEY, TEST_EXECUTION_TIME_KEY};
52 private static final List<Formula> FORMULAS = List.of(new UnitTestsFormula());
54 private final TreeRootHolder treeRootHolder;
55 private final MetricRepository metricRepository;
56 private final MeasureRepository measureRepository;
58 public UnitTestMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
59 this.treeRootHolder = treeRootHolder;
60 this.metricRepository = metricRepository;
61 this.measureRepository = measureRepository;
65 public void execute(ComputationStep.Context context) {
66 new PathAwareCrawler<>(
67 FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(FORMULAS))
68 .visit(treeRootHolder.getRoot());
71 private static class UnitTestsFormula implements Formula<UnitTestsCounter> {
74 public UnitTestsCounter createNewCounter() {
75 return new UnitTestsCounter();
79 public Optional<Measure> createMeasure(UnitTestsCounter counter, CreateMeasureContext context) {
80 String metricKey = context.getMetric().getKey();
81 Component leaf = counter.getLeaf();
84 return createIntMeasure(context.getComponent(), leaf, counter.testsCounter.getValue());
86 return createIntMeasure(context.getComponent(), leaf, counter.testsErrorsCounter.getValue());
87 case TEST_FAILURES_KEY:
88 return createIntMeasure(context.getComponent(), leaf, counter.testsFailuresCounter.getValue());
89 case SKIPPED_TESTS_KEY:
90 return createIntMeasure(context.getComponent(), leaf, counter.skippedTestsCounter.getValue());
91 case TEST_EXECUTION_TIME_KEY:
92 return createLongMeasure(context.getComponent(), leaf, counter.testExecutionTimeCounter.getValue());
93 case TEST_SUCCESS_DENSITY_KEY:
94 return createDensityMeasure(counter, context.getMetric().getDecimalScale());
96 throw new IllegalStateException(String.format("Metric '%s' is not supported", metricKey));
100 private static Optional<Measure> createIntMeasure(Component currentComponent, Component leafComponent, Optional<Integer> metricValue) {
101 if (metricValue.isPresent() && leafComponent.getType().isDeeperThan(currentComponent.getType())) {
102 return Optional.of(Measure.newMeasureBuilder().create(metricValue.get()));
104 return Optional.empty();
107 private static Optional<Measure> createLongMeasure(Component currentComponent, Component leafComponent, Optional<Long> metricValue) {
108 if (metricValue.isPresent() && leafComponent.getType().isDeeperThan(currentComponent.getType())) {
109 return Optional.of(Measure.newMeasureBuilder().create(metricValue.get()));
111 return Optional.empty();
114 private static Optional<Measure> createDensityMeasure(UnitTestsCounter counter, int decimalScale) {
115 if (isPositive(counter.testsCounter.getValue(), true)
116 && isPositive(counter.testsErrorsCounter.getValue(), false)
117 && isPositive(counter.testsFailuresCounter.getValue(), false)) {
118 int tests = counter.testsCounter.getValue().get();
119 int errors = counter.testsErrorsCounter.getValue().get();
120 int failures = counter.testsFailuresCounter.getValue().get();
121 double density = (errors + failures) * 100d / tests;
122 return Optional.of(Measure.newMeasureBuilder().create(100d - density, decimalScale));
124 return Optional.empty();
127 private static boolean isPositive(Optional<Integer> value, boolean isStrictComparison) {
128 return value.isPresent() && (isStrictComparison ? (value.get() > 0) : (value.get() >= 0));
132 public String[] getOutputMetricKeys() {
137 private static class UnitTestsCounter implements Counter<UnitTestsCounter> {
139 private final IntSumCounter testsCounter = new IntSumCounter(TESTS_KEY);
140 private final IntSumCounter testsErrorsCounter = new IntSumCounter(TEST_ERRORS_KEY);
141 private final IntSumCounter testsFailuresCounter = new IntSumCounter(TEST_FAILURES_KEY);
142 private final IntSumCounter skippedTestsCounter = new IntSumCounter(SKIPPED_TESTS_KEY);
143 private final LongSumCounter testExecutionTimeCounter = new LongSumCounter(TEST_EXECUTION_TIME_KEY);
145 private Component leaf;
148 public void aggregate(UnitTestsCounter counter) {
149 testsCounter.aggregate(counter.testsCounter);
150 testsErrorsCounter.aggregate(counter.testsErrorsCounter);
151 testsFailuresCounter.aggregate(counter.testsFailuresCounter);
152 skippedTestsCounter.aggregate(counter.skippedTestsCounter);
153 testExecutionTimeCounter.aggregate(counter.testExecutionTimeCounter);
157 public void initialize(CounterInitializationContext context) {
158 this.leaf = context.getLeaf();
159 testsCounter.initialize(context);
160 testsErrorsCounter.initialize(context);
161 testsFailuresCounter.initialize(context);
162 skippedTestsCounter.initialize(context);
163 testExecutionTimeCounter.initialize(context);
166 Component getLeaf() {
172 public String getDescription() {
173 return "Compute test measures";