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.MapSettings;
27 import org.sonar.api.config.Settings;
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;
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 MapSettings globalSettings;
54 SettingsRepository underTest;
57 public void createDao() {
58 globalSettings = new MapSettings();
59 session = dbClient.openSession(false);
60 underTest = new SettingsRepositoryImpl(new ProjectSettingsFactory(globalSettings, dbClient));
64 public void tearDown() {
69 public void get_project_settings_from_global_settings() {
70 globalSettings.setProperty("key", "value");
72 Settings settings = underTest.getSettings(ROOT);
74 assertThat(settings.getString("key")).isEqualTo("value");
78 public void get_project_settings_from_db() {
79 ComponentDto project = ComponentTesting.newProjectDto().setKey(ROOT.getKey());
80 dbClient.componentDao().insert(session, project);
81 dbClient.propertiesDao().insertProperty(session, new PropertyDto().setResourceId(project.getId()).setKey("key").setValue("value"));
84 Settings settings = underTest.getSettings(ROOT);
86 assertThat(settings.getString("key")).isEqualTo("value");
90 public void call_twice_get_project_settings() {
91 globalSettings.setProperty("key", "value");
93 Settings settings = underTest.getSettings(ROOT);
94 assertThat(settings.getString("key")).isEqualTo("value");
96 settings = underTest.getSettings(ROOT);
97 assertThat(settings.getString("key")).isEqualTo("value");