|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.aop.framework.autoproxy; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
| 21 | +import org.aopalliance.aop.Advice; |
| 22 | +import org.aopalliance.intercept.MethodInterceptor; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.aop.Pointcut; |
| 26 | +import org.springframework.aop.support.AbstractPointcutAdvisor; |
| 27 | +import org.springframework.aop.support.RootClassFilter; |
| 28 | +import org.springframework.aop.support.StaticMethodMatcherPointcut; |
| 29 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration tests for {@link DefaultAdvisorAutoProxyCreator}. |
| 35 | + * |
| 36 | + * @author Sam Brannen |
| 37 | + * @since 6.2.1 |
| 38 | + */ |
| 39 | +class DefaultAdvisorAutoProxyCreatorTests { |
| 40 | + |
| 41 | + /** |
| 42 | + * Indirectly tests behavior of {@link org.springframework.aop.framework.AdvisedSupport.MethodCacheKey}. |
| 43 | + * @see StaticMethodMatcherPointcut#matches(Method, Class) |
| 44 | + */ |
| 45 | + @Test // gh-33915 |
| 46 | + void staticMethodMatcherPointcutMatchesMethodIsInvokedAgainForActualMethodInvocation() { |
| 47 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 48 | + DemoBean.class, DemoPointcutAdvisor.class, DefaultAdvisorAutoProxyCreator.class); |
| 49 | + DemoPointcutAdvisor demoPointcutAdvisor = context.getBean(DemoPointcutAdvisor.class); |
| 50 | + DemoBean demoBean = context.getBean(DemoBean.class); |
| 51 | + |
| 52 | + assertThat(demoPointcutAdvisor.matchesInvocationCount).as("matches() invocations before").isEqualTo(2); |
| 53 | + assertThat(demoBean.sayHello()).isEqualTo("Advised: Hello!"); |
| 54 | + assertThat(demoPointcutAdvisor.matchesInvocationCount).as("matches() invocations after").isEqualTo(3); |
| 55 | + |
| 56 | + context.close(); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + static class DemoBean { |
| 61 | + |
| 62 | + public String sayHello() { |
| 63 | + return "Hello!"; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings("serial") |
| 68 | + static class DemoPointcutAdvisor extends AbstractPointcutAdvisor { |
| 69 | + |
| 70 | + int matchesInvocationCount = 0; |
| 71 | + |
| 72 | + @Override |
| 73 | + public Pointcut getPointcut() { |
| 74 | + StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() { |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean matches(Method method, Class<?> targetClass) { |
| 78 | + if (method.getName().equals("sayHello")) { |
| 79 | + matchesInvocationCount++; |
| 80 | + return true; |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + }; |
| 85 | + pointcut.setClassFilter(new RootClassFilter(DemoBean.class)); |
| 86 | + return pointcut; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Advice getAdvice() { |
| 91 | + return (MethodInterceptor) invocation -> "Advised: " + invocation.proceed(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments