Search in sources :

Example 1 with PerTargetAspect

use of test.aop.PerTargetAspect 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());
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Test(org.junit.Test)

Example 2 with PerTargetAspect

use of test.aop.PerTargetAspect in project spring-framework by spring-projects.

the class AspectJPointcutAdvisorTests method testPerTarget.

@Test
public void testPerTarget() throws SecurityException, NoSuchMethodException {
    AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
    ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);
    InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(ajexp, TestBean.class.getMethod("getAge"), af, new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean"), 1, "someBean");
    assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
    assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut);
    assertTrue(ajpa.isPerInstance());
    assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class));
    assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), TestBean.class));
    assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), TestBean.class));
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) TestBean(org.springframework.tests.sample.beans.TestBean) AspectJExpressionPointcut(org.springframework.aop.aspectj.AspectJExpressionPointcut) Test(org.junit.Test)

Example 3 with PerTargetAspect

use of test.aop.PerTargetAspect 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)

Aggregations

Test (org.junit.Test)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 PerTargetAspect (test.aop.PerTargetAspect)3 JoinPoint (org.aspectj.lang.JoinPoint)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 LinkedList (java.util.LinkedList)1 Advisor (org.springframework.aop.Advisor)1 AspectJExpressionPointcut (org.springframework.aop.aspectj.AspectJExpressionPointcut)1 Advised (org.springframework.aop.framework.Advised)1 OrderComparator (org.springframework.core.OrderComparator)1