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.

AbstractCoverageSensor.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.coverage;
  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.fs.InputFile;
  29. import org.sonar.api.batch.sensor.Sensor;
  30. import org.sonar.api.batch.sensor.SensorContext;
  31. import org.sonar.api.batch.sensor.SensorDescriptor;
  32. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  33. import org.sonar.api.utils.log.Logger;
  34. import org.sonar.api.utils.log.Loggers;
  35. import org.sonar.xoo.Xoo;
  36. public abstract class AbstractCoverageSensor implements Sensor {
  37. private static final Logger LOG = Loggers.get(AbstractCoverageSensor.class);
  38. private void processCoverage(InputFile inputFile, SensorContext context) {
  39. File coverageFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + getCoverageExtension());
  40. if (coverageFile.exists()) {
  41. LOG.debug("Processing " + coverageFile.getAbsolutePath());
  42. try {
  43. List<String> lines = FileUtils.readLines(coverageFile, context.fileSystem().encoding().name());
  44. NewCoverage coverageBuilder = context.newCoverage()
  45. .onFile(inputFile);
  46. int lineNumber = 0;
  47. for (String line : lines) {
  48. lineNumber++;
  49. if (StringUtils.isBlank(line)) {
  50. continue;
  51. }
  52. if (line.startsWith("#")) {
  53. continue;
  54. }
  55. try {
  56. Iterator<String> split = Splitter.on(":").split(line).iterator();
  57. int lineId = Integer.parseInt(split.next());
  58. int lineHits = Integer.parseInt(split.next());
  59. coverageBuilder.lineHits(lineId, lineHits);
  60. if (split.hasNext()) {
  61. int conditions = Integer.parseInt(split.next());
  62. int coveredConditions = Integer.parseInt(split.next());
  63. coverageBuilder.conditions(lineId, conditions, coveredConditions);
  64. }
  65. } catch (Exception e) {
  66. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverageFile.getAbsolutePath(), e);
  67. }
  68. }
  69. coverageBuilder.save();
  70. } catch (IOException e) {
  71. throw new RuntimeException(e);
  72. }
  73. }
  74. }
  75. protected abstract String getCoverageExtension();
  76. @Override
  77. public void describe(SensorDescriptor descriptor) {
  78. descriptor
  79. .name(getSensorName())
  80. .onlyOnLanguages(Xoo.KEY);
  81. }
  82. protected abstract String getSensorName();
  83. @Override
  84. public void execute(SensorContext context) {
  85. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) {
  86. processCoverage(file, context);
  87. }
  88. }
  89. }