Polishing

This commit is contained in:
Sam Brannen 2019-08-27 12:27:18 +02:00
parent d357ef706f
commit f8f3067419
4 changed files with 54 additions and 64 deletions

View File

@ -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,11 +67,8 @@ 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) -> {
@Override
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue(); assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
// Ensure that these don't cause problems // Ensure that these don't cause problems
@ -94,7 +88,7 @@ public class MethodInvocationProceedingJoinPointTests {
// Try reentrant call--will go through this advice. // Try reentrant call--will go through this advice.
// Be sure to increment depth to avoid infinite recursion // Be sure to increment depth to avoid infinite recursion
if (depth++ == 0) { if (depth.getAndIncrement() == 0) {
// Check that toString doesn't cause a problem // Check that toString doesn't cause a problem
thisProxy.toString(); thisProxy.toString();
// Change age, so this will be returned by invocation // Change age, so this will be returned by invocation
@ -117,7 +111,6 @@ public class MethodInvocationProceedingJoinPointTests {
assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue(); assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue();
msig.toLongString(); msig.toLongString();
msig.toShortString(); msig.toShortString();
}
}); });
ITestBean itb = (ITestBean) pf.getProxy(); ITestBean itb = (ITestBean) pf.getProxy();
// Any call will do // Any call will do

View File

@ -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.

View File

@ -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();
} }

View File

@ -182,4 +182,5 @@ public class ViewResolverRegistry {
getViewResolver(); getViewResolver();
} }
} }
} }