Search in sources :

Example 1 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class AnnotationProfileParser method addRule.

private void addRule(Class aClass, BelongsToProfile annotation, RulesProfile profile, String repositoryKey, ValidationMessages messages) {
    if ((annotation != null) && StringUtils.equals(annotation.title(), profile.getName())) {
        String ruleKey = RuleAnnotationUtils.getRuleKey(aClass);
        Rule rule = ruleFinder.findByKey(repositoryKey, ruleKey);
        if (rule == null) {
            messages.addWarningText("Rule not found: [repository=" + repositoryKey + ", key=" + ruleKey + "]");
        } else {
            RulePriority priority = null;
            if (annotation.priority() != null) {
                priority = RulePriority.fromCheckPriority(annotation.priority());
            }
            profile.activateRule(rule, priority);
        }
    }
}
Also used : Rule(org.sonar.api.rules.Rule) RulePriority(org.sonar.api.rules.RulePriority)

Example 2 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class FakeRule method shouldParseOnlyWantedProfile.

@Test
public void shouldParseOnlyWantedProfile() {
    RuleFinder ruleFinder = mock(RuleFinder.class);
    when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {

        public Rule answer(InvocationOnMock iom) throws Throwable {
            return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]);
        }
    });
    ValidationMessages messages = ValidationMessages.create();
    RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class, RuleOnOtherProfile.class), messages);
    assertThat(profile.getActiveRule("squid", "fake")).isNotNull();
    assertThat(profile.getActiveRule("squid", "other")).isNull();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Rule(org.sonar.api.rules.Rule) Matchers.anyString(org.mockito.Matchers.anyString) RuleFinder(org.sonar.api.rules.RuleFinder) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 3 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class FakeRule method shouldParseAnnotatedClasses.

@Test
public void shouldParseAnnotatedClasses() {
    RuleFinder ruleFinder = mock(RuleFinder.class);
    when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {

        public Rule answer(InvocationOnMock iom) throws Throwable {
            return Rule.create((String) iom.getArguments()[0], (String) iom.getArguments()[1], (String) iom.getArguments()[1]);
        }
    });
    ValidationMessages messages = ValidationMessages.create();
    RulesProfile profile = new AnnotationProfileParser(ruleFinder).parse("squid", "Foo way", "java", Lists.<Class>newArrayList(FakeRule.class), messages);
    assertThat(profile.getName()).isEqualTo("Foo way");
    assertThat(profile.getLanguage()).isEqualTo("java");
    assertThat(profile.getActiveRule("squid", "fake").getSeverity()).isEqualTo(RulePriority.BLOCKER);
    assertThat(messages.hasErrors()).isFalse();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Rule(org.sonar.api.rules.Rule) Matchers.anyString(org.mockito.Matchers.anyString) RuleFinder(org.sonar.api.rules.RuleFinder) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 4 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class IssuePatternTest method shouldMatchViolation.

@Test
public void shouldMatchViolation() {
    Rule rule = Rule.create("checkstyle", "IllegalRegexp", "");
    String javaFile = "org.foo.Bar";
    IssuePattern pattern = new IssuePattern("*", "*");
    pattern.addLine(12);
    assertThat(pattern.match(create(rule, javaFile, null))).isFalse();
    assertThat(pattern.match(create(rule, javaFile, 12))).isTrue();
    assertThat(pattern.match(create(rule, null, null))).isFalse();
}
Also used : Rule(org.sonar.api.rules.Rule) Test(org.junit.Test)

Example 5 with Rule

use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.

the class QProfileExporters method wrap.

private RulesProfile wrap(QualityProfileDto profile) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
        List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
        ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId);
        for (ActiveRuleDto activeRule : activeRuleDtos) {
            // TODO all rules should be loaded by using one query with all active rule keys as parameter
            Rule rule = ruleFinder.findByKey(activeRule.getKey().ruleKey());
            org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString()));
            List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId());
            for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
                wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
            }
        }
        return target;
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Rule(org.sonar.api.rules.Rule) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

Rule (org.sonar.api.rules.Rule)31 Test (org.junit.Test)22 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)11 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 List (java.util.List)9 DbSession (org.sonar.db.DbSession)9 Arrays (java.util.Arrays)8 Consumer (java.util.function.Consumer)8 Collectors.toList (java.util.stream.Collectors.toList)8 Nullable (javax.annotation.Nullable)8 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Before (org.junit.Before)8 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.verify (org.mockito.Mockito.verify)8 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)8 Mockito.when (org.mockito.Mockito.when)8 AlwaysIncreasingSystem2 (org.sonar.api.impl.utils.AlwaysIncreasingSystem2)8 RuleKey (org.sonar.api.rule.RuleKey)8