Polishing
This commit is contained in:
parent
d357ef706f
commit
f8f3067419
|
|
@ -17,8 +17,8 @@
|
||||||
package org.springframework.aop.aspectj;
|
package org.springframework.aop.aspectj;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.JoinPoint.StaticPart;
|
import org.aspectj.lang.JoinPoint.StaticPart;
|
||||||
|
|
@ -33,7 +33,6 @@ import org.springframework.aop.framework.AopContext;
|
||||||
import org.springframework.aop.framework.ProxyFactory;
|
import org.springframework.aop.framework.ProxyFactory;
|
||||||
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
|
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
|
||||||
import org.springframework.aop.support.AopUtils;
|
import org.springframework.aop.support.AopUtils;
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
import org.springframework.tests.sample.beans.ITestBean;
|
import org.springframework.tests.sample.beans.ITestBean;
|
||||||
import org.springframework.tests.sample.beans.TestBean;
|
import org.springframework.tests.sample.beans.TestBean;
|
||||||
|
|
||||||
|
|
@ -51,14 +50,12 @@ public class MethodInvocationProceedingJoinPointTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testingBindingWithJoinPoint() {
|
public void testingBindingWithJoinPoint() {
|
||||||
assertThatIllegalStateException().isThrownBy(
|
assertThatIllegalStateException().isThrownBy(AbstractAspectJAdvice::currentJoinPoint);
|
||||||
AbstractAspectJAdvice::currentJoinPoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testingBindingWithProceedingJoinPoint() {
|
public void testingBindingWithProceedingJoinPoint() {
|
||||||
assertThatIllegalStateException().isThrownBy(
|
assertThatIllegalStateException().isThrownBy(AbstractAspectJAdvice::currentJoinPoint);
|
||||||
AbstractAspectJAdvice::currentJoinPoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -70,54 +67,50 @@ public class MethodInvocationProceedingJoinPointTests {
|
||||||
ProxyFactory pf = new ProxyFactory(raw);
|
ProxyFactory pf = new ProxyFactory(raw);
|
||||||
pf.setExposeProxy(true);
|
pf.setExposeProxy(true);
|
||||||
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
|
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
|
||||||
pf.addAdvice(new MethodBeforeAdvice() {
|
AtomicInteger depth = new AtomicInteger();
|
||||||
private int depth;
|
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
|
||||||
|
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
|
||||||
|
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
|
||||||
|
// Ensure that these don't cause problems
|
||||||
|
jp.toShortString();
|
||||||
|
jp.toLongString();
|
||||||
|
|
||||||
@Override
|
assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target);
|
||||||
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
|
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
|
||||||
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
|
|
||||||
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
|
|
||||||
// Ensure that these don't cause problems
|
|
||||||
jp.toShortString();
|
|
||||||
jp.toLongString();
|
|
||||||
|
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target);
|
ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
|
||||||
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
|
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
|
||||||
|
|
||||||
ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
|
assertThat(thisProxy).isNotSameAs(target);
|
||||||
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
|
|
||||||
|
|
||||||
assertThat(thisProxy).isNotSameAs(target);
|
// Check getting again doesn't cause a problem
|
||||||
|
assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
|
||||||
|
|
||||||
// Check getting again doesn't cause a problem
|
// Try reentrant call--will go through this advice.
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
|
// Be sure to increment depth to avoid infinite recursion
|
||||||
|
if (depth.getAndIncrement() == 0) {
|
||||||
// Try reentrant call--will go through this advice.
|
// Check that toString doesn't cause a problem
|
||||||
// Be sure to increment depth to avoid infinite recursion
|
thisProxy.toString();
|
||||||
if (depth++ == 0) {
|
// Change age, so this will be returned by invocation
|
||||||
// Check that toString doesn't cause a problem
|
thisProxy.setAge(newAge);
|
||||||
thisProxy.toString();
|
assertThat(thisProxy.getAge()).isEqualTo(newAge);
|
||||||
// Change age, so this will be returned by invocation
|
|
||||||
thisProxy.setAge(newAge);
|
|
||||||
assertThat(thisProxy.getAge()).isEqualTo(newAge);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertThat(thisProxy).isSameAs(AopContext.currentProxy());
|
|
||||||
assertThat(raw).isSameAs(target);
|
|
||||||
|
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName());
|
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getModifiers());
|
|
||||||
|
|
||||||
MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
|
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
|
|
||||||
assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
|
|
||||||
assertThat(msig.getDeclaringType()).isEqualTo(method.getDeclaringClass());
|
|
||||||
assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue();
|
|
||||||
assertThat(msig.getReturnType()).isEqualTo(method.getReturnType());
|
|
||||||
assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue();
|
|
||||||
msig.toLongString();
|
|
||||||
msig.toShortString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertThat(thisProxy).isSameAs(AopContext.currentProxy());
|
||||||
|
assertThat(raw).isSameAs(target);
|
||||||
|
|
||||||
|
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName());
|
||||||
|
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getModifiers());
|
||||||
|
|
||||||
|
MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
|
||||||
|
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
|
||||||
|
assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
|
||||||
|
assertThat(msig.getDeclaringType()).isEqualTo(method.getDeclaringClass());
|
||||||
|
assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue();
|
||||||
|
assertThat(msig.getReturnType()).isEqualTo(method.getReturnType());
|
||||||
|
assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue();
|
||||||
|
msig.toLongString();
|
||||||
|
msig.toShortString();
|
||||||
});
|
});
|
||||||
ITestBean itb = (ITestBean) pf.getProxy();
|
ITestBean itb = (ITestBean) pf.getProxy();
|
||||||
// Any call will do
|
// Any call will do
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
package org.springframework.aop.framework;
|
package org.springframework.aop.framework;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.LinkedList;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.aopalliance.intercept.MethodInterceptor;
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
|
|
@ -32,39 +32,35 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 14.03.2003
|
* @since 14.03.2003
|
||||||
*/
|
*/
|
||||||
public class MethodInvocationTests {
|
class MethodInvocationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidInvocation() throws Throwable {
|
void testValidInvocation() throws Throwable {
|
||||||
Method m = Object.class.getMethod("hashCode");
|
Method method = Object.class.getMethod("hashCode");
|
||||||
Object proxy = new Object();
|
Object proxy = new Object();
|
||||||
final Object returnValue = new Object();
|
Object returnValue = new Object();
|
||||||
List<Object> is = new LinkedList<>();
|
List<Object> interceptors = Collections.singletonList((MethodInterceptor) invocation -> returnValue);
|
||||||
is.add((MethodInterceptor) invocation -> returnValue);
|
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, method, null, null, interceptors);
|
||||||
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, //?
|
|
||||||
m, null, null, is // list
|
|
||||||
);
|
|
||||||
Object rv = invocation.proceed();
|
Object rv = invocation.proceed();
|
||||||
assertThat(rv == returnValue).as("correct response").isTrue();
|
assertThat(rv).as("correct response").isSameAs(returnValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* toString on target can cause failure.
|
* toString on target can cause failure.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testToStringDoesntHitTarget() throws Throwable {
|
void testToStringDoesntHitTarget() throws Throwable {
|
||||||
Object target = new TestBean() {
|
Object target = new TestBean() {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
throw new UnsupportedOperationException("toString");
|
throw new UnsupportedOperationException("toString");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
List<Object> is = new LinkedList<>();
|
List<Object> interceptors = Collections.emptyList();
|
||||||
|
|
||||||
Method m = Object.class.getMethod("hashCode");
|
Method m = Object.class.getMethod("hashCode");
|
||||||
Object proxy = new Object();
|
Object proxy = new Object();
|
||||||
ReflectiveMethodInvocation invocation =
|
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, m, null, null, interceptors);
|
||||||
new ReflectiveMethodInvocation(proxy, target, m, null, null, is);
|
|
||||||
|
|
||||||
// If it hits target, the test will fail with the UnsupportedOpException
|
// If it hits target, the test will fail with the UnsupportedOpException
|
||||||
// in the inner class above.
|
// in the inner class above.
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
*/
|
*/
|
||||||
public class NullPrimitiveTests {
|
public class NullPrimitiveTests {
|
||||||
|
|
||||||
static interface Foo {
|
interface Foo {
|
||||||
int getValue();
|
int getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -182,4 +182,5 @@ public class ViewResolverRegistry {
|
||||||
getViewResolver();
|
getViewResolver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue