Search in sources :

Example 6 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice 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);
    assertEquals(1, cba.getCalls());
    assertEquals(1, nop.getCount());
    assertTrue(pf.removeAdvisor(advisor));
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null)));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) 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.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 7 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice 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);
    assertEquals(1, cba.getCalls());
    assertEquals(1, nop.getCount());
    assertEquals(1, nop2.getCount());
    // Removes counting before advisor
    pf.removeAdvisor(1);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(2, nop2.getCount());
    // Removes Nop1
    pf.removeAdvisor(0);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(3, nop2.getCount());
    // Check out of bounds
    try {
        pf.removeAdvisor(-1);
    } catch (AopConfigException ex) {
    // Ok
    }
    try {
        pf.removeAdvisor(2);
    } catch (AopConfigException ex) {
    // Ok
    }
    assertEquals(5, proxied.getAge());
    assertEquals(4, nop2.getCount());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) 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.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 8 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice 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);
    assertEquals(1, cba1.getCalls());
    assertEquals(0, cba2.getCalls());
    assertEquals(1, nop.getCount());
    assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2));
    assertTrue(advised.replaceAdvisor(advisor1, advisor2));
    assertEquals(advisor2, pf.getAdvisors()[0]);
    assertEquals(5, proxied.getAge());
    assertEquals(1, cba1.getCalls());
    assertEquals(2, nop.getCount());
    assertEquals(1, cba2.getCalls());
    assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) 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.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 9 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.

the class CountingAspectJAdvice method testAdviceInvokedCorrectly.

@Test
public void testAdviceInvokedCorrectly() throws Exception {
    CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
    CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");
    ITestBean bean = getTestBean();
    assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge"));
    assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName"));
    bean.getAge();
    assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge"));
    assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge"));
    bean.getName();
    assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName"));
    assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 10 with CountingBeforeAdvice

use of org.springframework.tests.aop.advice.CountingBeforeAdvice 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, 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);
    assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertEquals(0, cba.getCalls());
    assertEquals(0, cba.getCalls("getAge"));
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, cba.getCalls());
    assertEquals(1, cba.getCalls("getAge"));
    assertEquals(0, cba.getCalls("setAge"));
    // Won't be advised
    proxied.setAge(26);
    assertEquals(1, cba.getCalls());
    assertEquals(26, proxied.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) 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) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Aggregations

CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)17 Test (org.junit.Test)16 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)11 TestBean (org.springframework.tests.sample.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)6 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3 LockMixinAdvisor (test.mixin.LockMixinAdvisor)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1