|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.util;
|
18 | 18 |
|
19 | 19 | import java.util.Properties;
|
| 20 | +import java.util.stream.Stream; |
20 | 21 |
|
| 22 | +import org.junit.jupiter.api.Disabled; |
| 23 | +import org.junit.jupiter.api.Nested; |
21 | 24 | import org.junit.jupiter.api.Test;
|
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
22 | 28 |
|
23 | 29 | import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
24 | 30 |
|
25 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
26 | 32 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 33 | +import static org.mockito.BDDMockito.given; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
27 | 37 |
|
28 | 38 | /**
|
| 39 | + * Tests for {@link PropertyPlaceholderHelper}. |
| 40 | + * |
29 | 41 | * @author Rob Harrop
|
| 42 | + * @author Stephane Nicoll |
30 | 43 | */
|
31 | 44 | class PropertyPlaceholderHelperTests {
|
32 | 45 |
|
@@ -106,6 +119,57 @@ void unresolvedPlaceholderAsError() {
|
106 | 119 | PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
|
107 | 120 | assertThatIllegalArgumentException().isThrownBy(() ->
|
108 | 121 | helper.replacePlaceholders(text, props));
|
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Nested |
| 126 | + class DefaultValueTests { |
| 127 | + |
| 128 | + private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true); |
| 129 | + |
| 130 | + @ParameterizedTest(name = "{0} -> {1}") |
| 131 | + @MethodSource("defaultValues") |
| 132 | + void defaultValueIsApplied(String text, String value) { |
| 133 | + Properties properties = new Properties(); |
| 134 | + properties.setProperty("one", "1"); |
| 135 | + properties.setProperty("two", "2"); |
| 136 | + assertThat(this.helper.replacePlaceholders(text, properties)).isEqualTo(value); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @Disabled("gh-26268") |
| 141 | + void defaultValueIsNotEvaluatedEarly() { |
| 142 | + PlaceholderResolver resolver = mockPlaceholderResolver("one", "1"); |
| 143 | + assertThat(this.helper.replacePlaceholders("This is ${one:or${two}}",resolver)).isEqualTo("This is 1"); |
| 144 | + verify(resolver).resolvePlaceholder("one"); |
| 145 | + verifyNoMoreInteractions(resolver); |
| 146 | + } |
| 147 | + |
| 148 | + static Stream<Arguments> defaultValues() { |
| 149 | + return Stream.of( |
| 150 | + Arguments.of("${invalid:test}", "test"), |
| 151 | + Arguments.of("${invalid:${one}}", "1"), |
| 152 | + Arguments.of("${invalid:${one}${two}}", "12"), |
| 153 | + Arguments.of("${invalid:${one}:${two}}", "1:2"), |
| 154 | + Arguments.of("${invalid:${also_invalid:test}}", "test"), |
| 155 | + Arguments.of("${invalid:${also_invalid:${one}}}", "1") |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + PlaceholderResolver mockPlaceholderResolver(String... pairs) { |
| 162 | + if (pairs.length % 2 == 1) { |
| 163 | + throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); |
| 164 | + } |
| 165 | + PlaceholderResolver resolver = mock(PlaceholderResolver.class); |
| 166 | + for (int i = 0; i < pairs.length; i += 2) { |
| 167 | + String key = pairs[i]; |
| 168 | + String value = pairs[i + 1]; |
| 169 | + given(resolver.resolvePlaceholder(key)).willReturn(value); |
| 170 | + } |
| 171 | + return resolver; |
109 | 172 | }
|
110 | 173 |
|
| 174 | + |
111 | 175 | }
|
0 commit comments