@@ -619,7 +619,7 @@ public void convertPrimitiveArray() {
619
619
GenericConversionService conversionService = new DefaultConversionService ();
620
620
byte [] byteArray = new byte [] { 1 , 2 , 3 };
621
621
Byte [] converted = conversionService .convert (byteArray , Byte [].class );
622
- assertTrue (Arrays .equals (converted , new Byte [] { 1 , 2 , 3 }));
622
+ assertTrue (Arrays .equals (converted , new Byte [] {1 , 2 , 3 }));
623
623
}
624
624
625
625
@ Test
@@ -739,7 +739,7 @@ public Byte convert(Byte source) {
739
739
byte [] byteArray = new byte [] { 1 , 2 , 3 };
740
740
byte [] converted = conversionService .convert (byteArray , byte [].class );
741
741
assertNotSame (byteArray , converted );
742
- assertTrue (Arrays .equals (new byte [] { 2 , 3 , 4 }, converted ));
742
+ assertTrue (Arrays .equals (new byte [] {2 , 3 , 4 }, converted ));
743
743
}
744
744
745
745
@ Test
@@ -769,8 +769,11 @@ public void convertNullAnnotatedStringToString() throws Exception {
769
769
770
770
@ Test
771
771
public void multipleCollectionTypesFromSameSourceType () throws Exception {
772
+ conversionService .addConverter (new MyStringToRawCollectionConverter ());
773
+ conversionService .addConverter (new MyStringToGenericCollectionConverter ());
772
774
conversionService .addConverter (new MyStringToStringCollectionConverter ());
773
775
conversionService .addConverter (new MyStringToIntegerCollectionConverter ());
776
+
774
777
assertEquals (Collections .singleton (4 ), // should be "testX" from MyStringToStringCollectionConverter, ideally
775
778
conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("stringCollection" ))));
776
779
assertEquals (Collections .singleton (4 ),
@@ -788,6 +791,7 @@ public void multipleCollectionTypesFromSameSourceType() throws Exception {
788
791
@ Test
789
792
public void adaptedCollectionTypesFromSameSourceType () throws Exception {
790
793
conversionService .addConverter (new MyStringToStringCollectionConverter ());
794
+
791
795
assertEquals (Collections .singleton ("testX" ),
792
796
conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("stringCollection" ))));
793
797
assertEquals (Collections .singleton ("testX" ),
@@ -800,6 +804,42 @@ public void adaptedCollectionTypesFromSameSourceType() throws Exception {
800
804
conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("stringCollection" ))));
801
805
assertEquals (Collections .singleton ("testX" ),
802
806
conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("rawCollection" ))));
807
+
808
+ // The following is unpleasant but simply a consequence of the raw type matching algorithm in Spring 3.x
809
+ assertEquals (Collections .singleton ("testX" ),
810
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("integerCollection" ))));
811
+ }
812
+
813
+ @ Test
814
+ public void genericCollectionAsSource () throws Exception {
815
+ conversionService .addConverter (new MyStringToGenericCollectionConverter ());
816
+
817
+ assertEquals (Collections .singleton ("testX" ),
818
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("stringCollection" ))));
819
+ assertEquals (Collections .singleton ("testX" ),
820
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("genericCollection" ))));
821
+ assertEquals (Collections .singleton ("testX" ),
822
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("rawCollection" ))));
823
+
824
+ // The following is unpleasant but a consequence of the generic collection converter above...
825
+ assertEquals (Collections .singleton ("testX" ),
826
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("integerCollection" ))));
827
+ }
828
+
829
+ @ Test
830
+ public void rawCollectionAsSource () throws Exception {
831
+ conversionService .addConverter (new MyStringToRawCollectionConverter ());
832
+
833
+ assertEquals (Collections .singleton ("testX" ),
834
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("stringCollection" ))));
835
+ assertEquals (Collections .singleton ("testX" ),
836
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("genericCollection" ))));
837
+ assertEquals (Collections .singleton ("testX" ),
838
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("rawCollection" ))));
839
+
840
+ // The following is unpleasant but a consequence of the raw collection converter above...
841
+ assertEquals (Collections .singleton ("testX" ),
842
+ conversionService .convert ("test" , TypeDescriptor .valueOf (String .class ), new TypeDescriptor (getClass ().getField ("integerCollection" ))));
803
843
}
804
844
805
845
@@ -810,6 +850,7 @@ public void adaptedCollectionTypesFromSameSourceType() throws Exception {
810
850
public static @interface ExampleAnnotation {
811
851
}
812
852
853
+
813
854
private static class MyConditionalConverter implements Converter <String , Color >, ConditionalConverter {
814
855
815
856
private int matchAttempts = 0 ;
@@ -830,8 +871,8 @@ public int getMatchAttempts() {
830
871
}
831
872
}
832
873
833
- private static class MyConditionalGenericConverter implements GenericConverter ,
834
- ConditionalConverter {
874
+
875
+ private static class MyConditionalGenericConverter implements GenericConverter , ConditionalConverter {
835
876
836
877
private List <TypeDescriptor > sourceTypes = new ArrayList <TypeDescriptor >();
837
878
@@ -857,8 +898,8 @@ public List<TypeDescriptor> getSourceTypes() {
857
898
}
858
899
}
859
900
860
- private static class MyConditionalConverterFactory implements
861
- ConverterFactory <String , Color >, ConditionalConverter {
901
+
902
+ private static class MyConditionalConverterFactory implements ConverterFactory <String , Color >, ConditionalConverter {
862
903
863
904
private MyConditionalConverter converter = new MyConditionalConverter ();
864
905
@@ -885,6 +926,7 @@ public int getNestedMatchAttempts() {
885
926
}
886
927
}
887
928
929
+
888
930
interface MyEnumInterface {
889
931
890
932
String getCode ();
@@ -900,6 +942,23 @@ public String getCode() {
900
942
}
901
943
}
902
944
945
+
946
+ public static class MyStringToRawCollectionConverter implements Converter <String , Collection > {
947
+
948
+ @ Override
949
+ public Collection convert (String source ) {
950
+ return Collections .singleton (source + "X" );
951
+ }
952
+ }
953
+
954
+ public static class MyStringToGenericCollectionConverter implements Converter <String , Collection <?>> {
955
+
956
+ @ Override
957
+ public Collection <?> convert (String source ) {
958
+ return Collections .singleton (source + "X" );
959
+ }
960
+ }
961
+
903
962
private static class MyEnumInterfaceToStringConverter <T extends MyEnumInterface > implements Converter <T , String > {
904
963
905
964
@ Override
@@ -924,12 +983,13 @@ public Collection<Integer> convert(String source) {
924
983
}
925
984
}
926
985
927
- public Collection <String > stringCollection ;
928
-
929
- public Collection <Integer > integerCollection ;
930
986
931
987
public Collection rawCollection ;
932
988
933
989
public Collection <?> genericCollection ;
934
990
991
+ public Collection <String > stringCollection ;
992
+
993
+ public Collection <Integer > integerCollection ;
994
+
935
995
}
0 commit comments