use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.
@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertThat(AopContext.currentProxy()).isEqualTo(proxy);
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
assertThat(proxy.getAge()).isEqualTo(0);
}
use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.
/**
* Should see effect immediately on behavior.
*/
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
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));
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
TimeStamped ts = (TimeStamped) config.getProxy();
assertThat(ts.getTimeStamp() == time).isTrue();
// Can remove
config.removeAdvice(ti);
assertThat(config.getAdvisors().length == oldCount).isTrue();
assertThatExceptionOfType(RuntimeException.class).as("Existing object won't implement this interface any more").isThrownBy(// Existing reference will fail
ts::getTimeStamp);
assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertThat(config.getAdvisors().length == oldCount).isTrue();
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(1);
config.removeAdvice(debugInterceptor);
it.getSpouse();
// not invoked again
assertThat(debugInterceptor.getCount() == 1).isTrue();
}
use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryBeanTests method testDetectsInterfaces.
@Test
public void testDetectsInterfaces() {
ProxyFactoryBean fb = new ProxyFactoryBean();
fb.setTarget(new TestBean());
fb.addAdvice(new DebugInterceptor());
fb.setBeanFactory(new StandardBeanFactory());
ITestBean proxy = (ITestBean) fb.getObject();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
}
use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryBeanTests method testWithInterceptorNames.
@Test
public void testWithInterceptorNames() {
StandardBeanFactory bf = new StandardBeanFactory();
bf.registerSingleton("debug", new DebugInterceptor());
ProxyFactoryBean fb = new ProxyFactoryBean();
fb.setTarget(new TestBean());
fb.setInterceptorNames("debug");
fb.setBeanFactory(bf);
Advised proxy = (Advised) fb.getObject();
assertThat(proxy.getAdvisorCount()).isEqualTo(1);
}
use of cn.taketoday.aop.interceptor.DebugInterceptor in project today-infrastructure by TAKETODAY.
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(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(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));
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
TimeStamped ts = (TimeStamped) factory.getBean("test2");
assertThat(ts.getTimeStamp()).isEqualTo(time);
// Can remove
config.removeAdvice(ti);
assertThat(config.getAdvisors().length == oldCount).isTrue();
// Check no change on existing object reference
assertThat(ts.getTimeStamp() == time).isTrue();
assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertThat(config.getAdvisors().length == oldCount).isTrue();
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
// Won't affect existing reference
assertThat(debugInterceptor.getCount() == 0).isTrue();
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(1);
config.removeAdvice(debugInterceptor);
it.getSpouse();
// Still invoked with old reference
assertThat(debugInterceptor.getCount()).isEqualTo(2);
// not invoked with new object
it = (ITestBean) factory.getBean("test2");
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(2);
// Our own timestamped reference should still work
assertThat(ts.getTimeStamp()).isEqualTo(time);
}
Aggregations