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.

AbstractSettingsLoaderTest.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.repository.settings;
  21. import org.junit.Test;
  22. import org.sonarqube.ws.Settings.FieldValues;
  23. import org.sonarqube.ws.Settings.FieldValues.Value;
  24. import org.sonarqube.ws.Settings.FieldValues.Value.Builder;
  25. import org.sonarqube.ws.Settings.Setting;
  26. import org.sonarqube.ws.Settings.Values;
  27. import static java.util.Collections.singletonList;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. import static org.assertj.core.api.Assertions.entry;
  31. public class AbstractSettingsLoaderTest {
  32. @Test
  33. public void should_load_global_multivalue_settings() {
  34. assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
  35. .setKey("sonar.preview.supportedPlugins")
  36. .setValues(Values.newBuilder().addValues("java").addValues("php")).build())))
  37. .containsExactly(entry("sonar.preview.supportedPlugins", "java,php"));
  38. }
  39. @Test
  40. public void should_escape_global_multivalue_settings() {
  41. assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
  42. .setKey("sonar.preview.supportedPlugins")
  43. .setValues(Values.newBuilder().addValues("ja,va").addValues("p\"hp")).build())))
  44. .containsExactly(entry("sonar.preview.supportedPlugins", "\"ja,va\",\"p\"\"hp\""));
  45. }
  46. @Test
  47. public void should_throw_exception_when_no_value_of_non_secured_settings() {
  48. assertThatThrownBy(() -> AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
  49. .setKey("sonar.open.setting").build())))
  50. .isInstanceOf(IllegalStateException.class)
  51. .hasMessage("Unknown property value for sonar.open.setting");
  52. }
  53. @Test
  54. public void should_filter_secured_settings_without_value() {
  55. assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
  56. .setKey("sonar.setting.secured").build())))
  57. .isEmpty();
  58. }
  59. @Test
  60. public void should_load_global_propertyset_settings() {
  61. Builder valuesBuilder = Value.newBuilder();
  62. valuesBuilder.putValue("filepattern", "**/*.xml");
  63. valuesBuilder.putValue("rulepattern", "*:S12345");
  64. Value value1 = valuesBuilder.build();
  65. valuesBuilder.clear();
  66. valuesBuilder.putValue("filepattern", "**/*.java");
  67. valuesBuilder.putValue("rulepattern", "*:S456");
  68. Value value2 = valuesBuilder.build();
  69. assertThat(AbstractSettingsLoader.toMap(singletonList(Setting.newBuilder()
  70. .setKey("sonar.issue.exclusions.multicriteria")
  71. .setFieldValues(FieldValues.newBuilder().addFieldValues(value1).addFieldValues(value2)).build())))
  72. .containsOnly(entry("sonar.issue.exclusions.multicriteria", "1,2"),
  73. entry("sonar.issue.exclusions.multicriteria.1.filepattern", "**/*.xml"),
  74. entry("sonar.issue.exclusions.multicriteria.1.rulepattern", "*:S12345"),
  75. entry("sonar.issue.exclusions.multicriteria.2.filepattern", "**/*.java"),
  76. entry("sonar.issue.exclusions.multicriteria.2.rulepattern", "*:S456"));
  77. }
  78. }