use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializationSerializableTargetAndAdvice.
@Test
public void testSerializationSerializableTargetAndAdvice() throws Throwable {
SerializablePerson personTarget = new SerializablePerson();
personTarget.setName("jim");
personTarget.setAge(26);
assertTrue(SerializationTestUtils.isSerializable(personTarget));
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);
assertEquals(0, cta.getCalls());
try {
p.echo(new IOException());
} catch (IOException ex) {
/* expected */
}
assertEquals(1, cta.getCalls());
// Will throw exception if it fails
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
assertNotSame(p, p2);
assertEquals(p.getName(), p2.getName());
assertEquals(p.getAge(), p2.getAge());
assertTrue("Deserialized object is an AOP proxy", AopUtils.isAopProxy(p2));
Advised a1 = (Advised) p;
Advised a2 = (Advised) p2;
// Check we can manipulate state of p2
assertEquals(a1.getAdvisors().length, a2.getAdvisors().length);
// This should work as SerializablePerson is equal
assertEquals("Proxies should be equal, even after one was serialized", p, p2);
assertEquals("Proxies should be equal, even after one was serialized", p2, p);
// Check we can add a new advisor to the target
NopInterceptor ni = new NopInterceptor();
p2.getAge();
assertEquals(0, ni.getCount());
a2.addAdvice(ni);
p2.getAge();
assertEquals(1, ni.getCount());
cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
p2.echo(null);
assertEquals(1, cta.getCalls());
try {
p2.echo(new IOException());
} catch (IOException ex) {
}
assertEquals(2, cta.getCalls());
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testBeforeAdviceThrowsException.
@Test
public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial") CountingBeforeAdvice ba = new CountingBeforeAdvice() {
@Override
public void before(Method m, Object[] args, Object target) throws Throwable {
super.before(m, args, target);
if (m.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
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, ba.getCalls());
assertEquals(1, ba.getCalls("getAge"));
assertEquals(1, nop1.getCount());
assertEquals(1, nop2.getCount());
// Will fail, after invoking Nop1
try {
proxied.setAge(26);
fail("before advice should have ended chain");
} catch (RuntimeException ex) {
assertEquals(rex, ex);
}
assertEquals(2, ba.getCalls());
assertEquals(2, nop1.getCount());
// Nop2 didn't get invoked when the exception was thrown
assertEquals(1, nop2.getCount());
// Shouldn't have changed value in joinpoint
assertEquals(target.getAge(), proxied.getAge());
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryTests method testIndexOfMethods.
@Test
public void testIndexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
Advised advised = (Advised) pf.getProxy();
// Can use advised and ProxyFactory interchangeably
advised.addAdvice(nop);
pf.addAdvisor(advisor);
assertEquals(-1, pf.indexOf(new NopInterceptor()));
assertEquals(0, pf.indexOf(nop));
assertEquals(1, pf.indexOf(advisor));
assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
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);
assertEquals(0, di.getCount());
t.setAge(23);
assertEquals(23, t.getAge());
assertEquals(2, di.getCount());
Advised advised = (Advised) t;
assertEquals("Have 1 advisor", 1, advised.getAdvisors().length);
assertEquals(di, advised.getAdvisors()[0].getAdvice());
NopInterceptor di2 = new NopInterceptor();
advised.addAdvice(1, di2);
t.getName();
assertEquals(3, di.getCount());
assertEquals(1, di2.getCount());
// will remove di
advised.removeAdvisor(0);
t.getAge();
// Unchanged
assertEquals(3, di.getCount());
assertEquals(2, di2.getCount());
CountingBeforeAdvice cba = new CountingBeforeAdvice();
assertEquals(0, cba.getCalls());
advised.addAdvice(cba);
t.setAge(16);
assertEquals(16, t.getAge());
assertEquals(2, cba.getCalls());
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testAddAdviceAtRuntime.
@Test
public void testAddAdviceAtRuntime() {
TestBean bean = new TestBean();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.setTarget(bean);
pf.setFrozen(false);
pf.setOpaque(false);
pf.setProxyTargetClass(true);
TestBean proxy = (TestBean) pf.getProxy();
assertTrue(AopUtils.isCglibProxy(proxy));
proxy.getAge();
assertEquals(0, cba.getCalls());
((Advised) proxy).addAdvice(cba);
proxy.getAge();
assertEquals(1, cba.getCalls());
}
Aggregations