-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
JavaBeanBinderTests.java
1282 lines (994 loc) · 35.3 KB
/
JavaBeanBinderTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties.bind;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.JavaBeanBinder.Bean;
import org.springframework.boot.context.properties.bind.JavaBeanBinder.BeanProperty;
import org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.boot.convert.Delimiter;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.annotation.DateTimeFormat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.entry;
/**
* Tests for {@link JavaBeanBinder}.
*
* @author Phillip Webb
* @author Madhura Bhave
* @author Andy Wilkinson
* @author Lasse Wulff
*/
class JavaBeanBinderTests {
private final List<ConfigurationPropertySource> sources = new ArrayList<>();
private final Binder binder = new Binder(this.sources);
@Test
void bindToClassShouldCreateBoundBean() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.int-value", "12");
source.put("foo.long-value", "34");
source.put("foo.string-value", "foo");
source.put("foo.enum-value", "foo-bar");
this.sources.add(source);
ExampleValueBean bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class)).get();
assertThat(bean.getIntValue()).isEqualTo(12);
assertThat(bean.getLongValue()).isEqualTo(34);
assertThat(bean.getStringValue()).isEqualTo("foo");
assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
@Test
void bindRenamedPropertyToClassBean() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("renamed.public", "alpha");
this.sources.add(source);
ExampleRenamedPropertyBean bean = this.binder.bind("renamed", Bindable.of(ExampleRenamedPropertyBean.class))
.get();
assertThat(bean.getExampleProperty()).isEqualTo("alpha");
}
@Test
void bindRenamedPropertyToRecordBean() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("renamed.class", "alpha");
this.sources.add(source);
ExampleRenamedPropertyRecordBean bean = this.binder
.bind("renamed", Bindable.of(ExampleRenamedPropertyRecordBean.class))
.get();
assertThat(bean.exampleProperty()).isEqualTo("alpha");
}
@Test
void bindToClassWhenHasNoPrefixShouldCreateBoundBean() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("int-value", "12");
source.put("long-value", "34");
source.put("string-value", "foo");
source.put("enum-value", "foo-bar");
this.sources.add(source);
ExampleValueBean bean = this.binder.bind(ConfigurationPropertyName.of(""), Bindable.of(ExampleValueBean.class))
.get();
assertThat(bean.getIntValue()).isEqualTo(12);
assertThat(bean.getLongValue()).isEqualTo(34);
assertThat(bean.getStringValue()).isEqualTo("foo");
assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
@Test
void bindToInstanceShouldBindToInstance() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.int-value", "12");
source.put("foo.long-value", "34");
source.put("foo.string-value", "foo");
source.put("foo.enum-value", "foo-bar");
this.sources.add(source);
ExampleValueBean bean = new ExampleValueBean();
ExampleValueBean boundBean = this.binder
.bind("foo", Bindable.of(ExampleValueBean.class).withExistingValue(bean))
.get();
assertThat(boundBean).isSameAs(bean);
assertThat(bean.getIntValue()).isEqualTo(12);
assertThat(bean.getLongValue()).isEqualTo(34);
assertThat(bean.getStringValue()).isEqualTo("foo");
assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
@Test
void bindToInstanceWithNoPropertiesShouldReturnUnbound() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
this.sources.add(source);
ExampleDefaultsBean bean = new ExampleDefaultsBean();
BindResult<ExampleDefaultsBean> boundBean = this.binder.bind("foo",
Bindable.of(ExampleDefaultsBean.class).withExistingValue(bean));
assertThat(boundBean.isBound()).isFalse();
assertThat(bean.getFoo()).isEqualTo(123);
assertThat(bean.getBar()).isEqualTo(456);
}
@Test
void bindToClassShouldLeaveDefaults() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.bar", "999");
this.sources.add(source);
ExampleDefaultsBean bean = this.binder.bind("foo", Bindable.of(ExampleDefaultsBean.class)).get();
assertThat(bean.getFoo()).isEqualTo(123);
assertThat(bean.getBar()).isEqualTo(999);
}
@Test
void bindToExistingInstanceShouldLeaveDefaults() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.bar", "999");
this.sources.add(source);
ExampleDefaultsBean bean = new ExampleDefaultsBean();
bean.setFoo(888);
ExampleDefaultsBean boundBean = this.binder
.bind("foo", Bindable.of(ExampleDefaultsBean.class).withExistingValue(bean))
.get();
assertThat(boundBean).isSameAs(bean);
assertThat(bean.getFoo()).isEqualTo(888);
assertThat(bean.getBar()).isEqualTo(999);
}
@Test
void bindToClassShouldBindToMap() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.map.foo-bar", "1");
source.put("foo.map.bar-baz", "2");
this.sources.add(source);
ExampleMapBean bean = this.binder.bind("foo", Bindable.of(ExampleMapBean.class)).get();
assertThat(bean.getMap()).containsExactly(entry(ExampleEnum.FOO_BAR, 1), entry(ExampleEnum.BAR_BAZ, 2));
}
@Test
void bindToClassShouldBindToList() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.list[0]", "foo-bar");
source.put("foo.list[1]", "bar-baz");
this.sources.add(source);
ExampleListBean bean = this.binder.bind("foo", Bindable.of(ExampleListBean.class)).get();
assertThat(bean.getList()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToListIfUnboundElementsPresentShouldThrowException() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.list[0]", "foo-bar");
source.put("foo.list[2]", "bar-baz");
this.sources.add(source);
assertThatExceptionOfType(BindException.class)
.isThrownBy(() -> this.binder.bind("foo", Bindable.of(ExampleListBean.class)))
.withCauseInstanceOf(UnboundConfigurationPropertiesException.class);
}
@Test
void bindToClassShouldBindToSet() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.set[0]", "foo-bar");
source.put("foo.set[1]", "bar-baz");
this.sources.add(source);
ExampleSetBean bean = this.binder.bind("foo", Bindable.of(ExampleSetBean.class)).get();
assertThat(bean.getSet()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassShouldBindToCollection() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.collection[0]", "foo-bar");
source.put("foo.collection[1]", "bar-baz");
this.sources.add(source);
ExampleCollectionBean bean = this.binder.bind("foo", Bindable.of(ExampleCollectionBean.class)).get();
assertThat(bean.getCollection()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassShouldBindToCollectionWithDelimiter() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.collection", "foo-bar|bar-baz");
this.sources.add(source);
ExampleCollectionBeanWithDelimiter bean = this.binder
.bind("foo", Bindable.of(ExampleCollectionBeanWithDelimiter.class))
.get();
assertThat(bean.getCollection()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassWhenHasNoSetterShouldBindToMap() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.map.foo-bar", "1");
source.put("foo.map.bar-baz", "2");
this.sources.add(source);
ExampleMapBeanWithoutSetter bean = this.binder.bind("foo", Bindable.of(ExampleMapBeanWithoutSetter.class))
.get();
assertThat(bean.getMap()).containsExactly(entry(ExampleEnum.FOO_BAR, 1), entry(ExampleEnum.BAR_BAZ, 2));
}
@Test
void bindToClassWhenHasNoSetterShouldBindToList() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.list[0]", "foo-bar");
source.put("foo.list[1]", "bar-baz");
this.sources.add(source);
ExampleListBeanWithoutSetter bean = this.binder.bind("foo", Bindable.of(ExampleListBeanWithoutSetter.class))
.get();
assertThat(bean.getList()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassWhenHasNoSetterShouldBindToSet() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.set[0]", "foo-bar");
source.put("foo.set[1]", "bar-baz");
this.sources.add(source);
ExampleSetBeanWithoutSetter bean = this.binder.bind("foo", Bindable.of(ExampleSetBeanWithoutSetter.class))
.get();
assertThat(bean.getSet()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassWhenHasNoSetterShouldBindToCollection() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.collection[0]", "foo-bar");
source.put("foo.collection[1]", "bar-baz");
this.sources.add(source);
ExampleCollectionBeanWithoutSetter bean = this.binder
.bind("foo", Bindable.of(ExampleCollectionBeanWithoutSetter.class))
.get();
assertThat(bean.getCollection()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
@Test
void bindToClassShouldBindNested() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value-bean.int-value", "123");
source.put("foo.value-bean.string-value", "foo");
this.sources.add(source);
ExampleNestedBean bean = this.binder.bind("foo", Bindable.of(ExampleNestedBean.class)).get();
assertThat(bean.getValueBean().getIntValue()).isEqualTo(123);
assertThat(bean.getValueBean().getStringValue()).isEqualTo("foo");
}
@Test
void bindToClassWhenIterableShouldBindNestedBasedOnInstance() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value-bean.int-value", "123");
source.put("foo.value-bean.string-value", "foo");
this.sources.add(source);
ExampleNestedBeanWithoutSetterOrType bean = this.binder
.bind("foo", Bindable.of(ExampleNestedBeanWithoutSetterOrType.class))
.get();
ExampleValueBean valueBean = (ExampleValueBean) bean.getValueBean();
assertThat(valueBean.getIntValue()).isEqualTo(123);
assertThat(valueBean.getStringValue()).isEqualTo("foo");
}
@Test
void bindToClassWhenNotIterableShouldNotBindNestedBasedOnInstance() {
// If we can't tell that binding will happen, we don't want to randomly invoke
// getters on the class and cause side effects
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value-bean.int-value", "123");
source.put("foo.value-bean.string-value", "foo");
this.sources.add(source.nonIterable());
BindResult<ExampleNestedBeanWithoutSetterOrType> bean = this.binder.bind("foo",
Bindable.of(ExampleNestedBeanWithoutSetterOrType.class));
assertThat(bean.isBound()).isFalse();
}
@Test
void bindToClassWhenHasNoSetterShouldBindNested() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value-bean.int-value", "123");
source.put("foo.value-bean.string-value", "foo");
this.sources.add(source);
ExampleNestedBeanWithoutSetter bean = this.binder.bind("foo", Bindable.of(ExampleNestedBeanWithoutSetter.class))
.get();
assertThat(bean.getValueBean().getIntValue()).isEqualTo(123);
assertThat(bean.getValueBean().getStringValue()).isEqualTo("foo");
}
@Test
void bindToClassWhenHasNoSetterAndImmutableShouldThrowException() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.nested.foo", "bar");
this.sources.add(source);
assertThatExceptionOfType(BindException.class)
.isThrownBy(() -> this.binder.bind("foo", Bindable.of(ExampleImmutableNestedBeanWithoutSetter.class)));
}
@Test
void bindToInstanceWhenNoNestedShouldLeaveNestedAsNull() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("faf.value-bean.int-value", "123");
this.sources.add(source);
ExampleNestedBean bean = new ExampleNestedBean();
BindResult<ExampleNestedBean> boundBean = this.binder.bind("foo",
Bindable.of(ExampleNestedBean.class).withExistingValue(bean));
assertThat(boundBean.isBound()).isFalse();
assertThat(bean.getValueBean()).isNull();
}
@Test
void bindToClassWithOverriddenPropertyShouldSetSubclassProperty() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value-bean.int-value", "123");
source.put("foo.value-bean.sub-int-value", "456");
this.sources.add(source);
ExampleNestedSubclassBean bean = this.binder.bind("foo", Bindable.of(ExampleNestedSubclassBean.class)).get();
assertThat(bean.getValueBean()).isNotNull();
assertThat(bean.getValueBean().getIntValue()).isEqualTo(123);
assertThat(bean.getValueBean().getSubIntValue()).isEqualTo(456);
}
@Test
void bindToClassWhenPropertiesMissingShouldReturnUnbound() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("faf.int-value", "12");
this.sources.add(source);
BindResult<ExampleValueBean> bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class));
assertThat(bean.isBound()).isFalse();
}
@Test
void bindToClassWhenNoDefaultConstructorShouldBind() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "bar");
this.sources.add(source);
BindResult<ExampleWithNonDefaultConstructor> bean = this.binder.bind("foo",
Bindable.of(ExampleWithNonDefaultConstructor.class));
assertThat(bean.isBound()).isTrue();
ExampleWithNonDefaultConstructor boundBean = bean.get();
assertThat(boundBean.getValue()).isEqualTo("bar");
}
@Test
void bindToInstanceWhenNoDefaultConstructorShouldBind() {
Binder binder = new Binder(this.sources, null, (ConversionService) null, null, null,
(bindable, isNestedConstructorBinding) -> null);
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "bar");
this.sources.add(source);
ExampleWithNonDefaultConstructor bean = new ExampleWithNonDefaultConstructor("faf");
ExampleWithNonDefaultConstructor boundBean = binder
.bind("foo", Bindable.of(ExampleWithNonDefaultConstructor.class).withExistingValue(bean))
.get();
assertThat(boundBean).isSameAs(bean);
assertThat(bean.getValue()).isEqualTo("bar");
}
@Test
void bindToClassShouldBindHierarchy() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.int-value", "123");
source.put("foo.long-value", "456");
this.sources.add(source);
ExampleSubclassBean bean = this.binder.bind("foo", Bindable.of(ExampleSubclassBean.class)).get();
assertThat(bean.getIntValue()).isEqualTo(123);
assertThat(bean.getLongValue()).isEqualTo(456);
}
@Test
void bindToClassWhenPropertyCannotBeConvertedShouldThrowException() {
this.sources.add(new MockConfigurationPropertySource("foo.int-value", "foo"));
assertThatExceptionOfType(BindException.class)
.isThrownBy(() -> this.binder.bind("foo", Bindable.of(ExampleValueBean.class)));
}
@Test
void bindToClassWhenPropertyCannotBeConvertedAndIgnoreErrorsShouldNotSetValue() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.int-value", "12");
source.put("foo.long-value", "bang");
source.put("foo.string-value", "foo");
source.put("foo.enum-value", "foo-bar");
this.sources.add(source);
IgnoreErrorsBindHandler handler = new IgnoreErrorsBindHandler();
ExampleValueBean bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class), handler).get();
assertThat(bean.getIntValue()).isEqualTo(12);
assertThat(bean.getLongValue()).isZero();
assertThat(bean.getStringValue()).isEqualTo("foo");
assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
@Test
void bindToClassWhenMismatchedGetSetShouldBind() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "123");
this.sources.add(source);
ExampleMismatchBean bean = this.binder.bind("foo", Bindable.of(ExampleMismatchBean.class)).get();
assertThat(bean.getValue()).isEqualTo("123");
}
@Test
void bindToClassShouldNotInvokeExtraMethods() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource("foo.value", "123");
this.sources.add(source.nonIterable());
ExampleWithThrowingGetters bean = this.binder.bind("foo", Bindable.of(ExampleWithThrowingGetters.class)).get();
assertThat(bean.getValue()).isEqualTo(123);
}
@Test
void bindToClassWithSelfReferenceShouldBind() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "123");
this.sources.add(source);
ExampleWithSelfReference bean = this.binder.bind("foo", Bindable.of(ExampleWithSelfReference.class)).get();
assertThat(bean.getValue()).isEqualTo(123);
}
@Test
void bindToInstanceWithExistingValueShouldReturnUnbound() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
this.sources.add(source);
ExampleNestedBean existingValue = new ExampleNestedBean();
ExampleValueBean valueBean = new ExampleValueBean();
existingValue.setValueBean(valueBean);
BindResult<ExampleNestedBean> result = this.binder.bind("foo",
Bindable.of(ExampleNestedBean.class).withExistingValue(existingValue));
assertThat(result.isBound()).isFalse();
}
@Test
void bindWithAnnotations() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.date", "2014-04-01");
this.sources.add(source);
ConverterAnnotatedExampleBean bean = this.binder.bind("foo", Bindable.of(ConverterAnnotatedExampleBean.class))
.get();
assertThat(bean.getDate()).hasToString("2014-04-01");
}
@Test
void bindWhenValueIsConvertedWithPropertyEditorShouldBind() {
// gh-12166
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value", "java.lang.RuntimeException");
this.sources.add(source);
ExampleWithPropertyEditorType bean = this.binder.bind("foo", Bindable.of(ExampleWithPropertyEditorType.class))
.get();
assertThat(bean.getValue()).isEqualTo(RuntimeException.class);
}
@Test
void bindToClassShouldIgnoreInvalidAccessors() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.name", "something");
this.sources.add(source);
ExampleWithInvalidAccessors bean = this.binder.bind("foo", Bindable.of(ExampleWithInvalidAccessors.class))
.get();
assertThat(bean.getName()).isEqualTo("something");
}
@Test
void bindToClassShouldIgnoreStaticAccessors() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.name", "invalid");
source.put("foo.counter", "42");
this.sources.add(source);
ExampleWithStaticAccessors bean = this.binder.bind("foo", Bindable.of(ExampleWithStaticAccessors.class)).get();
assertThat(ExampleWithStaticAccessors.name).isNull();
assertThat(bean.getCounter()).isEqualTo(42);
}
@Test
void bindToClassShouldCacheWithGenerics() {
// gh-16821
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.integers[a].value", "1");
source.put("foo.booleans[b].value", "true");
this.sources.add(source);
ExampleWithGenericMap bean = this.binder.bind("foo", Bindable.of(ExampleWithGenericMap.class)).get();
assertThat(bean.getIntegers().get("a").getValue()).isOne();
assertThat(bean.getBooleans().get("b").getValue()).isTrue();
}
@Test
void bindToClassWithOverloadedSetterShouldUseSetterThatMatchesField() {
// gh-16206
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.property", "some string");
this.sources.add(source);
PropertyWithOverloadedSetter bean = this.binder.bind("foo", Bindable.of(PropertyWithOverloadedSetter.class))
.get();
assertThat(bean.getProperty()).isEqualTo("some string");
}
@Test
void beanPropertiesPreferMatchingType() {
// gh-16206
ResolvableType type = ResolvableType.forClass(PropertyWithOverloadedSetter.class);
Bean<PropertyWithOverloadedSetter> bean = new Bean<>(type, type.resolve()) {
@Override
protected void addProperties(Method[] declaredMethods, Field[] declaredFields) {
// We override here because we need a specific order of the declared
// methods and the JVM doesn't give us one
int intSetter = -1;
int stringSetter = -1;
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i];
if (method.getName().equals("setProperty")) {
if (method.getParameters()[0].getType().equals(int.class)) {
intSetter = i;
}
else {
stringSetter = i;
}
}
}
if (intSetter > stringSetter) {
Method method = declaredMethods[intSetter];
declaredMethods[intSetter] = declaredMethods[stringSetter];
declaredMethods[stringSetter] = method;
}
super.addProperties(declaredMethods, declaredFields);
}
};
BeanProperty property = bean.getProperties().get("property");
PropertyWithOverloadedSetter target = new PropertyWithOverloadedSetter();
property.setValue(() -> target, "some string");
}
@Test
void bindOrCreateWithNestedShouldReturnCreatedValue() {
NestedJavaBean result = this.binder.bindOrCreate("foo", Bindable.of(NestedJavaBean.class));
assertThat(result.getNested().getBar()).isEqualTo(456);
}
@Test
void bindWhenHasPackagePrivateSetterShouldBind() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.property", "test");
this.sources.add(source);
PackagePrivateSetterBean bean = this.binder.bind("foo", Bindable.of(PackagePrivateSetterBean.class)).get();
assertThat(bean.getProperty()).isEqualTo("test");
}
@Test
void bindUsesConsistentPropertyOrder() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.gamma", "0");
source.put("foo.alpha", "0");
source.put("foo.beta", "0");
this.sources.add(source);
PropertyOrderBean bean = this.binder.bind("foo", Bindable.of(PropertyOrderBean.class)).get();
assertThat(bean.getAlpha()).isZero();
assertThat(bean.getBeta()).isOne();
assertThat(bean.getGamma()).isEqualTo(2);
}
@Test // gh-23007
void bindWhenBeanWithGetSetIsMethodsFoundUsesGetterThatMatchesSetter() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("test.names", "spring,boot");
this.sources.add(source);
JavaBeanWithGetSetIs bean = this.binder.bind("test", Bindable.of(JavaBeanWithGetSetIs.class)).get();
assertThat(bean.getNames()).containsExactly("spring", "boot");
}
@Test // gh-23007
void bindWhenBeanWithGetIsMethodsFoundDoesNotUseIsGetter() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("test.names", "spring,boot");
this.sources.add(source);
JavaBeanWithGetIs bean = this.binder.bind("test", Bindable.of(JavaBeanWithGetIs.class)).get();
assertThat(bean.getNames()).containsExactly("spring", "boot");
}
@Test // gh-33105
void bindWhenHasBridgeMethods() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("test.value", "spring-boot");
this.sources.add(source);
ApplicationConversionService conversionService = new ApplicationConversionService();
conversionService.addConverter(String.class, BridgeType.class, BridgeType::new);
Binder binder = new Binder(this.sources, null, conversionService);
BridgeMethods bean = binder.bind("test", Bindable.of(BridgeMethods.class)).get();
assertThat(bean.getValue()).hasToString("spring-boot");
}
static class ExampleValueBean {
private int intValue;
private long longValue;
private String stringValue;
private ExampleEnum enumValue;
int getIntValue() {
return this.intValue;
}
void setIntValue(int intValue) {
this.intValue = intValue;
}
long getLongValue() {
return this.longValue;
}
void setLongValue(long longValue) {
this.longValue = longValue;
}
String getStringValue() {
return this.stringValue;
}
void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
ExampleEnum getEnumValue() {
return this.enumValue;
}
void setEnumValue(ExampleEnum enumValue) {
this.enumValue = enumValue;
}
}
static class ExampleRenamedPropertyBean {
@Name("public")
private String exampleProperty;
String getExampleProperty() {
return this.exampleProperty;
}
void setExampleProperty(String exampleProperty) {
this.exampleProperty = exampleProperty;
}
}
record ExampleRenamedPropertyRecordBean(@Name("class") String exampleProperty) {
}
static class ExampleDefaultsBean {
private int foo = 123;
private int bar = 456;
int getFoo() {
return this.foo;
}
void setFoo(int foo) {
this.foo = foo;
}
int getBar() {
return this.bar;
}
void setBar(int bar) {
this.bar = bar;
}
}
static class ExampleMapBean {
private Map<ExampleEnum, Integer> map;
Map<ExampleEnum, Integer> getMap() {
return this.map;
}
void setMap(Map<ExampleEnum, Integer> map) {
this.map = map;
}
}
static class ExampleListBean {
private List<ExampleEnum> list;
List<ExampleEnum> getList() {
return this.list;
}
void setList(List<ExampleEnum> list) {
this.list = list;
}
}
static class ExampleSetBean {
private Set<ExampleEnum> set;
Set<ExampleEnum> getSet() {
return this.set;
}
void setSet(Set<ExampleEnum> set) {
this.set = set;
}
}
static class ExampleCollectionBean {
private Collection<ExampleEnum> collection;
Collection<ExampleEnum> getCollection() {
return this.collection;
}
void setCollection(Collection<ExampleEnum> collection) {
this.collection = collection;
}
}
static class ExampleMapBeanWithoutSetter {
private final Map<ExampleEnum, Integer> map = new LinkedHashMap<>();
Map<ExampleEnum, Integer> getMap() {
return this.map;
}
}
static class ExampleListBeanWithoutSetter {
private final List<ExampleEnum> list = new ArrayList<>();
List<ExampleEnum> getList() {
return this.list;
}
}
static class ExampleSetBeanWithoutSetter {
private final Set<ExampleEnum> set = new LinkedHashSet<>();
Set<ExampleEnum> getSet() {
return this.set;
}
}
static class ExampleCollectionBeanWithoutSetter {
private final Collection<ExampleEnum> collection = new ArrayList<>();
Collection<ExampleEnum> getCollection() {
return this.collection;
}
}
static class ExampleCollectionBeanWithDelimiter {
@Delimiter("|")
private Collection<ExampleEnum> collection;
Collection<ExampleEnum> getCollection() {
return this.collection;
}
void setCollection(Collection<ExampleEnum> collection) {
this.collection = collection;
}
}
static class ExampleNestedBean {
private ExampleValueBean valueBean;
ExampleValueBean getValueBean() {
return this.valueBean;
}
void setValueBean(ExampleValueBean valueBean) {
this.valueBean = valueBean;
}
}
static class ExampleNestedBeanWithoutSetter {
private final ExampleValueBean valueBean = new ExampleValueBean();
ExampleValueBean getValueBean() {
return this.valueBean;
}
}
static class ExampleNestedBeanWithoutSetterOrType {
private final ExampleValueBean valueBean = new ExampleValueBean();
Object getValueBean() {
return this.valueBean;
}
}
static class ExampleImmutableNestedBeanWithoutSetter {
private final NestedImmutable nested = new NestedImmutable();
NestedImmutable getNested() {
return this.nested;
}
static class NestedImmutable {
String getFoo() {
return "foo";
}
}
}
static class ExampleNestedSubclassBean extends ExampleNestedBean {
private ExampleValueSubclassBean valueBean;
@Override
ExampleValueSubclassBean getValueBean() {
return this.valueBean;
}
void setValueBean(ExampleValueSubclassBean valueBean) {
this.valueBean = valueBean;
}
static class ExampleValueSubclassBean extends ExampleValueBean {
private int subIntValue;
int getSubIntValue() {
return this.subIntValue;
}
void setSubIntValue(int intValue) {
this.subIntValue = intValue;
}
}
}
static class ExampleWithNonDefaultConstructor {
private String value;
ExampleWithNonDefaultConstructor(String value) {
this.value = value;
}
String getValue() {
return this.value;
}
void setValue(String value) {
this.value = value;
}
}
static class ExampleSuperClassBean {
private int intValue;
int getIntValue() {
return this.intValue;
}
void setIntValue(int intValue) {
this.intValue = intValue;
}
}
static class ExampleSubclassBean extends ExampleSuperClassBean {
private long longValue;
long getLongValue() {
return this.longValue;
}
void setLongValue(long longValue) {
this.longValue = longValue;
}
}
static class ExampleMismatchBean {
private int value;
String getValue() {
return String.valueOf(this.value);
}
void setValue(int value) {
this.value = value;
}
}
static class ExampleWithThrowingGetters {
private int value;
int getValue() {
return this.value;
}
void setValue(int value) {
this.value = value;
}
List<String> getNames() {
throw new RuntimeException();
}
ExampleValueBean getNested() {
throw new RuntimeException();
}
}
static class ExampleWithSelfReference {
private int value;
private ExampleWithSelfReference self;
int getValue() {
return this.value;
}
void setValue(int value) {
this.value = value;