Skip to content

Commit

Permalink
Automatic merge from branch-5.1
Browse files Browse the repository at this point in the history
* origin/branch-5.1:
  SONAR-6706 org.sonar.api.rules.ActiveRule.getRule().getTemplate() is always null on batch
  • Loading branch information
Jenkins CI committed Jul 9, 2015
2 parents 726c1fa + ca253b8 commit 0bdf27c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@
package org.sonar.batch.rule;

import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.DefaultActiveRules;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;

import java.util.Collection;
import java.util.Map;

/**
* Ensures backward-compatibility with extensions that use {@link org.sonar.api.profiles.RulesProfile}.
*/
Expand Down Expand Up @@ -75,9 +73,15 @@ private RulesProfile select(QProfile qProfile, ActiveRules activeRules) {
// TODO deprecatedProfile.setVersion(qProfile.version());
deprecatedProfile.setName(qProfile.getName());
deprecatedProfile.setLanguage(qProfile.getLanguage());
for (org.sonar.api.batch.rule.ActiveRule activeRule : ((DefaultActiveRules) activeRules).findByLanguage(qProfile.getLanguage())) {
for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) {
Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule());
rule.setConfigKey(activeRule.internalKey());

// SONAR-6706
if (activeRule.templateRuleKey() != null) {
rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey()));
}

ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule,
RulePriority.valueOf(activeRule.severity()));
for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
*/
package org.sonar.batch.rule;

import java.util.Arrays;
import org.junit.Test;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;

import java.util.Arrays;
import org.sonar.api.rule.RuleKey;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -35,7 +34,6 @@
public class RulesProfileProviderTest {

ModuleQProfiles qProfiles = mock(ModuleQProfiles.class);
ActiveRules activeRules = new ActiveRulesBuilder().build();
Settings settings = new Settings();
RulesProfileProvider provider = new RulesProfileProvider();

Expand All @@ -44,7 +42,7 @@ 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, activeRules, settings);
RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings);

// merge of all profiles
assertThat(profile).isNotNull().isInstanceOf(RulesProfileWrapper.class);
Expand All @@ -66,12 +64,23 @@ public void keep_compatibility_with_single_language_projects() {
QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
when(qProfiles.findByLanguage("java")).thenReturn(qProfile);

RulesProfile profile = provider.provide(qProfiles, activeRules, settings);
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");
;
}

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

0 comments on commit 0bdf27c

Please sign in to comment.