use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class RegisterQualityProfiles method nameOfDefaultProfile.
private static String nameOfDefaultProfile(List<RulesProfile> profiles) {
String defaultName = null;
boolean hasSonarWay = false;
for (RulesProfile profile : profiles) {
if (profile.getDefaultProfile()) {
defaultName = profile.getName();
} else if (DEFAULT_PROFILE_NAME.equals(profile.getName())) {
hasSonarWay = true;
}
}
if (StringUtils.isBlank(defaultName) && !hasSonarWay && !profiles.isEmpty()) {
defaultName = profiles.get(0).getName();
}
return StringUtils.defaultIfBlank(defaultName, DEFAULT_PROFILE_NAME);
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class RegisterQualityProfiles method profilesByLanguage.
/**
* @return profiles by language
*/
private ListMultimap<String, RulesProfile> profilesByLanguage() {
ListMultimap<String, RulesProfile> byLang = ArrayListMultimap.create();
for (ProfileDefinition definition : definitions) {
ValidationMessages validation = ValidationMessages.create();
RulesProfile profile = definition.createProfile(validation);
validation.log(LOGGER);
if (profile != null && !validation.hasErrors()) {
byLang.put(StringUtils.lowerCase(profile.getLanguage()), profile);
}
}
return byLang;
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class CreateActionTest method createImporters.
private ProfileImporter[] createImporters() {
class DefaultProfileImporter extends ProfileImporter {
private DefaultProfileImporter() {
super("xoo_lint", "Xoo Lint");
setSupportedLanguages(XOO_LANGUAGE);
}
@Override
public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
RulesProfile rulesProfile = RulesProfile.create();
rulesProfile.activateRule(org.sonar.api.rules.Rule.create(RULE.getRepositoryKey(), RULE.getRuleKey()), RulePriority.BLOCKER);
return rulesProfile;
}
}
class ProfileImporterGeneratingMessages extends ProfileImporter {
private ProfileImporterGeneratingMessages() {
super("with_messages", "With messages");
setSupportedLanguages(XOO_LANGUAGE);
}
@Override
public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
RulesProfile rulesProfile = RulesProfile.create();
messages.addWarningText("a warning");
messages.addInfoText("an info");
return rulesProfile;
}
}
class ProfileImporterGeneratingErrors extends ProfileImporter {
private ProfileImporterGeneratingErrors() {
super("with_errors", "With errors");
setSupportedLanguages(XOO_LANGUAGE);
}
@Override
public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
RulesProfile rulesProfile = RulesProfile.create();
messages.addErrorText("error!");
return rulesProfile;
}
}
return new ProfileImporter[] { new DefaultProfileImporter(), new ProfileImporterGeneratingMessages(), new ProfileImporterGeneratingErrors() };
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class QProfileResetMediumTest method reset_language_profile.
@Test
public void reset_language_profile() {
RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MINOR);
x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
// Change the severity and the value of the parameter in the active rule
tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(ruleKey).setSeverity(BLOCKER).setParameter("acceptWhitespace", "false"), profile.getKey());
dbSession.commit();
// Verify severity and param has changed
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
reset.resetLanguage(ServerTester.Xoo.KEY);
dbSession.commit();
// Severity and parameter value come back to origin after reset
activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class QProfileResetMediumTest method reset_language_profile_param_when_rule_definition_has_changed.
@Test
public void reset_language_profile_param_when_rule_definition_has_changed() {
RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1"), null);
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
// Change param in the rule def
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
x1.createParam("acceptWhitespace").setDefaultValue("true").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
reset.resetLanguage(ServerTester.Xoo.KEY);
// Parameter value come back to origin after reset
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(params.get(0).getValue()).isEqualTo("true");
}
Aggregations