Search in sources :

Example 41 with NopInterceptor

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

the class AbstractAopProxyTests method testReentrance.

@Test
public void testReentrance() {
    int age1 = 33;
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    NopInterceptor di1 = new NopInterceptor();
    pf1.addAdvice(0, di1);
    ITestBean advised1 = (ITestBean) createProxy(pf1);
    // = 1 invocation
    advised1.setAge(age1);
    // = 2 invocations
    advised1.setSpouse(advised1);
    assertEquals("one was invoked correct number of times", 2, di1.getCount());
    // = 3 invocations
    assertEquals("Advised one has correct age", age1, advised1.getAge());
    assertEquals("one was invoked correct number of times", 3, di1.getCount());
    // = 5 invocations, as reentrant call to spouse is advised also
    assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge());
    assertEquals("one was invoked correct number of times", 5, di1.getCount());
}
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) Test(org.junit.Test)

Example 42 with NopInterceptor

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

the class AbstractAopProxyTests method testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters.

@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    // Could apply dynamically to getAge/setAge but not to getName
    TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
    pc.addAdvisor(dp);
    this.mockTargetSource.setTarget(tb);
    pc.setTargetSource(mockTargetSource);
    ITestBean it = (ITestBean) createProxy(pc);
    assertEquals(dp.count, 0);
    it.getAge();
    // Statically vetoed
    assertEquals(0, dp.count);
    it.setAge(11);
    assertEquals(it.getAge(), 11);
    assertEquals(dp.count, 1);
    // Applies statically but not dynamically
    it.setName("joe");
    assertEquals(dp.count, 1);
}
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) Test(org.junit.Test)

Example 43 with NopInterceptor

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

the class AbstractAopProxyTests method testAfterReturningAdvisorIsNotInvokedOnException.

@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
    CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvice(car);
    assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertEquals(0, car.getCalls());
    int age = 10;
    proxied.setAge(age);
    assertEquals(age, proxied.getAge());
    assertEquals(2, car.getCalls());
    Exception exc = new Exception();
    // On exception it won't be invoked
    try {
        proxied.exceptional(exc);
        fail();
    } catch (Throwable t) {
        assertSame(exc, t);
    }
    assertEquals(2, car.getCalls());
}
Also used : CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) 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) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 44 with NopInterceptor

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

the class AbstractAopProxyTests method testTargetCanGetProxy.

@Test
public void testTargetCanGetProxy() {
    NopInterceptor di = new NopInterceptor();
    INeedsToSeeProxy target = new TargetChecker();
    ProxyFactory proxyFactory = new ProxyFactory(target);
    proxyFactory.setExposeProxy(true);
    assertTrue(proxyFactory.isExposeProxy());
    proxyFactory.addAdvice(0, di);
    INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory);
    assertEquals(0, di.getCount());
    assertEquals(0, target.getCount());
    proxied.incrementViaThis();
    assertEquals("Increment happened", 1, target.getCount());
    assertEquals("Only one invocation via AOP as use of this wasn't proxied", 1, di.getCount());
    // 1 invocation
    assertEquals("Increment happened", 1, proxied.getCount());
    // 2 invoocations
    proxied.incrementViaProxy();
    assertEquals("Increment happened", 2, target.getCount());
    assertEquals("3 more invocations via AOP as the first call was reentrant through the proxy", 4, di.getCount());
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

Example 45 with NopInterceptor

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

the class UnsupportedInterceptor method testNoTarget.

@Test(expected = AopConfigException.class)
public void testNoTarget() {
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    AopProxy aop = createAopProxy(pc);
    aop.getProxy();
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) Test(org.junit.Test)

Aggregations

NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)61 Test (org.junit.Test)57 ITestBean (org.springframework.tests.sample.beans.ITestBean)40 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)34 TestBean (org.springframework.tests.sample.beans.TestBean)34 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.tests.aop.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 LockedException (test.mixin.LockedException)5 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 CountingAfterReturningAdvice (org.springframework.tests.aop.advice.CountingAfterReturningAdvice)4 FileNotFoundException (java.io.FileNotFoundException)3