aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/settings/AbstractSettingsLoaderTest.java
blob: 4928d6b672006ec54bb65f81baff2ca2540169ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.repository.settings;

import java.util.List;
import org.junit.Test;
import org.sonarqube.ws.Settings.FieldValues;
import org.sonarqube.ws.Settings.FieldValues.Value;
import org.sonarqube.ws.Settings.FieldValues.Value.Builder;
import org.sonarqube.ws.Settings.Setting;
import org.sonarqube.ws.Settings.Values;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

public class AbstractSettingsLoaderTest {

  @Test
  public void should_load_global_multivalue_settings() {
    assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
      .setKey("sonar.preview.supportedPlugins")
      .setValues(Values.newBuilder().addValues("java").addValues("php")).build())))
        .containsExactly(entry("sonar.preview.supportedPlugins", "java,php"));
  }

  @Test
  public void should_escape_global_multivalue_settings() {
    assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
      .setKey("sonar.preview.supportedPlugins")
      .setValues(Values.newBuilder().addValues("ja,va").addValues("p\"hp")).build())))
        .containsExactly(entry("sonar.preview.supportedPlugins", "\"ja,va\",\"p\"\"hp\""));
  }

  @Test
  public void should_throw_exception_when_no_value_of_non_secured_settings() {
    Setting setting = Setting.newBuilder().setKey("sonar.open.setting").build();

    List<Setting> singletonList = singletonList(setting);

    assertThatThrownBy(() -> AbstractSettingsLoader.toMap(singletonList))
        .isInstanceOf(IllegalStateException.class)
        .hasMessage("Unknown property value for sonar.open.setting");
  }

  @Test
  public void should_filter_secured_settings_without_value() {
    assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
      .setKey("sonar.setting.secured").build())))
        .isEmpty();
  }

  @Test
  public void should_load_global_propertyset_settings() {
    Builder valuesBuilder = Value.newBuilder();
    valuesBuilder.putValue("filepattern", "**/*.xml");
    valuesBuilder.putValue("rulepattern", "*:S12345");
    Value value1 = valuesBuilder.build();
    valuesBuilder.clear();
    valuesBuilder.putValue("filepattern", "**/*.java");
    valuesBuilder.putValue("rulepattern", "*:S456");
    Value value2 = valuesBuilder.build();

    assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
      .setKey("sonar.issue.exclusions.multicriteria")
      .setFieldValues(FieldValues.newBuilder().addFieldValues(value1).addFieldValues(value2)).build())))
        .containsOnly(entry("sonar.issue.exclusions.multicriteria", "1,2"),
          entry("sonar.issue.exclusions.multicriteria.1.filepattern", "**/*.xml"),
          entry("sonar.issue.exclusions.multicriteria.1.rulepattern", "*:S12345"),
          entry("sonar.issue.exclusions.multicriteria.2.filepattern", "**/*.java"),
          entry("sonar.issue.exclusions.multicriteria.2.rulepattern", "*:S456"));
  }

  @Test
  public void should_always_load_language_detection_properties() {
    assertThat(AbstractSettingsLoader.toMap(List.of(
      Setting.newBuilder()
        .setInherited(false)
        .setKey("sonar.xoo.file.suffixes")
        .setValues(Values.newBuilder().addValues(".xoo")).build(),
      Setting.newBuilder()
        .setInherited(false)
        .setKey("sonar.xoo.file.patterns")
        .setValues(Values.newBuilder().addValues("Xoofile")).build()
    ))).containsExactly(
      entry("sonar.xoo.file.suffixes", ".xoo"),
      entry("sonar.xoo.file.patterns", "Xoofile")
    );
  }

  @Test
  public void should_not_load_inherited_properties() {
    assertThat(AbstractSettingsLoader.toMap(List.of(
      Setting.newBuilder()
        .setInherited(true)
        .setKey("sonar.inherited.property")
        .setValues(Values.newBuilder().addValues("foo")).build()
    ))).isEmpty();
  }

}