|
16 | 16 |
|
17 | 17 | package org.springframework.aop.support;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
19 | 21 | import org.junit.jupiter.api.Test;
|
20 | 22 |
|
21 | 23 | import org.springframework.aop.Pointcut;
|
|
31 | 33 | *
|
32 | 34 | * @author Rod Johnson
|
33 | 35 | * @author Chris Beams
|
| 36 | + * @author Sam Brannen |
34 | 37 | */
|
35 | 38 | class ControlFlowPointcutTests {
|
36 | 39 |
|
@@ -60,6 +63,57 @@ void matches() {
|
60 | 63 | assertThat(cflow.getEvaluations()).isEqualTo(3);
|
61 | 64 | }
|
62 | 65 |
|
| 66 | + @Test |
| 67 | + void controlFlowPointcutIsExtensible() { |
| 68 | + @SuppressWarnings("serial") |
| 69 | + class CustomControlFlowPointcut extends ControlFlowPointcut { |
| 70 | + |
| 71 | + CustomControlFlowPointcut(Class<?> clazz, String methodName) { |
| 72 | + super(clazz, methodName); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean matches(Method method, Class<?> targetClass, Object... args) { |
| 77 | + super.incrementEvaluationCount(); |
| 78 | + return super.matches(method, targetClass, args); |
| 79 | + } |
| 80 | + |
| 81 | + Class<?> trackedClass() { |
| 82 | + return super.clazz; |
| 83 | + } |
| 84 | + |
| 85 | + String trackedMethod() { |
| 86 | + return super.methodName; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + CustomControlFlowPointcut cflow = new CustomControlFlowPointcut(One.class, "getAge"); |
| 91 | + |
| 92 | + assertThat(cflow.trackedClass()).isEqualTo(One.class); |
| 93 | + assertThat(cflow.trackedMethod()).isEqualTo("getAge"); |
| 94 | + |
| 95 | + TestBean target = new TestBean("Jane", 27); |
| 96 | + ProxyFactory pf = new ProxyFactory(target); |
| 97 | + NopInterceptor nop = new NopInterceptor(); |
| 98 | + pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop)); |
| 99 | + ITestBean proxy = (ITestBean) pf.getProxy(); |
| 100 | + |
| 101 | + // Not advised: the proxy is not invoked under One#getAge |
| 102 | + assertThat(proxy.getAge()).isEqualTo(target.getAge()); |
| 103 | + assertThat(nop.getCount()).isEqualTo(0); |
| 104 | + assertThat(cflow.getEvaluations()).isEqualTo(2); // intentional double increment |
| 105 | + |
| 106 | + // Will be advised: the proxy is invoked under One#getAge |
| 107 | + assertThat(new One().getAge(proxy)).isEqualTo(target.getAge()); |
| 108 | + assertThat(nop.getCount()).isEqualTo(1); |
| 109 | + assertThat(cflow.getEvaluations()).isEqualTo(4); // intentional double increment |
| 110 | + |
| 111 | + // Won't be advised: the proxy is not invoked under One#getAge |
| 112 | + assertThat(new One().nomatch(proxy)).isEqualTo(target.getAge()); |
| 113 | + assertThat(nop.getCount()).isEqualTo(1); |
| 114 | + assertThat(cflow.getEvaluations()).isEqualTo(6); // intentional double increment |
| 115 | + } |
| 116 | + |
63 | 117 | /**
|
64 | 118 | * Check that we can use a cflow pointcut only in conjunction with
|
65 | 119 | * a static pointcut: e.g. all setter methods that are invoked under
|
|
0 commit comments