]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaw
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 13 Jul 2012 21:41:09 +0000 (23:41 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 13 Jul 2012 21:41:09 +0000 (23:41 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/platform/NewUserHandler.java
sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java
sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
sonar-server/src/test/resources/org/sonar/server/platform/PersistentSettingsTest/conf/sonar.properties [new file with mode: 0644]

index 833c68d7deaf0f808bd9ac9640a06cc684ef5f2a..3e65e571a988045848fb8496e2103bb6556c407b 100644 (file)
@@ -58,7 +58,7 @@ public interface NewUserHandler extends ServerExtension {
       return new Builder();
     }
 
-    public static final class Builder {
+    public static class Builder {
       private String login;
       private String name;
       private String email;
index 0b922d9bb2574d0b6462d7a25a6b429622018afb..525c3be4ddf787d7ffd561df7b7775c1491f9ddf 100644 (file)
@@ -45,7 +45,7 @@ public class PersistentSettings implements ServerComponent {
     for (PropertyDto property : propertiesDao.selectGlobalProperties()) {
       databaseProperties.put(property.getKey(), property.getValue());
     }
-    settings.activateDatabaseSettings(SonarHome.getHome(), databaseProperties);
+    settings.activateDatabaseSettings(databaseProperties);
   }
 
   public PersistentSettings saveProperty(String key, @Nullable String value) {
@@ -54,7 +54,7 @@ public class PersistentSettings implements ServerComponent {
     return this;
   }
 
-  public PersistentSettings removeProperty(String key) {
+  public PersistentSettings deleteProperty(String key) {
     settings.removeProperty(key);
     propertiesDao.deleteGlobalProperty(key);
     return this;
index 07f0f4e65b433f62fa39e8a7959591417175b1cd..60d2d053263c95c52710463a8b3c4c762f14425d 100644 (file)
@@ -52,6 +52,7 @@ public class ServerSettings extends Settings implements ServerComponent {
 
   private Configuration deprecatedConfiguration;
   private File deployDir;
+  private File sonarHome;
   private GlobalPropertyChangeHandler[] changeHandlers;
 
   public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext, GlobalPropertyChangeHandler[] changeHandlers) {
@@ -67,15 +68,16 @@ public class ServerSettings extends Settings implements ServerComponent {
     super(definitions);
     this.deprecatedConfiguration = deprecatedConfiguration;
     this.deployDir = deployDir;
+    this.sonarHome = sonarHome;
     this.changeHandlers = changeHandlers;
-    load(sonarHome, Collections.<String, String>emptyMap());
+    load(Collections.<String, String>emptyMap());
   }
 
-  public ServerSettings activateDatabaseSettings(File sonarHome, Map<String, String> databaseProperties) {
-    return load(sonarHome, databaseProperties);
+  public ServerSettings activateDatabaseSettings(Map<String, String> databaseProperties) {
+    return load(databaseProperties);
   }
 
-  private ServerSettings load(File sonarHome, Map<String, String> databaseSettings) {
+  private ServerSettings load(Map<String, String> databaseSettings) {
     properties.clear();
     properties.put(CoreProperties.SONAR_HOME, sonarHome.getAbsolutePath());
     properties.put(DEPLOY_DIR, deployDir.getAbsolutePath());
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java
new file mode 100644 (file)
index 0000000..d858e48
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * 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.platform;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.config.GlobalPropertyChangeHandler;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.*;
+
+public class PersistentSettingsTest {
+
+  private PropertiesDao dao;
+  private ServerSettings settings;
+
+  @Before
+  public void init() throws URISyntaxException {
+    dao = mock(PropertiesDao.class);
+    settings = new ServerSettings(
+      new PropertyDefinitions(),
+      new PropertiesConfiguration(),
+      new File("."),
+      new File(PersistentSettingsTest.class.getResource("/org/sonar/server/platform/PersistentSettingsTest/").toURI()),
+      new GlobalPropertyChangeHandler[0]);
+  }
+
+  @Test
+  public void load_database_properties_at_startup() {
+    when(dao.selectGlobalProperties()).thenReturn(Arrays.asList(
+      new PropertyDto().setKey("in_db").setValue("bar")
+    ));
+
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+    persistentSettings.start();
+
+    assertThat(settings.getString("in_db")).isEqualTo("bar");
+  }
+
+  @Test
+  public void saveProperty() {
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+    persistentSettings.saveProperty("foo", "bar");
+
+    // kept in memory cache and persisted in db
+    assertThat(settings.getString("foo")).isEqualTo("bar");
+    verify(dao).setProperty(argThat(new BaseMatcher<PropertyDto>() {
+      public boolean matches(Object o) {
+        PropertyDto dto = (PropertyDto) o;
+        return dto.getKey().equals("foo");
+      }
+
+      public void describeTo(Description description) {
+      }
+    }));
+  }
+
+  @Test
+  public void deleteProperty() {
+    settings.setProperty("foo", "bar");
+    assertThat(settings.hasKey("foo")).isTrue();
+
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+    persistentSettings.deleteProperty("foo");
+
+    assertThat(settings.hasKey("foo")).isFalse();
+    verify(dao).deleteGlobalProperty("foo");
+  }
+
+  @Test
+  public void deleteProperties() {
+    settings.setProperty("foo", "bar");
+    assertThat(settings.hasKey("foo")).isTrue();
+
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+    persistentSettings.deleteProperties();
+
+    assertThat(settings.getProperties()).isEmpty();
+    verify(dao).deleteGlobalProperties();
+  }
+
+  @Test
+  public void shortcuts_on_settings() {
+    settings.setProperty("foo", "bar");
+    assertThat(settings.hasKey("foo")).isTrue();
+
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+
+    assertThat(persistentSettings.getProperties()).isEqualTo(settings.getProperties());
+    assertThat(persistentSettings.getString("foo")).isEqualTo("bar");
+    assertThat(persistentSettings.getSettings()).isEqualTo(settings);
+  }
+
+  @Test
+  public void saveProperties() {
+    PersistentSettings persistentSettings = new PersistentSettings(dao, settings);
+    ImmutableMap<String, String> props = ImmutableMap.of("foo", "bar");
+    persistentSettings.saveProperties(props);
+
+    assertThat(settings.getString("foo")).isEqualTo("bar");
+    verify(dao).saveGlobalProperties(props);
+  }
+
+}
index 2ffff06b75dcf20095e91207cf7e3812632469a7..a1ff85a216f57a4ea165b630e1584a298da0e934 100644 (file)
@@ -61,7 +61,7 @@ public class ServerSettingsTest {
     ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home, new GlobalPropertyChangeHandler[0]);
 
     Map<String, String> databaseProperties = ImmutableMap.of("in_db", "true");
-    settings.activateDatabaseSettings(home, databaseProperties);
+    settings.activateDatabaseSettings(databaseProperties);
 
     assertThat(settings.getString("in_db")).isEqualTo("true");
   }
@@ -72,7 +72,7 @@ public class ServerSettingsTest {
     assertThat(settings.getString("in_file")).isEqualTo("true");
 
     Map<String, String> databaseProperties = ImmutableMap.of("in_file", "false");
-    settings.activateDatabaseSettings(home, databaseProperties);
+    settings.activateDatabaseSettings(databaseProperties);
 
     assertThat(settings.getString("in_file")).isEqualTo("true");
   }
diff --git a/sonar-server/src/test/resources/org/sonar/server/platform/PersistentSettingsTest/conf/sonar.properties b/sonar-server/src/test/resources/org/sonar/server/platform/PersistentSettingsTest/conf/sonar.properties
new file mode 100644 (file)
index 0000000..b4a8077
--- /dev/null
@@ -0,0 +1 @@
+in_file: true
\ No newline at end of file