From 549fae5fbba5ae062565fd20269c61f408f715f1 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 3 Mar 2015 13:06:50 +0100 Subject: [PATCH] SONAR-6238 Updating only the name of a rule is not taking in account when synchronizing rules at startup --- .../org/sonar/server/rule/RegisterRules.java | 12 ++--- .../server/rule/RegisterRulesMediumTest.java | 50 +++++++++++++++++++ .../sonar/server/rule/RegisterRulesTest.java | 2 + 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index e2ea5e4ff98..674e9cade10 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -51,12 +51,8 @@ import org.sonar.server.startup.RegisterDebtModel; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; + +import java.util.*; import static com.google.common.collect.Lists.newArrayList; @@ -228,7 +224,9 @@ public class RegisterRules implements Startable { dto.setName(def.name()); changed = true; } - changed = mergeDescription(def, dto); + if (mergeDescription(def, dto)) { + changed= true; + } if (!dto.getSystemTags().containsAll(def.tags())) { dto.setSystemTags(def.tags()); changed = true; diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java index 292b0e3effb..2bc554247f7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java @@ -252,6 +252,56 @@ public class RegisterRulesMediumTest { assertThat(rule.effortToFixDescription()).isEqualTo("Effort2"); } + @Test + public void update_only_rule_name() { + register(new Rules() { + @Override + public void init(RulesDefinition.NewRepository repository) { + repository.createRule("x1") + .setName("Name1") + .setHtmlDescription("Desc1"); + } + }); + + register(new Rules() { + @Override + public void init(RulesDefinition.NewRepository repository) { + repository.createRule(RuleTesting.XOO_X1.rule()) + .setName("Name2") + .setHtmlDescription("Desc1"); + } + }); + + Rule rule = ruleIndex.getByKey(RuleTesting.XOO_X1); + assertThat(rule.name()).isEqualTo("Name2"); + assertThat(rule.htmlDescription()).isEqualTo("Desc1"); + } + + @Test + public void update_only_rule_description() { + register(new Rules() { + @Override + public void init(RulesDefinition.NewRepository repository) { + repository.createRule("x1") + .setName("Name1") + .setHtmlDescription("Desc1"); + } + }); + + register(new Rules() { + @Override + public void init(RulesDefinition.NewRepository repository) { + repository.createRule(RuleTesting.XOO_X1.rule()) + .setName("Name1") + .setHtmlDescription("Desc2"); + } + }); + + Rule rule = ruleIndex.getByKey(RuleTesting.XOO_X1); + assertThat(rule.name()).isEqualTo("Name1"); + assertThat(rule.htmlDescription()).isEqualTo("Desc2"); + } + @Test public void do_not_update_rules_if_no_changes() throws Exception { Rules rules = new Rules() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index 930515c1de7..7c1abbfedc7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -218,6 +218,8 @@ public class RegisterRulesTest extends AbstractDaoTestCase { RegisterRules task = new RegisterRules(loader, ruleActivator, dbClient, languages); task.start(); + // Execute a commit to refresh session state as the task is using its own session + dbSession.commit(); } private RuleParamDto getParam(List params, String key) { -- 2.39.5