From aff5bdd7ca4bdd5f435f00ec3469340beb14f086 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 23 Nov 2012 15:52:00 +0100 Subject: [PATCH] SONAR-3930 Backup of settings then restore to a new Sonar instance can cause deployment failures --- .../server/configuration/PropertiesBackup.java | 7 ++++--- .../server/platform/PersistentSettings.java | 8 +++++++- .../configuration/PropertiesBackupTest.java | 16 +++++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/PropertiesBackup.java b/sonar-server/src/main/java/org/sonar/server/configuration/PropertiesBackup.java index 359db78cbaf..4aab263043a 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/PropertiesBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/PropertiesBackup.java @@ -26,6 +26,7 @@ import org.apache.commons.collections.CollectionUtils; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.database.configuration.Property; +import org.sonar.core.properties.PropertyDto; import org.sonar.server.platform.PersistentSettings; import java.util.List; @@ -42,10 +43,10 @@ public class PropertiesBackup implements Backupable { public void exportXml(SonarConfig sonarConfig) { List xmlProperties = Lists.newArrayList(); - for (Map.Entry entry : persistentSettings.getProperties().entrySet()) { + for (PropertyDto property : persistentSettings.getGlobalProperties()){ // "sonar.core.id" must never be restored, it is unique for a server and it created once at the 1rst server startup - if (!CoreProperties.SERVER_ID.equals(entry.getKey())) { - xmlProperties.add(new Property(entry.getKey(), entry.getValue())); + if (!CoreProperties.SERVER_ID.equals(property.getKey())) { + xmlProperties.add(new Property(property.getKey(), property.getValue())); } } sonarConfig.setProperties(xmlProperties); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java b/sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java index 525c3be4ddf..bbcc32a5081 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java @@ -26,6 +26,8 @@ import org.sonar.core.properties.PropertiesDao; import org.sonar.core.properties.PropertyDto; import javax.annotation.Nullable; + +import java.util.List; import java.util.Map; /** @@ -42,7 +44,7 @@ public class PersistentSettings implements ServerComponent { public void start() { Map databaseProperties = Maps.newHashMap(); - for (PropertyDto property : propertiesDao.selectGlobalProperties()) { + for (PropertyDto property : getGlobalProperties()) { databaseProperties.put(property.getKey(), property.getValue()); } settings.activateDatabaseSettings(databaseProperties); @@ -83,4 +85,8 @@ public class PersistentSettings implements ServerComponent { public Settings getSettings() { return settings; } + + public List getGlobalProperties(){ + return propertiesDao.selectGlobalProperties(); + } } diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java index 4880caf32d3..1fd2a2c1be6 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java @@ -20,11 +20,13 @@ package org.sonar.server.configuration; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; import org.hamcrest.collection.IsMapContaining; import org.junit.Before; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.configuration.Property; +import org.sonar.core.properties.PropertyDto; import org.sonar.jpa.test.AbstractDbUnitTestCase; import org.sonar.server.platform.PersistentSettings; @@ -49,8 +51,8 @@ public class PropertiesBackupTest extends AbstractDbUnitTestCase { } @Test - public void export_properties() { - when(persistentSettings.getProperties()).thenReturn(ImmutableMap.of("key1", "value1", "key2", "value2")); + public void shouldExportProperties() { + when(persistentSettings.getGlobalProperties()).thenReturn(Lists.newArrayList(new PropertyDto().setKey("key1").setValue("value1"), new PropertyDto().setKey("key2").setValue("value2"))); SonarConfig config = new SonarConfig(); backup.exportXml(config); @@ -59,17 +61,17 @@ public class PropertiesBackupTest extends AbstractDbUnitTestCase { } @Test - public void do_not_export_server_id() { - when(persistentSettings.getProperties()).thenReturn(ImmutableMap.of(CoreProperties.SERVER_ID, "111")); + public void shouldNotExportServerId() { + when(persistentSettings.getGlobalProperties()).thenReturn(Lists.newArrayList(new PropertyDto().setKey(CoreProperties.SERVER_ID).setValue("111"), new PropertyDto().setKey("key").setValue("value"))); SonarConfig config = new SonarConfig(); backup.exportXml(config); - assertThat(config.getProperties()).isEmpty(); + assertThat(config.getProperties()).containsOnly(new Property("key", "value")); } @Test - public void import_backup_of_properties() { + public void shouldImportBackupOfProperties() { SonarConfig config = new SonarConfig(); config.setProperties(Arrays.asList(new Property("key1", "value1"))); @@ -79,7 +81,7 @@ public class PropertiesBackupTest extends AbstractDbUnitTestCase { } @Test - public void do_not_import_server_id() { + public void shouldNotImportServerId() { // initial server id when(persistentSettings.getString(CoreProperties.SERVER_ID)).thenReturn("111"); -- 2.39.5