|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * SonarScanner CLI
- * Copyright (C) 2011-2024 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.sonarsource.scanner.cli;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
-
- import static org.assertj.core.api.Assertions.assertThat;
-
- public class PropertyResolverTest {
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
- @Test
- public void resolve_properties() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
-
- // resolve
- map.put("A", "value a");
- map.put("B", "value b");
- map.put("C", "${A} ${B} ${nonexisting}");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("value a");
- assertThat(resolved.get("B")).isEqualTo("value b");
- assertThat(resolved.get("C")).isEqualTo("value a value b ");
-
- map.clear();
- map.put("sonar.login", "admin");
- map.put("sonar.password", "${sonar.login}");
-
- resolver = new PropertyResolver(map, env);
- resolved = resolver.resolve();
- assertThat(resolved.get("sonar.password")).isEqualTo("admin");
- }
-
- @Test
- public void use_env() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
-
- // resolve
- map.put("A", "invalid");
- map.put("B", "value b");
- map.put("C", "${env.A} ${B} ${nonexisting}");
- env.put("A", "value a");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("invalid");
- assertThat(resolved.get("B")).isEqualTo("value b");
- assertThat(resolved.get("C")).isEqualTo("value a value b ");
- }
-
- @Test
- public void resolve_recursively() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
- map.put("A", "value a");
- map.put("B", "${A}");
- map.put("C", "${A} ${B}");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("value a");
- assertThat(resolved.get("B")).isEqualTo("value a");
- assertThat(resolved.get("C")).isEqualTo("value a value a");
- }
-
- @Test
- public void dont_resolve_nested() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
- map.put("A", "value a");
- map.put("B", "value b");
- map.put("C", "${A ${B}}");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("value a");
- assertThat(resolved.get("B")).isEqualTo("value b");
- assertThat(resolved.get("C")).isEqualTo("${A value b}");
- }
-
- @Test
- public void missing_var() {
- Map<String, String> env = new HashMap<>();
- Properties map = new Properties();
- map.put("A", "/path/${missing} var/");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("/path/ var/");
-
- }
-
- @Test
- public void fail_loop_properties_resolution() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
-
- // resolve
- map.put("A", "${B}");
- map.put("B", "${A}");
-
- exception.expect(IllegalArgumentException.class);
- exception.expectMessage("variable: B");
- PropertyResolver resolver = new PropertyResolver(map, env);
- resolver.resolve();
- }
-
- @Test
- public void preserve_empty() {
- Properties map = new Properties();
- Map<String, String> env = new HashMap<>();
-
- map.put("A", "");
- map.put("B", "${A}");
-
- PropertyResolver resolver = new PropertyResolver(map, env);
- Properties resolved = resolver.resolve();
- assertThat(resolved.get("A")).isEqualTo("");
- assertThat(resolved.get("B")).isEqualTo("");
- }
- }
|