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 ;
66
68
import org .springframework .core .io .ProtocolResolver ;
67
69
import org .springframework .core .io .Resource ;
68
70
import org .springframework .core .io .ResourceLoader ;
71
+ import org .springframework .format .Formatter ;
69
72
import org .springframework .mock .env .MockEnvironment ;
70
73
import org .springframework .stereotype .Component ;
71
74
import org .springframework .test .context .support .TestPropertySourceUtils ;
@@ -597,7 +600,7 @@ public void customProtocolResolver() {
597
600
}
598
601
599
602
@ Test
600
- public void loadShouldUseConfigurationConverter () {
603
+ public void loadShouldUseConverterBean () {
601
604
prepareConverterContext (ConverterConfiguration .class , PersonProperties .class );
602
605
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
603
606
assertThat (person .firstName ).isEqualTo ("John" );
@@ -613,13 +616,21 @@ public void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
613
616
}
614
617
615
618
@ Test
616
- public void loadShouldUseGenericConfigurationConverter () {
619
+ public void loadShouldUseGenericConverterBean () {
617
620
prepareConverterContext (GenericConverterConfiguration .class , PersonProperties .class );
618
621
Person person = this .context .getBean (PersonProperties .class ).getPerson ();
619
622
assertThat (person .firstName ).isEqualTo ("John" );
620
623
assertThat (person .lastName ).isEqualTo ("Smith" );
621
624
}
622
625
626
+ @ Test
627
+ public void loadShouldUseFormatterBean () {
628
+ prepareConverterContext (FormatterConfiguration .class , PersonProperties .class );
629
+ Person person = this .context .getBean (PersonProperties .class ).getPerson ();
630
+ assertThat (person .firstName ).isEqualTo ("John" );
631
+ assertThat (person .lastName ).isEqualTo ("Smith" );
632
+ }
633
+
623
634
@ Test
624
635
public void loadWhenGenericConfigurationConverterIsNotQualifiedShouldNotConvert () {
625
636
assertThatExceptionOfType (BeanCreationException .class ).isThrownBy (
@@ -1043,6 +1054,17 @@ public GenericConverter genericPersonConverter() {
1043
1054
1044
1055
}
1045
1056
1057
+ @ Configuration
1058
+ static class FormatterConfiguration {
1059
+
1060
+ @ Bean
1061
+ @ ConfigurationPropertiesBinding
1062
+ Formatter <Person > personFormatter () {
1063
+ return new PersonFormatter ();
1064
+ }
1065
+
1066
+ }
1067
+
1046
1068
@ Configuration
1047
1069
static class NonQualifiedGenericConverterConfiguration {
1048
1070
@@ -1731,12 +1753,27 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
1731
1753
1732
1754
}
1733
1755
1756
+ static class PersonFormatter implements Formatter <Person > {
1757
+
1758
+ @ Override
1759
+ public String print (Person person , Locale locale ) {
1760
+ return person .getFirstName () + " " + person .getLastName ();
1761
+ }
1762
+
1763
+ @ Override
1764
+ public Person parse (String text , Locale locale ) throws ParseException {
1765
+ String [] content = text .split (" " );
1766
+ return new Person (content [0 ], content [1 ]);
1767
+ }
1768
+
1769
+ }
1770
+
1734
1771
static class PersonPropertyEditor extends PropertyEditorSupport {
1735
1772
1736
1773
@ Override
1737
1774
public void setAsText (String text ) throws IllegalArgumentException {
1738
- String [] split = text .split ("," );
1739
- setValue (new Person (split [1 ], split [0 ]));
1775
+ String [] content = text .split ("," );
1776
+ setValue (new Person (content [1 ], content [0 ]));
1740
1777
}
1741
1778
1742
1779
}
@@ -1752,6 +1789,14 @@ static class Person {
1752
1789
this .lastName = lastName ;
1753
1790
}
1754
1791
1792
+ String getFirstName () {
1793
+ return this .firstName ;
1794
+ }
1795
+
1796
+ String getLastName () {
1797
+ return this .lastName ;
1798
+ }
1799
+
1755
1800
}
1756
1801
1757
1802
static class Foo {
0 commit comments