Search in sources :

Example 81 with Advised

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

the class AbstractAopProxyTests method testSerializableTargetAndAdvice.

@Test
public void testSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
    ProxyFactory pf = new ProxyFactory(personTarget);
    CountingThrowsAdvice cta = new CountingThrowsAdvice();
    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();
    p.echo(null);
    assertThat(cta.getCalls()).isEqualTo(0);
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertThat(cta.getCalls()).isEqualTo(1);
    // Will throw exception if it fails
    Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo(p.getName());
    assertThat(p2.getAge()).isEqualTo(p.getAge());
    assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
    assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(0);
    a2.addAdvice(ni);
    p2.getAge();
    assertThat(ni.getCount()).isEqualTo(1);
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertThat(cta.getCalls()).isEqualTo(1);
    try {
        p2.echo(new IOException());
    } catch (IOException ignored) {
    }
    assertThat(cta.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) IOException(java.io.IOException) Person(cn.taketoday.aop.support.NameMatchMethodPointcutTests.Person) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 82 with Advised

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

the class AbstractAopProxyTests method testProxyConfigString.

/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    MethodBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
    pc.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) createProxy(pc);
    String proxyConfigString = ((Advised) proxied).toProxyConfigString();
    assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
    assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
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) Advised(cn.taketoday.aop.framework.Advised) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) 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) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 83 with Advised

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

the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.

@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
    final TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return TestBean.class;
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() throws Exception {
            assertThat(AopContext.currentProxy()).isEqualTo(proxy);
            return target;
        }

        @Override
        public void releaseTarget(Object target) throws Exception {
        }
    });
    // Just test anything: it will fail if context wasn't found
    assertThat(proxy.getAge()).isEqualTo(0);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) 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) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 84 with Advised

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

the class ProxyFactoryTests method testReplaceAdvisor.

@Test
public void testReplaceAdvisor() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
    CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
    Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
    Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
    pf.addAdvisor(advisor1);
    pf.addAdvice(nop);
    ITestBean proxied = (ITestBean) pf.getProxy();
    // Use the type cast feature
    // Replace etc methods on advised should be same as on ProxyFactory
    Advised advised = (Advised) proxied;
    proxied.setAge(5);
    assertThat(cba1.getCalls()).isEqualTo(1);
    assertThat(cba2.getCalls()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)).isFalse();
    assertThat(advised.replaceAdvisor(advisor1, advisor2)).isTrue();
    assertThat(pf.getAdvisors()[0]).isEqualTo(advisor2);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba1.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(cba2.getCalls()).isEqualTo(1);
    assertThat(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)).isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.NopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 85 with Advised

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

the class SimpleBeforeAdviceInterceptor method getAdviceImpl.

private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
    Advised advised = (Advised) tb;
    Advisor advisor = advised.getAdvisors()[0];
    return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
Also used : Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor)

Aggregations

Advised (cn.taketoday.aop.framework.Advised)85 Test (org.junit.jupiter.api.Test)58 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)40 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)23 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)18 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)18 Advisor (cn.taketoday.aop.Advisor)13 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)12 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)12 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)12 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)10 Test (org.junit.Test)10 NopInterceptor (cn.taketoday.aop.NopInterceptor)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)8 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)7 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 RetryContext (cn.taketoday.retry.RetryContext)6 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)6 ArrayList (java.util.ArrayList)6