From ca253b85929fa9c372e285b617a7c8991d89698b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 9 Jul 2015 15:40:45 +0200 Subject: [PATCH] SONAR-6706 org.sonar.api.rules.ActiveRule.getRule().getTemplate() is always null on batch --- .../batch/rule/RulesProfileProvider.java | 14 +++++++---- .../batch/rule/RulesProfileProviderTest.java | 23 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java index f890761ba37..7ab4959321d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java @@ -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}. */ @@ -75,9 +73,15 @@ public class RulesProfileProvider extends ProviderAdapter { // 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 param : activeRule.params().entrySet()) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java index e1a52981d12..93aab184a18 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProfileProviderTest.java @@ -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; @@ -35,7 +34,6 @@ import static org.mockito.Mockito.when; public class RulesProfileProviderTest { ModuleQProfiles qProfiles = mock(ModuleQProfiles.class); - ActiveRules activeRules = new ActiveRulesBuilder().build(); Settings settings = new Settings(); RulesProfileProvider provider = new RulesProfileProvider(); @@ -44,7 +42,7 @@ public class RulesProfileProviderTest { 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); @@ -66,12 +64,23 @@ public class RulesProfileProviderTest { 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"); } } -- 2.39.5