Search in sources :

Example 11 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method checks_as_objects.

/**
 * SONAR-2900
 */
@Test
public void checks_as_objects() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("pattern", "foo").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    CheckWithStringProperty check = new CheckWithStringProperty();
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(check);
    Object createdCheck = checks.of(ruleKey);
    assertThat(createdCheck).isSameAs(check);
    assertThat(((CheckWithStringProperty) createdCheck).getPattern()).isEqualTo("foo");
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 12 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method param_as_string_field.

@Test
public void param_as_string_field() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("pattern", "foo").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithStringProperty.class);
    assertThat(((CheckWithStringProperty) check).getPattern()).isEqualTo("foo");
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 13 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method override_field_key.

@Test
public void override_field_key() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithOverriddenPropertyKey");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("maximum", "300").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithOverriddenPropertyKey.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithOverriddenPropertyKey.class);
    assertThat(((CheckWithOverriddenPropertyKey) check).getMax()).isEqualTo(300);
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 14 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesProvider method transform.

private static ActiveRules transform(Collection<LoadedActiveRule> loadedRules) {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    for (LoadedActiveRule activeRule : loadedRules) {
        NewActiveRule newActiveRule = builder.create(activeRule.getRuleKey());
        newActiveRule.setName(activeRule.getName());
        newActiveRule.setSeverity(activeRule.getSeverity());
        newActiveRule.setCreatedAt(activeRule.getCreatedAt());
        newActiveRule.setLanguage(activeRule.getLanguage());
        newActiveRule.setInternalKey(activeRule.getInternalKey());
        newActiveRule.setTemplateRuleKey(activeRule.getTemplateRuleKey());
        // load parameters
        if (activeRule.getParams() != null) {
            for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) {
                newActiveRule.setParam(params.getKey(), params.getValue());
            }
        }
        newActiveRule.activate();
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarlint-core by SonarSource.

the class StandaloneActiveRulesProvider method createActiveRules.

private ActiveRules createActiveRules() {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    ListMultimap<String, RulesProfile> profilesByLanguage = profilesByLanguage(profileDefinitions);
    for (String language : profilesByLanguage.keySet()) {
        List<RulesProfile> defs = profilesByLanguage.get(language);
        registerProfilesForLanguage(builder, language, defs);
    }
    for (Repository repo : ruleDefsLoader.getContext().repositories()) {
        for (Rule rule : repo.rules()) {
            if (rule.activatedByDefault()) {
                NewActiveRule newAr = builder.create(RuleKey.of(repo.key(), rule.key())).setLanguage(repo.language()).setName(rule.name()).setSeverity(rule.severity()).setInternalKey(rule.internalKey());
                for (Param param : rule.params()) {
                    newAr.setParam(param.key(), param.defaultValue());
                }
                newAr.activate();
            }
        }
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Repository(org.sonar.api.server.rule.RulesDefinition.Repository) RulesProfile(org.sonar.api.profiles.RulesProfile) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) Param(org.sonar.api.server.rule.RulesDefinition.Param) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Rule(org.sonar.api.server.rule.RulesDefinition.Rule) ActiveRule(org.sonar.api.rules.ActiveRule)

Aggregations

NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)24 Test (org.junit.Test)17 RuleKey (org.sonar.api.rule.RuleKey)11 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)9 ActiveRules (org.sonar.api.batch.rule.ActiveRules)7 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 CheckFactory (org.sonar.api.batch.rule.CheckFactory)3 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)3 FileLinesContext (org.sonar.api.measures.FileLinesContext)3 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 File (java.io.File)1