]> source.dussan.org Git - sonarqube.git/blob
de328c1d514540fff02f2f73a46c1fe806a0c7af
[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.After;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.config.Settings;
27 import org.sonar.api.config.internal.MapSettings;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.settings.ProjectSettingsFactory;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.component.ComponentTesting;
35 import org.sonar.db.property.PropertyDto;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
39
40
41 public class SettingsRepositoryTest {
42
43   private static final Component ROOT = ReportComponent.builder(PROJECT, 1).setKey("ROOT").build();
44
45   @Rule
46   public final DbTester dbTester = DbTester.create(System2.INSTANCE);
47
48   DbClient dbClient = dbTester.getDbClient();
49
50   DbSession session;
51
52   MapSettings globalSettings;
53
54   SettingsRepository underTest;
55
56   @Before
57   public void createDao() {
58     globalSettings = new MapSettings();
59     session = dbClient.openSession(false);
60     underTest = new SettingsRepositoryImpl(new ProjectSettingsFactory(globalSettings, dbClient));
61   }
62
63   @After
64   public void tearDown() {
65     session.close();
66   }
67
68   @Test
69   public void get_project_settings_from_global_settings() {
70     globalSettings.setProperty("key", "value");
71
72     Settings settings = underTest.getSettings(ROOT);
73
74     assertThat(settings.getString("key")).isEqualTo("value");
75   }
76
77   @Test
78   public void get_project_settings_from_db() {
79     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()).setKey(ROOT.getKey());
80     dbClient.componentDao().insert(session, project);
81     dbClient.propertiesDao().saveProperty(session, new PropertyDto().setResourceId(project.getId()).setKey("key").setValue("value"));
82     session.commit();
83
84     Settings settings = underTest.getSettings(ROOT);
85
86     assertThat(settings.getString("key")).isEqualTo("value");
87   }
88
89   @Test
90   public void call_twice_get_project_settings() {
91     globalSettings.setProperty("key", "value");
92
93     Settings settings = underTest.getSettings(ROOT);
94     assertThat(settings.getString("key")).isEqualTo("value");
95
96     settings = underTest.getSettings(ROOT);
97     assertThat(settings.getString("key")).isEqualTo("value");
98   }
99 }