Search in sources :

Example 6 with RulesProfile

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);
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile)

Example 7 with RulesProfile

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;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileDefinition(org.sonar.api.profiles.ProfileDefinition) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 8 with RulesProfile

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() };
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileImporter(org.sonar.api.profiles.ProfileImporter) Reader(java.io.Reader) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 9 with RulesProfile

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");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleParam(org.sonar.api.rules.RuleParam) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 10 with RulesProfile

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");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Aggregations

RulesProfile (org.sonar.api.profiles.RulesProfile)19 Test (org.junit.Test)5 ValidationMessages (org.sonar.api.utils.ValidationMessages)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)3 DbSession (org.sonar.db.DbSession)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ProfileDefinition (org.sonar.api.profiles.ProfileDefinition)2 ProfileImporter (org.sonar.api.profiles.ProfileImporter)2 RuleKey (org.sonar.api.rule.RuleKey)2 ActiveRule (org.sonar.api.rules.ActiveRule)2 ActiveRuleParam (org.sonar.api.rules.ActiveRuleParam)2 Rule (org.sonar.api.rules.Rule)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)2 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)2 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)2