use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class PerThisAspect method testPerTargetAspect.
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
TestBean target = new TestBean();
int realAge = 65;
target.setAge(realAge);
TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class);
assertEquals("Around advice must NOT apply", realAge, itb.getAge());
Advised advised = (Advised) itb;
SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
assertFalse(maaif.isMaterialized());
// Check that the perclause pointcut is valid
assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
// Hit the method in the per clause to instantiate the aspect
itb.getSpouse();
assertTrue(maaif.isMaterialized());
assertEquals("Around advice must apply", 0, itb.getAge());
assertEquals("Around advice must apply", 1, itb.getAge());
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class PerThisAspect method testPerThisAspect.
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
TestBean target = new TestBean();
int realAge = 65;
target.setAge(realAge);
TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")), TestBean.class);
assertEquals("Around advice must NOT apply", realAge, itb.getAge());
Advised advised = (Advised) itb;
// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
assertEquals(4, advised.getAdvisors().length);
SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
assertFalse(maaif.isMaterialized());
// Check that the perclause pointcut is valid
assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
// Hit the method in the per clause to instantiate the aspect
itb.getSpouse();
assertTrue(maaif.isMaterialized());
assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));
assertEquals("Around advice must apply", 0, itb.getAge());
assertEquals("Around advice must apply", 1, itb.getAge());
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class CounterAspect method testProgrammaticProxyCreation.
@Test
public void testProgrammaticProxyCreation() {
ITestBean testBean = new TestBean();
AspectJProxyFactory factory = new AspectJProxyFactory();
factory.setTarget(testBean);
CounterAspect myCounterAspect = new CounterAspect();
factory.addAspect(myCounterAspect);
ITestBean proxyTestBean = factory.getProxy();
assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
proxyTestBean.setAge(20);
assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class TraceBeforeAdvice method testAfterReturningAdviceWithoutJoinPoint.
private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch();
sw.start(howmany + " repeated after returning advice invocations with " + technology);
ITestBean adrian = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian));
Advised a = (Advised) adrian;
assertTrue(a.getAdvisors().length >= 3);
// Hits joinpoint
adrian.setAge(25);
for (int i = 0; i < howmany; i++) {
adrian.setAge(i);
}
sw.stop();
System.out.println(sw.prettyPrint());
return sw.getLastTaskTimeMillis();
}
use of org.springframework.aop.framework.Advised in project spring-framework by spring-projects.
the class TraceBeforeAdvice method testMix.
private long testMix(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch();
sw.start(howmany + " repeated mixed invocations with " + technology);
ITestBean adrian = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian));
Advised a = (Advised) adrian;
assertTrue(a.getAdvisors().length >= 3);
for (int i = 0; i < howmany; i++) {
// Hit all 3 joinpoints
adrian.getAge();
adrian.getName();
adrian.setAge(i);
// Invoke three non-advised methods
adrian.getDoctor();
adrian.getLawyer();
adrian.getSpouse();
}
sw.stop();
System.out.println(sw.prettyPrint());
return sw.getLastTaskTimeMillis();
}
Aggregations