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.6KB

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