3131import org .junit .Before ;
3232import org .junit .Test ;
3333
34+ import org .springframework .beans .BeanUtils ;
35+ import org .springframework .beans .BeanWrapper ;
36+ import org .springframework .beans .PropertyAccessorFactory ;
3437import org .springframework .beans .factory .config .PropertyPlaceholderConfigurer ;
3538import org .springframework .beans .factory .support .RootBeanDefinition ;
3639import org .springframework .context .i18n .LocaleContextHolder ;
@@ -99,7 +102,7 @@ public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
99102 PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ();
100103 Properties props = new Properties ();
101104 props .setProperty ("dateStyle" , "S-" );
102- props .setProperty ("datePattern" , "M/d/ yy" );
105+ props .setProperty ("datePattern" , "M-d- yy" );
103106 ppc .setProperties (props );
104107 context .getBeanFactory ().registerSingleton ("ppc" , ppc );
105108 context .refresh ();
@@ -114,7 +117,7 @@ public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws
114117 PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ();
115118 Properties props = new Properties ();
116119 props .setProperty ("dateStyle" , "S-" );
117- props .setProperty ("datePattern" , "M/d/ yy" );
120+ props .setProperty ("datePattern" , "M-d- yy" );
118121 ppc .setProperties (props );
119122 context .registerBeanDefinition ("formattingService" , new RootBeanDefinition (FormattingConversionServiceFactoryBean .class ));
120123 context .getBeanFactory ().registerSingleton ("ppc" , ppc );
@@ -148,12 +151,28 @@ public Date convert(DateTime source) {
148151 dates .add (new LocalDate (2009 , 11 , 2 ).toDateTimeAtCurrentTime ().toDate ());
149152 formatted = (String ) formattingService .convert (dates ,
150153 new TypeDescriptor (modelClass .getField ("dates" )), TypeDescriptor .valueOf (String .class ));
151- assertEquals ("10/31/ 09,11/1/ 09,11/2/ 09" , formatted );
152- dates = (List <Date >) formattingService .convert ("10/31/ 09,11/1/ 09,11/2/ 09" ,
154+ assertEquals ("10-31- 09,11-1- 09,11-2- 09" , formatted );
155+ dates = (List <Date >) formattingService .convert ("10-31- 09,11-1- 09,11-2- 09" ,
153156 TypeDescriptor .valueOf (String .class ), new TypeDescriptor (modelClass .getField ("dates" )));
154157 assertEquals (new LocalDate (2009 , 10 , 31 ), new LocalDate (dates .get (0 )));
155158 assertEquals (new LocalDate (2009 , 11 , 1 ), new LocalDate (dates .get (1 )));
156159 assertEquals (new LocalDate (2009 , 11 , 2 ), new LocalDate (dates .get (2 )));
160+
161+ Object model = BeanUtils .instantiate (modelClass );
162+ BeanWrapper accessor = PropertyAccessorFactory .forBeanPropertyAccess (model );
163+ accessor .setConversionService (formattingService );
164+ accessor .setPropertyValue ("dates" , "10-31-09,11-1-09,11-2-09" );
165+ dates = (List <Date >) accessor .getPropertyValue ("dates" );
166+ assertEquals (new LocalDate (2009 , 10 , 31 ), new LocalDate (dates .get (0 )));
167+ assertEquals (new LocalDate (2009 , 11 , 1 ), new LocalDate (dates .get (1 )));
168+ assertEquals (new LocalDate (2009 , 11 , 2 ), new LocalDate (dates .get (2 )));
169+ accessor .setPropertyValue ("dates[0]" , "10-30-09" );
170+ accessor .setPropertyValue ("dates[1]" , "10-1-09" );
171+ accessor .setPropertyValue ("dates[2]" , "10-2-09" );
172+ dates = (List <Date >) accessor .getPropertyValue ("dates" );
173+ assertEquals (new LocalDate (2009 , 10 , 30 ), new LocalDate (dates .get (0 )));
174+ assertEquals (new LocalDate (2009 , 10 , 1 ), new LocalDate (dates .get (1 )));
175+ assertEquals (new LocalDate (2009 , 10 , 2 ), new LocalDate (dates .get (2 )));
157176 }
158177
159178 @ Test
@@ -190,20 +209,27 @@ public void testParseEmptyStringDefault() throws ParseException {
190209 }
191210
192211
193- private static class Model {
212+ public static class Model {
194213
195214 @ SuppressWarnings ("unused" )
196215 @ org .springframework .format .annotation .DateTimeFormat (style ="S-" )
197216 public Date date ;
198217
199218 @ SuppressWarnings ("unused" )
200- @ org .springframework .format .annotation .DateTimeFormat (pattern ="M/d/ yy" )
219+ @ org .springframework .format .annotation .DateTimeFormat (pattern ="M-d- yy" )
201220 public List <Date > dates ;
202221
222+ public List <Date > getDates () {
223+ return dates ;
224+ }
225+
226+ public void setDates (List <Date > dates ) {
227+ this .dates = dates ;
228+ }
203229 }
204230
205231
206- private static class ModelWithPlaceholders {
232+ public static class ModelWithPlaceholders {
207233
208234 @ SuppressWarnings ("unused" )
209235 @ org .springframework .format .annotation .DateTimeFormat (style ="${dateStyle}" )
@@ -213,6 +239,13 @@ private static class ModelWithPlaceholders {
213239 @ org .springframework .format .annotation .DateTimeFormat (pattern ="${datePattern}" )
214240 public List <Date > dates ;
215241
242+ public List <Date > getDates () {
243+ return dates ;
244+ }
245+
246+ public void setDates (List <Date > dates ) {
247+ this .dates = dates ;
248+ }
216249 }
217250
218251}
0 commit comments