54
54
55
55
import static org .assertj .core .api .Assertions .assertThat ;
56
56
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
57
+ import static org .assertj .core .api .Assertions .entry ;
57
58
58
59
/**
59
60
* Unit tests for {@link DefaultConversionService}.
@@ -324,8 +325,8 @@ void numberToNumber() {
324
325
325
326
@ Test
326
327
void numberToNumberNotSupportedNumber () {
327
- assertThatExceptionOfType (ConversionFailedException .class ). isThrownBy (() ->
328
- conversionService .convert (1 , CustomNumber .class ));
328
+ assertThatExceptionOfType (ConversionFailedException .class )
329
+ . isThrownBy (() -> conversionService .convert (1 , CustomNumber .class ));
329
330
}
330
331
331
332
@ Test
@@ -342,44 +343,45 @@ void characterToNumber() {
342
343
343
344
@ Test
344
345
void convertArrayToCollectionInterface () {
345
- Collection <?> result = conversionService .convert (new String [] {"1" , "2" , "3" }, Collection .class );
346
- assertThat (result ).isEqualTo (Set .of ("1" , "2" , "3" ));
346
+ @ SuppressWarnings ("unchecked" )
347
+ Collection <String > result = conversionService .convert (new String [] {"1" , "2" , "3" }, Collection .class );
348
+ assertThat (result ).isExactlyInstanceOf (LinkedHashSet .class ).containsExactly ("1" , "2" , "3" );
347
349
}
348
350
349
351
@ Test
350
352
void convertArrayToSetInterface () {
351
- Collection <?> result = conversionService .convert (new String [] {"1" , "2" , "3" }, Set .class );
352
- assertThat (result ).isEqualTo (Set .of ("1" , "2" , "3" ));
353
+ @ SuppressWarnings ("unchecked" )
354
+ Collection <String > result = conversionService .convert (new String [] {"1" , "2" , "3" }, Set .class );
355
+ assertThat (result ).isExactlyInstanceOf (LinkedHashSet .class ).containsExactly ("1" , "2" , "3" );
353
356
}
354
357
355
358
@ Test
356
359
void convertArrayToListInterface () {
357
- List <?> result = conversionService .convert (new String [] {"1" , "2" , "3" }, List .class );
358
- assertThat (result ).isEqualTo (List .of ("1" , "2" , "3" ));
360
+ @ SuppressWarnings ("unchecked" )
361
+ List <String > result = conversionService .convert (new String [] {"1" , "2" , "3" }, List .class );
362
+ assertThat (result ).isExactlyInstanceOf (ArrayList .class ).containsExactly ("1" , "2" , "3" );
359
363
}
360
364
361
365
@ Test
362
366
void convertArrayToCollectionGenericTypeConversion () throws Exception {
363
367
@ SuppressWarnings ("unchecked" )
364
- List <Integer > result = (List <Integer >) conversionService .convert (new String [] {"1" , "2" , "3" }, TypeDescriptor
365
- .valueOf (String [].class ), new TypeDescriptor (getClass ().getDeclaredField ("genericList" )));
366
- assertThat (result ).isEqualTo ( List . of (1 , 2 , 3 ) );
368
+ List <Integer > result = (List <Integer >) conversionService .convert (new String [] {"1" , "2" , "3" },
369
+ TypeDescriptor .valueOf (String [].class ), new TypeDescriptor (getClass ().getDeclaredField ("genericList" )));
370
+ assertThat (result ).isExactlyInstanceOf ( ArrayList . class ). containsExactly (1 , 2 , 3 );
367
371
}
368
372
369
373
@ Test
370
374
void convertArrayToStream () throws Exception {
371
375
String [] source = {"1" , "3" , "4" };
372
376
@ SuppressWarnings ("unchecked" )
373
377
Stream <Integer > result = (Stream <Integer >) this .conversionService .convert (source ,
374
- TypeDescriptor .valueOf (String [].class ),
375
- new TypeDescriptor (getClass ().getDeclaredField ("genericStream" )));
378
+ TypeDescriptor .valueOf (String [].class ), new TypeDescriptor (getClass ().getDeclaredField ("genericStream" )));
376
379
assertThat (result ).containsExactly (1 , 3 , 4 );
377
380
}
378
381
379
382
@ Test
380
383
void spr7766 () throws Exception {
381
- ConverterRegistry registry = (conversionService );
382
- registry .addConverter (new ColorConverter ());
384
+ conversionService .addConverter (new ColorConverter ());
383
385
@ SuppressWarnings ("unchecked" )
384
386
List <Color > colors = (List <Color >) conversionService .convert (new String [] {"ffffff" , "#000000" },
385
387
TypeDescriptor .valueOf (String [].class ),
@@ -389,14 +391,15 @@ void spr7766() throws Exception {
389
391
390
392
@ Test
391
393
void convertArrayToCollectionImpl () {
392
- ArrayList <?> result = conversionService .convert (new String [] {"1" , "2" , "3" }, ArrayList .class );
393
- assertThat (result ).isEqualTo (List .of ("1" , "2" , "3" ));
394
+ @ SuppressWarnings ("unchecked" )
395
+ ArrayList <String > result = conversionService .convert (new String [] {"1" , "2" , "3" }, ArrayList .class );
396
+ assertThat (result ).isExactlyInstanceOf (ArrayList .class ).containsExactly ("1" , "2" , "3" );
394
397
}
395
398
396
399
@ Test
397
400
void convertArrayToAbstractCollection () {
398
- assertThatExceptionOfType (ConversionFailedException .class ). isThrownBy (() ->
399
- conversionService .convert (new String []{"1" , "2" , "3" }, AbstractList .class ));
401
+ assertThatExceptionOfType (ConversionFailedException .class )
402
+ . isThrownBy (() -> conversionService .convert (new String []{"1" , "2" , "3" }, AbstractList .class ));
400
403
}
401
404
402
405
@ Test
@@ -465,8 +468,7 @@ void convertArrayToObjectAssignableTargetType() {
465
468
@ Test
466
469
void convertObjectToArray () {
467
470
Object [] result = conversionService .convert (3L , Object [].class );
468
- assertThat (result ).hasSize (1 );
469
- assertThat (result [0 ]).isEqualTo (3L );
471
+ assertThat (result ).containsExactly (3L );
470
472
}
471
473
472
474
@ Test
@@ -506,15 +508,17 @@ void convertCollectionToStringWithElementConversion() throws Exception {
506
508
507
509
@ Test
508
510
void convertStringToCollection () {
509
- List <?> result = conversionService .convert ("1,2,3" , List .class );
510
- assertThat (result ).isEqualTo (List .of ("1" , "2" , "3" ));
511
+ @ SuppressWarnings ("unchecked" )
512
+ List <String > result = conversionService .convert ("1,2,3" , List .class );
513
+ assertThat (result ).containsExactly ("1" , "2" , "3" );
511
514
}
512
515
513
516
@ Test
514
517
void convertStringToCollectionWithElementConversion () throws Exception {
515
- List <?> result = (List <?>) conversionService .convert ("1,2,3" , TypeDescriptor .valueOf (String .class ),
518
+ @ SuppressWarnings ("unchecked" )
519
+ List <Integer > result = (List <Integer >) conversionService .convert ("1,2,3" , TypeDescriptor .valueOf (String .class ),
516
520
new TypeDescriptor (getClass ().getField ("genericList" )));
517
- assertThat (result ).isEqualTo ( List . of ( 1 , 2 , 3 ) );
521
+ assertThat (result ).containsExactly ( 1 , 2 , 3 );
518
522
}
519
523
520
524
@ Test
@@ -539,26 +543,24 @@ void convertCollectionToObjectWithElementConversion() {
539
543
540
544
@ Test
541
545
void convertCollectionToObjectAssignableTarget () throws Exception {
542
- Collection <String > source = new ArrayList <>();
543
- source .add ("foo" );
546
+ Collection <String > source = List .of ("foo" );
544
547
Object result = conversionService .convert (source , new TypeDescriptor (getClass ().getField ("assignableTarget" )));
545
- assertThat (result ).isEqualTo (source );
548
+ assertThat (result ).isSameAs (source );
546
549
}
547
550
548
551
@ Test
549
552
void convertCollectionToObjectWithCustomConverter () {
550
- List <String > source = new ArrayList <>();
551
- source .add ("A" );
552
- source .add ("B" );
553
+ List <String > source = List .of ("A" , "B" );
553
554
conversionService .addConverter (List .class , ListWrapper .class , ListWrapper ::new );
554
555
ListWrapper result = conversionService .convert (source , ListWrapper .class );
555
556
assertThat (result .getList ()).isSameAs (source );
556
557
}
557
558
558
559
@ Test
559
560
void convertObjectToCollection () {
560
- List <?> result = conversionService .convert (3L , List .class );
561
- assertThat (result ).isEqualTo (List .of (3L ));
561
+ @ SuppressWarnings ("unchecked" )
562
+ List <Long > result = conversionService .convert (3L , List .class );
563
+ assertThat (result ).containsExactly (3L );
562
564
}
563
565
564
566
@ Test
@@ -607,7 +609,7 @@ void convertObjectArrayToIntArray() {
607
609
608
610
@ Test
609
611
void convertByteArrayToWrapperArray () {
610
- byte [] byteArray = new byte [] {1 , 2 , 3 };
612
+ byte [] byteArray = {1 , 2 , 3 };
611
613
Byte [] converted = conversionService .convert (byteArray , Byte [].class );
612
614
assertThat (converted ).isEqualTo (new Byte []{1 , 2 , 3 });
613
615
}
@@ -667,21 +669,18 @@ void convertCollectionToCollectionNull() throws Exception {
667
669
@ SuppressWarnings ("unchecked" )
668
670
List <Integer > bar = (List <Integer >) conversionService .convert (null ,
669
671
TypeDescriptor .valueOf (LinkedHashSet .class ), new TypeDescriptor (getClass ().getField ("genericList" )));
670
- assertThat (( Object ) bar ).isNull ();
672
+ assertThat (bar ).isNull ();
671
673
}
672
674
673
675
@ Test
674
- @ SuppressWarnings ("rawtypes" )
676
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" } )
675
677
void convertCollectionToCollectionNotGeneric () {
676
678
Set <String > foo = new LinkedHashSet <>();
677
679
foo .add ("1" );
678
680
foo .add ("2" );
679
681
foo .add ("3" );
680
- List bar = (List ) conversionService .convert (foo , TypeDescriptor .valueOf (LinkedHashSet .class ), TypeDescriptor
681
- .valueOf (List .class ));
682
- assertThat (bar .get (0 )).isEqualTo ("1" );
683
- assertThat (bar .get (1 )).isEqualTo ("2" );
684
- assertThat (bar .get (2 )).isEqualTo ("3" );
682
+ List bar = (List ) conversionService .convert (foo , TypeDescriptor .valueOf (LinkedHashSet .class ), TypeDescriptor .valueOf (List .class ));
683
+ assertThat (bar ).containsExactly ("1" , "2" , "3" );
685
684
}
686
685
687
686
@ Test
@@ -694,43 +693,35 @@ void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception {
694
693
Collection values = map .values ();
695
694
List <Integer > bar = (List <Integer >) conversionService .convert (values ,
696
695
TypeDescriptor .forObject (values ), new TypeDescriptor (getClass ().getField ("genericList" )));
697
- assertThat (bar ).hasSize (3 );
698
- assertThat (bar .get (0 )).isEqualTo (1 );
699
- assertThat (bar .get (1 )).isEqualTo (2 );
700
- assertThat (bar .get (2 )).isEqualTo (3 );
696
+ assertThat (bar ).containsExactly (1 , 2 , 3 );
701
697
}
702
698
703
699
@ Test
704
700
void collection () {
705
- List <String > strings = new ArrayList <>();
706
- strings .add ("3" );
707
- strings .add ("9" );
701
+ List <String > strings = List .of ("3" , "9" );
708
702
@ SuppressWarnings ("unchecked" )
709
703
List <Integer > integers = (List <Integer >) conversionService .convert (strings ,
710
704
TypeDescriptor .collection (List .class , TypeDescriptor .valueOf (Integer .class )));
711
- assertThat (integers .get (0 )).isEqualTo (3 );
712
- assertThat (integers .get (1 )).isEqualTo (9 );
705
+ assertThat (integers ).containsExactly (3 , 9 );
713
706
}
714
707
715
708
@ Test
716
709
void convertMapToMap () throws Exception {
717
- Map <String , String > foo = new HashMap <>();
718
- foo .put ("1" , "BAR" );
719
- foo .put ("2" , "BAZ" );
710
+ Map <String , String > foo = Map .of ("1" , "BAR" , "2" , "BAZ" );
720
711
@ SuppressWarnings ("unchecked" )
721
712
Map <Integer , Foo > map = (Map <Integer , Foo >) conversionService .convert (foo ,
722
713
TypeDescriptor .forObject (foo ), new TypeDescriptor (getClass ().getField ("genericMap" )));
723
- assertThat (map .get (1 )).isEqualTo (Foo .BAR );
724
- assertThat (map .get (2 )).isEqualTo (Foo .BAZ );
714
+ assertThat (map ).contains (entry (1 , Foo .BAR ), entry (2 , Foo .BAZ ));
725
715
}
726
716
727
717
@ Test
728
718
void convertHashMapValuesToList () {
729
719
Map <String , Integer > hashMap = new LinkedHashMap <>();
730
720
hashMap .put ("1" , 1 );
731
721
hashMap .put ("2" , 2 );
732
- List <?> converted = conversionService .convert (hashMap .values (), List .class );
733
- assertThat (converted ).isEqualTo (List .of (1 , 2 ));
722
+ @ SuppressWarnings ("unchecked" )
723
+ List <Integer > converted = conversionService .convert (hashMap .values (), List .class );
724
+ assertThat (converted ).containsExactly (1 , 2 );
734
725
}
735
726
736
727
@ Test
@@ -741,8 +732,7 @@ void map() {
741
732
@ SuppressWarnings ("unchecked" )
742
733
Map <Integer , Integer > integers = (Map <Integer , Integer >) conversionService .convert (strings ,
743
734
TypeDescriptor .map (Map .class , TypeDescriptor .valueOf (Integer .class ), TypeDescriptor .valueOf (Integer .class )));
744
- assertThat (integers .get (3 )).isEqualTo (9 );
745
- assertThat (integers .get (6 )).isEqualTo (31 );
735
+ assertThat (integers ).contains (entry (3 , 9 ), entry (6 , 31 ));
746
736
}
747
737
748
738
@ Test
@@ -751,25 +741,25 @@ void convertPropertiesToString() {
751
741
foo .setProperty ("1" , "BAR" );
752
742
foo .setProperty ("2" , "BAZ" );
753
743
String result = conversionService .convert (foo , String .class );
754
- assertThat (result ).contains ("1=BAR" );
755
- assertThat (result ).contains ("2=BAZ" );
744
+ assertThat (result ).contains ("1=BAR" , "2=BAZ" );
756
745
}
757
746
758
747
@ Test
759
748
void convertStringToProperties () {
760
- Properties result = conversionService .convert ("a=b \n c=2 \n d=" , Properties . class );
761
- assertThat ( result ). hasSize ( 3 );
762
- assertThat ( result . getProperty ( "a" )). isEqualTo ( "b" );
763
- assertThat ( result . getProperty ( "c" )). isEqualTo ( "2" );
764
- assertThat (result . getProperty ( "d" )). isEmpty ( );
749
+ Properties result = conversionService .convert ("""
750
+ a=b
751
+ c=2
752
+ d=""" , Properties . class );
753
+ assertThat (result ). contains ( entry ( "a" , "b" ), entry ( "c" , "2" ), entry ( "d" , "" ) );
765
754
}
766
755
767
756
@ Test
768
- void convertStringToPropertiesWithSpaces () {
769
- Properties result = conversionService .convert (" foo=bar\n bar=baz\n baz=boop" , Properties .class );
770
- assertThat (result .get ("foo" )).isEqualTo ("bar" );
771
- assertThat (result .get ("bar" )).isEqualTo ("baz" );
772
- assertThat (result .get ("baz" )).isEqualTo ("boop" );
757
+ void convertStringToPropertiesWithLeadingSpaces () {
758
+ Properties result = conversionService .convert ("""
759
+ \s foo=bar
760
+ \s bar=baz
761
+ \s baz=boo""" , Properties .class );
762
+ assertThat (result ).contains (entry ("foo" , "bar" ), entry ("bar" , "baz" ), entry ("baz" , "boo" ));
773
763
}
774
764
775
765
// generic object conversion
@@ -838,8 +828,8 @@ void convertObjectToObjectWithJavaTimeOfMethod() {
838
828
839
829
@ Test
840
830
void convertObjectToObjectNoValueOfMethodOrConstructor () {
841
- assertThatExceptionOfType (ConverterNotFoundException .class ). isThrownBy (() ->
842
- conversionService .convert (3L , SSN .class ));
831
+ assertThatExceptionOfType (ConverterNotFoundException .class )
832
+ . isThrownBy (() -> conversionService .convert (3L , SSN .class ));
843
833
}
844
834
845
835
@ Test
@@ -870,21 +860,20 @@ void convertCharArrayToString() {
870
860
@ Test
871
861
void convertStringToCharArray () {
872
862
char [] converted = conversionService .convert ("a,b,c" , char [].class );
873
- assertThat (converted ).isEqualTo ( new char []{ 'a' , 'b' , 'c' } );
863
+ assertThat (converted ).containsExactly ( 'a' , 'b' , 'c' );
874
864
}
875
865
876
866
@ Test
877
867
void convertStringToCustomCharArray () {
878
868
conversionService .addConverter (String .class , char [].class , String ::toCharArray );
879
869
char [] converted = conversionService .convert ("abc" , char [].class );
880
- assertThat (converted ).isEqualTo ( new char [] { 'a' , 'b' , 'c' } );
870
+ assertThat (converted ).containsExactly ( 'a' , 'b' , 'c' );
881
871
}
882
872
883
873
@ Test
884
874
@ SuppressWarnings ("unchecked" )
885
875
void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly () {
886
- String [][] grid = new String [][] {new String [] {"1" , "2" , "3" , "4" }, new String [] {"5" , "6" , "7" , "8" },
887
- new String [] {"9" , "10" , "11" , "12" }};
876
+ String [][] grid = new String [][] {{"1" , "2" , "3" , "4" }, {"5" , "6" , "7" , "8" }, {"9" , "10" , "11" , "12" }};
888
877
List <String []> converted = conversionService .convert (grid , List .class );
889
878
String [][] convertedBack = conversionService .convert (converted , String [][].class );
890
879
assertThat (convertedBack ).isEqualTo (grid );
@@ -893,10 +882,10 @@ void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly() {
893
882
@ Test
894
883
void convertCannotOptimizeArray () {
895
884
conversionService .addConverter (Byte .class , Byte .class , source -> (byte ) (source + 1 ));
896
- byte [] byteArray = new byte [] {1 , 2 , 3 };
885
+ byte [] byteArray = {1 , 2 , 3 };
897
886
byte [] converted = conversionService .convert (byteArray , byte [].class );
898
887
assertThat (converted ).isNotSameAs (byteArray );
899
- assertThat (converted ).isEqualTo ( new byte []{ 2 , 3 , 4 } );
888
+ assertThat (converted ).containsExactly ( 2 , 3 , 4 );
900
889
}
901
890
902
891
@ Test
0 commit comments