]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1987: Ignore incorrect rule params
authorGodin <mandrikov@gmail.com>
Wed, 8 Dec 2010 17:06:32 +0000 (17:06 +0000)
committerGodin <mandrikov@gmail.com>
Wed, 8 Dec 2010 17:06:32 +0000 (17:06 +0000)
sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java
sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java

index f97cee3685b7edd02cb616c6c47ca8e612d37762..5a9112237d95e7a51109571c907801f0fc856d28 100644 (file)
@@ -34,10 +34,7 @@ import org.sonar.api.rules.RuleParam;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.jpa.dao.RulesDao;
 
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class RulesBackup implements Backupable {
   private Collection<Rule> rules;
@@ -96,6 +93,16 @@ public class RulesBackup implements Backupable {
         LoggerFactory.getLogger(getClass()).error("Unable to find parent rule " + parent.getRepositoryKey() + ":" + parent.getKey());
         continue;
       }
+
+      for (Iterator<RuleParam> irp = rule.getParams().iterator(); irp.hasNext();) {
+        RuleParam param = irp.next();
+        RuleParam matchingRPInDb = rulesDao.getRuleParam(matchingParentRuleInDb, param.getKey());
+        if (matchingRPInDb == null) {
+          LoggerFactory.getLogger(getClass()).error("Unable to find rule parameter in parent " + param.getKey());
+          irp.remove();
+        }
+      }
+
       rule.setParent(matchingParentRuleInDb);
       Rule matchingRuleInDb = session.getSingleResult(Rule.class,
           "pluginName", rule.getRepositoryKey(),
index f5d32822f8383e6b5ece2a58a406e8daa345e87d..1d51c3fd775f56464fa20cc75f7edb63e47c53ef 100644 (file)
@@ -24,11 +24,14 @@ import org.junit.Test;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleParam;
 import org.sonar.api.rules.RulePriority;
+import org.sonar.jpa.dao.RulesDao;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
 import java.util.Arrays;
 
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
@@ -45,6 +48,7 @@ public class RulesBackupTest extends AbstractDbUnitTestCase {
     sonarConfig = new SonarConfig();
 
     rule = Rule.create("repo", "key", "name").setDescription("description");
+    rule.createParameter("param").setDefaultValue("value");
   }
 
   private Rule createUserRule() {
@@ -101,10 +105,24 @@ public class RulesBackupTest extends AbstractDbUnitTestCase {
   }
 
   @Test
-  public void shouldNotFailIfNoParentRule() {
+  public void shouldIgnoreIncorrectRule() {
     sonarConfig.setRules(Arrays.asList(createUserRule()));
     rulesBackup.importXml(sonarConfig);
 
     assertThat(getSession().getResults(Rule.class).size(), is(0));
   }
+
+  @Test
+  public void shouldIgnoreIncorrectParam() {
+    Rule rule = Rule.create("repo", "key", "name").setDescription("description");
+    getSession().save(rule);
+    sonarConfig.setRules(Arrays.asList(createUserRule()));
+    rulesBackup.importXml(sonarConfig);
+
+    assertThat(getSession().getResults(Rule.class).size(), is(2));
+    RulesDao rulesDao = getDao().getRulesDao();
+    Rule importedRule = rulesDao.getRuleByKey("repo", "key2");
+    assertThat(importedRule, notNullValue());
+    assertThat(rulesDao.getRuleParam(importedRule, "param"), nullValue());
+  }
 }