From: Simon Brandhof Date: Fri, 13 Jul 2012 21:41:09 +0000 (+0200) Subject: Fix some quality flaw X-Git-Tag: 3.2~86 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a5c7d63779283abdc46cfd65e19ee1a175568b8b;p=sonarqube.git Fix some quality flaw --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/NewUserHandler.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/NewUserHandler.java index 833c68d7dea..3e65e571a98 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/NewUserHandler.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/NewUserHandler.java @@ -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; 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.emptyMap()); + load(Collections.emptyMap()); } - public ServerSettings activateDatabaseSettings(File sonarHome, Map databaseProperties) { - return load(sonarHome, databaseProperties); + public ServerSettings activateDatabaseSettings(Map databaseProperties) { + return load(databaseProperties); } - private ServerSettings load(File sonarHome, Map databaseSettings) { + private ServerSettings load(Map 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() { + 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 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 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 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