use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure 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.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testBeforeAdviceThrowsException.
@Test
public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {
@Override
public void before(MethodInvocation invocation) throws Throwable {
super.before(invocation);
if (invocation.getMethod().getName().startsWith("set")) {
throw rex;
}
}
};
TestBean target = new TestBean();
target.setAge(80);
NopInterceptor nop1 = new NopInterceptor();
NopInterceptor nop2 = new NopInterceptor();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(nop1);
pf.addAdvice(ba);
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) createProxy(pf);
// Won't throw an exception
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(ba.getCalls()).isEqualTo(1);
assertThat(ba.getCalls("getAge")).isEqualTo(1);
assertThat(nop1.getCount()).isEqualTo(1);
assertThat(nop2.getCount()).isEqualTo(1);
// Will fail, after invoking Nop1
assertThatExceptionOfType(RuntimeException.class).as("before advice should have ended chain").isThrownBy(() -> proxied.setAge(26)).matches(rex::equals);
assertThat(ba.getCalls()).isEqualTo(2);
assertThat(nop1.getCount()).isEqualTo(2);
// Nop2 didn't get invoked when the exception was thrown
assertThat(nop2.getCount()).isEqualTo(1);
// Shouldn't have changed value in joinpoint
assertThat(proxied.getAge()).isEqualTo(target.getAge());
}
use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testSerializableTargetAndAdvice.
@Test
public void testSerializableTargetAndAdvice() throws Throwable {
SerializablePerson personTarget = new SerializablePerson();
personTarget.setName("jim");
personTarget.setAge(26);
assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
ProxyFactory pf = new ProxyFactory(personTarget);
CountingThrowsAdvice cta = new CountingThrowsAdvice();
pf.addAdvice(new SerializableNopInterceptor());
// Try various advice types
pf.addAdvice(new CountingBeforeAdvice());
pf.addAdvice(new CountingAfterReturningAdvice());
pf.addAdvice(cta);
Person p = (Person) createAopProxy(pf).getProxy();
p.echo(null);
assertThat(cta.getCalls()).isEqualTo(0);
try {
p.echo(new IOException());
} catch (IOException ex) {
/* expected */
}
assertThat(cta.getCalls()).isEqualTo(1);
// Will throw exception if it fails
Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p2).isNotSameAs(p);
assertThat(p2.getName()).isEqualTo(p.getName());
assertThat(p2.getAge()).isEqualTo(p.getAge());
assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
Advised a1 = (Advised) p;
Advised a2 = (Advised) p2;
// Check we can manipulate state of p2
assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
// This should work as SerializablePerson is equal
assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
// Check we can add a new advisor to the target
NopInterceptor ni = new NopInterceptor();
p2.getAge();
assertThat(ni.getCount()).isEqualTo(0);
a2.addAdvice(ni);
p2.getAge();
assertThat(ni.getCount()).isEqualTo(1);
cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
p2.echo(null);
assertThat(cta.getCalls()).isEqualTo(1);
try {
p2.echo(new IOException());
} catch (IOException ex) {
}
assertThat(cta.getCalls()).isEqualTo(2);
}
use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.
the class CountingAspectJAdvice method testAdviceInvokedCorrectly.
@Test
public void testAdviceInvokedCorrectly() throws Exception {
CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");
ITestBean bean = getTestBean();
assertThat(getAgeCounter.getCalls("getAge")).as("Incorrect initial getAge count").isEqualTo(0);
assertThat(getNameCounter.getCalls("getName")).as("Incorrect initial getName count").isEqualTo(0);
bean.getAge();
assertThat(getAgeCounter.getCalls("getAge")).as("Incorrect getAge count on getAge counter").isEqualTo(1);
assertThat(getNameCounter.getCalls("getAge")).as("Incorrect getAge count on getName counter").isEqualTo(0);
bean.getName();
assertThat(getNameCounter.getCalls("getName")).as("Incorrect getName count on getName counter").isEqualTo(1);
assertThat(getAgeCounter.getCalls("getName")).as("Incorrect getName count on getAge counter").isEqualTo(0);
}
use of cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testRemoveAdvisorByReference.
@Test
public void testRemoveAdvisorByReference() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(pf.removeAdvisor(advisor)).isTrue();
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
Aggregations