private Configuration deprecatedConfiguration;
private File deployDir;
+ private File sonarHome;
private GlobalPropertyChangeHandler[] changeHandlers;
public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext, GlobalPropertyChangeHandler[] changeHandlers) {
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());
--- /dev/null
+/*
+ * 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);
+ }
+
+}