3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.component;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.config.Configuration;
25 import org.sonar.api.config.internal.MapSettings;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.queue.CeTask;
28 import org.sonar.ce.settings.ProjectConfigurationFactory;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.ce.CeTaskTypes;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.property.PropertyDto;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
38 public class ConfigurationRepositoryTest {
40 private static final Component ROOT = ReportComponent.builder(PROJECT, 1).setKey("ROOT").build();
41 private static final CeTask TASK = new CeTask.Builder()
42 .setOrganizationUuid("foo")
44 .setType(CeTaskTypes.REPORT)
45 .setComponentUuid(ROOT.getUuid())
46 .setComponentKey(ROOT.getKey())
50 public final DbTester db = DbTester.create(System2.INSTANCE);
52 private DbClient dbClient = db.getDbClient();
53 private MapSettings globalSettings = new MapSettings();
54 private ConfigurationRepository underTest = new ConfigurationRepositoryImpl(TASK, new ProjectConfigurationFactory(globalSettings, dbClient));
57 public void get_project_settings_from_global_settings() {
58 globalSettings.setProperty("key", "value");
60 Configuration config = underTest.getConfiguration();
62 assertThat(config.get("key")).hasValue("value");
66 public void get_project_settings_from_db() {
67 ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey(ROOT.getKey()));
68 insertProjectProperty(project, "key", "value");
70 Configuration config = underTest.getConfiguration();
72 assertThat(config.get("key")).hasValue("value");
76 public void call_twice_get_project_settings() {
77 globalSettings.setProperty("key", "value");
79 Configuration config = underTest.getConfiguration();
80 assertThat(config.get("key")).hasValue("value");
82 config = underTest.getConfiguration();
83 assertThat(config.get("key")).hasValue("value");
87 public void project_settings_override_global_settings() {
88 globalSettings.setProperty("key", "value1");
89 ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey(ROOT.getKey()));
90 insertProjectProperty(project, "key", "value2");
92 Configuration config = underTest.getConfiguration();
93 assertThat(config.get("key")).hasValue("value2");
97 public void project_settings_are_cached_to_avoid_db_access() {
98 ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey(ROOT.getKey()));
99 insertProjectProperty(project, "key", "value");
101 Configuration config = underTest.getConfiguration();
102 assertThat(config.get("key")).hasValue("value");
104 db.executeUpdateSql("delete from properties");
107 assertThat(config.get("key")).hasValue("value");
110 private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
111 db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));