You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestExecutionPublisher.java 4.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.scanner.report;
  21. import java.util.Objects;
  22. import java.util.stream.StreamSupport;
  23. import org.sonar.api.batch.fs.InputComponent;
  24. import org.sonar.api.batch.fs.InputFile;
  25. import org.sonar.api.batch.fs.internal.DefaultInputComponent;
  26. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  27. import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
  28. import org.sonar.scanner.deprecated.test.DefaultTestCase;
  29. import org.sonar.scanner.deprecated.test.DefaultTestCase.Status;
  30. import org.sonar.scanner.deprecated.test.DefaultTestPlan;
  31. import org.sonar.scanner.deprecated.test.TestPlanBuilder;
  32. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  33. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  34. import static org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS;
  35. import static org.sonar.api.measures.CoreMetrics.TESTS;
  36. import static org.sonar.api.measures.CoreMetrics.TEST_ERRORS;
  37. import static org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME;
  38. import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES;
  39. import static org.sonar.scanner.sensor.DefaultSensorStorage.toReportMeasure;
  40. public class TestExecutionPublisher implements ReportPublisherStep {
  41. private final InputComponentStore componentStore;
  42. private final TestPlanBuilder testPlanBuilder;
  43. public TestExecutionPublisher(InputComponentStore componentStore, TestPlanBuilder testPlanBuilder) {
  44. this.componentStore = componentStore;
  45. this.testPlanBuilder = testPlanBuilder;
  46. }
  47. @Override
  48. public void publish(ScannerReportWriter writer) {
  49. for (final InputComponent c : componentStore.all()) {
  50. DefaultInputComponent component = (DefaultInputComponent) c;
  51. if (component.isFile()) {
  52. DefaultInputFile file = (DefaultInputFile) component;
  53. // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports
  54. updateTestExecutionFromTestPlan(file, writer);
  55. }
  56. }
  57. }
  58. private void updateTestExecutionFromTestPlan(final InputFile inputFile, ScannerReportWriter writer) {
  59. final DefaultTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile);
  60. if (testPlan == null || !testPlan.testCases().iterator().hasNext()) {
  61. return;
  62. }
  63. long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count();
  64. appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
  65. long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).map(DefaultTestCase::durationInMs).filter(Objects::nonNull).mapToLong(Long::longValue)
  66. .sum();
  67. appendMeasure(inputFile, writer, new DefaultMeasure<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
  68. long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count();
  69. appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
  70. long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count();
  71. appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
  72. long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count();
  73. appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_FAILURES).withValue((int) failedTests));
  74. }
  75. private static void appendMeasure(InputFile inputFile, ScannerReportWriter writer, DefaultMeasure measure) {
  76. writer.appendComponentMeasure(((DefaultInputComponent) inputFile).scannerId(), toReportMeasure(measure));
  77. }
  78. }