Search in sources :

Example 1 with CountingBeforeAdvice

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

the class AbstractAopProxyTests method testSerializationSerializableTargetAndAdvice.

@Test
public void testSerializationSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertTrue(SerializationTestUtils.isSerializable(personTarget));
    ProxyFactory pf = new ProxyFactory(personTarget);
    CountingThrowsAdvice cta = new CountingThrowsAdvice();
    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();
    p.echo(null);
    assertEquals(0, cta.getCalls());
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertEquals(1, cta.getCalls());
    // Will throw exception if it fails
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertNotSame(p, p2);
    assertEquals(p.getName(), p2.getName());
    assertEquals(p.getAge(), p2.getAge());
    assertTrue("Deserialized object is an AOP proxy", AopUtils.isAopProxy(p2));
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertEquals(a1.getAdvisors().length, a2.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertEquals("Proxies should be equal, even after one was serialized", p, p2);
    assertEquals("Proxies should be equal, even after one was serialized", p2, p);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertEquals(0, ni.getCount());
    a2.addAdvice(ni);
    p2.getAge();
    assertEquals(1, ni.getCount());
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertEquals(1, cta.getCalls());
    try {
        p2.echo(new IOException());
    } catch (IOException ex) {
    }
    assertEquals(2, cta.getCalls());
}
Also used : CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) IOException(java.io.IOException) Person(org.springframework.tests.sample.beans.Person) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 2 with CountingBeforeAdvice

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

the class AbstractAopProxyTests method testBeforeAdviceThrowsException.

@Test
public void testBeforeAdviceThrowsException() {
    final RuntimeException rex = new RuntimeException();
    @SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {

        @Override
        public void before(Method m, Object[] args, Object target) throws Throwable {
            super.before(m, args, target);
            if (m.getName().startsWith("set"))
                throw rex;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    NopInterceptor nop1 = new NopInterceptor();
    NopInterceptor nop2 = new NopInterceptor();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(nop1);
    pf.addAdvice(ba);
    pf.addAdvice(nop2);
    ITestBean proxied = (ITestBean) createProxy(pf);
    // Won't throw an exception
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(1, ba.getCalls());
    assertEquals(1, ba.getCalls("getAge"));
    assertEquals(1, nop1.getCount());
    assertEquals(1, nop2.getCount());
    // Will fail, after invoking Nop1
    try {
        proxied.setAge(26);
        fail("before advice should have ended chain");
    } catch (RuntimeException ex) {
        assertEquals(rex, ex);
    }
    assertEquals(2, ba.getCalls());
    assertEquals(2, nop1.getCount());
    // Nop2 didn't get invoked when the exception was thrown
    assertEquals(1, nop2.getCount());
    // Shouldn't have changed value in joinpoint
    assertEquals(target.getAge(), 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) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 3 with CountingBeforeAdvice

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

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertEquals(-1, pf.indexOf(new NopInterceptor()));
    assertEquals(0, pf.indexOf(nop));
    assertEquals(1, pf.indexOf(advisor));
    assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
}
Also used : 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 4 with CountingBeforeAdvice

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

the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.

@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    ITestBean t = (ITestBean) createProxy(pc);
    assertEquals(0, di.getCount());
    t.setAge(23);
    assertEquals(23, t.getAge());
    assertEquals(2, di.getCount());
    Advised advised = (Advised) t;
    assertEquals("Have 1 advisor", 1, advised.getAdvisors().length);
    assertEquals(di, advised.getAdvisors()[0].getAdvice());
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertEquals(3, di.getCount());
    assertEquals(1, di2.getCount());
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertEquals(3, di.getCount());
    assertEquals(2, di2.getCount());
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertEquals(0, cba.getCalls());
    advised.addAdvice(cba);
    t.setAge(16);
    assertEquals(16, t.getAge());
    assertEquals(2, cba.getCalls());
}
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) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 5 with CountingBeforeAdvice

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

the class UnsupportedInterceptor method testAddAdviceAtRuntime.

@Test
public void testAddAdviceAtRuntime() {
    TestBean bean = new TestBean();
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(bean);
    pf.setFrozen(false);
    pf.setOpaque(false);
    pf.setProxyTargetClass(true);
    TestBean proxy = (TestBean) pf.getProxy();
    assertTrue(AopUtils.isCglibProxy(proxy));
    proxy.getAge();
    assertEquals(0, cba.getCalls());
    ((Advised) proxy).addAdvice(cba);
    proxy.getAge();
    assertEquals(1, cba.getCalls());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) 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