Search in sources :

Example 31 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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 NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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 NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor 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 NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.

@Test
public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getParameterCount() == 0;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(cba.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(cba.getCalls("getAge")).isEqualTo(1);
    assertThat(cba.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(proxied.getAge()).isEqualTo(26);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Method(java.lang.reflect.Method) Nullable(org.springframework.lang.Nullable) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 35 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.

@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
    Overloads target = new Overloads();
    ProxyFactory pc = new ProxyFactory(target);
    NopInterceptor overLoadVoids = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 0;
        }
    });
    NopInterceptor overLoadInts = new NopInterceptor();
    pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
        }
    });
    IOverloads proxy = (IOverloads) createProxy(pc);
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(0);
    proxy.overload();
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    assertThat(proxy.overload(25)).isEqualTo(25);
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    proxy.noAdvice();
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)60 Test (org.junit.jupiter.api.Test)57 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)39 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)33 TestBean (org.springframework.beans.testfixture.beans.TestBean)32 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)13 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)11 Advisor (org.springframework.aop.Advisor)10 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 Method (java.lang.reflect.Method)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 IOException (java.io.IOException)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 Person (org.springframework.beans.testfixture.beans.Person)4 Nullable (org.springframework.lang.Nullable)4