*/
package org.sonar.server.setting;
-import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.property.PropertyDto;
}
@Override
- public void loadAll(ImmutableMap.Builder<String, String> appendTo) {
+ public Map<String, String> loadAll() {
try (DbSession dbSession = dbClient.openSession(false)) {
- dbClient.propertiesDao().selectGlobalProperties(dbSession)
- .forEach(p -> appendTo.put(p.getKey(), defaultString(p.getValue())));
+ return dbClient.propertiesDao().selectGlobalProperties(dbSession)
+ .stream()
+ .collect(MoreCollectors.uniqueIndex(PropertyDto::getKey, p -> defaultString(p.getValue())));
}
}
*/
package org.sonar.server.setting;
-import com.google.common.collect.ImmutableMap;
+import java.util.Collections;
+import java.util.Map;
public class NopSettingLoader implements SettingLoader {
@Override
}
@Override
- public void loadAll(ImmutableMap.Builder<String, String> appendTo) {
- // nothing to load
+ public Map<String, String> loadAll() {
+ return Collections.emptyMap();
}
}
*/
package org.sonar.server.setting;
-import com.google.common.collect.ImmutableMap;
+import java.util.Map;
import javax.annotation.CheckForNull;
public interface SettingLoader {
@CheckForNull
String load(String key);
- void loadAll(ImmutableMap.Builder<String, String> appendTo);
+ Map<String,String> loadAll();
}
package org.sonar.server.setting;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.sonar.api.server.ServerSide;
import static com.google.common.base.Preconditions.checkState;
+import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
/**
- * Merge of {@link SystemSettings} and the global properties stored in the db table "properties". These
- * settings do not contain the settings specific to a project.
+ * Merge of system settings (including conf/sonar.properties) and the global properties stored
+ * in the db table "properties". These settings do not contain the settings specific to a project.
*
* <p>
* System settings have precedence on others.
@Override
public Map<String, String> getProperties() {
- ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
- loadAll(builder);
- systemProps.forEach((key, value) -> builder.put((String) key, (String) value));
- return builder.build();
+ Map<String, String> result = new HashMap<>();
+ loadAll(result);
+ systemProps.forEach((key, value) -> result.put((String) key, (String) value));
+ return unmodifiableMap(result);
}
- private void loadAll(ImmutableMap.Builder<String, String> builder) {
+ private void loadAll(Map<String, String> appendTo) {
try {
- ImmutableMap.Builder<String, String> cacheBuilder = ImmutableMap.builder();
- settingLoader.loadAll(cacheBuilder);
- Map<String, String> cache = cacheBuilder.build();
- builder.putAll(cache);
+ Map<String, String> cache = settingLoader.loadAll();
+ appendTo.putAll(cache);
getPropertiesDbFailureCache = cache;
} catch (PersistenceException e) {
- builder.putAll(getPropertiesDbFailureCache);
+ appendTo.putAll(getPropertiesDbFailureCache);
}
}
}
*/
package org.sonar.server.setting;
-import com.google.common.collect.ImmutableMap;
+import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
@Test
public void null_value_in_db_is_considered_as_empty_string() {
insertPropertyIntoDb(A_KEY, null);
+
assertThat(underTest.load(A_KEY)).isEqualTo("");
}
@Test
public void test_loadAll_with_no_properties() {
- ImmutableMap.Builder<String, String> map = ImmutableMap.builder();
- underTest.loadAll(map);
- assertThat(map.build().isEmpty()).isTrue();
+ Map<String, String> map = underTest.loadAll();
+ assertThat(map).isEmpty();
}
- @Test
- public void test_loadAll() {
- insertPropertyIntoDb("foo", "1");
- insertPropertyIntoDb("bar", "2");
- ImmutableMap.Builder<String, String> map = ImmutableMap.builder();
- underTest.loadAll(map);
- assertThat(map.build()).containsOnly(entry("foo", "1"), entry("bar", "2"));
- }
+ @Test
+ public void test_loadAll() {
+ insertPropertyIntoDb("foo", "1");
+ insertPropertyIntoDb("bar", "2");
+
+ Map<String, String> map = underTest.loadAll();
+
+ assertThat(map).containsOnly(entry("foo", "1"), entry("bar", "2"));
+ }
private void insertPropertyIntoDb(String key, String value) {
dbTester.getDbClient().propertiesDao().saveProperty(new PropertyDto().setKey(key).setValue(value));
*/
package org.sonar.server.setting;
-import com.google.common.collect.ImmutableMap;
import org.junit.Test;
-import org.sonar.server.setting.NopSettingLoader;
import static org.assertj.core.api.Assertions.assertThat;
public void do_nothing() {
assertThat(underTest.load("foo")).isNull();
- ImmutableMap.Builder<String,String> map = ImmutableMap.builder();
- underTest.loadAll(map);
- assertThat(map.build()).isEmpty();
+ assertThat(underTest.loadAll()).isEmpty();
}
}
import org.junit.rules.TemporaryFolder;
import org.sonar.api.config.PropertyDefinitions;
+import static java.util.Collections.unmodifiableMap;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
-import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
assertThat(underTest.get("foo")).isNotPresent();
}
+ /**
+ * SONAR-8216 System info page fails when a setting is defined both in sonar.properties and in DB
+ */
+ @Test
+ public void getProperties_does_not_fail_on_duplicated_key() {
+ insertPropertyIntoDb("foo", "from_db");
+ underTest = create(ImmutableMap.of("foo", "from_system"));
+
+ assertThat(underTest.get("foo")).hasValue("from_system");
+ assertThat(underTest.getProperties().get("foo")).isEqualTo("from_system");
+ }
+
@Test
public void load_encryption_secret_key_from_system_properties() throws Exception {
File secretKey = temp.newFile();
public void getProperties_return_empty_if_DB_error_on_first_call_ever_out_of_thread_cache() {
SettingLoader settingLoaderMock = mock(SettingLoader.class);
PersistenceException toBeThrown = new PersistenceException("Faking an error connecting to DB");
- doThrow(toBeThrown).when(settingLoaderMock).loadAll(any(ImmutableMap.Builder.class));
+ doThrow(toBeThrown).when(settingLoaderMock).loadAll();
underTest = new ThreadLocalSettings(new PropertyDefinitions(), new Properties(), settingLoaderMock);
assertThat(underTest.getProperties())
public void getProperties_returns_empty_if_DB_error_on_first_call_ever_in_thread_cache() {
SettingLoader settingLoaderMock = mock(SettingLoader.class);
PersistenceException toBeThrown = new PersistenceException("Faking an error connecting to DB");
- doThrow(toBeThrown).when(settingLoaderMock).loadAll(any(ImmutableMap.Builder.class));
+ doThrow(toBeThrown).when(settingLoaderMock).loadAll();
underTest = new ThreadLocalSettings(new PropertyDefinitions(), new Properties(), settingLoaderMock);
underTest.load();
String value2 = randomAlphanumeric(5);
SettingLoader settingLoaderMock = mock(SettingLoader.class);
PersistenceException toBeThrown = new PersistenceException("Faking an error connecting to DB");
- doAnswer(invocationOnMock -> {
- ImmutableMap.Builder<String, String> builder = (ImmutableMap.Builder<String, String>) invocationOnMock.getArguments()[0];
- builder.put(key, value1);
- return null;
- }).doThrow(toBeThrown)
- .doAnswer(invocationOnMock -> {
- ImmutableMap.Builder<String, String> builder = (ImmutableMap.Builder<String, String>) invocationOnMock.getArguments()[0];
- builder.put(key, value2);
- return null;
- })
+ doAnswer(invocationOnMock -> ImmutableMap.of(key, value1))
+ .doThrow(toBeThrown)
+ .doAnswer(invocationOnMock -> ImmutableMap.of(key, value2))
.when(settingLoaderMock)
- .loadAll(any(ImmutableMap.Builder.class));
+ .loadAll();
underTest = new ThreadLocalSettings(new PropertyDefinitions(), new Properties(), settingLoaderMock);
underTest.load();
}
@Override
- public void loadAll(ImmutableMap.Builder<String, String> appendTo) {
- appendTo.putAll(map);
+ public Map<String, String> loadAll() {
+ return unmodifiableMap(map);
}
}
}