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