18
18
19
19
import java .beans .PropertyEditorSupport ;
20
20
import java .io .File ;
21
+ import java .text .ParseException ;
21
22
import java .time .Duration ;
22
23
import java .util .ArrayList ;
23
24
import java .util .Collections ;
24
25
import java .util .HashMap ;
25
26
import java .util .LinkedHashMap ;
26
27
import java .util .List ;
28
+ import java .util .Locale ;
27
29
import java .util .Map ;
28
30
import java .util .Properties ;
29
31
import java .util .Set ;
73
75
import org .springframework .core .io .ProtocolResolver ;
74
76
import org .springframework .core .io .Resource ;
75
77
import org .springframework .core .io .ResourceLoader ;
78
+ import org .springframework .format .Formatter ;
76
79
import org .springframework .mock .env .MockEnvironment ;
77
80
import org .springframework .stereotype .Component ;
78
81
import org .springframework .test .context .support .TestPropertySourceUtils ;
@@ -609,7 +612,7 @@ void customProtocolResolver() {
609
612
}
610
613
611
614
@ Test
612
- void loadShouldUseConfigurationConverter () {
615
+ void loadShouldUseConverterBean () {
613
616
prepareConverterContext (ConverterConfiguration .class , PersonProperties .class );
614
617
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
615
618
assertThat (person .firstName ).isEqualTo ("John" );
@@ -625,13 +628,21 @@ void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
625
628
}
626
629
627
630
@ Test
628
- void loadShouldUseGenericConfigurationConverter () {
631
+ void loadShouldUseGenericConverterBean () {
629
632
prepareConverterContext (GenericConverterConfiguration .class , PersonProperties .class );
630
633
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
631
634
assertThat (person .firstName ).isEqualTo ("John" );
632
635
assertThat (person .lastName ).isEqualTo ("Smith" );
633
636
}
634
637
638
+ @ Test
639
+ void loadShouldUseFormatterBean () {
640
+ prepareConverterContext (FormatterConfiguration .class , PersonProperties .class );
641
+ Person person = this .context .getBean (PersonProperties .class ).getPerson ();
642
+ assertThat (person .firstName ).isEqualTo ("John" );
643
+ assertThat (person .lastName ).isEqualTo ("Smith" );
644
+ }
645
+
635
646
@ Test
636
647
void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert () {
637
648
assertThatExceptionOfType (BeanCreationException .class ).isThrownBy (
@@ -1246,6 +1257,17 @@ GenericConverter genericPersonConverter() {
1246
1257
1247
1258
}
1248
1259
1260
+ @ Configuration (proxyBeanMethods = false )
1261
+ static class FormatterConfiguration {
1262
+
1263
+ @ Bean
1264
+ @ ConfigurationPropertiesBinding
1265
+ Formatter <Person > personFormatter () {
1266
+ return new PersonFormatter ();
1267
+ }
1268
+
1269
+ }
1270
+
1249
1271
@ Configuration (proxyBeanMethods = false )
1250
1272
static class NonQualifiedGenericConverterConfiguration {
1251
1273
@@ -2011,12 +2033,27 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
2011
2033
2012
2034
}
2013
2035
2036
+ static class PersonFormatter implements Formatter <Person > {
2037
+
2038
+ @ Override
2039
+ public String print (Person person , Locale locale ) {
2040
+ return person .getFirstName () + " " + person .getLastName ();
2041
+ }
2042
+
2043
+ @ Override
2044
+ public Person parse (String text , Locale locale ) throws ParseException {
2045
+ String [] content = text .split (" " );
2046
+ return new Person (content [0 ], content [1 ]);
2047
+ }
2048
+
2049
+ }
2050
+
2014
2051
static class PersonPropertyEditor extends PropertyEditorSupport {
2015
2052
2016
2053
@ Override
2017
2054
public void setAsText (String text ) throws IllegalArgumentException {
2018
- String [] split = text .split ("," );
2019
- setValue (new Person (split [1 ], split [0 ]));
2055
+ String [] content = text .split ("," );
2056
+ setValue (new Person (content [1 ], content [0 ]));
2020
2057
}
2021
2058
2022
2059
}
@@ -2032,6 +2069,14 @@ static class Person {
2032
2069
this .lastName = lastName ;
2033
2070
}
2034
2071
2072
+ String getFirstName () {
2073
+ return this .firstName ;
2074
+ }
2075
+
2076
+ String getLastName () {
2077
+ return this .lastName ;
2078
+ }
2079
+
2035
2080
}
2036
2081
2037
2082
static class Foo {
0 commit comments