3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.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.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ComponentTesting;
33 import org.sonar.db.property.PropertiesDao;
34 import org.sonar.db.property.PropertyDto;
35 import org.sonar.server.setting.ProjectSettingsFactory;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
41 public class SettingsRepositoryTest {
43 private static final Component ROOT = ReportComponent.builder(PROJECT, 1).setKey("ROOT").build();
46 public final DbTester dbTester = DbTester.create(System2.INSTANCE);
48 DbClient dbClient = dbTester.getDbClient();
52 Settings globalSettings;
54 SettingsRepository underTest;
57 public void createDao() {
58 globalSettings = new Settings();
59 PropertiesDao propertiesDao = new PropertiesDao(dbTester.myBatis());
60 session = dbClient.openSession(false);
61 underTest = new SettingsRepositoryImpl(new ProjectSettingsFactory(globalSettings, propertiesDao));
65 public void tearDown() {
70 public void get_project_settings_from_global_settings() {
71 globalSettings.setProperty("key", "value");
73 Settings settings = underTest.getSettings(ROOT);
75 assertThat(settings.getString("key")).isEqualTo("value");
79 public void get_project_settings_from_db() {
80 ComponentDto project = ComponentTesting.newProjectDto().setKey(ROOT.getKey());
81 dbClient.componentDao().insert(session, project);
82 dbClient.propertiesDao().insertProperty(session, new PropertyDto().setResourceId(project.getId()).setKey("key").setValue("value"));
85 Settings settings = underTest.getSettings(ROOT);
87 assertThat(settings.getString("key")).isEqualTo("value");
91 public void call_twice_get_project_settings() {
92 globalSettings.setProperty("key", "value");
94 Settings settings = underTest.getSettings(ROOT);
95 assertThat(settings.getString("key")).isEqualTo("value");
97 settings = underTest.getSettings(ROOT);
98 assertThat(settings.getString("key")).isEqualTo("value");