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 java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.settings.ProjectConfigurationFactory;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.property.PropertyDto;
33 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
35 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
41 public class ConfigurationRepositoryTest {
43 private static Project PROJECT = new Project("UUID", "KEY", "NAME");
46 public final DbTester db = DbTester.create(System2.INSTANCE);
48 private DbClient dbClient = db.getDbClient();
49 private MapSettings globalSettings = new MapSettings();
50 private AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
51 private ConfigurationRepository underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
54 public void get_project_settings_from_global_settings() {
55 analysisMetadataHolder.setProject(PROJECT).setBranch(null);
56 globalSettings.setProperty("key", "value");
58 Configuration config = underTest.getConfiguration();
60 assertThat(config.get("key")).hasValue("value");
64 public void get_project_settings_from_db() {
65 ComponentDto project = db.components().insertPrivateProject();
66 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
67 insertProjectProperty(project, "key", "value");
69 Configuration config = underTest.getConfiguration();
71 assertThat(config.get("key")).hasValue("value");
75 public void call_twice_get_project_settings() {
76 analysisMetadataHolder.setProject(PROJECT).setBranch(null);
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();
90 insertProjectProperty(project, "key", "value2");
91 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
93 Configuration config = underTest.getConfiguration();
94 assertThat(config.get("key")).hasValue("value2");
98 public void project_settings_are_cached_to_avoid_db_access() {
99 ComponentDto project = db.components().insertPrivateProject();
100 insertProjectProperty(project, "key", "value");
101 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
103 Configuration config = underTest.getConfiguration();
104 assertThat(config.get("key")).hasValue("value");
106 db.executeUpdateSql("delete from properties");
109 assertThat(config.get("key")).hasValue("value");
113 public void branch_settings() {
114 ComponentDto project = db.components().insertMainBranch();
115 ComponentDto branchDto = db.components().insertProjectBranch(project);
116 Branch branch = mock(Branch.class);
117 when(branch.getName()).thenReturn(Optional.of(branchDto.getBranch()));
118 analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(branch);
119 globalSettings.setProperty("global", "global value");
120 insertProjectProperty(project, "project", "project value");
121 insertProjectProperty(branchDto, "branch", "branch value");
123 Configuration config = underTest.getConfiguration();
125 assertThat(config.get("global")).hasValue("global value");
126 assertThat(config.get("project")).hasValue("project value");
127 assertThat(config.get("branch")).hasValue("branch value");
130 private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
131 db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));