Search in sources :

Example 21 with MethodInterceptor

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());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ConstructorBinding(com.google.inject.spi.ConstructorBinding) Method(java.lang.reflect.Method)

Example 22 with MethodInterceptor

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);
}
Also used : Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) Collections(java.util.Collections) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 23 with MethodInterceptor

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");
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) AopUtils(org.springframework.aop.support.AopUtils) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Disabled(org.junit.jupiter.api.Disabled) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) MethodInvocation(org.aopalliance.intercept.MethodInvocation) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) JFrame(javax.swing.JFrame) Advisor(org.springframework.aop.Advisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Order(org.springframework.core.annotation.Order) RootPaneContainer(javax.swing.RootPaneContainer) Accessible(javax.accessibility.Accessible) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 24 with MethodInterceptor

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();
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 25 with MethodInterceptor

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));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)57 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 List (java.util.List)20 Method (java.lang.reflect.Method)19 ArrayList (java.util.ArrayList)18 Test (org.junit.jupiter.api.Test)15 Advice (org.aopalliance.aop.Advice)14 Map (java.util.Map)13 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 AopUtils (org.springframework.aop.support.AopUtils)12 TestBean (org.springframework.beans.testfixture.beans.TestBean)12 ProxyFactory (org.springframework.aop.framework.ProxyFactory)11 HashMap (java.util.HashMap)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Advisor (org.springframework.aop.Advisor)9 Message (org.springframework.messaging.Message)8