Search in sources :

Example 1 with DebugInterceptor

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
	 * Try adding and removing interfaces and interceptors on prototype.
	 * Changes will only affect future references obtained from the factory.
	 * Each instance will be independent.
	 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat("Shouldn't implement TimeStamped before manipulation", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertTrue(config.getAdvisors().length == oldCount + 1);
    TimeStamped ts = (TimeStamped) factory.getBean("test2");
    assertEquals(time, ts.getTimeStamp());
    // Can remove
    config.removeAdvice(ti);
    assertTrue(config.getAdvisors().length == oldCount);
    // Check no change on existing object reference
    assertTrue(ts.getTimeStamp() == time);
    assertThat("Should no longer implement TimeStamped", factory.getBean("test2"), not(instanceOf(TimeStamped.class)));
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertTrue(config.getAdvisors().length == oldCount);
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    // Won't affect existing reference
    assertTrue(debugInterceptor.getCount() == 0);
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertEquals(1, debugInterceptor.getCount());
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked wiht old reference
    assertEquals(2, debugInterceptor.getCount());
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertEquals(2, debugInterceptor.getCount());
    // Our own timestamped reference should still work
    assertEquals(time, ts.getTimeStamp());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 2 with DebugInterceptor

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

the class ProxyFactoryBeanTests method testDetectsInterfaces.

@Test
public void testDetectsInterfaces() throws Exception {
    ProxyFactoryBean fb = new ProxyFactoryBean();
    fb.setTarget(new TestBean());
    fb.addAdvice(new DebugInterceptor());
    fb.setBeanFactory(new DefaultListableBeanFactory());
    ITestBean proxy = (ITestBean) fb.getObject();
    assertTrue(AopUtils.isJdkDynamicProxy(proxy));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 3 with DebugInterceptor

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

the class OverloadLookup method assertInterceptorCount.

private void assertInterceptorCount(int count) {
    DebugInterceptor interceptor = getInterceptor();
    assertEquals("Interceptor count is incorrect", count, interceptor.getCount());
}
Also used : DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor)

Example 4 with DebugInterceptor

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

the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.

/**
	 * Should see effect immediately on behavior.
	 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
    ProxyFactory config = new ProxyFactory(new TestBean());
    assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped);
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to front of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertTrue(config.getAdvisors().length == oldCount + 1);
    TimeStamped ts = (TimeStamped) config.getProxy();
    assertTrue(ts.getTimeStamp() == time);
    // Can remove
    config.removeAdvice(ti);
    assertTrue(config.getAdvisors().length == oldCount);
    try {
        // Existing reference will fail
        ts.getTimeStamp();
        fail("Existing object won't implement this interface any more");
    } catch (RuntimeException ex) {
    }
    assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped);
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertTrue(config.getAdvisors().length == oldCount);
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    assertEquals(1, debugInterceptor.getCount());
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // not invoked again
    assertTrue(debugInterceptor.getCount() == 1);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.Test)

Example 5 with DebugInterceptor

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

the class OverloadLookup method resetInterceptor.

private void resetInterceptor() {
    DebugInterceptor interceptor = getInterceptor();
    interceptor.resetCount();
}
Also used : DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor)

Aggregations

DebugInterceptor (org.springframework.aop.interceptor.DebugInterceptor)7 Test (org.junit.Test)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 TestBean (org.springframework.tests.sample.beans.TestBean)3 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)2 TimeStamped (org.springframework.tests.TimeStamped)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MarshalException (java.rmi.MarshalException)1 SQLException (java.sql.SQLException)1 TargetSource (org.springframework.aop.TargetSource)1 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)1 SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 TimestampIntroductionInterceptor (org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor)1 LockedException (test.mixin.LockedException)1