Search in sources :

Example 1 with IntroductionAdvisor

use of cn.taketoday.aop.IntroductionAdvisor in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.IntroductionAdvisor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) IntroductionInterceptor(cn.taketoday.aop.IntroductionInterceptor) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) Test(org.junit.jupiter.api.Test)

Example 2 with IntroductionAdvisor

use of cn.taketoday.aop.IntroductionAdvisor in project today-framework by TAKETODAY.

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(cn.taketoday.aop.IntroductionAdvisor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) IntroductionInterceptor(cn.taketoday.aop.IntroductionInterceptor) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

IntroductionAdvisor (cn.taketoday.aop.IntroductionAdvisor)2 IntroductionInterceptor (cn.taketoday.aop.IntroductionInterceptor)2 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)2 Test (org.junit.jupiter.api.Test)2