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.settings.ProjectConfigurationFactory;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.property.PropertyDto;
32 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
33 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
34 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
40 public class ConfigurationRepositoryTest {
42 private static Project PROJECT = new Project("UUID", "KEY", "NAME");
45 public final DbTester db = DbTester.create(System2.INSTANCE);
47 private DbClient dbClient = db.getDbClient();
48 private MapSettings globalSettings = new MapSettings();
49 private AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
50 private ConfigurationRepository underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
53 public void get_project_settings_from_global_settings() {
54 analysisMetadataHolder.setProject(PROJECT).setBranch(null);
55 globalSettings.setProperty("key", "value");
57 Configuration config = underTest.getConfiguration();
59 assertThat(config.get("key")).hasValue("value");
63 public void get_project_settings_from_db() {
64 ComponentDto project = db.components().insertPrivateProject();
65 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
66 insertProjectProperty(project, "key", "value");
68 Configuration config = underTest.getConfiguration();
70 assertThat(config.get("key")).hasValue("value");
74 public void call_twice_get_project_settings() {
75 analysisMetadataHolder.setProject(PROJECT).setBranch(null);
76 globalSettings.setProperty("key", "value");
78 Configuration config = underTest.getConfiguration();
79 assertThat(config.get("key")).hasValue("value");
81 config = underTest.getConfiguration();
82 assertThat(config.get("key")).hasValue("value");
86 public void project_settings_override_global_settings() {
87 globalSettings.setProperty("key", "value1");
88 ComponentDto project = db.components().insertPrivateProject();
89 insertProjectProperty(project, "key", "value2");
90 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
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();
99 insertProjectProperty(project, "key", "value");
100 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
102 Configuration config = underTest.getConfiguration();
103 assertThat(config.get("key")).hasValue("value");
105 db.executeUpdateSql("delete from properties");
108 assertThat(config.get("key")).hasValue("value");
112 public void branch_settings() {
113 ComponentDto project = db.components().insertMainBranch();
114 ComponentDto branchDto = db.components().insertProjectBranch(project);
115 Branch branch = mock(Branch.class);
116 when(branch.getName()).thenReturn(branchDto.getBranch());
117 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(branch);
118 globalSettings.setProperty("global", "global value");
119 insertProjectProperty(project, "project", "project value");
120 insertProjectProperty(branchDto, "branch", "branch value");
122 Configuration config = underTest.getConfiguration();
124 assertThat(config.get("global")).hasValue("global value");
125 assertThat(config.get("project")).hasValue("project value");
126 assertThat(config.get("branch")).hasValue("branch value");
129 private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
130 db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));