15
15
*/
16
16
package org .springframework .data .repository .util ;
17
17
18
- import static org .assertj .core .api .Assertions .* ;
19
- import static org .springframework . data . repository . util . ClassUtils .* ;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj . core . api . Assertions . assertThatIllegalStateException ;
20
20
21
21
import java .io .Serializable ;
22
+ import java .lang .reflect .Method ;
22
23
import java .util .List ;
23
24
import java .util .Map ;
24
25
import java .util .concurrent .Future ;
25
26
26
27
import org .junit .jupiter .api .Test ;
27
28
import org .springframework .data .domain .Page ;
28
29
import org .springframework .data .domain .Pageable ;
30
+ import org .springframework .data .domain .Sort ;
29
31
import org .springframework .data .repository .Repository ;
30
32
import org .springframework .scheduling .annotation .Async ;
31
33
32
34
/**
33
- * Unit test for {@link ClassUtils}.
35
+ * Unit tests for {@link ClassUtils}.
34
36
*
35
37
* @author Oliver Gierke
38
+ * @author John Blum
36
39
*/
37
40
class ClassUtilsUnitTests {
38
41
39
42
@ Test
40
43
void rejectsInvalidReturnType () {
41
- assertThatIllegalStateException ().isThrownBy (() -> assertReturnTypeAssignable (
44
+ assertThatIllegalStateException ().isThrownBy (() -> ClassUtils . assertReturnTypeAssignable (
42
45
SomeDao .class .getMethod ("findByFirstname" , Pageable .class , String .class ), User .class ));
43
46
}
44
47
45
48
@ Test
46
49
void determinesValidFieldsCorrectly () {
47
50
48
- assertThat (hasProperty (User .class , "firstname" )).isTrue ();
49
- assertThat (hasProperty (User .class , "Firstname" )).isTrue ();
50
- assertThat (hasProperty (User .class , "address" )).isFalse ();
51
+ assertThat (ClassUtils . hasProperty (User .class , "firstname" )).isTrue ();
52
+ assertThat (ClassUtils . hasProperty (User .class , "Firstname" )).isTrue ();
53
+ assertThat (ClassUtils . hasProperty (User .class , "address" )).isFalse ();
51
54
}
52
55
53
56
@ Test // DATACMNS-769
54
57
void unwrapsWrapperTypesBeforeAssignmentCheck () throws Exception {
55
- assertReturnTypeAssignable (UserRepository .class .getMethod ("findAsync" , Pageable .class ), Page .class );
58
+ ClassUtils .assertReturnTypeAssignable (UserRepository .class .getMethod ("findAsync" , Pageable .class ),
59
+ Page .class );
60
+ }
61
+
62
+ @ Test
63
+ public void numberOfOccurrencesForMultipleMethodParametersOfType () throws Exception {
64
+
65
+ Method findByAddress = AnotherDao .class .getMethod ("findByAddress" , Pageable .class , Pageable .class );
66
+
67
+ assertThat (ClassUtils .getNumberOfOccurrences (findByAddress , Pageable .class )).isEqualTo (2 );
68
+ }
69
+
70
+ @ Test
71
+ public void numberOfOccurrencesForNoMethodParameterOfType () throws Exception {
72
+
73
+ Method findByAddress = AnotherDao .class .getMethod ("findByAddress" , Pageable .class , Pageable .class );
74
+
75
+ assertThat (ClassUtils .getNumberOfOccurrences (findByAddress , Sort .class )).isZero ();
76
+ assertThat (ClassUtils .getNumberOfOccurrences (findByAddress , Page .class )).isZero ();
77
+ }
78
+
79
+ @ Test
80
+ public void numberOfOccurrencesForSingleMethodParameterOfType () throws Exception {
81
+
82
+ Method findByFirstname = SomeDao .class .getMethod ("findByFirstname" , Pageable .class , String .class );
83
+
84
+ assertThat (ClassUtils .getNumberOfOccurrences (findByFirstname , Pageable .class )).isOne ();
85
+ assertThat (ClassUtils .getNumberOfOccurrences (findByFirstname , String .class )).isOne ();
56
86
}
57
87
58
88
@ SuppressWarnings ("unused" )
@@ -66,7 +96,7 @@ String getAddress() {
66
96
}
67
97
}
68
98
69
- static interface UserRepository extends Repository <User , Integer > {
99
+ interface UserRepository extends Repository <User , Integer > {
70
100
71
101
@ Async
72
102
Future <Page <User >> findAsync (Pageable pageable );
@@ -81,6 +111,12 @@ interface SomeDao extends Serializable, UserRepository {
81
111
List <Map <String , Object >> anotherMethod ();
82
112
}
83
113
114
+ interface AnotherDao extends Repository <User , Integer > {
115
+
116
+ Page <User > findByAddress (Pageable pageableOne , Pageable pageableTwo );
117
+
118
+ }
119
+
84
120
class GenericType <T > {
85
121
86
122
}
0 commit comments