|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.env; |
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
19 | 22 | import java.util.Collections; |
20 | 23 | import java.util.LinkedHashMap; |
21 | 24 | import java.util.Map; |
|
39 | 42 | import org.springframework.core.env.Environment; |
40 | 43 | import org.springframework.core.env.MapPropertySource; |
41 | 44 | import org.springframework.core.env.StandardEnvironment; |
| 45 | +import org.springframework.core.io.InputStreamSource; |
42 | 46 | import org.springframework.mock.env.MockPropertySource; |
43 | 47 |
|
44 | 48 | import static org.assertj.core.api.Assertions.assertThat; |
|
54 | 58 | * @author Andy Wilkinson |
55 | 59 | * @author HaiTao Zhang |
56 | 60 | * @author Chris Bono |
| 61 | + * @author Scott Frederick |
57 | 62 | */ |
58 | 63 | class EnvironmentEndpointTests { |
59 | 64 |
|
@@ -194,15 +199,22 @@ void propertyWithSensitivePlaceholderNotResolved() { |
194 | 199 | } |
195 | 200 |
|
196 | 201 | @Test |
197 | | - @SuppressWarnings("unchecked") |
198 | 202 | void propertyWithTypeOtherThanStringShouldNotFail() { |
199 | 203 | ConfigurableEnvironment environment = emptyEnvironment(); |
200 | 204 | environment.getPropertySources() |
201 | 205 | .addFirst(singleKeyPropertySource("test", "foo", Collections.singletonMap("bar", "baz"))); |
202 | 206 | EnvironmentDescriptor descriptor = new EnvironmentEndpoint(environment).environment(null); |
203 | | - Map<String, String> foo = (Map<String, String>) propertySources(descriptor).get("test").getProperties() |
204 | | - .get("foo").getValue(); |
205 | | - assertThat(foo.get("bar")).isEqualTo("baz"); |
| 207 | + String value = (String) propertySources(descriptor).get("test").getProperties().get("foo").getValue(); |
| 208 | + assertThat(value).isEqualTo("Complex property type java.util.Collections$SingletonMap"); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void propertyWithCharSequenceTypeIsConvertedToString() throws Exception { |
| 213 | + ConfigurableEnvironment environment = emptyEnvironment(); |
| 214 | + environment.getPropertySources().addFirst(singleKeyPropertySource("test", "foo", new CharSequenceProperty())); |
| 215 | + EnvironmentDescriptor descriptor = new EnvironmentEndpoint(environment).environment(null); |
| 216 | + String value = (String) propertySources(descriptor).get("test").getProperties().get("foo").getValue(); |
| 217 | + assertThat(value).isEqualTo("test value"); |
206 | 218 | } |
207 | 219 |
|
208 | 220 | @Test |
@@ -360,4 +372,35 @@ EnvironmentEndpoint environmentEndpoint(Environment environment) { |
360 | 372 |
|
361 | 373 | } |
362 | 374 |
|
| 375 | + public static class CharSequenceProperty implements CharSequence, InputStreamSource { |
| 376 | + |
| 377 | + private final String value = "test value"; |
| 378 | + |
| 379 | + @Override |
| 380 | + public int length() { |
| 381 | + return this.value.length(); |
| 382 | + } |
| 383 | + |
| 384 | + @Override |
| 385 | + public char charAt(int index) { |
| 386 | + return this.value.charAt(index); |
| 387 | + } |
| 388 | + |
| 389 | + @Override |
| 390 | + public CharSequence subSequence(int start, int end) { |
| 391 | + return this.value.subSequence(start, end); |
| 392 | + } |
| 393 | + |
| 394 | + @Override |
| 395 | + public String toString() { |
| 396 | + return this.value; |
| 397 | + } |
| 398 | + |
| 399 | + @Override |
| 400 | + public InputStream getInputStream() throws IOException { |
| 401 | + return new ByteArrayInputStream(this.value.getBytes()); |
| 402 | + } |
| 403 | + |
| 404 | + } |
| 405 | + |
363 | 406 | } |
0 commit comments