31
31
import org .springframework .boot .actuate .context .properties .ConfigurationPropertiesReportEndpoint .ConfigurationPropertiesBeanDescriptor ;
32
32
import org .springframework .boot .actuate .context .properties .ConfigurationPropertiesReportEndpoint .ContextConfigurationProperties ;
33
33
import org .springframework .boot .context .properties .ConfigurationProperties ;
34
+ import org .springframework .boot .context .properties .ConstructorBinding ;
34
35
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
36
+ import org .springframework .boot .context .properties .bind .DefaultValue ;
35
37
import org .springframework .boot .test .context .assertj .AssertableApplicationContext ;
36
38
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
37
39
import org .springframework .boot .test .context .runner .ContextConsumer ;
40
42
import org .springframework .core .env .Environment ;
41
43
42
44
import static org .assertj .core .api .Assertions .assertThat ;
45
+ import static org .assertj .core .api .Assertions .entry ;
43
46
44
47
/**
45
48
* Tests for {@link ConfigurationPropertiesReportEndpoint}.
@@ -55,11 +58,35 @@ class ConfigurationPropertiesReportEndpointTests {
55
58
.withUserConfiguration (EndpointConfig .class );
56
59
57
60
@ Test
58
- void descriptorDetectsRelevantProperties () {
61
+ void descriptorWithJavaBeanBindMethodDetectsRelevantProperties () {
59
62
this .contextRunner .withUserConfiguration (TestPropertiesConfiguration .class ).run (assertProperties ("test" ,
60
63
(properties ) -> assertThat (properties ).containsOnlyKeys ("dbPassword" , "myTestProperty" , "duration" )));
61
64
}
62
65
66
+ @ Test
67
+ void descriptorWithValueObjectBindMethodDetectsRelevantProperties () {
68
+ this .contextRunner .withUserConfiguration (ImmutablePropertiesConfiguration .class ).run (assertProperties (
69
+ "immutable" ,
70
+ (properties ) -> assertThat (properties ).containsOnlyKeys ("dbPassword" , "myTestProperty" , "duration" )));
71
+ }
72
+
73
+ @ Test
74
+ void descriptorWithValueObjectBindMethodUseDedicatedConstructor () {
75
+ this .contextRunner .withUserConfiguration (MultiConstructorPropertiesConfiguration .class ).run (assertProperties (
76
+ "multiconstructor" , (properties ) -> assertThat (properties ).containsOnly (entry ("name" , "test" ))));
77
+ }
78
+
79
+ @ Test
80
+ void descriptorWithValueObjectBindMethodHandleNestedType () {
81
+ this .contextRunner .withPropertyValues ("immutablenested.nested.name=nested" , "immutablenested.nested.counter=42" )
82
+ .withUserConfiguration (ImmutableNestedPropertiesConfiguration .class )
83
+ .run (assertProperties ("immutablenested" , (properties ) -> {
84
+ assertThat (properties ).containsOnlyKeys ("name" , "nested" );
85
+ Map <String , Object > nested = (Map <String , Object >) properties .get ("nested" );
86
+ assertThat (nested ).containsOnly (entry ("name" , "nested" ), entry ("counter" , 42 ));
87
+ }));
88
+ }
89
+
63
90
@ Test
64
91
void descriptorDoesNotIncludePropertyWithNullValue () {
65
92
this .contextRunner .withUserConfiguration (TestPropertiesConfiguration .class )
@@ -236,6 +263,8 @@ public static class TestProperties {
236
263
237
264
private Duration duration = Duration .ofSeconds (10 );
238
265
266
+ private String ignored = "dummy" ;
267
+
239
268
public String getDbPassword () {
240
269
return this .dbPassword ;
241
270
}
@@ -268,6 +297,146 @@ public void setDuration(Duration duration) {
268
297
this .duration = duration ;
269
298
}
270
299
300
+ public String getIgnored () {
301
+ return this .ignored ;
302
+ }
303
+
304
+ }
305
+
306
+ @ Configuration (proxyBeanMethods = false )
307
+ @ EnableConfigurationProperties (ImmutableProperties .class )
308
+ static class ImmutablePropertiesConfiguration {
309
+
310
+ }
311
+
312
+ @ ConfigurationProperties (prefix = "immutable" )
313
+ @ ConstructorBinding
314
+ public static class ImmutableProperties {
315
+
316
+ private final String dbPassword ;
317
+
318
+ private final String myTestProperty ;
319
+
320
+ private final String nullValue ;
321
+
322
+ private final Duration duration ;
323
+
324
+ private final String ignored ;
325
+
326
+ ImmutableProperties (@ DefaultValue ("123456" ) String dbPassword , @ DefaultValue ("654321" ) String myTestProperty ,
327
+ String nullValue , @ DefaultValue ("10s" ) Duration duration ) {
328
+ this .dbPassword = dbPassword ;
329
+ this .myTestProperty = myTestProperty ;
330
+ this .nullValue = nullValue ;
331
+ this .duration = duration ;
332
+ this .ignored = "dummy" ;
333
+ }
334
+
335
+ public String getDbPassword () {
336
+ return this .dbPassword ;
337
+ }
338
+
339
+ public String getMyTestProperty () {
340
+ return this .myTestProperty ;
341
+ }
342
+
343
+ public String getNullValue () {
344
+ return this .nullValue ;
345
+ }
346
+
347
+ public Duration getDuration () {
348
+ return this .duration ;
349
+ }
350
+
351
+ public String getIgnored () {
352
+ return this .ignored ;
353
+ }
354
+
355
+ }
356
+
357
+ @ Configuration (proxyBeanMethods = false )
358
+ @ EnableConfigurationProperties (MultiConstructorProperties .class )
359
+ static class MultiConstructorPropertiesConfiguration {
360
+
361
+ }
362
+
363
+ @ ConfigurationProperties (prefix = "multiconstructor" )
364
+ @ ConstructorBinding
365
+ public static class MultiConstructorProperties {
366
+
367
+ private final String name ;
368
+
369
+ private final int counter ;
370
+
371
+ MultiConstructorProperties (String name , int counter ) {
372
+ this .name = name ;
373
+ this .counter = counter ;
374
+ }
375
+
376
+ @ ConstructorBinding
377
+ MultiConstructorProperties (@ DefaultValue ("test" ) String name ) {
378
+ this .name = name ;
379
+ this .counter = 42 ;
380
+ }
381
+
382
+ public String getName () {
383
+ return this .name ;
384
+ }
385
+
386
+ public int getCounter () {
387
+ return this .counter ;
388
+ }
389
+
390
+ }
391
+
392
+ @ Configuration (proxyBeanMethods = false )
393
+ @ EnableConfigurationProperties (ImmutableNestedProperties .class )
394
+ static class ImmutableNestedPropertiesConfiguration {
395
+
396
+ }
397
+
398
+ @ ConfigurationProperties ("immutablenested" )
399
+ @ ConstructorBinding
400
+ public static class ImmutableNestedProperties {
401
+
402
+ private final String name ;
403
+
404
+ private final Nested nested ;
405
+
406
+ ImmutableNestedProperties (@ DefaultValue ("parent" ) String name , Nested nested ) {
407
+ this .name = name ;
408
+ this .nested = nested ;
409
+ }
410
+
411
+ public String getName () {
412
+ return this .name ;
413
+ }
414
+
415
+ public Nested getNested () {
416
+ return this .nested ;
417
+ }
418
+
419
+ public static class Nested {
420
+
421
+ private final String name ;
422
+
423
+ private final int counter ;
424
+
425
+ Nested (String name , int counter ) {
426
+ this .name = name ;
427
+ this .counter = counter ;
428
+ }
429
+
430
+ public String getName () {
431
+ return this .name ;
432
+ }
433
+
434
+ public int getCounter () {
435
+ return this .counter ;
436
+ }
437
+
438
+ }
439
+
271
440
}
272
441
273
442
@ Configuration (proxyBeanMethods = false )
0 commit comments