aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/config/DefaultConfigurationTest.java
blob: ba7842a2b2c5fd2a085b42dee25868a115862130 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * SonarQube
 * Copyright (C) 2009-2018 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.config;

import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.Encryption;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.PropertyFieldDefinition;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.scanner.bootstrap.GlobalAnalysisMode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;

public class DefaultConfigurationTest {

  @Rule
  public LogTester logTester = new LogTester();

  @Test
  public void accessingMultiValuedPropertiesShouldBeConsistentWithDeclaration() {
    Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
      PropertyDefinition.builder("single").multiValues(false).build(),
      PropertyDefinition.builder("multiA").multiValues(true).build())), new Encryption(null),
      mock(GlobalAnalysisMode.class),
      ImmutableMap.of("single", "foo", "multiA", "a,b", "notDeclared", "c,d")) {
    };

    assertThat(config.get("multiA")).hasValue("a,b");
    assertThat(logTester.logs(LoggerLevel.WARN))
      .contains(
        "Access to the multi-values/property set property 'multiA' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.");

    logTester.clear();

    assertThat(config.getStringArray("single")).containsExactly("foo");
    assertThat(logTester.logs(LoggerLevel.WARN))
      .contains(
        "Property 'single' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated.");

    logTester.clear();

    assertThat(config.get("notDeclared")).hasValue("c,d");
    assertThat(config.getStringArray("notDeclared")).containsExactly("c", "d");
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
  }

  @Test
  public void accessingPropertySetPropertiesShouldBeConsistentWithDeclaration() {
    Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
      PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())),
      new Encryption(null),
      mock(GlobalAnalysisMode.class),
      ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) {
    };

    assertThat(config.get("props")).hasValue("1,2");
    assertThat(logTester.logs(LoggerLevel.WARN))
      .contains(
        "Access to the multi-values/property set property 'props' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.");

    logTester.clear();

    assertThat(config.getStringArray("props")).containsExactly("1", "2");
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
  }

  @Test
  public void getDefaultValues() {
    Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
      PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(),
      PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null),
      mock(GlobalAnalysisMode.class),
      ImmutableMap.of()) {
    };

    assertThat(config.get("multiA")).hasValue("foo,bar");
    assertThat(config.getStringArray("multiA")).containsExactly("foo", "bar");
    assertThat(config.get("single")).hasValue("default");
    assertThat(config.getStringArray("single")).containsExactly("default");
  }

  @Test
  public void testParsingMultiValues() {
    assertThat(getStringArray("")).isEmpty();
    assertThat(getStringArray(",")).isEmpty();
    assertThat(getStringArray(",,")).isEmpty();
    assertThat(getStringArray("a")).containsExactly("a");
    assertThat(getStringArray("a b")).containsExactly("a b");
    assertThat(getStringArray("a , b")).containsExactly("a", "b");
    assertThat(getStringArray("\"a \",\" b\"")).containsExactly("a ", " b");
    assertThat(getStringArray("\"a,b\",c")).containsExactly("a,b", "c");
    assertThat(getStringArray("\"a\nb\",c")).containsExactly("a\nb", "c");
    assertThat(getStringArray("\"a\",\n  b\n")).containsExactly("a", "b");
    assertThat(getStringArray("a\n,b\n")).containsExactly("a", "b");
    assertThat(getStringArray("a\n,b\n,\"\"")).containsExactly("a", "b", "");
    assertThat(getStringArray("a\n,  \"  \"  ,b\n")).containsExactly("a", "  ", "b");
    assertThat(getStringArray("  \" , ,, \", a\n,b\n")).containsExactly(" , ,, ", "a","b");
    assertThat(getStringArray("a\n,,b\n")).containsExactly("a", "b");
    assertThat(getStringArray("a,\n\nb,c")).containsExactly("a", "b", "c");
    assertThat(getStringArray("a,b\n\nc,d")).containsExactly("a", "b\nc", "d");
    try {
      getStringArray("\"a ,b");
      fail("Expected exception");
    } catch (Exception e) {
      assertThat(e).hasMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
    }
  }

  private String[] getStringArray(String value) {
    return new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
      PropertyDefinition.builder("multi").multiValues(true).build())), new Encryption(null),
      mock(GlobalAnalysisMode.class),
      ImmutableMap.of("multi", value)) {
    }.getStringArray("multi");
  }
}