use of cn.taketoday.aop.testfixture.interceptor.NopInterceptor in project today-infrastructure by TAKETODAY.
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);
assertThat(proxyFactory.isExposeProxy()).isTrue();
proxyFactory.addAdvice(0, di);
INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(proxyFactory);
assertThat(di.getCount()).isEqualTo(0);
assertThat(target.getCount()).isEqualTo(0);
proxied.incrementViaThis();
assertThat(target.getCount()).as("Increment happened").isEqualTo(1);
assertThat(di.getCount()).as("Only one invocation via AOP as use of this wasn't proxied").isEqualTo(1);
// 1 invocation
assertThat(proxied.getCount()).as("Increment happened").isEqualTo(1);
// 2 invocations
proxied.incrementViaProxy();
assertThat(target.getCount()).as("Increment happened").isEqualTo(2);
assertThat(di.getCount()).as("3 more invocations via AOP as the first call was reentrant through the proxy").isEqualTo(4);
}
use of cn.taketoday.aop.testfixture.interceptor.NopInterceptor in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.
@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(th);
IEcho proxied = (IEcho) createProxy(pf);
assertThat(th.getCalls()).isEqualTo(0);
assertThat(proxied.getA()).isEqualTo(target.getA());
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
// Subclass of RemoteException
MarshalException mex = new MarshalException("");
assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex)).matches(mex::equals);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
use of cn.taketoday.aop.testfixture.interceptor.NopInterceptor in project today-infrastructure 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.testfixture.interceptor.NopInterceptor in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.
/**
* Check that casting to Advised can't get around advice freeze.
*/
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThat(pc.isFrozen()).isFalse();
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(advised.getAdvisors().length).isEqualTo(1);
}
use of cn.taketoday.aop.testfixture.interceptor.NopInterceptor in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testOneAdvisedObjectCallsAnother.
/**
* Check that the two MethodInvocations necessary are independent and
* don't conflict.
* Check also proxy exposure.
*/
@Test
public void testOneAdvisedObjectCallsAnother() {
int age1 = 33;
int age2 = 37;
TestBean target1 = new TestBean();
ProxyFactory pf1 = new ProxyFactory(target1);
// Permit proxy and invocation checkers to get context from AopContext
pf1.setExposeProxy(true);
NopInterceptor di1 = new NopInterceptor();
pf1.addAdvice(0, di1);
pf1.addAdvice(1, new ProxyMatcherInterceptor());
pf1.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
pf1.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
// Must be first
pf1.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
ITestBean advised1 = (ITestBean) pf1.getProxy();
// = 1 invocation
advised1.setAge(age1);
TestBean target2 = new TestBean();
ProxyFactory pf2 = new ProxyFactory(target2);
pf2.setExposeProxy(true);
NopInterceptor di2 = new NopInterceptor();
pf2.addAdvice(0, di2);
pf2.addAdvice(1, new ProxyMatcherInterceptor());
pf2.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
pf2.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
pf2.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
ITestBean advised2 = (ITestBean) createProxy(pf2);
advised2.setAge(age2);
// = 2 invocations
advised1.setSpouse(advised2);
// = 3 invocations
assertThat(advised1.getAge()).as("Advised one has correct age").isEqualTo(age1);
assertThat(advised2.getAge()).as("Advised two has correct age").isEqualTo(age2);
// Means extra call on advised 2
// = 4 invocations on 1 and another one on 2
assertThat(advised1.getSpouse().getAge()).as("Advised one spouse has correct age").isEqualTo(age2);
assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(4);
// Got hit by call to advised1.getSpouse().getAge()
assertThat(di2.getCount()).as("one was invoked correct number of times").isEqualTo(3);
}
Aggregations