33
33
import org .junit .Before ;
34
34
import org .junit .Test ;
35
35
import org .springframework .beans .BeanUtils ;
36
- import org .springframework .beans .BeanWrapper ;
36
+ import org .springframework .beans .ConfigurablePropertyAccessor ;
37
37
import org .springframework .beans .PropertyAccessorFactory ;
38
38
import org .springframework .beans .factory .config .PropertyPlaceholderConfigurer ;
39
39
import org .springframework .beans .factory .support .RootBeanDefinition ;
@@ -94,7 +94,13 @@ public LocalDate convert(DateTime source) {
94
94
@ Test
95
95
public void testFormatFieldForAnnotation () throws Exception {
96
96
formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory ());
97
- doTestFormatFieldForAnnotation (Model .class );
97
+ doTestFormatFieldForAnnotation (Model .class , false );
98
+ }
99
+
100
+ @ Test
101
+ public void testFormatFieldForAnnotationWithDirectFieldAccess () throws Exception {
102
+ formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory ());
103
+ doTestFormatFieldForAnnotation (Model .class , true );
98
104
}
99
105
100
106
@ Test
@@ -109,7 +115,7 @@ public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
109
115
context .refresh ();
110
116
context .getBeanFactory ().initializeBean (formattingService , "formattingService" );
111
117
formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory ());
112
- doTestFormatFieldForAnnotation (ModelWithPlaceholders .class );
118
+ doTestFormatFieldForAnnotation (ModelWithPlaceholders .class , false );
113
119
}
114
120
115
121
@ Test
@@ -124,10 +130,10 @@ public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws
124
130
context .getBeanFactory ().registerSingleton ("ppc" , ppc );
125
131
context .refresh ();
126
132
formattingService = context .getBean ("formattingService" , FormattingConversionService .class );
127
- doTestFormatFieldForAnnotation (ModelWithPlaceholders .class );
133
+ doTestFormatFieldForAnnotation (ModelWithPlaceholders .class , false );
128
134
}
129
135
130
- private void doTestFormatFieldForAnnotation (Class <?> modelClass ) throws Exception {
136
+ private void doTestFormatFieldForAnnotation (Class <?> modelClass , boolean directFieldAccess ) throws Exception {
131
137
formattingService .addConverter (new Converter <Date , Long >() {
132
138
public Long convert (Date source ) {
133
139
return source .getTime ();
@@ -160,20 +166,23 @@ public Date convert(DateTime source) {
160
166
assertEquals (new LocalDate (2009 , 11 , 2 ), new LocalDate (dates .get (2 )));
161
167
162
168
Object model = BeanUtils .instantiate (modelClass );
163
- BeanWrapper accessor = PropertyAccessorFactory .forBeanPropertyAccess (model );
169
+ ConfigurablePropertyAccessor accessor = directFieldAccess ? PropertyAccessorFactory .forDirectFieldAccess (model ) :
170
+ PropertyAccessorFactory .forBeanPropertyAccess (model );
164
171
accessor .setConversionService (formattingService );
165
172
accessor .setPropertyValue ("dates" , "10-31-09,11-1-09,11-2-09" );
166
173
dates = (List <Date >) accessor .getPropertyValue ("dates" );
167
174
assertEquals (new LocalDate (2009 , 10 , 31 ), new LocalDate (dates .get (0 )));
168
175
assertEquals (new LocalDate (2009 , 11 , 1 ), new LocalDate (dates .get (1 )));
169
176
assertEquals (new LocalDate (2009 , 11 , 2 ), new LocalDate (dates .get (2 )));
170
- accessor .setPropertyValue ("dates[0]" , "10-30-09" );
171
- accessor .setPropertyValue ("dates[1]" , "10-1-09" );
172
- accessor .setPropertyValue ("dates[2]" , "10-2-09" );
173
- dates = (List <Date >) accessor .getPropertyValue ("dates" );
174
- assertEquals (new LocalDate (2009 , 10 , 30 ), new LocalDate (dates .get (0 )));
175
- assertEquals (new LocalDate (2009 , 10 , 1 ), new LocalDate (dates .get (1 )));
176
- assertEquals (new LocalDate (2009 , 10 , 2 ), new LocalDate (dates .get (2 )));
177
+ if (!directFieldAccess ) {
178
+ accessor .setPropertyValue ("dates[0]" , "10-30-09" );
179
+ accessor .setPropertyValue ("dates[1]" , "10-1-09" );
180
+ accessor .setPropertyValue ("dates[2]" , "10-2-09" );
181
+ dates = (List <Date >) accessor .getPropertyValue ("dates" );
182
+ assertEquals (new LocalDate (2009 , 10 , 30 ), new LocalDate (dates .get (0 )));
183
+ assertEquals (new LocalDate (2009 , 10 , 1 ), new LocalDate (dates .get (1 )));
184
+ assertEquals (new LocalDate (2009 , 10 , 2 ), new LocalDate (dates .get (2 )));
185
+ }
177
186
}
178
187
179
188
@ Test
@@ -191,7 +200,7 @@ public void testParseNull() throws ParseException {
191
200
@ Test
192
201
public void testParseEmptyString () throws ParseException {
193
202
formattingService .addFormatterForFieldType (Number .class , new NumberFormatter ());
194
- assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (Integer .class )));
203
+ assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (String . class ), TypeDescriptor . valueOf ( Integer .class )));
195
204
}
196
205
197
206
@ Test
@@ -206,7 +215,7 @@ public void testParseNullDefault() throws ParseException {
206
215
207
216
@ Test
208
217
public void testParseEmptyStringDefault () throws ParseException {
209
- assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (Integer .class )));
218
+ assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (String . class ), TypeDescriptor . valueOf ( Integer .class )));
210
219
}
211
220
212
221
0 commit comments