1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -134,7 +134,7 @@ public void testBooleanToString() {
134
134
}
135
135
136
136
@ Test
137
- public void testStringToByte () throws Exception {
137
+ public void testStringToByte () {
138
138
assertEquals (Byte .valueOf ("1" ), conversionService .convert ("1" , Byte .class ));
139
139
}
140
140
@@ -225,12 +225,12 @@ public void testStringToNumberEmptyString() {
225
225
}
226
226
227
227
@ Test
228
- public void testStringToEnum () throws Exception {
228
+ public void testStringToEnum () {
229
229
assertEquals (Foo .BAR , conversionService .convert ("BAR" , Foo .class ));
230
230
}
231
231
232
232
@ Test
233
- public void testStringToEnumWithSubclass () throws Exception {
233
+ public void testStringToEnumWithSubclass () {
234
234
assertEquals (SubFoo .BAZ , conversionService .convert ("BAZ" , SubFoo .BAR .getClass ()));
235
235
}
236
236
@@ -245,12 +245,12 @@ public void testEnumToString() {
245
245
}
246
246
247
247
@ Test
248
- public void testIntegerToEnum () throws Exception {
248
+ public void testIntegerToEnum () {
249
249
assertEquals (Foo .BAR , conversionService .convert (0 , Foo .class ));
250
250
}
251
251
252
252
@ Test
253
- public void testIntegerToEnumWithSubclass () throws Exception {
253
+ public void testIntegerToEnumWithSubclass () {
254
254
assertEquals (SubFoo .BAZ , conversionService .convert (1 , SubFoo .BAR .getClass ()));
255
255
}
256
256
@@ -395,10 +395,6 @@ public void convertArrayToAbstractCollection() {
395
395
conversionService .convert (new String []{"1" , "2" , "3" }, AbstractList .class );
396
396
}
397
397
398
- public static enum FooEnum {
399
- BAR , BAZ
400
- }
401
-
402
398
@ Test
403
399
public void convertArrayToString () {
404
400
String result = conversionService .convert (new String [] {"1" , "2" , "3" }, String .class );
@@ -525,19 +521,17 @@ public void convertCollectionToStringWithElementConversion() throws Exception {
525
521
}
526
522
527
523
@ Test
528
- @ SuppressWarnings ("rawtypes" )
529
524
public void convertStringToCollection () {
530
- List result = conversionService .convert ("1,2,3" , List .class );
525
+ List <?> result = conversionService .convert ("1,2,3" , List .class );
531
526
assertEquals (3 , result .size ());
532
527
assertEquals ("1" , result .get (0 ));
533
528
assertEquals ("2" , result .get (1 ));
534
529
assertEquals ("3" , result .get (2 ));
535
530
}
536
531
537
532
@ Test
538
- @ SuppressWarnings ("rawtypes" )
539
533
public void convertStringToCollectionWithElementConversion () throws Exception {
540
- List result = (List ) conversionService .convert ("1,2,3" , TypeDescriptor .valueOf (String .class ),
534
+ List <?> result = (List ) conversionService .convert ("1,2,3" , TypeDescriptor .valueOf (String .class ),
541
535
new TypeDescriptor (getClass ().getField ("genericList" )));
542
536
assertEquals (3 , result .size ());
543
537
assertEquals (1 , result .get (0 ));
@@ -546,9 +540,8 @@ public void convertStringToCollectionWithElementConversion() throws Exception {
546
540
}
547
541
548
542
@ Test
549
- @ SuppressWarnings ("rawtypes" )
550
543
public void convertEmptyStringToCollection () {
551
- Collection result = conversionService .convert ("" , Collection .class );
544
+ Collection <?> result = conversionService .convert ("" , Collection .class );
552
545
assertEquals (0 , result .size ());
553
546
}
554
547
@@ -575,25 +568,18 @@ public void convertCollectionToObjectAssignableTarget() throws Exception {
575
568
}
576
569
577
570
@ Test
578
- @ SuppressWarnings ("rawtypes" )
579
- public void convertCollectionToObjectWithCustomConverter () throws Exception {
571
+ public void convertCollectionToObjectWithCustomConverter () {
580
572
List <String > source = new ArrayList <>();
581
573
source .add ("A" );
582
574
source .add ("B" );
583
- conversionService .addConverter (new Converter <List , ListWrapper >() {
584
- @ Override
585
- public ListWrapper convert (List source ) {
586
- return new ListWrapper (source );
587
- }
588
- });
575
+ conversionService .addConverter (List .class , ListWrapper .class , ListWrapper ::new );
589
576
ListWrapper result = conversionService .convert (source , ListWrapper .class );
590
577
assertSame (source , result .getList ());
591
578
}
592
579
593
580
@ Test
594
- @ SuppressWarnings ("rawtypes" )
595
581
public void convertObjectToCollection () {
596
- List result = conversionService .convert (3L , List .class );
582
+ List <?> result = conversionService .convert (3L , List .class );
597
583
assertEquals (1 , result .size ());
598
584
assertEquals (3L , result .get (0 ));
599
585
}
@@ -608,23 +594,55 @@ public void convertObjectToCollectionWithElementConversion() throws Exception {
608
594
}
609
595
610
596
@ Test
611
- public void convertArrayToArray () {
597
+ public void convertStringArrayToIntegerArray () {
612
598
Integer [] result = conversionService .convert (new String [] {"1" , "2" , "3" }, Integer [].class );
613
599
assertEquals (Integer .valueOf (1 ), result [0 ]);
614
600
assertEquals (Integer .valueOf (2 ), result [1 ]);
615
601
assertEquals (Integer .valueOf (3 ), result [2 ]);
616
602
}
617
603
618
604
@ Test
619
- public void convertArrayToPrimitiveArray () {
605
+ public void convertStringArrayToIntArray () {
620
606
int [] result = conversionService .convert (new String [] {"1" , "2" , "3" }, int [].class );
621
607
assertEquals (1 , result [0 ]);
622
608
assertEquals (2 , result [1 ]);
623
609
assertEquals (3 , result [2 ]);
624
610
}
625
611
626
612
@ Test
627
- public void convertArrayToWrapperArray () {
613
+ public void convertIntegerArrayToIntegerArray () {
614
+ Integer [] result = conversionService .convert (new Integer [] {1 , 2 , 3 }, Integer [].class );
615
+ assertEquals (Integer .valueOf (1 ), result [0 ]);
616
+ assertEquals (Integer .valueOf (2 ), result [1 ]);
617
+ assertEquals (Integer .valueOf (3 ), result [2 ]);
618
+ }
619
+
620
+ @ Test
621
+ public void convertIntegerArrayToIntArray () {
622
+ int [] result = conversionService .convert (new Integer [] {1 , 2 , 3 }, int [].class );
623
+ assertEquals (1 , result [0 ]);
624
+ assertEquals (2 , result [1 ]);
625
+ assertEquals (3 , result [2 ]);
626
+ }
627
+
628
+ @ Test
629
+ public void convertObjectArrayToIntegerArray () {
630
+ Integer [] result = conversionService .convert (new Object [] {1 , 2 , 3 }, Integer [].class );
631
+ assertEquals (Integer .valueOf (1 ), result [0 ]);
632
+ assertEquals (Integer .valueOf (2 ), result [1 ]);
633
+ assertEquals (Integer .valueOf (3 ), result [2 ]);
634
+ }
635
+
636
+ @ Test
637
+ public void convertObjectArrayToIntArray () {
638
+ int [] result = conversionService .convert (new Object [] {1 , 2 , 3 }, int [].class );
639
+ assertEquals (1 , result [0 ]);
640
+ assertEquals (2 , result [1 ]);
641
+ assertEquals (3 , result [2 ]);
642
+ }
643
+
644
+ @ Test
645
+ public void convertByteArrayToWrapperArray () {
628
646
byte [] byteArray = new byte [] {1 , 2 , 3 };
629
647
Byte [] converted = conversionService .convert (byteArray , Byte [].class );
630
648
assertThat (converted , equalTo (new Byte []{1 , 2 , 3 }));
@@ -694,7 +712,7 @@ public void convertCollectionToCollectionNull() throws Exception {
694
712
695
713
@ Test
696
714
@ SuppressWarnings ("rawtypes" )
697
- public void convertCollectionToCollectionNotGeneric () throws Exception {
715
+ public void convertCollectionToCollectionNotGeneric () {
698
716
Set <String > foo = new LinkedHashSet <>();
699
717
foo .add ("1" );
700
718
foo .add ("2" );
@@ -740,10 +758,10 @@ public void convertMapToMap() throws Exception {
740
758
foo .put ("1" , "BAR" );
741
759
foo .put ("2" , "BAZ" );
742
760
@ SuppressWarnings ("unchecked" )
743
- Map <Integer , FooEnum > map = (Map <Integer , FooEnum >) conversionService .convert (foo ,
761
+ Map <Integer , Foo > map = (Map <Integer , Foo >) conversionService .convert (foo ,
744
762
TypeDescriptor .forObject (foo ), new TypeDescriptor (getClass ().getField ("genericMap" )));
745
- assertEquals (FooEnum .BAR , map .get (1 ));
746
- assertEquals (FooEnum .BAZ , map .get (2 ));
763
+ assertEquals (Foo .BAR , map .get (1 ));
764
+ assertEquals (Foo .BAZ , map .get (2 ));
747
765
}
748
766
749
767
@ Test
@@ -881,25 +899,20 @@ public void convertObjectToObjectFinderMethodWithIdConversion() {
881
899
}
882
900
883
901
@ Test
884
- public void convertCharArrayToString () throws Exception {
902
+ public void convertCharArrayToString () {
885
903
String converted = conversionService .convert (new char [] {'a' , 'b' , 'c' }, String .class );
886
904
assertThat (converted , equalTo ("a,b,c" ));
887
905
}
888
906
889
907
@ Test
890
- public void convertStringToCharArray () throws Exception {
908
+ public void convertStringToCharArray () {
891
909
char [] converted = conversionService .convert ("a,b,c" , char [].class );
892
910
assertThat (converted , equalTo (new char []{'a' , 'b' , 'c' }));
893
911
}
894
912
895
913
@ Test
896
- public void convertStringToCustomCharArray () throws Exception {
897
- conversionService .addConverter (new Converter <String , char []>() {
898
- @ Override
899
- public char [] convert (String source ) {
900
- return source .toCharArray ();
901
- }
902
- });
914
+ public void convertStringToCustomCharArray () {
915
+ conversionService .addConverter (String .class , char [].class , String ::toCharArray );
903
916
char [] converted = conversionService .convert ("abc" , char [].class );
904
917
assertThat (converted , equalTo (new char [] {'a' , 'b' , 'c' }));
905
918
}
@@ -916,16 +929,11 @@ public void multidimensionalArrayToListConversionShouldConvertEntriesCorrectly()
916
929
917
930
@ Test
918
931
public void convertCannotOptimizeArray () {
919
- conversionService .addConverter (new Converter <Byte , Byte >() {
920
- @ Override
921
- public Byte convert (Byte source ) {
922
- return (byte ) (source + 1 );
923
- }
924
- });
932
+ conversionService .addConverter (Byte .class , Byte .class , source -> (byte ) (source + 1 ));
925
933
byte [] byteArray = new byte [] {1 , 2 , 3 };
926
934
byte [] converted = conversionService .convert (byteArray , byte [].class );
927
935
assertNotSame (byteArray , converted );
928
- assertTrue ( Arrays . equals ( new byte [] {2 , 3 , 4 }, converted ) );
936
+ assertArrayEquals ( new byte []{2 , 3 , 4 }, converted );
929
937
}
930
938
931
939
@ Test
@@ -977,7 +985,7 @@ public void testPerformance1() {
977
985
978
986
public Stream <Integer > genericStream ;
979
987
980
- public Map <Integer , FooEnum > genericMap = new HashMap <>();
988
+ public Map <Integer , Foo > genericMap = new HashMap <>();
981
989
982
990
public EnumSet <Foo > enumSet ;
983
991
0 commit comments