Search in sources :

Example 56 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInSubclass.

@Test
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
    final long t = 1001L;
    @SuppressWarnings("serial")
    class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new TestII();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
    assertThat(ia.isPerInstance()).isTrue();
    pf.addAdvisor(0, ia);
    // assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertThat(ts).isInstanceOf(TimeStamped.class);
    // Shouldn't proxy framework interfaces
    assertThat(!(ts instanceof MethodInterceptor)).isTrue();
    assertThat(!(ts instanceof IntroductionInterceptor)).isTrue();
    assertThat(ts.getTimeStamp() == t).isTrue();
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
    // Test removal
    ii.suppressInterface(TimeStamped.class);
    // Note that we need to construct a new proxy factory,
    // or suppress the interface on the proxy factory
    pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    Object o = pf.getProxy();
    assertThat(!(o instanceof TimeStamped)).isTrue();
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntroductionInterceptor(org.springframework.aop.IntroductionInterceptor) TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)56 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 Method (java.lang.reflect.Method)19 List (java.util.List)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 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 Assert.assertEquals (org.junit.Assert.assertEquals)7