diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-09 16:45:34 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-06-09 16:45:40 +0200 |
commit | 5aba56ac9e17d9fc9f34715db31dcab404261da5 (patch) | |
tree | cf883d6cf553ead36f936a2b2b172603596f831b | |
parent | 3127e7a9ab7ccec1690e3ee637aeb78bf56db934 (diff) | |
download | sonarqube-5aba56ac9e17d9fc9f34715db31dcab404261da5.tar.gz sonarqube-5aba56ac9e17d9fc9f34715db31dcab404261da5.zip |
SONAR-5007 fix restore of empty backup
3 files changed, 43 insertions, 5 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java index d90724aa032..0f5a972797b 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java @@ -105,6 +105,9 @@ public class QProfileBackuper implements ServerComponent { void restore(Reader reader, @Nullable QualityProfileKey profileKey) { try { String profileLang = null, profileName = null; + List<RuleActivation> ruleActivations = Lists.newArrayList(); + QualityProfileKey targetKey; + SMInputFactory inputFactory = initStax(); SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader); rootC.advance(); // <profile> @@ -121,18 +124,20 @@ public class QProfileBackuper implements ServerComponent { profileLang = StringUtils.trim(cursor.collectDescendantText(false)); } else if (StringUtils.equals("rules", nodeName)) { - QualityProfileKey targetKey = (QualityProfileKey) ObjectUtils.defaultIfNull( - profileKey, QualityProfileKey.of(profileName, profileLang)); + targetKey = (QualityProfileKey) ObjectUtils.defaultIfNull(profileKey, QualityProfileKey.of(profileName, profileLang)); SMInputCursor rulesCursor = cursor.childElementCursor("rule"); - doRestore(rulesCursor, targetKey); + ruleActivations = parseRuleActivations(rulesCursor, targetKey); } } + + targetKey = (QualityProfileKey) ObjectUtils.defaultIfNull(profileKey, QualityProfileKey.of(profileName, profileLang)); + reset.reset(targetKey, ruleActivations); } catch (XMLStreamException e) { throw new IllegalStateException("Fail to restore Quality profile backup", e); } } - private void doRestore(SMInputCursor rulesCursor, QualityProfileKey profileKey) throws XMLStreamException { + private List<RuleActivation> parseRuleActivations(SMInputCursor rulesCursor, QualityProfileKey profileKey) throws XMLStreamException { List<RuleActivation> activations = Lists.newArrayList(); while (rulesCursor.getNext() != null) { SMInputCursor ruleCursor = rulesCursor.childElementCursor(); @@ -160,7 +165,8 @@ public class QProfileBackuper implements ServerComponent { activation.setParameters(parameters); activations.add(activation); } - reset.reset(profileKey, activations); + return activations; + //reset.reset(profileKey, activations); } private void readParameters(SMInputCursor propsCursor, Map<String, String> parameters) throws XMLStreamException { diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java index afb863076ae..e7d574ef418 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java @@ -271,4 +271,31 @@ public class QProfileBackuperMediumTest { assertThat(e).hasMessage("Backup XML is not valid. Root element must be <profile>."); } } + + @Test + public void restore_and_override_profile_name() throws Exception { + QualityProfileKey targetKey = QualityProfileKey.of("newName", "xoo"); + tester.get(QProfileBackuper.class).restore(new StringReader( + Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), Charsets.UTF_8)), + targetKey); + + List<ActiveRule> activeRules = tester.get(QProfileService.class).findActiveRulesByProfile(XOO_PROFILE_KEY); + assertThat(activeRules).hasSize(0); + + activeRules = tester.get(QProfileService.class).findActiveRulesByProfile(targetKey); + assertThat(activeRules).hasSize(1); + } + + @Test + public void restore_profile_with_zero_rules() throws Exception { + tester.get(QProfileBackuper.class).restore(new StringReader( + Resources.toString(getClass().getResource("QProfileBackuperMediumTest/empty.xml"), Charsets.UTF_8)), + null); + + dbSession.clearCache(); + assertThat(db.activeRuleDao().findAll(dbSession)).hasSize(0); + List<QualityProfileDto> profiles = db.qualityProfileDao().findAll(dbSession); + assertThat(profiles).hasSize(1); + assertThat(profiles.get(0).getName()).isEqualTo("P1"); + } } diff --git a/sonar-server/src/test/resources/org/sonar/server/qualityprofile/QProfileBackuperMediumTest/empty.xml b/sonar-server/src/test/resources/org/sonar/server/qualityprofile/QProfileBackuperMediumTest/empty.xml new file mode 100644 index 00000000000..937009dce6c --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/qualityprofile/QProfileBackuperMediumTest/empty.xml @@ -0,0 +1,5 @@ +<?xml version='1.0' encoding='UTF-8'?> +<profile> + <name>P1</name> + <language>xoo</language> +</profile> |