19
19
import java .beans .PropertyEditor ;
20
20
import java .beans .PropertyEditorSupport ;
21
21
import java .io .StringReader ;
22
+ import java .text .ParseException ;
22
23
import java .util .ArrayList ;
23
24
import java .util .Collections ;
24
25
import java .util .Comparator ;
28
29
import java .util .Locale ;
29
30
import java .util .Map ;
30
31
import java .util .TreeMap ;
31
-
32
32
import javax .servlet .jsp .JspException ;
33
33
import javax .servlet .jsp .tagext .Tag ;
34
34
40
40
41
41
import org .springframework .beans .TestBean ;
42
42
import org .springframework .beans .propertyeditors .CustomCollectionEditor ;
43
+ import org .springframework .format .Formatter ;
44
+ import org .springframework .format .support .FormattingConversionService ;
43
45
import org .springframework .mock .web .MockHttpServletRequest ;
44
46
import org .springframework .validation .BeanPropertyBindingResult ;
45
47
import org .springframework .validation .BindingResult ;
@@ -59,7 +61,7 @@ public class SelectTagTests extends AbstractFormTagTests {
59
61
60
62
private SelectTag tag ;
61
63
62
- private TestBean bean ;
64
+ private TestBeanWithRealCountry bean ;
63
65
64
66
65
67
protected void onSetUp () {
@@ -454,9 +456,102 @@ public void testWithMultiList() throws Exception {
454
456
455
457
Element e = (Element ) selectElement .selectSingleNode ("option[@value = 'UK']" );
456
458
assertEquals ("UK node not selected" , "selected" , e .attribute ("selected" ).getValue ());
459
+ assertEquals ("United Kingdom(UK)" , e .getText ());
460
+
461
+ e = (Element ) selectElement .selectSingleNode ("option[@value = 'AT']" );
462
+ assertEquals ("AT node not selected" , "selected" , e .attribute ("selected" ).getValue ());
463
+ assertEquals ("Austria(AT)" , e .getText ());
464
+ }
465
+
466
+ public void testWithElementConverter () throws Exception {
467
+ this .bean .setRealCountry (Country .COUNTRY_UK );
468
+
469
+ BeanPropertyBindingResult errors = new BeanPropertyBindingResult (this .bean , COMMAND_NAME );
470
+ FormattingConversionService cs = new FormattingConversionService ();
471
+ cs .addFormatterForFieldType (Country .class , new Formatter <Country >() {
472
+ public String print (Country object , Locale locale ) {
473
+ return object .getName ();
474
+ }
475
+ public Country parse (String text , Locale locale ) throws ParseException {
476
+ return new Country (text , text );
477
+ }
478
+ });
479
+ errors .initConversion (cs );
480
+ exposeBindingResult (errors );
481
+
482
+ this .tag .setPath ("realCountry" );
483
+ this .tag .setItems ("${countries}" );
484
+ this .tag .setItemValue ("isoCode" );
485
+ int result = this .tag .doStartTag ();
486
+ assertEquals (Tag .SKIP_BODY , result );
487
+
488
+ String output = getOutput ();
489
+ output = "<doc>" + output + "</doc>" ;
490
+
491
+ SAXReader reader = new SAXReader ();
492
+ Document document = reader .read (new StringReader (output ));
493
+ Element rootElement = document .getRootElement ();
494
+ assertEquals (1 , rootElement .elements ().size ());
495
+
496
+ Element selectElement = rootElement .element ("select" );
497
+ assertEquals ("select" , selectElement .getName ());
498
+ assertEquals ("realCountry" , selectElement .attribute ("name" ).getValue ());
499
+
500
+ List children = selectElement .elements ();
501
+ assertEquals ("Incorrect number of children" , 4 , children .size ());
502
+
503
+ Element e = (Element ) selectElement .selectSingleNode ("option[@value = 'UK']" );
504
+ assertEquals ("UK node not selected" , "selected" , e .attribute ("selected" ).getValue ());
505
+ assertEquals ("United Kingdom" , e .getText ());
506
+ }
507
+
508
+ public void testWithMultiListAndElementConverter () throws Exception {
509
+ List list = new ArrayList ();
510
+ list .add (Country .COUNTRY_UK );
511
+ list .add (Country .COUNTRY_AT );
512
+ this .bean .setSomeList (list );
513
+
514
+ BeanPropertyBindingResult errors = new BeanPropertyBindingResult (this .bean , COMMAND_NAME );
515
+ FormattingConversionService cs = new FormattingConversionService ();
516
+ cs .addFormatterForFieldType (Country .class , new Formatter <Country >() {
517
+ public String print (Country object , Locale locale ) {
518
+ return object .getName ();
519
+ }
520
+ public Country parse (String text , Locale locale ) throws ParseException {
521
+ return new Country (text , text );
522
+ }
523
+ });
524
+ errors .initConversion (cs );
525
+ exposeBindingResult (errors );
526
+
527
+ this .tag .setPath ("someList" );
528
+ this .tag .setItems ("${countries}" );
529
+ this .tag .setItemValue ("isoCode" );
530
+ int result = this .tag .doStartTag ();
531
+ assertEquals (Tag .SKIP_BODY , result );
532
+
533
+ String output = getOutput ();
534
+ output = "<doc>" + output + "</doc>" ;
535
+
536
+ SAXReader reader = new SAXReader ();
537
+ Document document = reader .read (new StringReader (output ));
538
+ Element rootElement = document .getRootElement ();
539
+ assertEquals (2 , rootElement .elements ().size ());
540
+
541
+ Element selectElement = rootElement .element ("select" );
542
+ assertEquals ("select" , selectElement .getName ());
543
+ assertEquals ("someList" , selectElement .attribute ("name" ).getValue ());
544
+
545
+ List children = selectElement .elements ();
546
+ assertEquals ("Incorrect number of children" , 4 , children .size ());
547
+
548
+ Element e = (Element ) selectElement .selectSingleNode ("option[@value = 'UK']" );
549
+ assertEquals ("UK node not selected" , "selected" , e .attribute ("selected" ).getValue ());
550
+ assertEquals ("United Kingdom" , e .getText ());
457
551
458
552
e = (Element ) selectElement .selectSingleNode ("option[@value = 'AT']" );
459
553
assertEquals ("AT node not selected" , "selected" , e .attribute ("selected" ).getValue ());
554
+ assertEquals ("Austria" , e .getText ());
460
555
}
461
556
462
557
public void testWithMultiListAndCustomEditor () throws Exception {
0 commit comments