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());
}
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));
}
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());
}
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);
}
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();
}
Aggregations