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}.
*/
// 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()) {
*/
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;
public class RulesProfileProviderTest {
ModuleQProfiles qProfiles = mock(ModuleQProfiles.class);
- ActiveRules activeRules = new ActiveRulesBuilder().build();
Settings settings = new Settings();
RulesProfileProvider provider = new RulesProfileProvider();
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);
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");
}
}