Search in sources :

Example 26 with NopInterceptor

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

the class AbstractAopProxyTests method testValuesStick.

/**
 * Simple test that if we set values we can get them out again.
 */
@Test
public void testValuesStick() {
    int age1 = 33;
    int age2 = 37;
    String name = "tony";
    TestBean target1 = new TestBean();
    target1.setAge(age1);
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
    pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
    ITestBean tb = (ITestBean) pf1.getProxy();
    assertThat(tb.getAge()).isEqualTo(age1);
    tb.setAge(age2);
    assertThat(tb.getAge()).isEqualTo(age2);
    assertThat(tb.getName()).isNull();
    tb.setName(name);
    assertThat(tb.getName()).isEqualTo(name);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 27 with NopInterceptor

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

the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.

@Test
public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getParameterCount() == 0;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(cba.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(cba.getCalls("getAge")).isEqualTo(1);
    assertThat(cba.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(proxied.getAge()).isEqualTo(26);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Advisor(cn.taketoday.aop.Advisor) LockMixinAdvisor(cn.taketoday.aop.mixin.LockMixinAdvisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 28 with NopInterceptor

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

the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.

@Test
public void testSerializationAdviceNotSerializable() throws Exception {
    SerializablePerson sp = new SerializablePerson();
    assertThat(SerializationTestUtils.isSerializable(sp)).isTrue();
    ProxyFactory pf = new ProxyFactory(sp);
    // This isn't serializable
    Advice i = new NopInterceptor();
    pf.addAdvice(i);
    assertThat(SerializationTestUtils.isSerializable(i)).isFalse();
    Object proxy = createAopProxy(pf).getProxy();
    assertThat(SerializationTestUtils.isSerializable(proxy)).isFalse();
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) ThrowsAdvice(cn.taketoday.aop.ThrowsAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) Advice(org.aopalliance.aop.Advice) DynamicIntroductionAdvice(cn.taketoday.aop.DynamicIntroductionAdvice) CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) Test(org.junit.jupiter.api.Test)

Example 29 with NopInterceptor

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

the class AbstractAopProxyTests method testStaticMethodPointcut.

@Test
public void testStaticMethodPointcut() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    NopInterceptor di = new NopInterceptor();
    TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
    pc.addAdvisor(sp);
    pc.setTarget(tb);
    ITestBean it = (ITestBean) createProxy(pc);
    assertThat(0).isEqualTo(di.getCount());
    it.getAge();
    assertThat(1).isEqualTo(di.getCount());
    it.setAge(11);
    assertThat(11).isEqualTo(it.getAge());
    assertThat(2).isEqualTo(di.getCount());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 30 with NopInterceptor

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

the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.

@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
    Overloads target = new Overloads();
    ProxyFactory pc = new ProxyFactory(target);
    NopInterceptor overLoadVoids = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 0;
        }
    });
    NopInterceptor overLoadInts = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
        }
    });
    IOverloads proxy = (IOverloads) createProxy(pc);
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(0);
    proxy.overload();
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    assertThat(proxy.overload(25)).isEqualTo(25);
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    proxy.noAdvice();
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (cn.taketoday.aop.NopInterceptor)47 Test (org.junit.jupiter.api.Test)46 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)39 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)30 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)11 Advisor (cn.taketoday.aop.Advisor)10 Advised (cn.taketoday.aop.framework.Advised)9 AopConfigException (cn.taketoday.aop.framework.AopConfigException)9 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)8 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)8 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)7 AopProxy (cn.taketoday.aop.framework.AopProxy)6 LockMixinAdvisor (cn.taketoday.aop.mixin.LockMixinAdvisor)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 CountingAfterReturningAdvice (cn.taketoday.aop.CountingAfterReturningAdvice)4 IJmxTestBean (cn.taketoday.jmx.IJmxTestBean)4