1515 */
1616package org .springframework .data .repository .util ;
1717
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 ;
2020
2121import java .io .Serializable ;
22+ import java .lang .reflect .Method ;
2223import java .util .List ;
2324import java .util .Map ;
2425import java .util .concurrent .Future ;
2526
2627import org .junit .jupiter .api .Test ;
2728import org .springframework .data .domain .Page ;
2829import org .springframework .data .domain .Pageable ;
30+ import org .springframework .data .domain .Sort ;
2931import org .springframework .data .repository .Repository ;
3032import org .springframework .scheduling .annotation .Async ;
3133
3234/**
33- * Unit test for {@link ClassUtils}.
35+ * Unit tests for {@link ClassUtils}.
3436 *
3537 * @author Oliver Gierke
38+ * @author John Blum
3639 */
3740class ClassUtilsUnitTests {
3841
3942 @ Test
4043 void rejectsInvalidReturnType () {
41- assertThatIllegalStateException ().isThrownBy (() -> assertReturnTypeAssignable (
44+ assertThatIllegalStateException ().isThrownBy (() -> ClassUtils . assertReturnTypeAssignable (
4245 SomeDao .class .getMethod ("findByFirstname" , Pageable .class , String .class ), User .class ));
4346 }
4447
4548 @ Test
4649 void determinesValidFieldsCorrectly () {
4750
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 ();
5154 }
5255
5356 @ Test // DATACMNS-769
5457 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 ();
5686 }
5787
5888 @ SuppressWarnings ("unused" )
@@ -66,7 +96,7 @@ String getAddress() {
6696 }
6797 }
6898
69- static interface UserRepository extends Repository <User , Integer > {
99+ interface UserRepository extends Repository <User , Integer > {
70100
71101 @ Async
72102 Future <Page <User >> findAsync (Pageable pageable );
@@ -81,6 +111,12 @@ interface SomeDao extends Serializable, UserRepository {
81111 List <Map <String , Object >> anotherMethod ();
82112 }
83113
114+ interface AnotherDao extends Repository <User , Integer > {
115+
116+ Page <User > findByAddress (Pageable pageableOne , Pageable pageableTwo );
117+
118+ }
119+
84120 class GenericType <T > {
85121
86122 }
0 commit comments