You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultConfigurationTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.scanner.config;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Arrays;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.config.internal.Encryption;
  27. import org.sonar.api.config.PropertyDefinition;
  28. import org.sonar.api.config.PropertyDefinitions;
  29. import org.sonar.api.config.PropertyFieldDefinition;
  30. import org.sonar.api.utils.log.LogTester;
  31. import org.sonar.api.utils.log.LoggerLevel;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.fail;
  34. public class DefaultConfigurationTest {
  35. @Rule
  36. public LogTester logTester = new LogTester();
  37. @Test
  38. public void accessingMultiValuedPropertiesShouldBeConsistentWithDeclaration() {
  39. Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
  40. PropertyDefinition.builder("single").multiValues(false).build(),
  41. PropertyDefinition.builder("multiA").multiValues(true).build())), new Encryption(null),
  42. ImmutableMap.of("single", "foo", "multiA", "a,b", "notDeclared", "c,d")) {
  43. };
  44. assertThat(config.get("multiA")).hasValue("a,b");
  45. assertThat(logTester.logs(LoggerLevel.WARN))
  46. .contains(
  47. "Access to the multi-values/property set property 'multiA' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.");
  48. logTester.clear();
  49. assertThat(config.getStringArray("single")).containsExactly("foo");
  50. assertThat(logTester.logs(LoggerLevel.WARN))
  51. .contains(
  52. "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.");
  53. logTester.clear();
  54. assertThat(config.get("notDeclared")).hasValue("c,d");
  55. assertThat(config.getStringArray("notDeclared")).containsExactly("c", "d");
  56. assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
  57. }
  58. @Test
  59. public void accessingPropertySetPropertiesShouldBeConsistentWithDeclaration() {
  60. Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
  61. PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())),
  62. new Encryption(null),
  63. ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) {
  64. };
  65. assertThat(config.get("props")).hasValue("1,2");
  66. assertThat(logTester.logs(LoggerLevel.WARN))
  67. .contains(
  68. "Access to the multi-values/property set property 'props' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.");
  69. logTester.clear();
  70. assertThat(config.getStringArray("props")).containsExactly("1", "2");
  71. assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
  72. }
  73. @Test
  74. public void getDefaultValues() {
  75. Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
  76. PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(),
  77. PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null),
  78. ImmutableMap.of()) {
  79. };
  80. assertThat(config.get("multiA")).hasValue("foo,bar");
  81. assertThat(config.getStringArray("multiA")).containsExactly("foo", "bar");
  82. assertThat(config.get("single")).hasValue("default");
  83. assertThat(config.getStringArray("single")).containsExactly("default");
  84. }
  85. @Test
  86. public void testParsingMultiValues() {
  87. assertThat(getStringArray("")).isEmpty();
  88. assertThat(getStringArray(",")).isEmpty();
  89. assertThat(getStringArray(",,")).isEmpty();
  90. assertThat(getStringArray("a")).containsExactly("a");
  91. assertThat(getStringArray("a b")).containsExactly("a b");
  92. assertThat(getStringArray("a , b")).containsExactly("a", "b");
  93. assertThat(getStringArray("\"a \",\" b\"")).containsExactly("a ", " b");
  94. assertThat(getStringArray("\"a,b\",c")).containsExactly("a,b", "c");
  95. assertThat(getStringArray("\"a\nb\",c")).containsExactly("a\nb", "c");
  96. assertThat(getStringArray("\"a\",\n b\n")).containsExactly("a", "b");
  97. assertThat(getStringArray("a\n,b\n")).containsExactly("a", "b");
  98. assertThat(getStringArray("a\n,b\n,\"\"")).containsExactly("a", "b", "");
  99. assertThat(getStringArray("a\n, \" \" ,b\n")).containsExactly("a", " ", "b");
  100. assertThat(getStringArray(" \" , ,, \", a\n,b\n")).containsExactly(" , ,, ", "a","b");
  101. assertThat(getStringArray("a\n,,b\n")).containsExactly("a", "b");
  102. assertThat(getStringArray("a,\n\nb,c")).containsExactly("a", "b", "c");
  103. assertThat(getStringArray("a,b\n\nc,d")).containsExactly("a", "b\nc", "d");
  104. try {
  105. getStringArray("\"a ,b");
  106. fail("Expected exception");
  107. } catch (Exception e) {
  108. assertThat(e).hasMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
  109. }
  110. }
  111. private String[] getStringArray(String value) {
  112. return new DefaultConfiguration(new PropertyDefinitions(Arrays.asList(
  113. PropertyDefinition.builder("multi").multiValues(true).build())), new Encryption(null),
  114. ImmutableMap.of("multi", value)) {
  115. }.getStringArray("multi");
  116. }
  117. }