Search in sources :

Example 31 with DefaultPointcutAdvisor

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

the class ProxyFactoryTests method testRemoveAdvisorByIndex.

@Test
public void testRemoveAdvisorByIndex() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    NopInterceptor nop2 = new NopInterceptor();
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(nop2.getCount()).isEqualTo(1);
    // Removes counting before advisor
    pf.removeAdvisor(1);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(2);
    // Removes Nop1
    pf.removeAdvisor(0);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(3);
    // Check out of bounds
    try {
        pf.removeAdvisor(-1);
    } catch (AopConfigException ex) {
    // Ok
    }
    try {
        pf.removeAdvisor(2);
    } catch (AopConfigException ex) {
    // Ok
    }
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(4);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 32 with DefaultPointcutAdvisor

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

the class ProxyFactoryTests method testRemoveAdvisorByReference.

@Test
public void testRemoveAdvisorByReference() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(cba);
    pf.addAdvice(nop);
    pf.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) pf.getProxy();
    proxied.setAge(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(pf.removeAdvisor(advisor)).isTrue();
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 33 with DefaultPointcutAdvisor

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

the class ProxyFactoryTests method testReplaceAdvisor.

@Test
public void testReplaceAdvisor() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
    CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
    Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
    Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
    pf.addAdvisor(advisor1);
    pf.addAdvice(nop);
    ITestBean proxied = (ITestBean) pf.getProxy();
    // Use the type cast feature
    // Replace etc methods on advised should be same as on ProxyFactory
    Advised advised = (Advised) proxied;
    proxied.setAge(5);
    assertThat(cba1.getCalls()).isEqualTo(1);
    assertThat(cba2.getCalls()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)).isFalse();
    assertThat(advised.replaceAdvisor(advisor1, advisor2)).isTrue();
    assertThat(pf.getAdvisors()[0]).isEqualTo(advisor2);
    assertThat(proxied.getAge()).isEqualTo(5);
    assertThat(cba1.getCalls()).isEqualTo(1);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(cba2.getCalls()).isEqualTo(1);
    assertThat(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)).isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 34 with DefaultPointcutAdvisor

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

the class TrickyAspectJPointcutExpressionTests method testManualProxyJavaWithStaticPointcut.

@Test
public void testManualProxyJavaWithStaticPointcut() throws Exception {
    TestService target = new TestServiceImpl();
    LogUserAdvice logAdvice = new LogUserAdvice();
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName()));
    testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl");
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 35 with DefaultPointcutAdvisor

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

the class TrickyAspectJPointcutExpressionTests method testManualProxyJavaWithDynamicPointcutAndProxyTargetClass.

@Test
public void testManualProxyJavaWithDynamicPointcutAndProxyTargetClass() throws Exception {
    TestService target = new TestServiceImpl();
    LogUserAdvice logAdvice = new LogUserAdvice();
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName()));
    testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl", true);
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)45 Test (org.junit.jupiter.api.Test)29 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)16 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 TestBean (org.springframework.beans.testfixture.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)8 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 AspectJExpressionPointcut (org.springframework.aop.aspectj.AspectJExpressionPointcut)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 Pointcut (org.springframework.aop.Pointcut)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 ResourceScriptSource (org.springframework.scripting.support.ResourceScriptSource)4 Test (org.junit.Test)3 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)3 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3