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,30 @@ 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 .withUserConfiguration (ImmutableNestedPropertiesConfiguration .class ).run (assertProperties (
82
+ "immutablenested" , (properties ) -> assertThat (properties ).containsOnlyKeys ("name" , "nested" )));
83
+ }
84
+
63
85
@ Test
64
86
void descriptorDoesNotIncludePropertyWithNullValue () {
65
87
this .contextRunner .withUserConfiguration (TestPropertiesConfiguration .class )
@@ -236,6 +258,8 @@ public static class TestProperties {
236
258
237
259
private Duration duration = Duration .ofSeconds (10 );
238
260
261
+ private String ignored = "dummy" ;
262
+
239
263
public String getDbPassword () {
240
264
return this .dbPassword ;
241
265
}
@@ -268,6 +292,146 @@ public void setDuration(Duration duration) {
268
292
this .duration = duration ;
269
293
}
270
294
295
+ public String getIgnored () {
296
+ return this .ignored ;
297
+ }
298
+
299
+ }
300
+
301
+ @ Configuration (proxyBeanMethods = false )
302
+ @ EnableConfigurationProperties (ImmutableProperties .class )
303
+ static class ImmutablePropertiesConfiguration {
304
+
305
+ }
306
+
307
+ @ ConfigurationProperties (prefix = "immutable" )
308
+ @ ConstructorBinding
309
+ public static class ImmutableProperties {
310
+
311
+ private final String dbPassword ;
312
+
313
+ private final String myTestProperty ;
314
+
315
+ private final String nullValue ;
316
+
317
+ private final Duration duration ;
318
+
319
+ private final String ignored ;
320
+
321
+ ImmutableProperties (@ DefaultValue ("123456" ) String dbPassword , @ DefaultValue ("654321" ) String myTestProperty ,
322
+ String nullValue , @ DefaultValue ("10s" ) Duration duration ) {
323
+ this .dbPassword = dbPassword ;
324
+ this .myTestProperty = myTestProperty ;
325
+ this .nullValue = nullValue ;
326
+ this .duration = duration ;
327
+ this .ignored = "dummy" ;
328
+ }
329
+
330
+ public String getDbPassword () {
331
+ return this .dbPassword ;
332
+ }
333
+
334
+ public String getMyTestProperty () {
335
+ return this .myTestProperty ;
336
+ }
337
+
338
+ public String getNullValue () {
339
+ return this .nullValue ;
340
+ }
341
+
342
+ public Duration getDuration () {
343
+ return this .duration ;
344
+ }
345
+
346
+ public String getIgnored () {
347
+ return this .ignored ;
348
+ }
349
+
350
+ }
351
+
352
+ @ Configuration (proxyBeanMethods = false )
353
+ @ EnableConfigurationProperties (MultiConstructorProperties .class )
354
+ static class MultiConstructorPropertiesConfiguration {
355
+
356
+ }
357
+
358
+ @ ConfigurationProperties (prefix = "multiconstructor" )
359
+ @ ConstructorBinding
360
+ public static class MultiConstructorProperties {
361
+
362
+ private final String name ;
363
+
364
+ private final int counter ;
365
+
366
+ MultiConstructorProperties (String name , int counter ) {
367
+ this .name = name ;
368
+ this .counter = counter ;
369
+ }
370
+
371
+ @ ConstructorBinding
372
+ MultiConstructorProperties (@ DefaultValue ("test" ) String name ) {
373
+ this .name = name ;
374
+ this .counter = 42 ;
375
+ }
376
+
377
+ public String getName () {
378
+ return this .name ;
379
+ }
380
+
381
+ public int getCounter () {
382
+ return this .counter ;
383
+ }
384
+
385
+ }
386
+
387
+ @ Configuration (proxyBeanMethods = false )
388
+ @ EnableConfigurationProperties (ImmutableNestedProperties .class )
389
+ static class ImmutableNestedPropertiesConfiguration {
390
+
391
+ }
392
+
393
+ @ ConfigurationProperties ("immutablenested" )
394
+ @ ConstructorBinding
395
+ public static class ImmutableNestedProperties {
396
+
397
+ private final String name ;
398
+
399
+ private final Nested nested ;
400
+
401
+ ImmutableNestedProperties (@ DefaultValue ("parent" ) String name , Nested nested ) {
402
+ this .name = name ;
403
+ this .nested = nested ;
404
+ }
405
+
406
+ public String getName () {
407
+ return this .name ;
408
+ }
409
+
410
+ public Nested getNested () {
411
+ return this .nested ;
412
+ }
413
+
414
+ public static class Nested {
415
+
416
+ private final String name ;
417
+
418
+ private final int counter ;
419
+
420
+ Nested (@ DefaultValue ("nested" ) String name , int counter ) {
421
+ this .name = name ;
422
+ this .counter = counter ;
423
+ }
424
+
425
+ public String getName () {
426
+ return this .name ;
427
+ }
428
+
429
+ public int getCounter () {
430
+ return this .counter ;
431
+ }
432
+
433
+ }
434
+
271
435
}
272
436
273
437
@ Configuration (proxyBeanMethods = false )
0 commit comments