DATAREST-582 - Add unit test to makes sure method is invoked on proxy.

See this Twitter thread for details: https://twitter.com/PreAuthorize/status/1227865219264741376
This commit is contained in:
Oliver Drotbohm
2020-02-13 17:12:49 +01:00
parent f5bc8a9304
commit ac95b17acd

View File

@@ -18,6 +18,12 @@ package org.springframework.data.rest.core.event;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.annotation.Order;
@@ -28,6 +34,7 @@ import org.springframework.data.rest.core.domain.Person;
import org.springframework.data.rest.core.event.AnnotatedEventHandlerInvoker.EventHandlerMethod;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link AnnotatedEventHandlerInvoker}.
@@ -116,6 +123,26 @@ public class AnnotatedEventHandlerInvokerUnitTests {
verify(handler, times(1)).doAfterCreate(payload);
}
@Test // DATAREST-582
public void invocesInterceptorForProxiedListener() {
SampleInterceptor interceptor = new SampleInterceptor();
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new Sample());
factory.addAdvice(interceptor);
factory.setProxyTargetClass(true);
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
invoker.postProcessAfterInitialization(factory.getProxy(), "proxy");
invoker.onApplicationEvent(new BeforeCreateEvent(new Sample()));
Method method = ReflectionUtils.findMethod(Sample.class, "method", Sample.class);
assertThat(interceptor.invocations.get(method)).isEqualTo(1);
}
@RepositoryEventHandler
static class Sample {
@@ -193,4 +220,21 @@ public class AnnotatedEventHandlerInvokerUnitTests {
}
static class Payload {}
static class SampleInterceptor implements MethodInterceptor {
Map<Method, Integer> invocations = new HashMap<>();
/*
* (non-Javadoc)
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
invocations.compute(invocation.getMethod(), (method, value) -> value == null ? 1 : value++);
return invocation.proceed();
}
}
}