use of org.aopalliance.intercept.MethodInterceptor in project roboguice by roboguice.
the class MethodInterceptionTest method testSpiAccessToInterceptors.
public void testSpiAccessToInterceptors() throws NoSuchMethodException {
final MethodInterceptor countingInterceptor = new CountingInterceptor();
final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)), countingInterceptor);
bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class).or(only(Bar.class))), returnNullInterceptor);
}
});
ConstructorBinding<?> interceptedBinding = (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
Method barMethod = Interceptable.class.getMethod("bar");
Method fooMethod = Interceptable.class.getMethod("foo");
assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor), barMethod, ImmutableList.of(returnNullInterceptor)), interceptedBinding.getMethodInterceptors());
ConstructorBinding<?> nonInterceptedBinding = (ConstructorBinding<?>) injector.getBinding(Foo.class);
assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(), nonInterceptedBinding.getMethodInterceptors());
injector.getInstance(Interceptable.class).foo();
assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class MethodInvocationTests method testValidInvocation.
@Test
void testValidInvocation() throws Throwable {
Method method = Object.class.getMethod("hashCode");
Object proxy = new Object();
Object returnValue = new Object();
List<Object> interceptors = Collections.singletonList((MethodInterceptor) invocation -> returnValue);
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, method, null, null, interceptors);
Object rv = invocation.proceed();
assertThat(rv).as("correct response").isSameAs(returnValue);
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryTests method testInterceptorWithoutJoinpoint.
@Test
public void testInterceptorWithoutJoinpoint() {
final TestBean target = new TestBean("tb");
ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> {
assertThat(invocation.getThis()).isNull();
return invocation.getMethod().invoke(target, invocation.getArguments());
});
assertThat(proxy.getName()).isEqualTo("tb");
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryTests method testInterceptorInclusionMethods.
@Test
public void testInterceptorInclusionMethods() {
class MyInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
throw new UnsupportedOperationException();
}
}
NopInterceptor di = new NopInterceptor();
NopInterceptor diUnused = new NopInterceptor();
ProxyFactory factory = new ProxyFactory(new TestBean());
factory.addAdvice(0, di);
assertThat(factory.getProxy()).isInstanceOf(ITestBean.class);
assertThat(factory.adviceIncluded(di)).isTrue();
assertThat(!factory.adviceIncluded(diUnused)).isTrue();
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 1).isTrue();
assertThat(factory.countAdvicesOfType(MyInterceptor.class) == 0).isTrue();
factory.addAdvice(0, diUnused);
assertThat(factory.adviceIncluded(diUnused)).isTrue();
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 2).isTrue();
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testUndeclaredCheckedException.
/**
* An interceptor throws a checked exception not on the method signature.
* For efficiency, we don't bother unifying java.lang.reflect and
* org.springframework.cglib UndeclaredThrowableException
*/
@Test
public void testUndeclaredCheckedException() throws Throwable {
final Exception unexpectedException = new Exception();
// Test return value
MethodInterceptor mi = invocation -> {
throw unexpectedException;
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
// We don't care about the object
pc.setTarget(new TestBean());
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(tb::getAge).satisfies(ex -> assertThat(ex.getUndeclaredThrowable()).isEqualTo(unexpectedException));
}
Aggregations