Search in sources :

Example 1 with ActiveRule

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

the class XMLProfileSerializer method appendRules.

private static void appendRules(RulesProfile profile, Writer writer) throws IOException {
    if (!profile.getActiveRules().isEmpty()) {
        writer.append("<rules>");
        for (ActiveRule activeRule : profile.getActiveRules()) {
            appendRule(activeRule, writer);
        }
        writer.append("</rules>");
    }
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule)

Example 2 with ActiveRule

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

the class XMLProfileParserTest method importProfileWithUnknownRuleParameter.

@Test
public void importProfileWithUnknownRuleParameter() {
    ValidationMessages validation = ValidationMessages.create();
    RulesProfile profile = parse("importProfileWithUnknownRuleParameter.xml", validation);
    assertThat(validation.getWarnings()).hasSize(1);
    ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp");
    assertThat(rule.getParameter("unknown")).isNull();
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 3 with ActiveRule

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

the class XMLProfileSerializerTest method exportRuleParameters.

@Test
public void exportRuleParameters() throws IOException, SAXException {
    Writer writer = new StringWriter();
    RulesProfile profile = RulesProfile.create("sonar way", "java");
    Rule rule = Rule.create("checkstyle", "IllegalRegexp", "illegal regexp");
    rule.createParameter("format");
    rule.createParameter("message");
    rule.createParameter("tokens");
    ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
    activeRule.setParameter("format", "foo");
    activeRule.setParameter("message", "with special characters < > &");
    // the tokens parameter is not set
    new XMLProfileSerializer().write(profile, writer);
    assertSimilarXml("exportRuleParameters.xml", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) ActiveRule(org.sonar.api.rules.ActiveRule) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with ActiveRule

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

the class QProfileReset method resetLanguage.

/**
   * Reset built-in profiles for the given language. Missing profiles are created and
   * existing ones are updated.
   */
public void resetLanguage(String language) {
    DbSession dbSession = db.openSession(false);
    try {
        ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
        for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) {
            QProfileName profileName = entry.getKey();
            QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
            List<RuleActivation> activations = Lists.newArrayList();
            for (RulesProfile def : entry.getValue()) {
                for (ActiveRule activeRule : def.getActiveRules()) {
                    RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
                    activation.setSeverity(activeRule.getSeverity().name());
                    if (!activeRule.getActiveRuleParams().isEmpty()) {
                        for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                            activation.setParameter(param.getParamKey(), param.getValue());
                        }
                    } else {
                        for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
                            activation.setParameter(param.getName(), param.getDefaultValue());
                        }
                    }
                    activations.add(activation);
                }
            }
            doReset(dbSession, profile, activations);
        }
    } finally {
        dbSession.close();
    }
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRule(org.sonar.api.rules.ActiveRule) DbSession(org.sonar.db.DbSession) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) Collection(java.util.Collection) RuleParamDto(org.sonar.db.rule.RuleParamDto) Map(java.util.Map) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 5 with ActiveRule

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

the class RulesProfile method activateRule.

/**
   * @param optionalSeverity if null, then the default rule severity is used
   */
public ActiveRule activateRule(final Rule rule, @Nullable RulePriority optionalSeverity) {
    if (Iterables.any(activeRules, new MatchRule(rule))) {
        throw MessageException.of(String.format("The definition of the profile '%s' (language '%s') contains multiple occurrences of the '%s:%s' rule. The plugin which declares this profile should fix this.", getName(), getLanguage(), rule.getRepositoryKey(), rule.getKey()));
    }
    ActiveRule activeRule = new ActiveRule();
    activeRule.setRule(rule);
    activeRule.setRulesProfile(this);
    activeRule.setSeverity(optionalSeverity == null ? rule.getSeverity() : optionalSeverity);
    activeRules.add(activeRule);
    return activeRule;
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule)

Aggregations

ActiveRule (org.sonar.api.rules.ActiveRule)8 Map (java.util.Map)3 Test (org.junit.Test)3 Rule (org.sonar.api.rules.Rule)3 RulesProfile (org.sonar.api.profiles.RulesProfile)2 ValidationMessages (org.sonar.api.utils.ValidationMessages)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 ActiveRuleParam (org.sonar.api.rules.ActiveRuleParam)1 RulePriority (org.sonar.api.rules.RulePriority)1 DbSession (org.sonar.db.DbSession)1 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1