Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UnitTestMeasuresStep.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. import java.util.List;
  22. import java.util.Optional;
  23. import org.sonar.ce.task.projectanalysis.component.Component;
  24. import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler;
  25. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  26. import org.sonar.ce.task.projectanalysis.formula.Counter;
  27. import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext;
  28. import org.sonar.ce.task.projectanalysis.formula.CreateMeasureContext;
  29. import org.sonar.ce.task.projectanalysis.formula.Formula;
  30. import org.sonar.ce.task.projectanalysis.formula.FormulaExecutorComponentVisitor;
  31. import org.sonar.ce.task.projectanalysis.formula.counter.IntSumCounter;
  32. import org.sonar.ce.task.projectanalysis.formula.counter.LongSumCounter;
  33. import org.sonar.ce.task.projectanalysis.measure.Measure;
  34. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  35. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  36. import org.sonar.ce.task.step.ComputationStep;
  37. import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY;
  38. import static org.sonar.api.measures.CoreMetrics.TESTS_KEY;
  39. import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY;
  40. import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY;
  41. import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY;
  42. import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY;
  43. /**
  44. * Computes unit test measures on files and then aggregates them on higher components.
  45. */
  46. public class UnitTestMeasuresStep implements ComputationStep {
  47. 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};
  48. private static final List<Formula> FORMULAS = List.of(new UnitTestsFormula());
  49. private final TreeRootHolder treeRootHolder;
  50. private final MetricRepository metricRepository;
  51. private final MeasureRepository measureRepository;
  52. public UnitTestMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
  53. this.treeRootHolder = treeRootHolder;
  54. this.metricRepository = metricRepository;
  55. this.measureRepository = measureRepository;
  56. }
  57. @Override
  58. public void execute(ComputationStep.Context context) {
  59. new PathAwareCrawler<>(
  60. FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(FORMULAS))
  61. .visit(treeRootHolder.getRoot());
  62. }
  63. private static class UnitTestsFormula implements Formula<UnitTestsCounter> {
  64. @Override
  65. public UnitTestsCounter createNewCounter() {
  66. return new UnitTestsCounter();
  67. }
  68. @Override
  69. public Optional<Measure> createMeasure(UnitTestsCounter counter, CreateMeasureContext context) {
  70. String metricKey = context.getMetric().getKey();
  71. Component leaf = counter.getLeaf();
  72. switch (metricKey) {
  73. case TESTS_KEY:
  74. return createIntMeasure(context.getComponent(), leaf, counter.testsCounter.getValue());
  75. case TEST_ERRORS_KEY:
  76. return createIntMeasure(context.getComponent(), leaf, counter.testsErrorsCounter.getValue());
  77. case TEST_FAILURES_KEY:
  78. return createIntMeasure(context.getComponent(), leaf, counter.testsFailuresCounter.getValue());
  79. case SKIPPED_TESTS_KEY:
  80. return createIntMeasure(context.getComponent(), leaf, counter.skippedTestsCounter.getValue());
  81. case TEST_EXECUTION_TIME_KEY:
  82. return createLongMeasure(context.getComponent(), leaf, counter.testExecutionTimeCounter.getValue());
  83. case TEST_SUCCESS_DENSITY_KEY:
  84. return createDensityMeasure(counter, context.getMetric().getDecimalScale());
  85. default:
  86. throw new IllegalStateException(String.format("Metric '%s' is not supported", metricKey));
  87. }
  88. }
  89. private static Optional<Measure> createIntMeasure(Component currentComponent, Component leafComponent, Optional<Integer> metricValue) {
  90. if (metricValue.isPresent() && leafComponent.getType().isDeeperThan(currentComponent.getType())) {
  91. return Optional.of(Measure.newMeasureBuilder().create(metricValue.get()));
  92. }
  93. return Optional.empty();
  94. }
  95. private static Optional<Measure> createLongMeasure(Component currentComponent, Component leafComponent, Optional<Long> metricValue) {
  96. if (metricValue.isPresent() && leafComponent.getType().isDeeperThan(currentComponent.getType())) {
  97. return Optional.of(Measure.newMeasureBuilder().create(metricValue.get()));
  98. }
  99. return Optional.empty();
  100. }
  101. private static Optional<Measure> createDensityMeasure(UnitTestsCounter counter, int decimalScale) {
  102. if (isPositive(counter.testsCounter.getValue(), true)
  103. && isPositive(counter.testsErrorsCounter.getValue(), false)
  104. && isPositive(counter.testsFailuresCounter.getValue(), false)) {
  105. int tests = counter.testsCounter.getValue().get();
  106. int errors = counter.testsErrorsCounter.getValue().get();
  107. int failures = counter.testsFailuresCounter.getValue().get();
  108. double density = (errors + failures) * 100D / tests;
  109. return Optional.of(Measure.newMeasureBuilder().create(100D - density, decimalScale));
  110. }
  111. return Optional.empty();
  112. }
  113. private static boolean isPositive(Optional<Integer> value, boolean isStrictComparison) {
  114. return value.isPresent() && (isStrictComparison ? (value.get() > 0) : (value.get() >= 0));
  115. }
  116. @Override
  117. public String[] getOutputMetricKeys() {
  118. return METRICS;
  119. }
  120. }
  121. private static class UnitTestsCounter implements Counter<UnitTestsCounter> {
  122. private final IntSumCounter testsCounter = new IntSumCounter(TESTS_KEY);
  123. private final IntSumCounter testsErrorsCounter = new IntSumCounter(TEST_ERRORS_KEY);
  124. private final IntSumCounter testsFailuresCounter = new IntSumCounter(TEST_FAILURES_KEY);
  125. private final IntSumCounter skippedTestsCounter = new IntSumCounter(SKIPPED_TESTS_KEY);
  126. private final LongSumCounter testExecutionTimeCounter = new LongSumCounter(TEST_EXECUTION_TIME_KEY);
  127. private Component leaf;
  128. @Override
  129. public void aggregate(UnitTestsCounter counter) {
  130. testsCounter.aggregate(counter.testsCounter);
  131. testsErrorsCounter.aggregate(counter.testsErrorsCounter);
  132. testsFailuresCounter.aggregate(counter.testsFailuresCounter);
  133. skippedTestsCounter.aggregate(counter.skippedTestsCounter);
  134. testExecutionTimeCounter.aggregate(counter.testExecutionTimeCounter);
  135. }
  136. @Override
  137. public void initialize(CounterInitializationContext context) {
  138. this.leaf = context.getLeaf();
  139. testsCounter.initialize(context);
  140. testsErrorsCounter.initialize(context);
  141. testsFailuresCounter.initialize(context);
  142. skippedTestsCounter.initialize(context);
  143. testExecutionTimeCounter.initialize(context);
  144. }
  145. Component getLeaf() {
  146. return leaf;
  147. }
  148. }
  149. @Override
  150. public String getDescription() {
  151. return "Compute test measures";
  152. }
  153. }