1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 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.
42
42
*/
43
43
class ArgumentBindingTests {
44
44
45
+ @ Test
46
+ void annotationArgumentNameBinding () {
47
+ AspectJProxyFactory proxyFactory = new AspectJProxyFactory (new TransactionalBean ());
48
+ proxyFactory .addAspect (PointcutWithAnnotationArgument .class );
49
+ ITransactionalBean proxiedTestBean = proxyFactory .getProxy ();
50
+
51
+ assertThatIllegalStateException ()
52
+ .isThrownBy (proxiedTestBean ::doInTransaction )
53
+ .withMessage ("Invoked with @Transactional" );
54
+ }
55
+
45
56
@ Test
46
57
void bindingInPointcutUsedByAdvice () {
47
58
AspectJProxyFactory proxyFactory = new AspectJProxyFactory (new TestBean ());
48
59
proxyFactory .addAspect (NamedPointcutWithArgs .class );
49
-
50
60
ITestBean proxiedTestBean = proxyFactory .getProxy ();
61
+
51
62
assertThatIllegalArgumentException ()
52
- .isThrownBy (() -> proxiedTestBean .setName ("enigma" ))
53
- .withMessage ("enigma" );
63
+ .isThrownBy (() -> proxiedTestBean .setName ("enigma" ))
64
+ .withMessage ("enigma" );
54
65
}
55
66
56
67
@ Test
57
- void annotationArgumentNameBinding () {
58
- AspectJProxyFactory proxyFactory = new AspectJProxyFactory (new TransactionalBean ());
59
- proxyFactory .addAspect (PointcutWithAnnotationArgument .class );
68
+ void bindingWithDynamicAdvice () {
69
+ AspectJProxyFactory proxyFactory = new AspectJProxyFactory (new TestBean ());
70
+ proxyFactory .addAspect (DynamicPointcutWithArgs .class );
71
+ ITestBean proxiedTestBean = proxyFactory .getProxy ();
60
72
61
- ITransactionalBean proxiedTestBean = proxyFactory . getProxy ( );
62
- assertThatIllegalStateException ()
63
- .isThrownBy (proxiedTestBean :: doInTransaction )
64
- .withMessage ("Invoked with @Transactional " );
73
+ proxiedTestBean . applyName ( 1 );
74
+ assertThatIllegalArgumentException ()
75
+ .isThrownBy (() -> proxiedTestBean . applyName ( "enigma" ) )
76
+ .withMessage ("enigma " );
65
77
}
66
78
67
79
@ Test
@@ -94,23 +106,25 @@ public void doInTransaction() {
94
106
}
95
107
}
96
108
109
+
97
110
/**
98
111
* Mimics Spring's @Transactional annotation without actually introducing the dependency.
99
112
*/
100
113
@ Retention (RetentionPolicy .RUNTIME )
101
114
@interface Transactional {
102
115
}
103
116
117
+
104
118
@ Aspect
105
119
static class PointcutWithAnnotationArgument {
106
120
107
- @ Around (value = "execution(* org.springframework..*.*(..)) && @annotation(transactional)" )
121
+ @ Around ("execution(* org.springframework..*.*(..)) && @annotation(transactional)" )
108
122
public Object around (ProceedingJoinPoint pjp , Transactional transactional ) throws Throwable {
109
123
throw new IllegalStateException ("Invoked with @Transactional" );
110
124
}
111
-
112
125
}
113
126
127
+
114
128
@ Aspect
115
129
static class NamedPointcutWithArgs {
116
130
@@ -121,7 +135,16 @@ public void pointcutWithArgs(String s) {}
121
135
public Object doAround (ProceedingJoinPoint pjp , String aString ) throws Throwable {
122
136
throw new IllegalArgumentException (aString );
123
137
}
138
+ }
124
139
140
+
141
+ @ Aspect ("pertarget(execution(* *(..)))" )
142
+ static class DynamicPointcutWithArgs {
143
+
144
+ @ Around ("execution(* *(..)) && args(java.lang.String)" )
145
+ public Object doAround (ProceedingJoinPoint pjp ) throws Throwable {
146
+ throw new IllegalArgumentException (String .valueOf (pjp .getArgs ()[0 ]));
147
+ }
125
148
}
126
149
127
150
}
0 commit comments