|
56 | 56 | import org.springframework.boot.context.properties.bind.validation.BindValidationException;
|
57 | 57 | import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
58 | 58 | import org.springframework.boot.convert.DataSizeUnit;
|
| 59 | +import org.springframework.boot.convert.DurationFormat; |
| 60 | +import org.springframework.boot.convert.DurationStyle; |
59 | 61 | import org.springframework.boot.convert.DurationUnit;
|
| 62 | +import org.springframework.boot.convert.PeriodFormat; |
| 63 | +import org.springframework.boot.convert.PeriodStyle; |
60 | 64 | import org.springframework.boot.convert.PeriodUnit;
|
61 | 65 | import org.springframework.boot.testsupport.system.CapturedOutput;
|
62 | 66 | import org.springframework.boot.testsupport.system.OutputCaptureExtension;
|
@@ -808,6 +812,51 @@ void loadWhenBindingToConstructorParametersWithDefaultDataUnitShouldBind() {
|
808 | 812 | assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(4));
|
809 | 813 | }
|
810 | 814 |
|
| 815 | + @Test |
| 816 | + void loadWhenBindingToConstructorParametersWithCustomDataFormatShouldBind() { |
| 817 | + MutablePropertySources sources = this.context.getEnvironment().getPropertySources(); |
| 818 | + Map<String, Object> source = new HashMap<>(); |
| 819 | + source.put("test.duration", "12d"); |
| 820 | + source.put("test.period", "13y"); |
| 821 | + sources.addLast(new MapPropertySource("test", source)); |
| 822 | + load(ConstructorParameterWithFormatConfiguration.class); |
| 823 | + ConstructorParameterWithFormatProperties bean = this.context |
| 824 | + .getBean(ConstructorParameterWithFormatProperties.class); |
| 825 | + assertThat(bean.getDuration()).isEqualTo(Duration.ofDays(12)); |
| 826 | + assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(13)); |
| 827 | + } |
| 828 | + |
| 829 | + @Test |
| 830 | + void loadWhenBindingToConstructorParametersWithNotMatchingCustomDurationFormatShouldFail() { |
| 831 | + MutablePropertySources sources = this.context.getEnvironment().getPropertySources(); |
| 832 | + Map<String, Object> source = new HashMap<>(); |
| 833 | + source.put("test.duration", "P12D"); |
| 834 | + sources.addLast(new MapPropertySource("test", source)); |
| 835 | + assertThatExceptionOfType(Exception.class) |
| 836 | + .isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class)).havingCause() |
| 837 | + .isInstanceOf(BindException.class); |
| 838 | + } |
| 839 | + |
| 840 | + @Test |
| 841 | + void loadWhenBindingToConstructorParametersWithNotMatchingCustomPeriodFormatShouldFail() { |
| 842 | + MutablePropertySources sources = this.context.getEnvironment().getPropertySources(); |
| 843 | + Map<String, Object> source = new HashMap<>(); |
| 844 | + source.put("test.period", "P12D"); |
| 845 | + sources.addLast(new MapPropertySource("test", source)); |
| 846 | + assertThatExceptionOfType(Exception.class) |
| 847 | + .isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class)).havingCause() |
| 848 | + .isInstanceOf(BindException.class); |
| 849 | + } |
| 850 | + |
| 851 | + @Test |
| 852 | + void loadWhenBindingToConstructorParametersWithDefaultDataFormatShouldBind() { |
| 853 | + load(ConstructorParameterWithFormatConfiguration.class); |
| 854 | + ConstructorParameterWithFormatProperties bean = this.context |
| 855 | + .getBean(ConstructorParameterWithFormatProperties.class); |
| 856 | + assertThat(bean.getDuration()).isEqualTo(Duration.ofDays(2)); |
| 857 | + assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(3)); |
| 858 | + } |
| 859 | + |
811 | 860 | @Test
|
812 | 861 | void loadWhenBindingToConstructorParametersShouldValidate() {
|
813 | 862 | assertThatExceptionOfType(Exception.class)
|
@@ -2007,6 +2056,31 @@ Period getPeriod() {
|
2007 | 2056 |
|
2008 | 2057 | }
|
2009 | 2058 |
|
| 2059 | + @ConstructorBinding |
| 2060 | + @ConfigurationProperties(prefix = "test") |
| 2061 | + static class ConstructorParameterWithFormatProperties { |
| 2062 | + |
| 2063 | + private final Duration duration; |
| 2064 | + |
| 2065 | + private final Period period; |
| 2066 | + |
| 2067 | + ConstructorParameterWithFormatProperties( |
| 2068 | + @DefaultValue("2d") @DurationFormat(DurationStyle.SIMPLE) Duration duration, |
| 2069 | + @DefaultValue("3y") @PeriodFormat(PeriodStyle.SIMPLE) Period period) { |
| 2070 | + this.duration = duration; |
| 2071 | + this.period = period; |
| 2072 | + } |
| 2073 | + |
| 2074 | + Duration getDuration() { |
| 2075 | + return this.duration; |
| 2076 | + } |
| 2077 | + |
| 2078 | + Period getPeriod() { |
| 2079 | + return this.period; |
| 2080 | + } |
| 2081 | + |
| 2082 | + } |
| 2083 | + |
2010 | 2084 | @ConstructorBinding
|
2011 | 2085 | @ConfigurationProperties(prefix = "test")
|
2012 | 2086 | @Validated
|
@@ -2035,6 +2109,11 @@ static class ConstructorParameterWithUnitConfiguration {
|
2035 | 2109 |
|
2036 | 2110 | }
|
2037 | 2111 |
|
| 2112 | + @EnableConfigurationProperties(ConstructorParameterWithFormatProperties.class) |
| 2113 | + static class ConstructorParameterWithFormatConfiguration { |
| 2114 | + |
| 2115 | + } |
| 2116 | + |
2038 | 2117 | @EnableConfigurationProperties(ConstructorParameterValidatedProperties.class)
|
2039 | 2118 | static class ConstructorParameterValidationConfiguration {
|
2040 | 2119 |
|
|
0 commit comments