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);
}
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();
}
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();
}
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);
}
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);
}
Aggregations