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.

PropertyResolverTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 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.sonarsource.scanner.cli;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import org.junit.jupiter.api.Test;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  27. class PropertyResolverTest {
  28. @Test
  29. void resolve_properties() {
  30. Properties map = new Properties();
  31. Map<String, String> env = new HashMap<>();
  32. // resolve
  33. map.put("A", "value a");
  34. map.put("B", "value b");
  35. map.put("C", "${A} ${B} ${nonexisting}");
  36. PropertyResolver resolver = new PropertyResolver(map, env);
  37. Properties resolved = resolver.resolve();
  38. assertThat(resolved.get("A")).isEqualTo("value a");
  39. assertThat(resolved.get("B")).isEqualTo("value b");
  40. assertThat(resolved.get("C")).isEqualTo("value a value b ");
  41. map.clear();
  42. map.put("sonar.login", "admin");
  43. map.put("sonar.password", "${sonar.login}");
  44. resolver = new PropertyResolver(map, env);
  45. resolved = resolver.resolve();
  46. assertThat(resolved.get("sonar.password")).isEqualTo("admin");
  47. }
  48. @Test
  49. void use_env() {
  50. Properties map = new Properties();
  51. Map<String, String> env = new HashMap<>();
  52. // resolve
  53. map.put("A", "invalid");
  54. map.put("B", "value b");
  55. map.put("C", "${env.A} ${B} ${nonexisting}");
  56. env.put("A", "value a");
  57. PropertyResolver resolver = new PropertyResolver(map, env);
  58. Properties resolved = resolver.resolve();
  59. assertThat(resolved.get("A")).isEqualTo("invalid");
  60. assertThat(resolved.get("B")).isEqualTo("value b");
  61. assertThat(resolved.get("C")).isEqualTo("value a value b ");
  62. }
  63. @Test
  64. void resolve_recursively() {
  65. Properties map = new Properties();
  66. Map<String, String> env = new HashMap<>();
  67. map.put("A", "value a");
  68. map.put("B", "${A}");
  69. map.put("C", "${A} ${B}");
  70. PropertyResolver resolver = new PropertyResolver(map, env);
  71. Properties resolved = resolver.resolve();
  72. assertThat(resolved.get("A")).isEqualTo("value a");
  73. assertThat(resolved.get("B")).isEqualTo("value a");
  74. assertThat(resolved.get("C")).isEqualTo("value a value a");
  75. }
  76. @Test
  77. void dont_resolve_nested() {
  78. Properties map = new Properties();
  79. Map<String, String> env = new HashMap<>();
  80. map.put("A", "value a");
  81. map.put("B", "value b");
  82. map.put("C", "${A ${B}}");
  83. PropertyResolver resolver = new PropertyResolver(map, env);
  84. Properties resolved = resolver.resolve();
  85. assertThat(resolved.get("A")).isEqualTo("value a");
  86. assertThat(resolved.get("B")).isEqualTo("value b");
  87. assertThat(resolved.get("C")).isEqualTo("${A value b}");
  88. }
  89. @Test
  90. void missing_var() {
  91. Map<String, String> env = new HashMap<>();
  92. Properties map = new Properties();
  93. map.put("A", "/path/${missing} var/");
  94. PropertyResolver resolver = new PropertyResolver(map, env);
  95. Properties resolved = resolver.resolve();
  96. assertThat(resolved.get("A")).isEqualTo("/path/ var/");
  97. }
  98. @Test
  99. void fail_loop_properties_resolution() {
  100. Properties map = new Properties();
  101. Map<String, String> env = new HashMap<>();
  102. // resolve
  103. map.put("A", "${B}");
  104. map.put("B", "${A}");
  105. PropertyResolver resolver = new PropertyResolver(map, env);
  106. assertThatThrownBy(resolver::resolve).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("variable: B");
  107. }
  108. @Test
  109. void preserve_empty() {
  110. Properties map = new Properties();
  111. Map<String, String> env = new HashMap<>();
  112. map.put("A", "");
  113. map.put("B", "${A}");
  114. PropertyResolver resolver = new PropertyResolver(map, env);
  115. Properties resolved = resolver.resolve();
  116. assertThat(resolved.get("A")).isEqualTo("");
  117. assertThat(resolved.get("B")).isEqualTo("");
  118. }
  119. }