]> source.dussan.org Git - sonarqube.git/blob
eb1d1bae8934c07be086809c15b6bf3a2f1ef1ed
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
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;
38
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;
45
46 /**
47  * Computes unit test measures on files and then aggregates them on higher components.
48  */
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};
51
52   private static final List<Formula> FORMULAS = List.of(new UnitTestsFormula());
53
54   private final TreeRootHolder treeRootHolder;
55   private final MetricRepository metricRepository;
56   private final MeasureRepository measureRepository;
57
58   public UnitTestMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
59     this.treeRootHolder = treeRootHolder;
60     this.metricRepository = metricRepository;
61     this.measureRepository = measureRepository;
62   }
63
64   @Override
65   public void execute(ComputationStep.Context context) {
66     new PathAwareCrawler<>(
67       FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(FORMULAS))
68         .visit(treeRootHolder.getRoot());
69   }
70
71   private static class UnitTestsFormula implements Formula<UnitTestsCounter> {
72
73     @Override
74     public UnitTestsCounter createNewCounter() {
75       return new UnitTestsCounter();
76     }
77
78     @Override
79     public Optional<Measure> createMeasure(UnitTestsCounter counter, CreateMeasureContext context) {
80       String metricKey = context.getMetric().getKey();
81       Component leaf = counter.getLeaf();
82       switch (metricKey) {
83         case TESTS_KEY:
84           return createIntMeasure(context.getComponent(), leaf, counter.testsCounter.getValue());
85         case TEST_ERRORS_KEY:
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());
95         default:
96           throw new IllegalStateException(String.format("Metric '%s' is not supported", metricKey));
97       }
98     }
99
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()));
103       }
104       return Optional.empty();
105     }
106
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()));
110       }
111       return Optional.empty();
112     }
113
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));
123       }
124       return Optional.empty();
125     }
126
127     private static boolean isPositive(Optional<Integer> value, boolean isStrictComparison) {
128       return value.isPresent() && (isStrictComparison ? (value.get() > 0) : (value.get() >= 0));
129     }
130
131     @Override
132     public String[] getOutputMetricKeys() {
133       return METRICS;
134     }
135   }
136
137   private static class UnitTestsCounter implements Counter<UnitTestsCounter> {
138
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);
144
145     private Component leaf;
146
147     @Override
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);
154     }
155
156     @Override
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);
164     }
165
166     Component getLeaf() {
167       return leaf;
168     }
169   }
170
171   @Override
172   public String getDescription() {
173     return "Compute test measures";
174   }
175 }