16
16
17
17
package org .springframework .aop .aspectj ;
18
18
19
- import java .io .Serial ;
20
- import java .lang .reflect .Field ;
21
19
import java .lang .reflect .Method ;
22
20
import java .util .Arrays ;
21
+ import java .util .function .Consumer ;
23
22
24
23
import org .aspectj .lang .JoinPoint ;
25
24
import org .aspectj .lang .ProceedingJoinPoint ;
26
- import org .jetbrains . annotations . NotNull ;
25
+ import org .assertj . core . api . InstanceOfAssertFactories ;
27
26
import org .junit .jupiter .api .Test ;
28
27
29
28
import static org .assertj .core .api .Assertions .assertThat ;
33
32
* Tests for {@link AbstractAspectJAdvice}.
34
33
*
35
34
* @author Joshua Chen
35
+ * @author Stephane Nicoll
36
36
*/
37
37
class AbstractAspectJAdviceTests {
38
38
39
+ @ Test
40
+ void setArgumentNamesFromStringArray_withoutJoinPointParameter () {
41
+ AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithNoJoinPoint" );
42
+ assertThat (advice ).satisfies (hasArgumentNames ("arg1" , "arg2" ));
43
+ }
44
+
39
45
@ Test
40
46
void setArgumentNamesFromStringArray_withJoinPointAsFirstParameter () {
41
47
AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithJoinPointAsFirstParameter" );
42
- assertArgumentNamesFromStringArray (advice );
48
+ assertThat (advice ). satisfies ( hasArgumentNames ( "THIS_JOIN_POINT" , "arg1" , "arg2" ) );
43
49
}
44
50
45
51
@ Test
46
52
void setArgumentNamesFromStringArray_withJoinPointAsLastParameter () {
47
53
AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithJoinPointAsLastParameter" );
48
- assertThat (getArgumentNames (advice )[0 ]).isEqualTo ("arg1" );
49
- assertThat (getArgumentNames (advice )[1 ]).isEqualTo ("arg2" );
50
- assertThat (getArgumentNames (advice )[2 ]).isEqualTo ("THIS_JOIN_POINT" );
54
+ assertThat (advice ).satisfies (hasArgumentNames ("arg1" , "arg2" , "THIS_JOIN_POINT" ));
51
55
}
52
56
53
57
@ Test
54
58
void setArgumentNamesFromStringArray_withJoinPointAsMiddleParameter () {
55
59
AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithJoinPointAsMiddleParameter" );
56
- assertThat (getArgumentNames (advice )[0 ]).isEqualTo ("arg1" );
57
- assertThat (getArgumentNames (advice )[1 ]).isEqualTo ("THIS_JOIN_POINT" );
58
- assertThat (getArgumentNames (advice )[2 ]).isEqualTo ("arg2" );
60
+ assertThat (advice ).satisfies (hasArgumentNames ("arg1" , "THIS_JOIN_POINT" , "arg2" ));
59
61
}
60
62
61
63
@ Test
62
64
void setArgumentNamesFromStringArray_withProceedingJoinPoint () {
63
65
AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithProceedingJoinPoint" );
64
- assertArgumentNamesFromStringArray (advice );
66
+ assertThat (advice ). satisfies ( hasArgumentNames ( "THIS_JOIN_POINT" , "arg1" , "arg2" ) );
65
67
}
66
68
67
69
@ Test
68
70
void setArgumentNamesFromStringArray_withStaticPart () {
69
71
AbstractAspectJAdvice advice = getAspectJAdvice ("methodWithStaticPart" );
70
- assertArgumentNamesFromStringArray (advice );
72
+ assertThat (advice ). satisfies ( hasArgumentNames ( "THIS_JOIN_POINT" , "arg1" , "arg2" ) );
71
73
}
72
74
73
- private void assertArgumentNamesFromStringArray ( AbstractAspectJAdvice advice ) {
74
- assertThat (getArgumentNames ( advice )[ 0 ]). isEqualTo ( "THIS_JOIN_POINT" );
75
- assertThat ( getArgumentNames ( advice )[ 1 ]). isEqualTo ( "arg1" );
76
- assertThat ( getArgumentNames ( advice )[ 2 ]). isEqualTo ( "arg2" );
75
+ private Consumer < AbstractAspectJAdvice > hasArgumentNames ( String ... argumentNames ) {
76
+ return advice -> assertThat (advice ). extracting ( "argumentNames" )
77
+ . asInstanceOf ( InstanceOfAssertFactories . array ( String []. class ))
78
+ . containsExactly ( argumentNames );
77
79
}
78
80
79
- private @ NotNull AbstractAspectJAdvice getAspectJAdvice (final String methodName ) {
80
- AbstractAspectJAdvice advice = new TestAspectJAdvice (getMethod (methodName ), mock (AspectJExpressionPointcut .class ), mock (AspectInstanceFactory .class ));
81
+ private AbstractAspectJAdvice getAspectJAdvice (final String methodName ) {
82
+ AbstractAspectJAdvice advice = new TestAspectJAdvice (getMethod (methodName ),
83
+ mock (AspectJExpressionPointcut .class ), mock (AspectInstanceFactory .class ));
81
84
advice .setArgumentNamesFromStringArray ("arg1" , "arg2" );
82
85
return advice ;
83
86
}
84
87
85
- private Method getMethod (final String name ) {
86
- return Arrays .stream (this .getClass ().getDeclaredMethods ()).filter (m -> m .getName ().equals (name )).findFirst ().orElseThrow ();
87
- }
88
-
89
- private String [] getArgumentNames (final AbstractAspectJAdvice advice ) {
90
- try {
91
- Field field = AbstractAspectJAdvice .class .getDeclaredField ("argumentNames" );
92
- field .setAccessible (true );
93
- return (String []) field .get (advice );
94
- }
95
- catch (NoSuchFieldException | IllegalAccessException e ) {
96
- throw new RuntimeException (e );
97
- }
88
+ private Method getMethod (final String methodName ) {
89
+ return Arrays .stream (Sample .class .getDeclaredMethods ())
90
+ .filter (method -> method .getName ().equals (methodName )).findFirst ()
91
+ .orElseThrow ();
98
92
}
99
93
94
+ @ SuppressWarnings ("serial" )
100
95
public static class TestAspectJAdvice extends AbstractAspectJAdvice {
101
- @ Serial private static final long serialVersionUID = 1L ;
102
96
103
- public TestAspectJAdvice (Method aspectJAdviceMethod , AspectJExpressionPointcut pointcut , AspectInstanceFactory aspectInstanceFactory ) {
97
+ public TestAspectJAdvice (Method aspectJAdviceMethod , AspectJExpressionPointcut pointcut ,
98
+ AspectInstanceFactory aspectInstanceFactory ) {
104
99
super (aspectJAdviceMethod , pointcut , aspectInstanceFactory );
105
100
}
106
101
@@ -115,18 +110,26 @@ public boolean isAfterAdvice() {
115
110
}
116
111
}
117
112
118
- void methodWithJoinPointAsFirstParameter ( JoinPoint joinPoint , String arg1 , String arg2 ) {
119
- }
113
+ @ SuppressWarnings ( "unused" )
114
+ static class Sample {
120
115
121
- void methodWithJoinPointAsLastParameter (String arg1 , String arg2 , JoinPoint joinPoint ) {
122
- }
116
+ void methodWithNoJoinPoint (String arg1 , String arg2 ) {
117
+ }
123
118
124
- void methodWithJoinPointAsMiddleParameter ( String arg1 , JoinPoint joinPoint , String arg2 ) {
125
- }
119
+ void methodWithJoinPointAsFirstParameter ( JoinPoint joinPoint , String arg1 , String arg2 ) {
120
+ }
126
121
127
- void methodWithProceedingJoinPoint (ProceedingJoinPoint joinPoint , String arg1 , String arg2 ) {
128
- }
122
+ void methodWithJoinPointAsLastParameter (String arg1 , String arg2 , JoinPoint joinPoint ) {
123
+ }
124
+
125
+ void methodWithJoinPointAsMiddleParameter (String arg1 , JoinPoint joinPoint , String arg2 ) {
126
+ }
127
+
128
+ void methodWithProceedingJoinPoint (ProceedingJoinPoint joinPoint , String arg1 , String arg2 ) {
129
+ }
129
130
130
- void methodWithStaticPart (JoinPoint .StaticPart staticPart , String arg1 , String arg2 ) {
131
+ void methodWithStaticPart (JoinPoint .StaticPart staticPart , String arg1 , String arg2 ) {
132
+ }
131
133
}
134
+
132
135
}
0 commit comments