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.

CoverageRuleTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.ce.task.projectanalysis.issue.commonrule;
  21. import com.google.common.collect.ImmutableMap;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.measures.CoreMetrics;
  26. import org.sonar.api.rule.RuleKey;
  27. import org.sonar.api.rule.Severity;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  30. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  31. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  32. import org.sonar.ce.task.projectanalysis.measure.Measure;
  33. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  34. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  35. import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
  36. import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderRule;
  37. import org.sonar.core.issue.DefaultIssue;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. public abstract class CoverageRuleTest {
  40. private static final String PLUGIN_KEY = "java";
  41. private static final String QP_KEY = "qp1";
  42. static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
  43. .setFileAttributes(new FileAttributes(false, "java", 1))
  44. .build();
  45. @Rule
  46. public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule();
  47. @Rule
  48. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  49. .add(CoreMetrics.LINE_COVERAGE)
  50. .add(CoreMetrics.LINES_TO_COVER)
  51. .add(CoreMetrics.UNCOVERED_LINES)
  52. .add(CoreMetrics.BRANCH_COVERAGE)
  53. .add(CoreMetrics.CONDITIONS_TO_COVER)
  54. .add(CoreMetrics.UNCOVERED_CONDITIONS);
  55. @Rule
  56. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  57. @Rule
  58. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  59. CommonRule underTest = createRule();
  60. @Before
  61. public void setUp() {
  62. treeRootHolder.setRoot(ReportComponent.DUMB_PROJECT);
  63. }
  64. protected abstract CommonRule createRule();
  65. protected abstract RuleKey getRuleKey();
  66. protected abstract String getMinPropertyKey();
  67. protected abstract String getCoverageMetricKey();
  68. protected abstract String getToCoverMetricKey();
  69. protected abstract String getUncoveredMetricKey();
  70. @Test
  71. public void no_issue_if_enough_coverage() {
  72. activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
  73. measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1));
  74. DefaultIssue issue = underTest.processFile(FILE, "java");
  75. assertThat(issue).isNull();
  76. }
  77. @Test
  78. public void issue_if_coverage_is_too_low() {
  79. activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
  80. measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
  81. measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40));
  82. measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50));
  83. DefaultIssue issue = underTest.processFile(FILE, "java");
  84. assertThat(issue.ruleKey()).isEqualTo(getRuleKey());
  85. assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
  86. // FIXME explain
  87. assertThat(issue.gap()).isEqualTo(23.0);
  88. assertThat(issue.message()).isEqualTo(getExpectedIssueMessage());
  89. }
  90. protected abstract String getExpectedIssueMessage();
  91. @Test
  92. public void no_issue_if_coverage_is_not_set() {
  93. activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY, QP_KEY));
  94. DefaultIssue issue = underTest.processFile(FILE, "java");
  95. assertThat(issue).isNull();
  96. }
  97. @Test
  98. public void ignored_if_rule_is_deactivated() {
  99. // coverage is too low, but rule is not activated
  100. measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
  101. DefaultIssue issue = underTest.processFile(FILE, "java");
  102. assertThat(issue).isNull();
  103. }
  104. }