]> source.dussan.org Git - sonarqube.git/blob
139407dd6f530b39f978c3999b2643aba35f72d9
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.component;
21
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;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class ConfigurationRepositoryTest {
41
42   private static Project PROJECT = new Project("UUID", "KEY", "NAME");
43
44   @Rule
45   public final DbTester db = DbTester.create(System2.INSTANCE);
46
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));
51
52   @Test
53   public void get_project_settings_from_global_settings() {
54     analysisMetadataHolder.setProject(PROJECT).setBranch(null);
55     globalSettings.setProperty("key", "value");
56
57     Configuration config = underTest.getConfiguration();
58
59     assertThat(config.get("key")).hasValue("value");
60   }
61
62   @Test
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");
67
68     Configuration config = underTest.getConfiguration();
69
70     assertThat(config.get("key")).hasValue("value");
71   }
72
73   @Test
74   public void call_twice_get_project_settings() {
75     analysisMetadataHolder.setProject(PROJECT).setBranch(null);
76     globalSettings.setProperty("key", "value");
77
78     Configuration config = underTest.getConfiguration();
79     assertThat(config.get("key")).hasValue("value");
80
81     config = underTest.getConfiguration();
82     assertThat(config.get("key")).hasValue("value");
83   }
84
85   @Test
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);
91
92     Configuration config = underTest.getConfiguration();
93     assertThat(config.get("key")).hasValue("value2");
94   }
95
96   @Test
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);
101
102     Configuration config = underTest.getConfiguration();
103     assertThat(config.get("key")).hasValue("value");
104
105     db.executeUpdateSql("delete from properties");
106     db.commit();
107
108     assertThat(config.get("key")).hasValue("value");
109   }
110
111   @Test
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");
121
122     Configuration config = underTest.getConfiguration();
123
124     assertThat(config.get("global")).hasValue("global value");
125     assertThat(config.get("project")).hasValue("project value");
126     assertThat(config.get("branch")).hasValue("branch value");
127   }
128
129   private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
130     db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));
131   }
132 }