Skip to content

Commit 320831b

Browse files
committed
Test status quo for StaticMethodMatcherPointcut#matches invocations
This commit introduces a test which verifies how many times the matches() method of a StaticMethodMatcherPointcut is invoked during ApplicationContext startup as well as during actual method invocations via the advice chain, which also indirectly tests the behavior of the equals() implementation in AdvisedSupport.MethodCacheKey. In addition, this commit revises BeanFactoryTransactionTests to assert that a transaction is started for the setAge() method. See gh-33915
1 parent 172c8b2 commit 320831b

File tree

2 files changed

+108
-16
lines changed

2 files changed

+108
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

spring-tx/src/test/java/org/springframework/transaction/interceptor/BeanFactoryTransactionTests.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.lang.reflect.Proxy;
2121
import java.util.Map;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223

2324
import org.aopalliance.intercept.MethodInterceptor;
2425
import org.aopalliance.intercept.MethodInvocation;
@@ -52,6 +53,7 @@
5253
*
5354
* @author Rod Johnson
5455
* @author Juergen Hoeller
56+
* @author Sam Brannen
5557
* @since 23.04.2003
5658
*/
5759
class BeanFactoryTransactionTests {
@@ -145,30 +147,25 @@ void getsAreNotTransactionalWithProxyFactory3() {
145147
assertThat(postInterceptor.counter).as("postInterceptor").isEqualTo(3);
146148
}
147149

148-
private void assertGetsAreNotTransactional(final ITestBean testBean) {
150+
private void assertGetsAreNotTransactional(ITestBean testBean) {
149151
// Install facade
150152
PlatformTransactionManager ptm = mock();
151153
PlatformTransactionManagerFacade.delegate = ptm;
152154

153155
assertThat(testBean.getAge()).as("Age").isEqualTo(666);
154156

155-
// Expect no methods
157+
// Expect no interactions with the transaction manager.
156158
verifyNoInteractions(ptm);
157159

158160
// Install facade expecting a call
159-
final TransactionStatus ts = mock();
161+
AtomicBoolean invoked = new AtomicBoolean();
162+
TransactionStatus ts = mock();
160163
ptm = new PlatformTransactionManager() {
161-
private boolean invoked;
162164
@Override
163165
public TransactionStatus getTransaction(@Nullable TransactionDefinition def) throws TransactionException {
164-
if (invoked) {
165-
throw new IllegalStateException("getTransaction should not get invoked more than once");
166-
}
167-
invoked = true;
168-
if (!(def.getName().contains(DerivedTestBean.class.getName()) && def.getName().contains("setAge"))) {
169-
throw new IllegalStateException(
170-
"transaction name should contain class and method name: " + def.getName());
171-
}
166+
assertThat(invoked.compareAndSet(false, true))
167+
.as("getTransaction() should not get invoked more than once").isTrue();
168+
assertThat(def.getName()).as("transaction name").contains(DerivedTestBean.class.getName(), "setAge");
172169
return ts;
173170
}
174171
@Override
@@ -182,10 +179,10 @@ public void rollback(TransactionStatus status) throws TransactionException {
182179
};
183180
PlatformTransactionManagerFacade.delegate = ptm;
184181

185-
// same as old age to avoid ordering effect for now
186-
int age = 666;
187-
testBean.setAge(age);
188-
assertThat(testBean.getAge()).isEqualTo(age);
182+
assertThat(invoked).as("getTransaction() invoked before setAge()").isFalse();
183+
testBean.setAge(42);
184+
assertThat(invoked).as("getTransaction() invoked after setAge()").isTrue();
185+
assertThat(testBean.getAge()).as("Age").isEqualTo(42);
189186
}
190187

191188
@Test

0 commit comments

Comments
 (0)