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>");
}
}
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();
}
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());
}
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();
}
}
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;
}
Aggregations