Search in sources :

Example 26 with Advisor

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

the class PerThisAspect method multiplePerTargetAspects.

@Test
void multiplePerTargetAspects() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new ArrayList<>();
    PerTargetAspect aspect1 = new PerTargetAspect();
    aspect1.count = 100;
    aspect1.setOrder(10);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspect aspect2 = new PerTargetAspect();
    aspect2.setOrder(5);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    OrderComparator.sort(advisors);
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    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) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 27 with Advisor

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

the class PerThisAspect method multiplePerTargetAspectsWithOrderAnnotation.

@Test
void multiplePerTargetAspectsWithOrderAnnotation() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new ArrayList<>();
    PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
    aspect1.count = 100;
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    OrderComparator.sort(advisors);
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0);
    assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1);
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 28 with Advisor

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

the class PerThisAspect method introductionBasedOnAnnotationMatch_SPR5307.

@Test
void introductionBasedOnAnnotationMatch_SPR5307() {
    AnnotatedTarget target = new AnnotatedTargetImpl();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(), "someBean"));
    Object proxy = createProxy(target, advisors, AnnotatedTarget.class);
    System.out.println(advisors.get(1));
    assertThat(proxy instanceof Lockable).isTrue();
    Lockable lockable = (Lockable) proxy;
    lockable.locked();
}
Also used : Advisor(org.springframework.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) Test(org.junit.jupiter.api.Test)

Example 29 with Advisor

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

the class TestNamespaceHandler method testProxyingDecorator.

@Test
public void testProxyingDecorator() throws Exception {
    ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean");
    assertTestBean(bean);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    Advisor[] advisors = ((Advised) bean).getAdvisors();
    assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(1);
    assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 30 with Advisor

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

the class AbstractAopProxyTests method testThrowsAdvisorIsInvoked.

@Test
public void testThrowsAdvisorIsInvoked() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    @SuppressWarnings("serial") Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().startsWith("echo");
        }
    };
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesEchoInvocations);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesEchoInvocations);
    IEcho proxied = (IEcho) createProxy(pf);
    assertThat(th.getCalls()).isEqualTo(0);
    assertThat(proxied.getA()).isEqualTo(target.getA());
    assertThat(th.getCalls()).isEqualTo(0);
    Exception ex = new Exception();
    // Will be advised but doesn't match
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
    FileNotFoundException fex = new FileNotFoundException();
    assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> proxied.echoException(1, fex)).matches(fex::equals);
    assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) FileNotFoundException(java.io.FileNotFoundException) 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) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) Nullable(org.springframework.lang.Nullable) 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