+/*
+ * 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;
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);
@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));
}
}