Polish tests in spring-aop
This commit polishes tests in spring-aop by using OrderComparator.sort() and lambda expressions instead of anonymous classes where feasible. Closes gh-23458
This commit is contained in:
parent
c6a4898ac5
commit
a0d50a546b
|
@ -129,15 +129,12 @@ public class MethodInvocationProceedingJoinPointTests {
|
|||
final Object raw = new TestBean();
|
||||
ProxyFactory pf = new ProxyFactory(raw);
|
||||
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
|
||||
pf.addAdvice(new MethodBeforeAdvice() {
|
||||
@Override
|
||||
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
|
||||
SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
|
||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc);
|
||||
assertThat(sloc.getWithinType()).isEqualTo(TestBean.class);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
|
||||
}
|
||||
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
|
||||
SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
|
||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc);
|
||||
assertThat(sloc.getWithinType()).isEqualTo(TestBean.class);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
|
||||
});
|
||||
ITestBean itb = (ITestBean) pf.getProxy();
|
||||
// Any call will do
|
||||
|
@ -149,15 +146,12 @@ public class MethodInvocationProceedingJoinPointTests {
|
|||
final Object raw = new TestBean();
|
||||
ProxyFactory pf = new ProxyFactory(raw);
|
||||
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
|
||||
pf.addAdvice(new MethodBeforeAdvice() {
|
||||
@Override
|
||||
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
|
||||
StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
|
||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
|
||||
assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
|
||||
assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
|
||||
assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
|
||||
}
|
||||
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
|
||||
StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
|
||||
assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
|
||||
assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
|
||||
assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
|
||||
assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
|
||||
});
|
||||
ITestBean itb = (ITestBean) pf.getProxy();
|
||||
// Any call will do
|
||||
|
@ -169,22 +163,19 @@ public class MethodInvocationProceedingJoinPointTests {
|
|||
final Object raw = new TestBean();
|
||||
ProxyFactory pf = new ProxyFactory(raw);
|
||||
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
|
||||
pf.addAdvice(new MethodBeforeAdvice() {
|
||||
@Override
|
||||
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
|
||||
// makeEncSJP, although meant for computing the enclosing join point,
|
||||
// it serves our purpose here
|
||||
JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
|
||||
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
|
||||
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
|
||||
// makeEncSJP, although meant for computing the enclosing join point,
|
||||
// it serves our purpose here
|
||||
StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
|
||||
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
|
||||
|
||||
assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
|
||||
assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
|
||||
assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
|
||||
assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
|
||||
assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
|
||||
assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
|
||||
|
||||
assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
|
||||
assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
|
||||
assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString());
|
||||
}
|
||||
assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
|
||||
assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
|
||||
assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString());
|
||||
});
|
||||
ITestBean itb = (ITestBean) pf.getProxy();
|
||||
itb.getAge();
|
||||
|
|
|
@ -144,7 +144,7 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
|
|||
aspect2.setOrder(5);
|
||||
advisors.addAll(
|
||||
getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
|
||||
Collections.sort(advisors, new OrderComparator());
|
||||
OrderComparator.sort(advisors);
|
||||
|
||||
TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
|
||||
assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
|
||||
|
@ -170,7 +170,7 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
|
|||
PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
|
||||
advisors.addAll(
|
||||
getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
|
||||
Collections.sort(advisors, new OrderComparator());
|
||||
OrderComparator.sort(advisors);
|
||||
|
||||
TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
|
||||
assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
|
||||
|
|
|
@ -195,10 +195,7 @@ public class AspectJPrecedenceComparatorTests {
|
|||
}
|
||||
|
||||
private Advisor createSpringAOPAfterAdvice(int order) {
|
||||
AfterReturningAdvice advice = new AfterReturningAdvice() {
|
||||
@Override
|
||||
public void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable {
|
||||
}
|
||||
AfterReturningAdvice advice = (returnValue, method, args, target) -> {
|
||||
};
|
||||
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
|
||||
advisor.setOrder(order);
|
||||
|
|
|
@ -41,12 +41,7 @@ public class MethodInvocationTests {
|
|||
Object proxy = new Object();
|
||||
final Object returnValue = new Object();
|
||||
List<Object> is = new LinkedList<>();
|
||||
is.add(new MethodInterceptor() {
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
return returnValue;
|
||||
}
|
||||
});
|
||||
is.add((MethodInterceptor) invocation -> returnValue);
|
||||
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, //?
|
||||
m, null, null, is // list
|
||||
);
|
||||
|
|
|
@ -48,12 +48,7 @@ public class NullPrimitiveTests {
|
|||
|
||||
SimpleFoo target = new SimpleFoo();
|
||||
ProxyFactory factory = new ProxyFactory(target);
|
||||
factory.addAdvice(new MethodInterceptor() {
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
factory.addAdvice((MethodInterceptor) invocation -> null);
|
||||
|
||||
Foo foo = (Foo) factory.getProxy();
|
||||
|
||||
|
@ -73,12 +68,7 @@ public class NullPrimitiveTests {
|
|||
|
||||
Bar target = new Bar();
|
||||
ProxyFactory factory = new ProxyFactory(target);
|
||||
factory.addAdvice(new MethodInterceptor() {
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
factory.addAdvice((MethodInterceptor) invocation -> null);
|
||||
|
||||
Bar bar = (Bar) factory.getProxy();
|
||||
|
||||
|
|
|
@ -170,11 +170,8 @@ public class ProxyFactoryTests {
|
|||
|
||||
@Test
|
||||
public void testAddRepeatedInterface() {
|
||||
TimeStamped tst = new TimeStamped() {
|
||||
@Override
|
||||
public long getTimeStamp() {
|
||||
throw new UnsupportedOperationException("getTimeStamp");
|
||||
}
|
||||
TimeStamped tst = () -> {
|
||||
throw new UnsupportedOperationException("getTimeStamp");
|
||||
};
|
||||
ProxyFactory pf = new ProxyFactory(tst);
|
||||
// We've already implicitly added this interface.
|
||||
|
|
|
@ -56,12 +56,7 @@ public class PointcutsTests {
|
|||
public static Pointcut allTestBeanMethodsPointcut = new StaticMethodMatcherPointcut() {
|
||||
@Override
|
||||
public ClassFilter getClassFilter() {
|
||||
return new ClassFilter() {
|
||||
@Override
|
||||
public boolean matches(Class<?> clazz) {
|
||||
return clazz.equals(TestBean.class);
|
||||
}
|
||||
};
|
||||
return type -> type.equals(TestBean.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -182,5 +182,4 @@ public class ViewResolverRegistry {
|
|||
getViewResolver();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue