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 ;
72
74
import org .springframework .core .io .ProtocolResolver ;
73
75
import org .springframework .core .io .Resource ;
74
76
import org .springframework .core .io .ResourceLoader ;
77
+ import org .springframework .format .Formatter ;
75
78
import org .springframework .mock .env .MockEnvironment ;
76
79
import org .springframework .stereotype .Component ;
77
80
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 (
@@ -1238,6 +1249,17 @@ GenericConverter genericPersonConverter() {
1238
1249
1239
1250
}
1240
1251
1252
+ @ Configuration (proxyBeanMethods = false )
1253
+ static class FormatterConfiguration {
1254
+
1255
+ @ Bean
1256
+ @ ConfigurationPropertiesBinding
1257
+ Formatter <Person > personFormatter () {
1258
+ return new PersonFormatter ();
1259
+ }
1260
+
1261
+ }
1262
+
1241
1263
@ Configuration (proxyBeanMethods = false )
1242
1264
static class NonQualifiedGenericConverterConfiguration {
1243
1265
@@ -2003,12 +2025,27 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
2003
2025
2004
2026
}
2005
2027
2028
+ static class PersonFormatter implements Formatter <Person > {
2029
+
2030
+ @ Override
2031
+ public String print (Person person , Locale locale ) {
2032
+ return person .getFirstName () + " " + person .getLastName ();
2033
+ }
2034
+
2035
+ @ Override
2036
+ public Person parse (String text , Locale locale ) throws ParseException {
2037
+ String [] content = text .split (" " );
2038
+ return new Person (content [0 ], content [1 ]);
2039
+ }
2040
+
2041
+ }
2042
+
2006
2043
static class PersonPropertyEditor extends PropertyEditorSupport {
2007
2044
2008
2045
@ Override
2009
2046
public void setAsText (String text ) throws IllegalArgumentException {
2010
- String [] split = text .split ("," );
2011
- setValue (new Person (split [1 ], split [0 ]));
2047
+ String [] content = text .split ("," );
2048
+ setValue (new Person (content [1 ], content [0 ]));
2012
2049
}
2013
2050
2014
2051
}
@@ -2024,6 +2061,14 @@ static class Person {
2024
2061
this .lastName = lastName ;
2025
2062
}
2026
2063
2064
+ String getFirstName () {
2065
+ return this .firstName ;
2066
+ }
2067
+
2068
+ String getLastName () {
2069
+ return this .lastName ;
2070
+ }
2071
+
2027
2072
}
2028
2073
2029
2074
static class Foo {
0 commit comments