22
22
import java .util .List ;
23
23
import java .util .Map ;
24
24
25
+ import com .zaxxer .hikari .HikariDataSource ;
25
26
import org .junit .After ;
26
27
import org .junit .Before ;
27
28
import org .junit .Test ;
@@ -100,8 +101,8 @@ public void testNestedNaming() throws Exception {
100
101
101
102
@ Test
102
103
@ SuppressWarnings ("unchecked" )
103
- public void testCycle () throws Exception {
104
- this .context .register (CycleConfig .class );
104
+ public void testSelfReferentialProperty () throws Exception {
105
+ this .context .register (SelfReferentialConfig .class );
105
106
EnvironmentTestUtils .addEnvironment (this .context , "foo.name:foo" );
106
107
this .context .refresh ();
107
108
ConfigurationPropertiesReportEndpoint report = this .context
@@ -114,8 +115,30 @@ public void testCycle() throws Exception {
114
115
Map <String , Object > map = (Map <String , Object >) nestedProperties
115
116
.get ("properties" );
116
117
assertThat (map ).isNotNull ();
117
- assertThat (map ).hasSize (1 );
118
- assertThat (map .get ("error" )).isEqualTo ("Cannot serialize 'foo'" );
118
+ assertThat (map ).containsOnlyKeys ("bar" , "name" );
119
+ assertThat (map ).containsEntry ("name" , "foo" );
120
+ Map <String , Object > bar = (Map <String , Object >) map .get ("bar" );
121
+ assertThat (bar ).containsOnlyKeys ("name" );
122
+ assertThat (bar ).containsEntry ("name" , "123456" );
123
+ }
124
+
125
+ @ Test
126
+ @ SuppressWarnings ("unchecked" )
127
+ public void testCycle () {
128
+ this .context .register (CycleConfig .class );
129
+ this .context .refresh ();
130
+ ConfigurationPropertiesReportEndpoint report = this .context
131
+ .getBean (ConfigurationPropertiesReportEndpoint .class );
132
+ Map <String , Object > properties = report .invoke ();
133
+ Map <String , Object > nestedProperties = (Map <String , Object >) properties
134
+ .get ("cycle" );
135
+ assertThat (nestedProperties ).isNotNull ();
136
+ assertThat (nestedProperties .get ("prefix" )).isEqualTo ("cycle" );
137
+ Map <String , Object > map = (Map <String , Object >) nestedProperties
138
+ .get ("properties" );
139
+ assertThat (map ).isNotNull ();
140
+ assertThat (map ).containsOnlyKeys ("error" );
141
+ assertThat (map ).containsEntry ("error" , "Cannot serialize 'cycle'" );
119
142
}
120
143
121
144
@ Test
@@ -149,7 +172,6 @@ public void testEmptyMapIsNotAdded() throws Exception {
149
172
Map <String , Object > nestedProperties = (Map <String , Object >) properties
150
173
.get ("foo" );
151
174
assertThat (nestedProperties ).isNotNull ();
152
- System .err .println (nestedProperties );
153
175
assertThat (nestedProperties .get ("prefix" )).isEqualTo ("foo" );
154
176
Map <String , Object > map = (Map <String , Object >) nestedProperties
155
177
.get ("properties" );
@@ -190,7 +212,6 @@ public void testInetAddress() throws Exception {
190
212
Map <String , Object > nestedProperties = (Map <String , Object >) properties
191
213
.get ("foo" );
192
214
assertThat (nestedProperties ).isNotNull ();
193
- System .err .println (nestedProperties );
194
215
assertThat (nestedProperties .get ("prefix" )).isEqualTo ("foo" );
195
216
Map <String , Object > map = (Map <String , Object >) nestedProperties
196
217
.get ("properties" );
@@ -223,6 +244,20 @@ public void testInitializedMapAndList() throws Exception {
223
244
assertThat (list ).containsExactly ("abc" );
224
245
}
225
246
247
+ @ Test
248
+ @ SuppressWarnings ("unchecked" )
249
+ public void hikariDataSourceConfigurationPropertiesBeanCanBeSerialized () {
250
+ this .context = new AnnotationConfigApplicationContext ();
251
+ this .context .register (HikariDataSourceConfig .class );
252
+ this .context .refresh ();
253
+ ConfigurationPropertiesReportEndpoint endpoint = this .context
254
+ .getBean (ConfigurationPropertiesReportEndpoint .class );
255
+ Map <String , Object > properties = endpoint .invoke ();
256
+ Map <String , Object > nestedProperties = (Map <String , Object >) ((Map <String , Object >) properties
257
+ .get ("hikariDataSource" )).get ("properties" );
258
+ assertThat (nestedProperties ).doesNotContainKey ("error" );
259
+ }
260
+
226
261
@ Configuration
227
262
@ EnableConfigurationProperties
228
263
public static class Base {
@@ -248,24 +283,12 @@ public Foo foo() {
248
283
249
284
@ Configuration
250
285
@ Import (Base .class )
251
- public static class CycleConfig {
286
+ public static class SelfReferentialConfig {
252
287
253
288
@ Bean
254
289
@ ConfigurationProperties (prefix = "foo" )
255
- public Cycle foo () {
256
- return new Cycle ();
257
- }
258
-
259
- }
260
-
261
- @ Configuration
262
- @ Import (Base .class )
263
- public static class MetadataCycleConfig {
264
-
265
- @ Bean
266
- @ ConfigurationProperties (prefix = "bar" )
267
- public Cycle foo () {
268
- return new Cycle ();
290
+ public SelfReferential foo () {
291
+ return new SelfReferential ();
269
292
}
270
293
271
294
}
@@ -373,11 +396,11 @@ public void setName(String name) {
373
396
374
397
}
375
398
376
- public static class Cycle extends Foo {
399
+ public static class SelfReferential extends Foo {
377
400
378
401
private Foo self ;
379
402
380
- public Cycle () {
403
+ public SelfReferential () {
381
404
this .self = this ;
382
405
}
383
406
@@ -449,4 +472,57 @@ public List<String> getList() {
449
472
450
473
}
451
474
475
+ static class Cycle {
476
+
477
+ private final Alpha alpha = new Alpha (this );
478
+
479
+ public Alpha getAlpha () {
480
+ return this .alpha ;
481
+ }
482
+
483
+ static class Alpha {
484
+
485
+ private final Cycle cycle ;
486
+
487
+ Alpha (Cycle cycle ) {
488
+ this .cycle = cycle ;
489
+ }
490
+
491
+ public Cycle getCycle () {
492
+ return this .cycle ;
493
+ }
494
+
495
+ }
496
+
497
+ }
498
+
499
+ @ Configuration
500
+ @ Import (Base .class )
501
+ static class CycleConfig {
502
+
503
+ @ Bean
504
+ @ ConfigurationProperties (prefix = "cycle" )
505
+ public Cycle cycle () {
506
+ return new Cycle ();
507
+ }
508
+
509
+ }
510
+
511
+ @ Configuration
512
+ @ EnableConfigurationProperties
513
+ static class HikariDataSourceConfig {
514
+
515
+ @ Bean
516
+ public ConfigurationPropertiesReportEndpoint endpoint () {
517
+ return new ConfigurationPropertiesReportEndpoint ();
518
+ }
519
+
520
+ @ Bean
521
+ @ ConfigurationProperties (prefix = "test.datasource" )
522
+ public HikariDataSource hikariDataSource () {
523
+ return new HikariDataSource ();
524
+ }
525
+
526
+ }
527
+
452
528
}
0 commit comments