Search in sources :

Example 1 with Advised

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

the class PerThisAspect method perTargetAspect.

@Test
void perTargetAspect() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class);
    assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
    Advised advised = (Advised) itb;
    ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertThat(maaif.isMaterialized()).isFalse();
    // Check that the perclause pointcut is valid
    assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
    assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertThat(maaif.isMaterialized()).isTrue();
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0);
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1);
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Advised(cn.taketoday.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 2 with Advised

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

the class CountingAspectJAdvice method testIsProxy.

@Test
public void testIsProxy() throws Exception {
    ITestBean bean = getTestBean();
    assertThat(AopUtils.isAopProxy(bean)).as("Bean is not a proxy").isTrue();
    // check the advice details
    Advised advised = (Advised) bean;
    Advisor[] advisors = advised.getAdvisors();
    assertThat(advisors.length > 0).as("Advisors should not be empty").isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 3 with Advised

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

the class EnableAsyncTests method customAsyncAnnotationIsPropagated.

@Test
public void customAsyncAnnotationIsPropagated() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class);
    ctx.refresh();
    Object bean = ctx.getBean(CustomAsyncBean.class);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    boolean isAsyncAdvised = false;
    for (Advisor advisor : ((Advised) bean).getAdvisors()) {
        if (advisor instanceof AsyncAnnotationAdvisor) {
            isAsyncAdvised = true;
            break;
        }
    }
    assertThat(isAsyncAdvised).as("bean was not async advised as expected").isTrue();
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 4 with Advised

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

the class AnnotationTransactionAttributeSourceTests method serializable.

@Test
public void serializable() throws Exception {
    TestBean1 tb = new TestBean1();
    CallCountingTransactionManager ptm = new CallCountingTransactionManager();
    AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
    TransactionInterceptor ti = new TransactionInterceptor((PlatformTransactionManager) ptm, tas);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean1.class);
    proxyFactory.addAdvice(ti);
    proxyFactory.setTarget(tb);
    ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
    proxy.getAge();
    assertThat(ptm.commits).isEqualTo(1);
    ITestBean1 serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    serializedProxy.getAge();
    Advised advised = (Advised) serializedProxy;
    TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
    CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager();
    assertThat(serializedPtm.commits).isEqualTo(2);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 5 with Advised

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

the class JoinPointMonitorAtAspectJAspect method checkAtAspectJAspect.

private void checkAtAspectJAspect(String appContextFile) {
    ApplicationContext context = new ClassPathXmlApplicationContext(appContextFile, getClass());
    ICounter counter = (ICounter) context.getBean("counter");
    boolean condition = counter instanceof Advised;
    assertThat(condition).as("Proxy didn't get created").isTrue();
    counter.increment();
    JoinPointMonitorAtAspectJAspect callCountingAspect = (JoinPointMonitorAtAspectJAspect) context.getBean("monitoringAspect");
    assertThat(callCountingAspect.beforeExecutions).as("Advise didn't get executed").isEqualTo(1);
    assertThat(callCountingAspect.aroundExecutions).as("Advise didn't get executed").isEqualTo(1);
}
Also used : ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) Advised(cn.taketoday.aop.framework.Advised)

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