]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7676 move property change listener notification afer commit
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 8 Sep 2016 09:26:17 +0000 (11:26 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Sep 2016 07:11:42 +0000 (09:11 +0200)
in PersistentSettings and when possible (ie. not when DbSession is provided by PersistentSettings' caller)

server/sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java
server/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java

index 006ca5b87916fb7706de3911104ffb2a9eab7d46..7dbbcbc3d2a1a14fa586708e76896b6d9d2983f7 100644 (file)
@@ -49,10 +49,7 @@ public class PersistentSettings {
    * are executed.
    */
   public PersistentSettings saveProperty(DbSession dbSession, String key, @Nullable String value) {
-    dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(key).setValue(value));
-    // refresh the cache of settings
-    delegate.setProperty(key, value);
-
+    savePropertyImpl(dbSession, key, value);
     changeNotifier.onGlobalPropertyChange(key, value);
     return this;
   }
@@ -63,12 +60,23 @@ public class PersistentSettings {
    */
   public PersistentSettings saveProperty(String key, @Nullable String value) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      saveProperty(dbSession, key, value);
+      savePropertyImpl(dbSession, key, value);
       dbSession.commit();
+      changeNotifier.onGlobalPropertyChange(key, value);
       return this;
     }
   }
 
+  private void savePropertyImpl(DbSession dbSession, String key, @Nullable String value) {
+    if (value == null) {
+      dbClient.propertiesDao().deleteGlobalProperty(key, dbSession);
+    } else {
+      dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(key).setValue(value));
+    }
+    // refresh the cache of settings
+    delegate.setProperty(key, value);
+  }
+
   public Settings getSettings() {
     return delegate;
   }
index 044a1cfeaeda8634ae6269321c236faf56cde3bc..0239f6d8cbf225a5c43514fc11b0af50e84fabd7 100644 (file)
@@ -25,7 +25,6 @@ import org.sonar.api.config.MapSettings;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
-import org.sonar.db.property.PropertyDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
@@ -55,8 +54,7 @@ public class PersistentSettingsTest {
     underTest.saveProperty("foo", null);
 
     assertThat(underTest.getString("foo")).isNull();
-    PropertyDto dto = dbTester.getDbClient().propertiesDao().selectGlobalProperty("foo");
-    assertThat(dto.getValue()).isEmpty();
+    assertThat(dbTester.getDbClient().propertiesDao().selectGlobalProperty("foo")).isNull();
     verify(changeNotifier).onGlobalPropertyChange("foo", null);
   }