3 * Copyright (C) 2009-2024 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.scanner.repository.settings;
22 import java.util.List;
23 import org.junit.Test;
24 import org.sonarqube.ws.Settings.FieldValues;
25 import org.sonarqube.ws.Settings.FieldValues.Value;
26 import org.sonarqube.ws.Settings.FieldValues.Value.Builder;
27 import org.sonarqube.ws.Settings.Setting;
28 import org.sonarqube.ws.Settings.Values;
30 import static java.util.Collections.singletonList;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.entry;
33 import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
35 public class AbstractSettingsLoaderTest {
38 public void should_load_global_multivalue_settings() {
39 assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
40 .setKey("sonar.preview.supportedPlugins")
41 .setValues(Values.newBuilder().addValues("java").addValues("php")).build())))
42 .containsExactly(entry("sonar.preview.supportedPlugins", "java,php"));
46 public void should_escape_global_multivalue_settings() {
47 assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
48 .setKey("sonar.preview.supportedPlugins")
49 .setValues(Values.newBuilder().addValues("ja,va").addValues("p\"hp")).build())))
50 .containsExactly(entry("sonar.preview.supportedPlugins", "\"ja,va\",\"p\"\"hp\""));
54 public void should_throw_exception_when_no_value_of_non_secured_settings() {
55 Setting setting = Setting.newBuilder().setKey("sonar.open.setting").build();
57 List<Setting> singletonList = singletonList(setting);
59 assertThatThrownBy(() -> AbstractSettingsLoader.toMap(singletonList))
60 .isInstanceOf(IllegalStateException.class)
61 .hasMessage("Unknown property value for sonar.open.setting");
65 public void should_filter_secured_settings_without_value() {
66 assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
67 .setKey("sonar.setting.secured").build())))
72 public void should_load_global_propertyset_settings() {
73 Builder valuesBuilder = Value.newBuilder();
74 valuesBuilder.putValue("filepattern", "**/*.xml");
75 valuesBuilder.putValue("rulepattern", "*:S12345");
76 Value value1 = valuesBuilder.build();
77 valuesBuilder.clear();
78 valuesBuilder.putValue("filepattern", "**/*.java");
79 valuesBuilder.putValue("rulepattern", "*:S456");
80 Value value2 = valuesBuilder.build();
82 assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
83 .setKey("sonar.issue.exclusions.multicriteria")
84 .setFieldValues(FieldValues.newBuilder().addFieldValues(value1).addFieldValues(value2)).build())))
85 .containsOnly(entry("sonar.issue.exclusions.multicriteria", "1,2"),
86 entry("sonar.issue.exclusions.multicriteria.1.filepattern", "**/*.xml"),
87 entry("sonar.issue.exclusions.multicriteria.1.rulepattern", "*:S12345"),
88 entry("sonar.issue.exclusions.multicriteria.2.filepattern", "**/*.java"),
89 entry("sonar.issue.exclusions.multicriteria.2.rulepattern", "*:S456"));