Search in sources :

Example 56 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class AspectJPrecedenceComparatorTests method testSameAspectNoAfterAdvice.

@Test
public void testSameAspectNoAfterAdvice() {
    Advisor advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
    Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted before advisor2").isEqualTo(-1);
    advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
    advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor2 sorted before advisor1").isEqualTo(1);
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 57 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class AspectJPrecedenceComparatorTests method testSameAdvisorPrecedenceDifferentAspectAfterAdvice.

@Test
public void testSameAdvisorPrecedenceDifferentAspectAfterAdvice() {
    Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
    Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0);
    advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
    advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("nothing to say about order here").isEqualTo(0);
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 58 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class ProxyFactoryTests method testRemoveAdvisorByIndex.

@Test
public void testRemoveAdvisorByIndex() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    NopInterceptor nop2 = new NopInterceptor();
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(nop2.getCount()).isEqualTo(1);
    // Removes counting before advisor
    pf.removeAdvisor(1);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(2);
    // Removes Nop1
    pf.removeAdvisor(0);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(3);
    // Check out of bounds
    try {
        pf.removeAdvisor(-1);
    } catch (AopConfigException ex) {
    // Ok
    }
    try {
        pf.removeAdvisor(2);
    } catch (AopConfigException ex) {
    // Ok
    }
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(4);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 59 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class ProxyFactoryTests method testRemoveAdvisorByReference.

@Test
public void testRemoveAdvisorByReference() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(pf.removeAdvisor(advisor)).isTrue();
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 60 with Advisor

use of org.springframework.aop.Advisor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.

@Test
public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getParameterCount() == 0;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(cba.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(cba.getCalls("getAge")).isEqualTo(1);
    assertThat(cba.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(proxied.getAge()).isEqualTo(26);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Method(java.lang.reflect.Method) Nullable(org.springframework.lang.Nullable) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Aggregations

Advisor (org.springframework.aop.Advisor)70 Test (org.junit.jupiter.api.Test)33 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)25 Advised (org.springframework.aop.framework.Advised)21 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)19 Test (org.junit.Test)16 TestBean (org.springframework.beans.testfixture.beans.TestBean)16 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)14 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)11 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 ArrayList (java.util.ArrayList)10 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 Method (java.lang.reflect.Method)7 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)7 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 LockMixinAdvisor (test.mixin.LockMixinAdvisor)7 Advice (org.aopalliance.aop.Advice)6 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)6