Search in sources :

Example 6 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class ActiveRulesProvider method load.

private static DefaultActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) {
    Collection<String> qProfileKeys = getKeys(qProfiles);
    Set<RuleKey> loadedRulesKey = new HashSet<>();
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    for (String qProfileKey : qProfileKeys) {
        Collection<LoadedActiveRule> qProfileRules = load(loader, qProfileKey);
        for (LoadedActiveRule r : qProfileRules) {
            if (!loadedRulesKey.contains(r.getRuleKey())) {
                loadedRulesKey.add(r.getRuleKey());
                builder.addRule(transform(r, qProfileKey, r.getDeprecatedKeys()));
            }
        }
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RuleKey(org.sonar.api.rule.RuleKey) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule) HashSet(java.util.HashSet)

Example 7 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class ActiveRulesProvider method transform.

private static ActiveRules transform(Collection<LoadedActiveRule> loadedRules) {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    for (LoadedActiveRule activeRule : loadedRules) {
        NewActiveRule newActiveRule = builder.create(activeRule.getRuleKey());
        newActiveRule.setName(activeRule.getName());
        newActiveRule.setSeverity(activeRule.getSeverity());
        newActiveRule.setCreatedAt(activeRule.getCreatedAt());
        newActiveRule.setLanguage(activeRule.getLanguage());
        newActiveRule.setInternalKey(activeRule.getInternalKey());
        newActiveRule.setTemplateRuleKey(activeRule.getTemplateRuleKey());
        // load parameters
        if (activeRule.getParams() != null) {
            for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) {
                newActiveRule.setParam(params.getKey(), params.getValue());
            }
        }
        newActiveRule.activate();
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class RulesProfileProviderTest method keep_compatibility_with_single_language_projects.

@Test
public void keep_compatibility_with_single_language_projects() {
    settings.setProperty("sonar.language", "java");
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findByLanguage("java")).thenReturn(qProfile);
    RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings);
    // no merge, directly the old hibernate profile
    assertThat(profile).isNotNull();
    assertThat(profile.getLanguage()).isEqualTo("java");
    assertThat(profile.getName()).isEqualTo("Sonar way");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Example 9 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class RulesProfileProviderTest method merge_profiles.

@Test
public void merge_profiles() {
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile));
    RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings);
    // merge of all profiles
    assertThat(profile).isNotNull().isInstanceOf(RulesProfileWrapper.class);
    assertThat(profile.getLanguage()).isEqualTo("");
    assertThat(profile.getName()).isEqualTo("SonarQube");
    assertThat(profile.getActiveRules()).isEmpty();
    try {
        profile.getId();
        fail();
    } catch (IllegalStateException e) {
    // id must not be used at all
    }
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Example 10 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class RulesProfileProviderTest method support_rule_templates.

@Test
public void support_rule_templates() {
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile));
    ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder();
    activeRulesBuilder.create(RuleKey.of("java", "S001")).setTemplateRuleKey("T001").setLanguage("java").activate();
    RulesProfile profile = provider.provide(qProfiles, activeRulesBuilder.build(), settings);
    assertThat(profile.getActiveRule("java", "S001").getRule().getTemplate().getKey()).isEqualTo("T001");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Aggregations

ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)22 Test (org.junit.Test)11 ActiveRules (org.sonar.api.batch.rule.ActiveRules)9 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)9 Before (org.junit.Before)6 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)5 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)4 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 MetricFinder (org.sonar.api.batch.measure.MetricFinder)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 File (java.io.File)1