|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 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.
|
|
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;
|
@@ -357,6 +369,30 @@ void bindToAnnotationNamedParameter() {
|
357 | 369 | assertThat(bound.getImportName()).isEqualTo("test");
|
358 | 370 | }
|
359 | 371 |
|
| 372 | + @Test |
| 373 | + @EnabledForJreRange(min = JRE.JAVA_16) |
| 374 | + void bindToRecordWithDefaultValue(@TempDir File tempDir) throws IOException, ClassNotFoundException { |
| 375 | + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); |
| 376 | + source.put("test.record.property1", "value-from-config-1"); |
| 377 | + this.sources.add(source); |
| 378 | + File recordProperties = new File(tempDir, "RecordProperties.java"); |
| 379 | + try (PrintWriter writer = new PrintWriter(new FileWriter(recordProperties))) { |
| 380 | + writer.println("public record RecordProperties("); |
| 381 | + writer.println( |
| 382 | + "@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-1\") String property1,"); |
| 383 | + writer.println( |
| 384 | + "@org.springframework.boot.context.properties.bind.DefaultValue(\"default-value-2\") String property2"); |
| 385 | + writer.println(") {"); |
| 386 | + writer.println("}"); |
| 387 | + } |
| 388 | + TestCompiler compiler = new TestCompiler(tempDir); |
| 389 | + compiler.getTask(Arrays.asList(recordProperties)).call(); |
| 390 | + ClassLoader ucl = new URLClassLoader(new URL[] { tempDir.toURI().toURL() }); |
| 391 | + Object bean = this.binder.bind("test.record", Class.forName("RecordProperties", true, ucl)).get(); |
| 392 | + assertThat(ReflectionTestUtils.getField(bean, "property1")).isEqualTo("value-from-config-1"); |
| 393 | + assertThat(ReflectionTestUtils.getField(bean, "property2")).isEqualTo("default-value-2"); |
| 394 | + } |
| 395 | + |
360 | 396 | private void noConfigurationProperty(BindException ex) {
|
361 | 397 | assertThat(ex.getProperty()).isNull();
|
362 | 398 | }
|
|
0 commit comments