summaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-12-08 15:54:55 +0000
committerGodin <mandrikov@gmail.com>2010-12-08 15:54:55 +0000
commitee3e40b22465e90477a772d5222ea7bf8e858060 (patch)
tree21139b1f9bd21a32adc4917f04770b27e25b43c2 /sonar-server
parent1ba217e3e27485a9c4e7176a04acf4cfdd10ac6b (diff)
downloadsonarqube-ee3e40b22465e90477a772d5222ea7bf8e858060.tar.gz
sonarqube-ee3e40b22465e90477a772d5222ea7bf8e858060.zip
SONAR-1987: Fix bug and improve test
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java4
-rw-r--r--sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java85
2 files changed, 71 insertions, 18 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java b/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java
index 62382851002..87ac7b5803d 100644
--- a/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java
+++ b/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java
@@ -97,7 +97,9 @@ public class RulesBackup implements Backupable {
continue;
}
rule.setParent(matchingParentRuleInDb);
- Rule matchingRuleInDb = rulesDao.getRuleByKey(rule.getRepositoryKey(), rule.getKey());
+ Rule matchingRuleInDb = session.getSingleResult(Rule.class,
+ "pluginName", rule.getRepositoryKey(),
+ "key", rule.getKey());
if (matchingRuleInDb != null) {
matchingRuleInDb.setName(rule.getName());
matchingRuleInDb.setDescription(rule.getDescription());
diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java
index 9886fdc5bf0..d8caaa5a93c 100644
--- a/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java
@@ -1,12 +1,32 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
package org.sonar.server.configuration;
+import org.junit.Before;
import org.junit.Test;
import org.sonar.api.rules.Rule;
-import org.sonar.jpa.dao.RulesDao;
+import org.sonar.api.rules.RuleParam;
+import org.sonar.api.rules.RulePriority;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.util.Arrays;
-import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@@ -14,16 +34,30 @@ import static org.junit.Assert.assertTrue;
public class RulesBackupTest extends AbstractDbUnitTestCase {
- @Test
- public void shouldExportRules() {
- SonarConfig sonarConfig = new SonarConfig();
+ private RulesBackup rulesBackup;
+ private SonarConfig sonarConfig;
+
+ private Rule rule;
- Rule rule = Rule.create("repo", "key", "name").setDescription("description");
+ @Before
+ public void setUp() {
+ rulesBackup = new RulesBackup(getSession());
+ sonarConfig = new SonarConfig();
+ rule = Rule.create("repo", "key", "name").setDescription("description");
+ }
+
+ private Rule createUserRule() {
Rule userRule = Rule.create("repo", "key2", "name2").setDescription("description2");
userRule.setParent(rule);
+ userRule.setSeverity(RulePriority.INFO);
userRule.createParameter("param").setDefaultValue("value");
+ return userRule;
+ }
+ @Test
+ public void shouldExportRules() {
+ Rule userRule = createUserRule();
RulesBackup rulesBackup = new RulesBackup(Arrays.asList(userRule));
rulesBackup.exportXml(sonarConfig);
@@ -33,23 +67,40 @@ public class RulesBackupTest extends AbstractDbUnitTestCase {
@Test
public void shouldImportRules() {
- RulesDao rulesDao = getDao().getRulesDao();
-
- RulesBackup rulesBackup = new RulesBackup(getSession());
- SonarConfig sonarConfig = new SonarConfig();
+ getSession().save(rule);
- Rule rule = Rule.create("repo", "key", "name").setDescription("description");
+ sonarConfig.setRules(Arrays.asList(createUserRule()));
+ rulesBackup.importXml(sonarConfig);
- Rule userRule = Rule.create("repo", "key2", "name2").setDescription("description2");
- userRule.setParent(rule);
- userRule.createParameter("param").setDefaultValue("value");
+ assertThat(getSession().getResults(Rule.class).size(), is(2));
+ Rule importedRule = getDao().getRulesDao().getRuleByKey("repo", "key2");
+ assertThat(importedRule.getParent(), is(rule));
+ assertThat(importedRule.isEnabled(), is(true));
+ assertThat(importedRule.getName(), is("name2"));
+ assertThat(importedRule.getDescription(), is("description2"));
+ assertThat(importedRule.getSeverity(), is(RulePriority.INFO));
+ assertThat(importedRule.getParams().size(), is(1));
+ RuleParam param = importedRule.getParams().get(0);
+ assertThat(param.getKey(), is("param"));
+ assertThat(param.getDefaultValue(), is("value"));
+ }
+ @Test
+ public void shouldUpdateRules() {
getSession().save(rule);
+ getSession().save(createUserRule());
- sonarConfig.setRules(Arrays.asList(userRule));
+ sonarConfig.setRules(Arrays.asList(createUserRule()));
+ rulesBackup.importXml(sonarConfig);
+
+ assertThat(getSession().getResults(Rule.class).size(), is(2));
+ }
+
+ @Test
+ public void shouldNotFailIfNoParentRule() {
+ sonarConfig.setRules(Arrays.asList(createUserRule()));
rulesBackup.importXml(sonarConfig);
- List<Rule> rules = rulesDao.getRules();
- assertThat(rules.size(), is(2));
+ assertThat(getSession().getResults(Rule.class).size(), is(0));
}
}