use of cn.taketoday.aop.framework.Advised in project today-framework 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 ignored) {
}
assertThat(cta.getCalls()).isEqualTo(2);
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
use of cn.taketoday.aop.framework.Advised in project today-framework 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.framework.Advised in project today-framework by TAKETODAY.
the class ProxyFactoryTests method testReplaceAdvisor.
@Test
public void testReplaceAdvisor() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
pf.addAdvisor(advisor1);
pf.addAdvice(nop);
ITestBean proxied = (ITestBean) pf.getProxy();
// Use the type cast feature
// Replace etc methods on advised should be same as on ProxyFactory
Advised advised = (Advised) proxied;
proxied.setAge(5);
assertThat(cba1.getCalls()).isEqualTo(1);
assertThat(cba2.getCalls()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)).isFalse();
assertThat(advised.replaceAdvisor(advisor1, advisor2)).isTrue();
assertThat(pf.getAdvisors()[0]).isEqualTo(advisor2);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba1.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(cba2.getCalls()).isEqualTo(1);
assertThat(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)).isFalse();
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class SimpleBeforeAdviceInterceptor method getAdviceImpl.
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
Advised advised = (Advised) tb;
Advisor advisor = advised.getAdvisors()[0];
return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
Aggregations