|
16 | 16 |
|
17 | 17 | package org.springframework.boot.context.properties.bind;
|
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.FileWriter; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.PrintWriter; |
19 | 23 | import java.lang.reflect.Constructor;
|
| 24 | +import java.net.URL; |
| 25 | +import java.net.URLClassLoader; |
20 | 26 | import java.nio.file.Path;
|
21 | 27 | import java.nio.file.Paths;
|
22 | 28 | import java.time.LocalDate;
|
23 | 29 | import java.util.ArrayList;
|
| 30 | +import java.util.Arrays; |
24 | 31 | import java.util.List;
|
25 | 32 | import java.util.Map;
|
26 | 33 | import java.util.Objects;
|
27 | 34 |
|
28 | 35 | import org.junit.jupiter.api.Test;
|
| 36 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 37 | +import org.junit.jupiter.api.condition.JRE; |
| 38 | +import org.junit.jupiter.api.io.TempDir; |
29 | 39 |
|
30 | 40 | import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
31 | 41 | import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
32 | 42 | import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
| 43 | +import org.springframework.boot.testsupport.compiler.TestCompiler; |
33 | 44 | import org.springframework.core.ResolvableType;
|
34 | 45 | import org.springframework.core.convert.ConversionService;
|
35 | 46 | import org.springframework.format.annotation.DateTimeFormat;
|
| 47 | +import org.springframework.test.util.ReflectionTestUtils; |
36 | 48 | import org.springframework.util.Assert;
|
37 | 49 |
|
38 | 50 | import static org.assertj.core.api.Assertions.assertThat;
|
|
43 | 55 | *
|
44 | 56 | * @author Madhura Bhave
|
45 | 57 | * @author Phillip Webb
|
| 58 | + * @author Pavel Anisimov |
46 | 59 | */
|
47 | 60 | class ValueObjectBinderTests {
|
48 | 61 |
|
@@ -357,6 +370,30 @@ void bindToAnnotationNamedParameter() {
|
357 | 370 | assertThat(bound.getImportName()).isEqualTo("test");
|
358 | 371 | }
|
359 | 372 |
|
| 373 | + @Test |
| 374 | + @EnabledForJreRange(min = JRE.JAVA_16) |
| 375 | + void bindToRecordWithDefaultValue(@TempDir File tempDir) throws IOException, ClassNotFoundException { |
| 376 | + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); |
| 377 | + source.put("test.record.property1", "value-from-config-1"); |
| 378 | + this.sources.add(source); |
| 379 | + File recordProperties = new File(tempDir, "RecordProperties.java"); |
| 380 | + try (PrintWriter writer = new PrintWriter(new FileWriter(recordProperties))) { |
| 381 | + writer.println("public record RecordProperties("); |
| 382 | + writer.println( |
| 383 | + "@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-1\") String property1,"); |
| 384 | + writer.println( |
| 385 | + "@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-2\") String property2"); |
| 386 | + writer.println(") {"); |
| 387 | + writer.println("}"); |
| 388 | + } |
| 389 | + TestCompiler compiler = new TestCompiler(tempDir); |
| 390 | + compiler.getTask(Arrays.asList(recordProperties)).call(); |
| 391 | + ClassLoader ucl = new URLClassLoader(new URL[] { tempDir.toURI().toURL() }); |
| 392 | + Object bean = this.binder.bind("test.record", Class.forName("RecordProperties", true, ucl)).get(); |
| 393 | + assertThat(ReflectionTestUtils.getField(bean, "property1")).isEqualTo("value-from-config-1"); |
| 394 | + assertThat(ReflectionTestUtils.getField(bean, "property2")).isEqualTo("default-value-2"); |
| 395 | + } |
| 396 | + |
360 | 397 | private void noConfigurationProperty(BindException ex) {
|
361 | 398 | assertThat(ex.getProperty()).isNull();
|
362 | 399 | }
|
|
0 commit comments