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.7KB

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