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 .time .Period ;
23
24
import java .time .temporal .ChronoUnit ;
26
27
import java .util .HashMap ;
27
28
import java .util .LinkedHashMap ;
28
29
import java .util .List ;
30
+ import java .util .Locale ;
29
31
import java .util .Map ;
30
32
import java .util .Properties ;
31
33
import java .util .Set ;
81
83
import org .springframework .core .io .ProtocolResolver ;
82
84
import org .springframework .core .io .Resource ;
83
85
import org .springframework .core .io .ResourceLoader ;
86
+ import org .springframework .format .Formatter ;
84
87
import org .springframework .mock .env .MockEnvironment ;
85
88
import org .springframework .stereotype .Component ;
86
89
import org .springframework .test .context .support .TestPropertySourceUtils ;
@@ -618,7 +621,7 @@ void customProtocolResolver() {
618
621
}
619
622
620
623
@ Test
621
- void loadShouldUseConfigurationConverter () {
624
+ void loadShouldUseConverterBean () {
622
625
prepareConverterContext (ConverterConfiguration .class , PersonProperties .class );
623
626
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
624
627
assertThat (person .firstName ).isEqualTo ("John" );
@@ -634,13 +637,21 @@ void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
634
637
}
635
638
636
639
@ Test
637
- void loadShouldUseGenericConfigurationConverter () {
640
+ void loadShouldUseGenericConverterBean () {
638
641
prepareConverterContext (GenericConverterConfiguration .class , PersonProperties .class );
639
642
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
640
643
assertThat (person .firstName ).isEqualTo ("John" );
641
644
assertThat (person .lastName ).isEqualTo ("Smith" );
642
645
}
643
646
647
+ @ Test
648
+ void loadShouldUseFormatterBean () {
649
+ prepareConverterContext (FormatterConfiguration .class , PersonProperties .class );
650
+ Person person = this .context .getBean (PersonProperties .class ).getPerson ();
651
+ assertThat (person .firstName ).isEqualTo ("John" );
652
+ assertThat (person .lastName ).isEqualTo ("Smith" );
653
+ }
654
+
644
655
@ Test
645
656
void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert () {
646
657
assertThatExceptionOfType (BeanCreationException .class ).isThrownBy (
@@ -1326,6 +1337,17 @@ GenericConverter genericPersonConverter() {
1326
1337
1327
1338
}
1328
1339
1340
+ @ Configuration (proxyBeanMethods = false )
1341
+ static class FormatterConfiguration {
1342
+
1343
+ @ Bean
1344
+ @ ConfigurationPropertiesBinding
1345
+ Formatter <Person > personFormatter () {
1346
+ return new PersonFormatter ();
1347
+ }
1348
+
1349
+ }
1350
+
1329
1351
@ Configuration (proxyBeanMethods = false )
1330
1352
static class NonQualifiedGenericConverterConfiguration {
1331
1353
@@ -2158,12 +2180,27 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
2158
2180
2159
2181
}
2160
2182
2183
+ static class PersonFormatter implements Formatter <Person > {
2184
+
2185
+ @ Override
2186
+ public String print (Person person , Locale locale ) {
2187
+ return person .getFirstName () + " " + person .getLastName ();
2188
+ }
2189
+
2190
+ @ Override
2191
+ public Person parse (String text , Locale locale ) throws ParseException {
2192
+ String [] content = text .split (" " );
2193
+ return new Person (content [0 ], content [1 ]);
2194
+ }
2195
+
2196
+ }
2197
+
2161
2198
static class PersonPropertyEditor extends PropertyEditorSupport {
2162
2199
2163
2200
@ Override
2164
2201
public void setAsText (String text ) throws IllegalArgumentException {
2165
- String [] split = text .split ("," );
2166
- setValue (new Person (split [1 ], split [0 ]));
2202
+ String [] content = text .split ("," );
2203
+ setValue (new Person (content [1 ], content [0 ]));
2167
2204
}
2168
2205
2169
2206
}
@@ -2179,6 +2216,14 @@ static class Person {
2179
2216
this .lastName = lastName ;
2180
2217
}
2181
2218
2219
+ String getFirstName () {
2220
+ return this .firstName ;
2221
+ }
2222
+
2223
+ String getLastName () {
2224
+ return this .lastName ;
2225
+ }
2226
+
2182
2227
}
2183
2228
2184
2229
static class Foo {
0 commit comments