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