use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
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);
assertThat(di.getCount()).isEqualTo(0);
t.setAge(23);
assertThat(t.getAge()).isEqualTo(23);
assertThat(di.getCount()).isEqualTo(2);
Advised advised = (Advised) t;
assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
NopInterceptor di2 = new NopInterceptor();
advised.addAdvice(1, di2);
t.getName();
assertThat(di.getCount()).isEqualTo(3);
assertThat(di2.getCount()).isEqualTo(1);
// will remove di
advised.removeAdvisor(0);
t.getAge();
// Unchanged
assertThat(di.getCount()).isEqualTo(3);
assertThat(di2.getCount()).isEqualTo(2);
CountingBeforeAdvice cba = new CountingBeforeAdvice();
assertThat(cba.getCalls()).isEqualTo(0);
advised.addAdvice(cba);
t.setAge(16);
assertThat(t.getAge()).isEqualTo(16);
assertThat(cba.getCalls()).isEqualTo(2);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
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);
assertThat(dp.count).isEqualTo(0);
it.getAge();
// Statically vetoed
assertThat(dp.count).isEqualTo(0);
it.setAge(11);
assertThat(it.getAge()).isEqualTo(11);
assertThat(dp.count).isEqualTo(1);
// Applies statically but not dynamically
it.setName("joe");
assertThat(dp.count).isEqualTo(1);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
pc.addAdvisor(advisor);
assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
pc.setOpaque(true);
assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertThat(proxied.getAge()).isEqualTo(10);
assertThat(mba.getCalls()).isEqualTo(1);
boolean condition = proxied instanceof Advised;
assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testExistingProxyChangesTarget.
@Test
public void testExistingProxyChangesTarget() throws Throwable {
TestBean tb1 = new TestBean();
tb1.setAge(33);
TestBean tb2 = new TestBean();
tb2.setAge(26);
tb2.setName("Juergen");
TestBean tb3 = new TestBean();
tb3.setAge(37);
ProxyFactory pc = new ProxyFactory(tb1);
NopInterceptor nop = new NopInterceptor();
pc.addAdvice(nop);
ITestBean proxy = (ITestBean) createProxy(pc);
assertThat(0).isEqualTo(nop.getCount());
assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
assertThat(1).isEqualTo(nop.getCount());
// Change to a new static target
pc.setTarget(tb2);
assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
assertThat(2).isEqualTo(nop.getCount());
// Change to a new dynamic target
HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
pc.setTargetSource(hts);
assertThat(proxy.getAge()).isEqualTo(tb3.getAge());
assertThat(3).isEqualTo(nop.getCount());
hts.swap(tb1);
assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
tb1.setName("Colin");
assertThat(proxy.getName()).isEqualTo(tb1.getName());
assertThat(5).isEqualTo(nop.getCount());
// Change back, relying on casting to Advised
Advised advised = (Advised) proxy;
assertThat(advised.getTargetSource()).isSameAs(hts);
SingletonTargetSource sts = new SingletonTargetSource(tb2);
advised.setTargetSource(sts);
assertThat(proxy.getName()).isEqualTo(tb2.getName());
assertThat(advised.getTargetSource()).isSameAs(sts);
assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testAdviceSupportListeners.
@Test
public void testAdviceSupportListeners() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
CountingAdvisorListener l = new CountingAdvisorListener(pc);
pc.addListener(l);
RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
// Should be automatically added as a listener
pc.addListener(acf);
assertThat(pc.isActive()).isFalse();
assertThat(l.activates).isEqualTo(0);
assertThat(acf.refreshes).isEqualTo(0);
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(acf.refreshes).isEqualTo(1);
assertThat(l.activates).isEqualTo(1);
assertThat(pc.isActive()).isTrue();
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(l.adviceChanges).isEqualTo(0);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
assertThat(l.adviceChanges).isEqualTo(1);
assertThat(acf.refreshes).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.removeAdvice(di);
assertThat(l.adviceChanges).isEqualTo(2);
assertThat(acf.refreshes).isEqualTo(3);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.getProxy();
assertThat(l.activates).isEqualTo(1);
pc.removeListener(l);
assertThat(l.adviceChanges).isEqualTo(2);
pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
// No longer counting
assertThat(l.adviceChanges).isEqualTo(2);
}
Aggregations