|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.context.properties;
|
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
19 | 22 | import java.net.InetAddress;
|
20 | 23 | import java.util.ArrayList;
|
| 24 | +import java.util.Collections; |
21 | 25 | import java.util.HashMap;
|
22 | 26 | import java.util.List;
|
23 | 27 | import java.util.Map;
|
|
28 | 32 | import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ApplicationConfigurationProperties;
|
29 | 33 | import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesBeanDescriptor;
|
30 | 34 | import org.springframework.boot.context.properties.ConfigurationProperties;
|
| 35 | +import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; |
31 | 36 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
32 | 37 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
33 | 38 | import org.springframework.context.annotation.Bean;
|
34 | 39 | import org.springframework.context.annotation.Configuration;
|
35 | 40 | import org.springframework.context.annotation.Import;
|
| 41 | +import org.springframework.core.convert.converter.Converter; |
| 42 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 43 | +import org.springframework.core.env.MapPropertySource; |
| 44 | +import org.springframework.core.io.InputStreamSource; |
36 | 45 |
|
37 | 46 | import static org.assertj.core.api.Assertions.assertThat;
|
38 | 47 | import static org.assertj.core.api.Assertions.entry;
|
@@ -238,6 +247,43 @@ void hikariDataSourceConfigurationPropertiesBeanCanBeSerialized() {
|
238 | 247 | });
|
239 | 248 | }
|
240 | 249 |
|
| 250 | + @Test |
| 251 | + @SuppressWarnings("unchecked") |
| 252 | + void endpointResponseUsesToStringOfCharSequenceAsPropertyValue() throws IOException { |
| 253 | + ApplicationContextRunner contextRunner = new ApplicationContextRunner().withInitializer((context) -> { |
| 254 | + ConfigurableEnvironment environment = context.getEnvironment(); |
| 255 | + environment.getPropertySources().addFirst(new MapPropertySource("test", |
| 256 | + Collections.singletonMap("foo.name", new CharSequenceProperty("Spring Boot")))); |
| 257 | + }).withUserConfiguration(FooConfig.class); |
| 258 | + contextRunner.run((context) -> { |
| 259 | + ConfigurationPropertiesReportEndpoint endpoint = context |
| 260 | + .getBean(ConfigurationPropertiesReportEndpoint.class); |
| 261 | + ApplicationConfigurationProperties applicationProperties = endpoint.configurationProperties(); |
| 262 | + ConfigurationPropertiesBeanDescriptor descriptor = applicationProperties.getContexts().get(context.getId()) |
| 263 | + .getBeans().get("foo"); |
| 264 | + assertThat((Map<String, Object>) descriptor.getInputs().get("name")).containsEntry("value", "Spring Boot"); |
| 265 | + }); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + @SuppressWarnings("unchecked") |
| 270 | + void endpointResponseUsesPlaceholderForComplexValueAsPropertyValue() throws IOException { |
| 271 | + ApplicationContextRunner contextRunner = new ApplicationContextRunner().withInitializer((context) -> { |
| 272 | + ConfigurableEnvironment environment = context.getEnvironment(); |
| 273 | + environment.getPropertySources().addFirst(new MapPropertySource("test", |
| 274 | + Collections.singletonMap("foo.name", new ComplexProperty("Spring Boot")))); |
| 275 | + }).withUserConfiguration(ComplexPropertyToStringConverter.class, FooConfig.class); |
| 276 | + contextRunner.run((context) -> { |
| 277 | + ConfigurationPropertiesReportEndpoint endpoint = context |
| 278 | + .getBean(ConfigurationPropertiesReportEndpoint.class); |
| 279 | + ApplicationConfigurationProperties applicationProperties = endpoint.configurationProperties(); |
| 280 | + ConfigurationPropertiesBeanDescriptor descriptor = applicationProperties.getContexts().get(context.getId()) |
| 281 | + .getBeans().get("foo"); |
| 282 | + assertThat((Map<String, Object>) descriptor.getInputs().get("name")).containsEntry("value", |
| 283 | + "Complex property value " + ComplexProperty.class.getName()); |
| 284 | + }); |
| 285 | + } |
| 286 | + |
241 | 287 | @Configuration(proxyBeanMethods = false)
|
242 | 288 | @EnableConfigurationProperties
|
243 | 289 | static class Base {
|
@@ -518,4 +564,59 @@ HikariDataSource hikariDataSource() {
|
518 | 564 |
|
519 | 565 | }
|
520 | 566 |
|
| 567 | + static class CharSequenceProperty implements CharSequence, InputStreamSource { |
| 568 | + |
| 569 | + private final String value; |
| 570 | + |
| 571 | + CharSequenceProperty(String value) { |
| 572 | + this.value = value; |
| 573 | + } |
| 574 | + |
| 575 | + @Override |
| 576 | + public int length() { |
| 577 | + return this.value.length(); |
| 578 | + } |
| 579 | + |
| 580 | + @Override |
| 581 | + public char charAt(int index) { |
| 582 | + return this.value.charAt(index); |
| 583 | + } |
| 584 | + |
| 585 | + @Override |
| 586 | + public CharSequence subSequence(int start, int end) { |
| 587 | + return this.value.subSequence(start, end); |
| 588 | + } |
| 589 | + |
| 590 | + @Override |
| 591 | + public String toString() { |
| 592 | + return this.value; |
| 593 | + } |
| 594 | + |
| 595 | + @Override |
| 596 | + public InputStream getInputStream() throws IOException { |
| 597 | + return new ByteArrayInputStream(this.value.getBytes()); |
| 598 | + } |
| 599 | + |
| 600 | + } |
| 601 | + |
| 602 | + static class ComplexProperty { |
| 603 | + |
| 604 | + private final String value; |
| 605 | + |
| 606 | + ComplexProperty(String value) { |
| 607 | + this.value = value; |
| 608 | + } |
| 609 | + |
| 610 | + } |
| 611 | + |
| 612 | + @ConfigurationPropertiesBinding |
| 613 | + static class ComplexPropertyToStringConverter implements Converter<ComplexProperty, String> { |
| 614 | + |
| 615 | + @Override |
| 616 | + public String convert(ComplexProperty source) { |
| 617 | + return source.value; |
| 618 | + } |
| 619 | + |
| 620 | + } |
| 621 | + |
521 | 622 | }
|
0 commit comments