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.

TestExecutionSensor.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.xoo.test;
  21. import com.google.common.base.Splitter;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.apache.commons.io.FileUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.sonar.api.batch.DependedUpon;
  29. import org.sonar.api.batch.fs.FilePredicates;
  30. import org.sonar.api.batch.fs.FileSystem;
  31. import org.sonar.api.batch.fs.InputFile;
  32. import org.sonar.api.batch.fs.InputFile.Type;
  33. import org.sonar.api.batch.sensor.Sensor;
  34. import org.sonar.api.batch.sensor.SensorContext;
  35. import org.sonar.api.batch.sensor.SensorDescriptor;
  36. import org.sonar.api.component.ResourcePerspectives;
  37. import org.sonar.api.test.MutableTestCase;
  38. import org.sonar.api.test.MutableTestPlan;
  39. import org.sonar.api.test.TestCase;
  40. import org.sonar.api.utils.log.Logger;
  41. import org.sonar.api.utils.log.Loggers;
  42. import org.sonar.xoo.Xoo;
  43. /**
  44. * Parse files *.xoo.test
  45. */
  46. @DependedUpon("test-exec")
  47. public class TestExecutionSensor implements Sensor {
  48. private static final Logger LOG = Loggers.get(TestExecutionSensor.class);
  49. private static final String TEST_EXTENSION = ".test";
  50. private final FileSystem fs;
  51. private final ResourcePerspectives perspectives;
  52. public TestExecutionSensor(FileSystem fileSystem, ResourcePerspectives perspectives) {
  53. this.fs = fileSystem;
  54. this.perspectives = perspectives;
  55. }
  56. private void processTestFile(InputFile inputFile, SensorContext context) {
  57. File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
  58. if (testExecutionFile.exists()) {
  59. LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
  60. try {
  61. List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
  62. int lineNumber = 0;
  63. MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
  64. for (String line : lines) {
  65. lineNumber++;
  66. if (StringUtils.isBlank(line)) {
  67. continue;
  68. }
  69. if (line.startsWith("#")) {
  70. continue;
  71. }
  72. try {
  73. Iterator<String> split = Splitter.on(":").split(line).iterator();
  74. String name = split.next();
  75. String durationStr = split.next();
  76. Long duration = StringUtils.isNotBlank(durationStr) ? Long.parseLong(durationStr) : null;
  77. String msg = split.next();
  78. String stack = split.next();
  79. String status = split.next();
  80. String type = split.next();
  81. MutableTestCase testCase = testPlan.addTestCase(name);
  82. testCase.setDurationInMs(duration);
  83. testCase.setMessage(msg);
  84. testCase.setStackTrace(stack);
  85. testCase.setStatus(TestCase.Status.valueOf(status));
  86. testCase.setType(type);
  87. } catch (Exception e) {
  88. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
  89. }
  90. }
  91. } catch (IOException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. }
  96. @Override
  97. public void describe(SensorDescriptor descriptor) {
  98. descriptor
  99. .name("Xoo Test Execution Sensor")
  100. .onlyOnLanguages(Xoo.KEY);
  101. }
  102. @Override
  103. public void execute(SensorContext context) {
  104. FilePredicates p = context.fileSystem().predicates();
  105. for (InputFile file : context.fileSystem().inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.TEST)))) {
  106. processTestFile(file, context);
  107. }
  108. }
  109. }