Search in sources :

Example 1 with Advisor

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");
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Advisor(cn.taketoday.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with Advisor

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);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Advisor(cn.taketoday.aop.Advisor) RemoteException(java.rmi.RemoteException) Test(org.junit.jupiter.api.Test)

Example 3 with Advisor

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);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Advisor(cn.taketoday.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 4 with Advisor

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);
}
Also used : NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) Advisor(cn.taketoday.aop.Advisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) Method(java.lang.reflect.Method) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Nullable(cn.taketoday.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 5 with Advisor

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

Aggregations

Advisor (cn.taketoday.aop.Advisor)76 Test (org.junit.jupiter.api.Test)70 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)52 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)43 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)41 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)32 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)22 AspectJPointcutAdvisor (cn.taketoday.aop.aspectj.AspectJPointcutAdvisor)21 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)20 CountingBeforeAdvice (cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice)16 Method (java.lang.reflect.Method)16 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)14 LockMixinAdvisor (test.mixin.LockMixinAdvisor)14 Advised (cn.taketoday.aop.framework.Advised)13 NopInterceptor (cn.taketoday.aop.NopInterceptor)10 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)10 Nullable (cn.taketoday.lang.Nullable)10 NameMatchMethodPointcut (cn.taketoday.aop.support.NameMatchMethodPointcut)8 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8