use of org.springframework.tests.aop.advice.CountingAfterReturningAdvice 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.CountingAfterReturningAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAfterReturningAdvisorIsNotInvokedOnException.
@Test
public void testAfterReturningAdvisorIsNotInvokedOnException() {
CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(car);
assertEquals("Advice was wrapped in Advisor and added", car, pf.getAdvisors()[1].getAdvice());
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, car.getCalls());
int age = 10;
proxied.setAge(age);
assertEquals(age, proxied.getAge());
assertEquals(2, car.getCalls());
Exception exc = new Exception();
// On exception it won't be invoked
try {
proxied.exceptional(exc);
fail();
} catch (Throwable t) {
assertSame(exc, t);
}
assertEquals(2, car.getCalls());
}
Aggregations