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;
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(),
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;
sonarConfig = new SonarConfig();
rule = Rule.create("repo", "key", "name").setDescription("description");
+ rule.createParameter("param").setDefaultValue("value");
}
private Rule createUserRule() {
}
@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());
+ }
}