Search in sources :

Example 6 with ActiveRule

use of org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule in project sonarqube by SonarSource.

the class SkippedTestRuleTest method issue_if_skipped_tests.

@Test
public void issue_if_skipped_tests() {
    activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY, QP_KEY));
    measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(2));
    DefaultIssue issue = underTest.processFile(FILE, "java");
    assertThat(issue.ruleKey()).isEqualTo(RULE_KEY);
    assertThat(issue.severity()).isEqualTo(Severity.CRITICAL);
    assertThat(issue.gap()).isEqualTo(2.0);
    assertThat(issue.message()).isEqualTo("Fix or remove skipped unit tests in file \"FooTest.java\".");
}
Also used : ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 7 with ActiveRule

use of org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule in project sonarqube by SonarSource.

the class CommonRule method processFile.

@CheckForNull
public DefaultIssue processFile(Component file, String fileLanguage) {
    DefaultIssue issue = null;
    RuleKey ruleKey = RuleKey.of(commonRepositoryForLang(fileLanguage), key);
    Optional<ActiveRule> activeRule = activeRulesHolder.get(ruleKey);
    if (activeRule.isPresent()) {
        CommonRuleIssue cri = doProcessFile(file, activeRule.get());
        if (cri != null) {
            issue = new DefaultIssue();
            issue.setGap(cri.effortToFix);
            issue.setMessage(cri.message);
            issue.setRuleKey(ruleKey);
            issue.setSeverity(activeRule.get().getSeverity());
            issue.setLine(null);
            issue.setChecksum("");
            issue.setIsFromExternalRuleEngine(false);
        }
    }
    return issue;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule) DefaultIssue(org.sonar.core.issue.DefaultIssue) CheckForNull(javax.annotation.CheckForNull)

Example 8 with ActiveRule

use of org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule in project sonarqube by SonarSource.

the class IssueCreationDateCalculator method onIssue.

@Override
public void onIssue(Component component, DefaultIssue issue) {
    if (!issue.isNew()) {
        return;
    }
    Optional<Long> lastAnalysisOptional = lastAnalysis();
    boolean firstAnalysis = !lastAnalysisOptional.isPresent();
    if (firstAnalysis || isNewFile(component)) {
        backdateIssue(component, issue);
        return;
    }
    Rule rule = ruleRepository.findByKey(issue.getRuleKey()).orElseThrow(illegalStateException("The rule with key '%s' raised an issue, but no rule with that key was found", issue.getRuleKey()));
    if (rule.isExternal()) {
        backdateIssue(component, issue);
    } else {
        // Rule can't be inactive (see contract of IssueVisitor)
        ActiveRule activeRule = activeRulesHolder.get(issue.getRuleKey()).get();
        if (activeRuleIsNewOrChanged(activeRule, lastAnalysisOptional.get()) || ruleImplementationChanged(activeRule.getRuleKey(), activeRule.getPluginKey(), lastAnalysisOptional.get()) || qualityProfileChanged(activeRule.getQProfileKey())) {
            backdateIssue(component, issue);
        }
    }
}
Also used : ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule) ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule)

Example 9 with ActiveRule

use of org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule in project sonarqube by SonarSource.

the class CommentDensityRuleTest method no_issues_if_enough_comments.

@Test
public void no_issues_if_enough_comments() {
    activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, PLUGIN_KEY, QP_KEY));
    measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(90.0, 1));
    DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY);
    assertThat(issue).isNull();
}
Also used : ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 10 with ActiveRule

use of org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule in project sonarqube by SonarSource.

the class CommentDensityRuleTest method prepareForIssue.

private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) {
    activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, PLUGIN_KEY, QP_KEY));
    measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(commentLineDensity, 1));
    measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_KEY, Measure.newMeasureBuilder().create(commentLines));
    measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.NCLOC_KEY, Measure.newMeasureBuilder().create(ncloc));
}
Also used : ActiveRule(org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule)

Aggregations

ActiveRule (org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule)21 Test (org.junit.Test)17 DefaultIssue (org.sonar.core.issue.DefaultIssue)13 RuleKey (org.sonar.api.rule.RuleKey)2 HashMap (java.util.HashMap)1 CheckForNull (javax.annotation.CheckForNull)1 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)1