use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class PerThisAspect method introductionWithArgumentBinding.
// TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed...
@Test
@Disabled
void introductionWithArgumentBinding() {
TestBean target = new TestBean();
List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeITestBeanModifiable(), "someBean"));
advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")));
Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class);
assertThat(modifiable).isInstanceOf(Modifiable.class);
Lockable lockable = (Lockable) modifiable;
assertThat(lockable.locked()).isFalse();
ITestBean itb = (ITestBean) modifiable;
assertThat(modifiable.isModified()).isFalse();
int oldAge = itb.getAge();
itb.setAge(oldAge + 1);
assertThat(modifiable.isModified()).isTrue();
modifiable.acceptChanges();
assertThat(modifiable.isModified()).isFalse();
itb.setAge(itb.getAge());
assertThat(modifiable.isModified()).as("Setting same value does not modify").isFalse();
itb.setName("And now for something completely different");
assertThat(modifiable.isModified()).isTrue();
lockable.lock();
assertThat(lockable.locked()).isTrue();
assertThatIllegalStateException().as("Should be locked").isThrownBy(() -> itb.setName("Else"));
lockable.unlock();
itb.setName("Tony");
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class PerThisAspect method aspectMethodThrowsExceptionIllegalOnSignature.
// TODO document this behaviour.
// Is it different AspectJ behaviour, at least for checked exceptions?
@Test
void aspectMethodThrowsExceptionIllegalOnSignature() {
TestBean target = new TestBean();
RemoteException expectedException = new RemoteException();
List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(itb::getAge).withCause(expectedException);
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class PerThisAspect method aspectMethodThrowsExceptionLegalOnSignature.
@Test
void aspectMethodThrowsExceptionLegalOnSignature() {
TestBean target = new TestBean();
UnsupportedOperationException expectedException = new UnsupportedOperationException();
List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(itb::getAge);
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testAfterReturningAdvisorIsInvoked.
@Test
public void testAfterReturningAdvisorIsInvoked() {
class SummingAfterAdvice implements AfterReturningAdvice {
public int sum;
@Override
public void afterReturning(Object returnValue, MethodInvocation invocation) throws Throwable {
sum += ((Integer) returnValue).intValue();
}
}
SummingAfterAdvice aa = new SummingAfterAdvice();
@SuppressWarnings("serial") Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {
@Override
public boolean matches(Method m, @Nullable Class<?> targetClass) {
return m.getReturnType() == int.class;
}
};
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesInt);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesInt);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(aa.sum).isEqualTo(0);
int i1 = 12;
int i2 = 13;
// Won't be advised
proxied.setAge(i1);
assertThat(proxied.getAge()).isEqualTo(i1);
assertThat(aa.sum).isEqualTo(i1);
proxied.setAge(i2);
assertThat(proxied.getAge()).isEqualTo(i2);
assertThat(aa.sum).isEqualTo((i1 + i2));
assertThat(proxied.getAge()).isEqualTo(i2);
}
use of cn.taketoday.aop.Advisor 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();
}
Aggregations