Search in sources :

Example 41 with Advisor

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

the class PerThisAspect method testAspectMethodThrowsExceptionIllegalOnSignature.

// TODO document this behaviour.
// Is it different AspectJ behaviour, at least for checked exceptions?
@Test
public void testAspectMethodThrowsExceptionIllegalOnSignature() {
    TestBean target = new TestBean();
    RemoteException expectedException = new RemoteException();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
    assertEquals("One advice method was found", 1, advisors.size());
    ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
    try {
        itb.getAge();
        fail();
    } catch (UndeclaredThrowableException ex) {
        assertSame(expectedException, ex.getCause());
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) RemoteException(java.rmi.RemoteException) Test(org.junit.Test)

Example 42 with Advisor

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

the class PerThisAspect method testAspectMethodThrowsExceptionLegalOnSignature.

@Test
public void testAspectMethodThrowsExceptionLegalOnSignature() {
    TestBean target = new TestBean();
    UnsupportedOperationException expectedException = new UnsupportedOperationException();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
    assertEquals("One advice method was found", 1, advisors.size());
    ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
    try {
        itb.getAge();
        fail();
    } catch (UnsupportedOperationException ex) {
        assertSame(expectedException, ex);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) Test(org.junit.Test)

Example 43 with Advisor

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

the class PerThisAspect method testMultiplePerTargetAspects.

@Test
public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new LinkedList<>();
    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")));
    Collections.sort(advisors, new OrderComparator());
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) OrderComparator(org.springframework.core.OrderComparator) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 44 with Advisor

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

the class AopUtils method findAdvisorsThatCanApply.

/**
	 * Determine the sublist of the {@code candidateAdvisors} list
	 * that is applicable to the given class.
	 * @param candidateAdvisors the Advisors to evaluate
	 * @param clazz the target class
	 * @return sublist of Advisors that can apply to an object of the given class
	 * (may be the incoming List as-is)
	 */
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
    if (candidateAdvisors.isEmpty()) {
        return candidateAdvisors;
    }
    List<Advisor> eligibleAdvisors = new LinkedList<>();
    for (Advisor candidate : candidateAdvisors) {
        if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
            eligibleAdvisors.add(candidate);
        }
    }
    boolean hasIntroductions = !eligibleAdvisors.isEmpty();
    for (Advisor candidate : candidateAdvisors) {
        if (candidate instanceof IntroductionAdvisor) {
            // already processed
            continue;
        }
        if (canApply(candidate, clazz, hasIntroductions)) {
            eligibleAdvisors.add(candidate);
        }
    }
    return eligibleAdvisors;
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) Advisor(org.springframework.aop.Advisor) LinkedList(java.util.LinkedList)

Example 45 with Advisor

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

the class PerThisAspect method testAfterAdviceTypes.

@Test
public void testAfterAdviceTypes() throws Exception {
    Echo target = new Echo();
    ExceptionHandling afterReturningAspect = new ExceptionHandling();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(afterReturningAspect, "someBean"));
    Echo echo = (Echo) createProxy(target, advisors, Echo.class);
    assertEquals(0, afterReturningAspect.successCount);
    assertEquals("", echo.echo(""));
    assertEquals(1, afterReturningAspect.successCount);
    assertEquals(0, afterReturningAspect.failureCount);
    try {
        echo.echo(new FileNotFoundException());
        fail();
    } catch (FileNotFoundException ex) {
    // Ok
    } catch (Exception ex) {
        fail();
    }
    assertEquals(1, afterReturningAspect.successCount);
    assertEquals(1, afterReturningAspect.failureCount);
    assertEquals(afterReturningAspect.failureCount + afterReturningAspect.successCount, afterReturningAspect.afterCount);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) AopConfigException(org.springframework.aop.framework.AopConfigException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) FileNotFoundException(java.io.FileNotFoundException) RemoteException(java.rmi.RemoteException) Test(org.junit.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