17
17
package org .springframework .resilience ;
18
18
19
19
import java .io .IOException ;
20
+ import java .lang .reflect .InvocationTargetException ;
20
21
import java .lang .reflect .Method ;
21
22
import java .nio .file .AccessDeniedException ;
22
23
import java .time .Duration ;
23
24
import java .util .Properties ;
24
25
import java .util .concurrent .atomic .AtomicInteger ;
25
26
27
+ import org .aopalliance .intercept .MethodInterceptor ;
26
28
import org .junit .jupiter .api .Test ;
27
29
28
30
import org .springframework .aop .framework .AopProxyUtils ;
29
31
import org .springframework .aop .framework .ProxyConfig ;
30
32
import org .springframework .aop .framework .ProxyFactory ;
31
33
import org .springframework .aop .framework .autoproxy .AutoProxyUtils ;
34
+ import org .springframework .aop .interceptor .SimpleTraceInterceptor ;
32
35
import org .springframework .aop .support .AopUtils ;
33
36
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
34
37
import org .springframework .beans .factory .support .RootBeanDefinition ;
@@ -59,7 +62,30 @@ void withSimpleInterceptor() {
59
62
pf .setTarget (target );
60
63
pf .addAdvice (new SimpleRetryInterceptor (
61
64
new MethodRetrySpec ((m , t ) -> true , 5 , Duration .ofMillis (10 ))));
62
- NonAnnotatedBean proxy = (NonAnnotatedBean ) pf .getProxy ();
65
+ pf .addAdvice (new SimpleTraceInterceptor ());
66
+ PlainInterface proxy = (PlainInterface ) pf .getProxy ();
67
+
68
+ assertThatIOException ().isThrownBy (proxy ::retryOperation ).withMessage ("6" );
69
+ assertThat (target .counter ).isEqualTo (6 );
70
+ }
71
+
72
+ @ Test
73
+ void withSimpleInterceptorAndNoTarget () {
74
+ NonAnnotatedBean target = new NonAnnotatedBean ();
75
+ ProxyFactory pf = new ProxyFactory ();
76
+ pf .addAdvice (new SimpleRetryInterceptor (
77
+ new MethodRetrySpec ((m , t ) -> true , 5 , Duration .ofMillis (10 ))));
78
+ pf .addAdvice (new SimpleTraceInterceptor ());
79
+ pf .addAdvice ((MethodInterceptor ) invocation -> {
80
+ try {
81
+ return invocation .getMethod ().invoke (target , invocation .getArguments ());
82
+ }
83
+ catch (InvocationTargetException ex ) {
84
+ throw ex .getTargetException ();
85
+ }
86
+ });
87
+ pf .addInterface (PlainInterface .class );
88
+ PlainInterface proxy = (PlainInterface ) pf .getProxy ();
63
89
64
90
assertThatIOException ().isThrownBy (proxy ::retryOperation ).withMessage ("6" );
65
91
assertThat (target .counter ).isEqualTo (6 );
@@ -237,7 +263,7 @@ void withEnableAnnotation() throws Exception {
237
263
}
238
264
239
265
240
- static class NonAnnotatedBean {
266
+ static class NonAnnotatedBean implements PlainInterface {
241
267
242
268
int counter = 0 ;
243
269
@@ -248,6 +274,12 @@ public void retryOperation() throws IOException {
248
274
}
249
275
250
276
277
+ public interface PlainInterface {
278
+
279
+ void retryOperation () throws IOException ;
280
+ }
281
+
282
+
251
283
static class AnnotatedMethodBean {
252
284
253
285
int counter = 0 ;
0 commit comments