aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-07-13 23:41:09 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-07-13 23:41:09 +0200
commita5c7d63779283abdc46cfd65e19ee1a175568b8b (patch)
treec04201e00d72e55ec5ef61243abff21c2ff8d326 /sonar-server
parent6cc4d52f9791ad2547111543e41662c7522f89b8 (diff)
downloadsonarqube-a5c7d63779283abdc46cfd65e19ee1a175568b8b.tar.gz
sonarqube-a5c7d63779283abdc46cfd65e19ee1a175568b8b.zip
Fix some quality flaw
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/PersistentSettings.java4
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java10
-rw-r--r--sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java133
-rw-r--r--sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java4
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/platform/PersistentSettingsTest/conf/sonar.properties1
5 files changed, 144 insertions, 8 deletions
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 0b922d9bb25..525c3be4ddf 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
@@ -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;
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
index 07f0f4e65b4..60d2d053263 100644
--- a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
+++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
@@ -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
index 00000000000..d858e48093f
--- /dev/null
+++ b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java
@@ -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);
+ }
+
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
index 2ffff06b75d..a1ff85a216f 100644
--- a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
@@ -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
index 00000000000..b4a8077471c
--- /dev/null
+++ b/sonar-server/src/test/resources/org/sonar/server/platform/PersistentSettingsTest/conf/sonar.properties
@@ -0,0 +1 @@
+in_file: true \ No newline at end of file