Search in sources :

Example 86 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class CheckFactoryTest method fail_if_missing_field.

@Test
public void fail_if_missing_field() {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty");
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty");
    builder.create(ruleKey).setParam("unknown", "foo").activate();
    CheckFactory checkFactory = new CheckFactory(builder.build());
    checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 87 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class CheckFactoryTest method use_template_rule_key.

@Test
public void use_template_rule_key() {
    RuleKey ruleKey = RuleKey.of("squid", "S0001_123");
    builder.create(ruleKey).setTemplateRuleKey("S0001").activate();
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithKey.class);
    assertThat(checks.of(ruleKey)).isSameAs(check);
    assertThat(checks.ruleKey(check)).isEqualTo(ruleKey);
    assertThat(checks.all()).containsOnly(check);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 88 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class CheckFactoryTest method param_as_inherited_field.

/**
   * SONAR-3164
   */
@Test
public void param_as_inherited_field() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties");
    builder.create(ruleKey).setParam("max", "300").activate();
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class);
    assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 89 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class DefaultActiveRulesLoader method readPage.

private static List<LoadedActiveRule> readPage(SearchResponse response) {
    List<LoadedActiveRule> loadedRules = new LinkedList<>();
    List<Rule> rulesList = response.getRulesList();
    Map<String, ActiveList> actives = response.getActives().getActives();
    for (Rule r : rulesList) {
        ActiveList activeList = actives.get(r.getKey());
        Active active = activeList.getActiveList(0);
        LoadedActiveRule loadedRule = new LoadedActiveRule();
        loadedRule.setRuleKey(RuleKey.parse(r.getKey()));
        loadedRule.setName(r.getName());
        loadedRule.setSeverity(active.getSeverity());
        loadedRule.setCreatedAt(dateToLong(parseDateTime(active.getCreatedAt())));
        loadedRule.setLanguage(r.getLang());
        loadedRule.setInternalKey(r.getInternalKey());
        if (r.hasTemplateKey()) {
            RuleKey templateRuleKey = RuleKey.parse(r.getTemplateKey());
            loadedRule.setTemplateRuleKey(templateRuleKey.rule());
        }
        Map<String, String> params = new HashMap<>();
        for (Rules.Rule.Param param : r.getParams().getParamsList()) {
            params.put(param.getKey(), param.getDefaultValue());
        }
        // overrides defaultValue if the key is the same
        for (Param param : active.getParamsList()) {
            params.put(param.getKey(), param.getValue());
        }
        loadedRule.setParams(params);
        loadedRules.add(loadedRule);
    }
    return loadedRules;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) HashMap(java.util.HashMap) ActiveList(org.sonarqube.ws.Rules.ActiveList) LinkedList(java.util.LinkedList) Active(org.sonarqube.ws.Rules.Active) Param(org.sonarqube.ws.Rules.Active.Param) Rule(org.sonarqube.ws.Rules.Rule)

Example 90 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class JSONReport method writeJson.

@VisibleForTesting
void writeJson(Writer writer) {
    try (JsonWriter json = JsonWriter.of(writer)) {
        json.beginObject();
        json.prop("version", server.getVersion());
        Set<RuleKey> ruleKeys = new LinkedHashSet<>();
        Set<String> userLogins = new LinkedHashSet<>();
        writeJsonIssues(json, ruleKeys, userLogins);
        writeJsonComponents(json);
        writeJsonRules(json, ruleKeys);
        writeUsers(json, userLogins);
        json.endObject();
    } catch (IOException e) {
        throw new IllegalStateException("Unable to write JSON report", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RuleKey(org.sonar.api.rule.RuleKey) IOException(java.io.IOException) JsonWriter(org.sonar.api.utils.text.JsonWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3