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.

DefaultCoverage.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.api.batch.sensor.coverage.internal;
  21. import java.util.Collections;
  22. import java.util.SortedMap;
  23. import java.util.TreeMap;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  27. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  28. import org.sonar.api.batch.sensor.internal.DefaultStorable;
  29. import org.sonar.api.batch.sensor.internal.SensorStorage;
  30. import static java.util.Objects.requireNonNull;
  31. import static org.sonar.api.utils.Preconditions.checkState;
  32. public class DefaultCoverage extends DefaultStorable implements NewCoverage {
  33. private InputFile inputFile;
  34. private int totalCoveredLines = 0;
  35. private int totalConditions = 0;
  36. private int totalCoveredConditions = 0;
  37. private SortedMap<Integer, Integer> hitsByLine = new TreeMap<>();
  38. private SortedMap<Integer, Integer> conditionsByLine = new TreeMap<>();
  39. private SortedMap<Integer, Integer> coveredConditionsByLine = new TreeMap<>();
  40. public DefaultCoverage() {
  41. super();
  42. }
  43. public DefaultCoverage(@Nullable SensorStorage storage) {
  44. super(storage);
  45. }
  46. @Override
  47. public DefaultCoverage onFile(InputFile inputFile) {
  48. this.inputFile = inputFile;
  49. return this;
  50. }
  51. public InputFile inputFile() {
  52. return inputFile;
  53. }
  54. @Override
  55. public NewCoverage lineHits(int line, int hits) {
  56. validateFile();
  57. if (isExcluded()) {
  58. return this;
  59. }
  60. validateLine(line);
  61. if (!hitsByLine.containsKey(line)) {
  62. hitsByLine.put(line, hits);
  63. if (hits > 0) {
  64. totalCoveredLines += 1;
  65. }
  66. }
  67. return this;
  68. }
  69. private void validateLine(int line) {
  70. checkState(line <= inputFile.lines(), "Line %s is out of range in the file %s (lines: %s)", line, inputFile, inputFile.lines());
  71. checkState(line > 0, "Line number must be strictly positive: %s", line);
  72. }
  73. private void validateFile() {
  74. requireNonNull(inputFile, "Call onFile() first");
  75. }
  76. @Override
  77. public NewCoverage conditions(int line, int conditions, int coveredConditions) {
  78. validateFile();
  79. if (isExcluded()) {
  80. return this;
  81. }
  82. validateLine(line);
  83. if (conditions > 0 && !conditionsByLine.containsKey(line)) {
  84. totalConditions += conditions;
  85. totalCoveredConditions += coveredConditions;
  86. conditionsByLine.put(line, conditions);
  87. coveredConditionsByLine.put(line, coveredConditions);
  88. }
  89. return this;
  90. }
  91. public int coveredLines() {
  92. return totalCoveredLines;
  93. }
  94. public int linesToCover() {
  95. return hitsByLine.size();
  96. }
  97. public int conditions() {
  98. return totalConditions;
  99. }
  100. public int coveredConditions() {
  101. return totalCoveredConditions;
  102. }
  103. public SortedMap<Integer, Integer> hitsByLine() {
  104. return Collections.unmodifiableSortedMap(hitsByLine);
  105. }
  106. public SortedMap<Integer, Integer> conditionsByLine() {
  107. return Collections.unmodifiableSortedMap(conditionsByLine);
  108. }
  109. public SortedMap<Integer, Integer> coveredConditionsByLine() {
  110. return Collections.unmodifiableSortedMap(coveredConditionsByLine);
  111. }
  112. @Override
  113. public void doSave() {
  114. validateFile();
  115. if (!isExcluded()) {
  116. storage.store(this);
  117. }
  118. }
  119. private boolean isExcluded() {
  120. return ((DefaultInputFile) inputFile).isExcludedForCoverage();
  121. }
  122. }