32
32
import org .junit .Before ;
33
33
import org .junit .Test ;
34
34
35
- import org .springframework .beans . BeanUtils ;
35
+ import org .springframework .aop . framework . ProxyFactory ;
36
36
import org .springframework .beans .ConfigurablePropertyAccessor ;
37
37
import org .springframework .beans .PropertyAccessorFactory ;
38
38
import org .springframework .beans .factory .annotation .Value ;
45
45
import org .springframework .core .convert .ConversionFailedException ;
46
46
import org .springframework .core .convert .TypeDescriptor ;
47
47
import org .springframework .core .convert .converter .Converter ;
48
+ import org .springframework .core .convert .converter .ConverterFactory ;
48
49
import org .springframework .core .convert .support .DefaultConversionService ;
49
50
import org .springframework .format .Formatter ;
50
51
import org .springframework .format .Printer ;
@@ -81,7 +82,7 @@ public void tearDown() {
81
82
82
83
83
84
@ Test
84
- public void testFormatFieldForTypeWithFormatter () throws ParseException {
85
+ public void formatFieldForTypeWithFormatter () throws ParseException {
85
86
formattingService .addFormatterForFieldType (Number .class , new NumberStyleFormatter ());
86
87
String formatted = formattingService .convert (3 , String .class );
87
88
assertEquals ("3" , formatted );
@@ -90,7 +91,7 @@ public void testFormatFieldForTypeWithFormatter() throws ParseException {
90
91
}
91
92
92
93
@ Test
93
- public void testFormatFieldForTypeWithPrinterParserWithCoercion () throws ParseException {
94
+ public void formatFieldForTypeWithPrinterParserWithCoercion () throws ParseException {
94
95
formattingService .addConverter (new Converter <DateTime , LocalDate >() {
95
96
@ Override
96
97
public LocalDate convert (DateTime source ) {
@@ -107,7 +108,7 @@ public LocalDate convert(DateTime source) {
107
108
108
109
@ Test
109
110
@ SuppressWarnings ("resource" )
110
- public void testFormatFieldForValueInjection () {
111
+ public void formatFieldForValueInjection () {
111
112
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext ();
112
113
ac .registerBeanDefinition ("valueBean" , new RootBeanDefinition (ValueBean .class ));
113
114
ac .registerBeanDefinition ("conversionService" , new RootBeanDefinition (FormattingConversionServiceFactoryBean .class ));
@@ -118,7 +119,7 @@ public void testFormatFieldForValueInjection() {
118
119
119
120
@ Test
120
121
@ SuppressWarnings ("resource" )
121
- public void testFormatFieldForValueInjectionUsingMetaAnnotations () {
122
+ public void formatFieldForValueInjectionUsingMetaAnnotations () {
122
123
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext ();
123
124
RootBeanDefinition bd = new RootBeanDefinition (MetaValueBean .class );
124
125
bd .setScope (BeanDefinition .SCOPE_PROTOTYPE );
@@ -140,20 +141,20 @@ public void testFormatFieldForValueInjectionUsingMetaAnnotations() {
140
141
}
141
142
142
143
@ Test
143
- public void testFormatFieldForAnnotation () throws Exception {
144
+ public void formatFieldForAnnotation () throws Exception {
144
145
formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory ());
145
146
doTestFormatFieldForAnnotation (Model .class , false );
146
147
}
147
148
148
149
@ Test
149
- public void testFormatFieldForAnnotationWithDirectFieldAccess () throws Exception {
150
+ public void formatFieldForAnnotationWithDirectFieldAccess () throws Exception {
150
151
formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory ());
151
152
doTestFormatFieldForAnnotation (Model .class , true );
152
153
}
153
154
154
155
@ Test
155
156
@ SuppressWarnings ("resource" )
156
- public void testFormatFieldForAnnotationWithPlaceholders () throws Exception {
157
+ public void formatFieldForAnnotationWithPlaceholders () throws Exception {
157
158
GenericApplicationContext context = new GenericApplicationContext ();
158
159
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ();
159
160
Properties props = new Properties ();
@@ -169,7 +170,7 @@ public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
169
170
170
171
@ Test
171
172
@ SuppressWarnings ("resource" )
172
- public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean () throws Exception {
173
+ public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean () throws Exception {
173
174
GenericApplicationContext context = new GenericApplicationContext ();
174
175
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ();
175
176
Properties props = new Properties ();
@@ -239,61 +240,61 @@ public Date convert(DateTime source) {
239
240
}
240
241
241
242
@ Test
242
- public void testPrintNull () throws ParseException {
243
+ public void printNull () throws ParseException {
243
244
formattingService .addFormatterForFieldType (Number .class , new NumberStyleFormatter ());
244
245
assertEquals ("" , formattingService .convert (null , TypeDescriptor .valueOf (Integer .class ), TypeDescriptor .valueOf (String .class )));
245
246
}
246
247
247
248
@ Test
248
- public void testParseNull () throws ParseException {
249
+ public void parseNull () throws ParseException {
249
250
formattingService .addFormatterForFieldType (Number .class , new NumberStyleFormatter ());
250
251
assertNull (formattingService
251
252
.convert (null , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
252
253
}
253
254
254
255
@ Test
255
- public void testParseEmptyString () throws ParseException {
256
+ public void parseEmptyString () throws ParseException {
256
257
formattingService .addFormatterForFieldType (Number .class , new NumberStyleFormatter ());
257
258
assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
258
259
}
259
260
260
261
@ Test
261
- public void testParseBlankString () throws ParseException {
262
+ public void parseBlankString () throws ParseException {
262
263
formattingService .addFormatterForFieldType (Number .class , new NumberStyleFormatter ());
263
264
assertNull (formattingService .convert (" " , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
264
265
}
265
266
266
267
@ Test (expected = ConversionFailedException .class )
267
- public void testParseParserReturnsNull () throws ParseException {
268
+ public void parseParserReturnsNull () throws ParseException {
268
269
formattingService .addFormatterForFieldType (Integer .class , new NullReturningFormatter ());
269
270
assertNull (formattingService .convert ("1" , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
270
271
}
271
272
272
273
@ Test (expected = ConversionFailedException .class )
273
- public void testParseNullPrimitiveProperty () throws ParseException {
274
+ public void parseNullPrimitiveProperty () throws ParseException {
274
275
formattingService .addFormatterForFieldType (Integer .class , new NumberStyleFormatter ());
275
276
assertNull (formattingService .convert (null , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (int .class )));
276
277
}
277
278
278
279
@ Test
279
- public void testPrintNullDefault () throws ParseException {
280
+ public void printNullDefault () throws ParseException {
280
281
assertEquals (null , formattingService
281
282
.convert (null , TypeDescriptor .valueOf (Integer .class ), TypeDescriptor .valueOf (String .class )));
282
283
}
283
284
284
285
@ Test
285
- public void testParseNullDefault () throws ParseException {
286
+ public void parseNullDefault () throws ParseException {
286
287
assertNull (formattingService
287
288
.convert (null , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
288
289
}
289
290
290
291
@ Test
291
- public void testParseEmptyStringDefault () throws ParseException {
292
+ public void parseEmptyStringDefault () throws ParseException {
292
293
assertNull (formattingService .convert ("" , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
293
294
}
294
295
295
296
@ Test
296
- public void testFormatFieldForAnnotationWithSubclassAsFieldType () throws Exception {
297
+ public void formatFieldForAnnotationWithSubclassAsFieldType () throws Exception {
297
298
formattingService .addFormatterForFieldAnnotation (new JodaDateTimeFormatAnnotationFormatterFactory () {
298
299
@ Override
299
300
public Printer <?> getPrinter (org .springframework .format .annotation .DateTimeFormat annotation , Class <?> fieldType ) {
@@ -319,7 +320,7 @@ public Date convert(MyDate source) {
319
320
}
320
321
321
322
@ Test
322
- public void testRegisterDefaultValueViaFormatter () {
323
+ public void registerDefaultValueViaFormatter () {
323
324
registerDefaultValue (Date .class , new Date ());
324
325
}
325
326
@@ -340,6 +341,45 @@ public String toString() {
340
341
});
341
342
}
342
343
344
+ @ Test
345
+ public void introspectedFormatter () throws ParseException {
346
+ formattingService .addFormatter (new NumberStyleFormatter ());
347
+ assertNull (formattingService .convert (null , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
348
+ }
349
+
350
+ @ Test
351
+ public void proxiedFormatter () throws ParseException {
352
+ Formatter <?> formatter = new NumberStyleFormatter ();
353
+ formattingService .addFormatter ((Formatter <?>) new ProxyFactory (formatter ).getProxy ());
354
+ assertNull (formattingService .convert (null , TypeDescriptor .valueOf (String .class ), TypeDescriptor .valueOf (Integer .class )));
355
+ }
356
+
357
+ @ Test
358
+ public void introspectedConverter () {
359
+ formattingService .addConverter (new IntegerConverter ());
360
+ assertEquals (Integer .valueOf (1 ), formattingService .convert ("1" , Integer .class ));
361
+ }
362
+
363
+ @ Test
364
+ public void proxiedConverter () {
365
+ Converter <?, ?> converter = new IntegerConverter ();
366
+ formattingService .addConverter ((Converter <?, ?>) new ProxyFactory (converter ).getProxy ());
367
+ assertEquals (Integer .valueOf (1 ), formattingService .convert ("1" , Integer .class ));
368
+ }
369
+
370
+ @ Test
371
+ public void introspectedConverterFactory () {
372
+ formattingService .addConverterFactory (new IntegerConverterFactory ());
373
+ assertEquals (Integer .valueOf (1 ), formattingService .convert ("1" , Integer .class ));
374
+ }
375
+
376
+ @ Test
377
+ public void proxiedConverterFactory () {
378
+ ConverterFactory <?, ?> converterFactory = new IntegerConverterFactory ();
379
+ formattingService .addConverterFactory ((ConverterFactory <?, ?>) new ProxyFactory (converterFactory ).getProxy ());
380
+ assertEquals (Integer .valueOf (1 ), formattingService .convert ("1" , Integer .class ));
381
+ }
382
+
343
383
344
384
public static class ValueBean {
345
385
@@ -348,6 +388,7 @@ public static class ValueBean {
348
388
public Date date ;
349
389
}
350
390
391
+
351
392
public static class MetaValueBean {
352
393
353
394
@ MyDateAnn
@@ -357,18 +398,21 @@ public static class MetaValueBean {
357
398
public Double number ;
358
399
}
359
400
401
+
360
402
@ Value ("${myDate}" )
361
403
@ org .springframework .format .annotation .DateTimeFormat (pattern ="MM-d-yy" )
362
404
@ Retention (RetentionPolicy .RUNTIME )
363
- public static @interface MyDateAnn {
405
+ public @interface MyDateAnn {
364
406
}
365
407
408
+
366
409
@ Value ("${myNumber}" )
367
410
@ NumberFormat (style = NumberFormat .Style .PERCENT )
368
411
@ Retention (RetentionPolicy .RUNTIME )
369
- public static @interface MyNumberAnn {
412
+ public @interface MyNumberAnn {
370
413
}
371
414
415
+
372
416
public static class Model {
373
417
374
418
@ org .springframework .format .annotation .DateTimeFormat (style ="S-" )
@@ -386,6 +430,7 @@ public void setDates(List<Date> dates) {
386
430
}
387
431
}
388
432
433
+
389
434
public static class ModelWithPlaceholders {
390
435
391
436
@ org .springframework .format .annotation .DateTimeFormat (style ="${dateStyle}" )
@@ -403,11 +448,13 @@ public void setDates(List<Date> dates) {
403
448
}
404
449
}
405
450
451
+
406
452
@ org .springframework .format .annotation .DateTimeFormat (pattern ="${datePattern}" )
407
453
@ Retention (RetentionPolicy .RUNTIME )
408
- public static @interface MyDatePattern {
454
+ public @interface MyDatePattern {
409
455
}
410
456
457
+
411
458
public static class NullReturningFormatter implements Formatter <Integer > {
412
459
413
460
@ Override
@@ -419,17 +466,42 @@ public String print(Integer object, Locale locale) {
419
466
public Integer parse (String text , Locale locale ) throws ParseException {
420
467
return null ;
421
468
}
422
-
423
469
}
424
470
471
+
425
472
@ SuppressWarnings ("serial" )
426
473
public static class MyDate extends Date {
427
474
}
428
475
476
+
429
477
private static class ModelWithSubclassField {
430
478
431
479
@ org .springframework .format .annotation .DateTimeFormat (style = "S-" )
432
480
public MyDate date ;
433
481
}
434
482
483
+
484
+ private static class IntegerConverter implements Converter <String , Integer > {
485
+
486
+ @ Override
487
+ public Integer convert (String source ) {
488
+ return Integer .parseInt (source );
489
+ }
490
+ }
491
+
492
+
493
+ private static class IntegerConverterFactory implements ConverterFactory <String , Number > {
494
+
495
+ @ Override
496
+ @ SuppressWarnings ("unchecked" )
497
+ public <T extends Number > Converter <String , T > getConverter (Class <T > targetType ) {
498
+ if (Integer .class == targetType ) {
499
+ return (Converter <String , T >) new IntegerConverter ();
500
+ }
501
+ else {
502
+ throw new IllegalStateException ();
503
+ }
504
+ }
505
+ }
506
+
435
507
}
0 commit comments