use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class CreatesTestBean method testJdkIntroduction.
@Test
public void testJdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertEquals(0, nop.getCount());
assertTrue(AopUtils.isJdkDynamicProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertTrue("Introduction was made", tb instanceof TimeStamped);
assertEquals(0, ((TimeStamped) tb).getTimeStamp());
assertEquals(3, nop.getCount());
assertEquals("introductionUsingJdk", tb.getName());
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertFalse(lockable1.locked());
assertFalse(lockable2.locked());
tb.setAge(65);
assertEquals(65, tb.getAge());
lockable1.lock();
assertTrue(lockable1.locked());
// Shouldn't affect second
assertFalse(lockable2.locked());
// Can still mod second object
tb2.setAge(12);
// But can't mod first
try {
tb.setAge(6);
fail("Mixin should have locked this object");
} catch (LockedException ex) {
// Ok
}
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testMultiAdvice.
@Test
public void testMultiAdvice() throws Throwable {
CountingMultiAdvice cca = new CountingMultiAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cca.getCalls());
assertEquals(0, cca.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(2, cca.getCalls());
assertEquals(2, cca.getCalls("getAge"));
assertEquals(0, cca.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(2, cca.getCalls());
assertEquals(26, proxied.getAge());
assertEquals(4, cca.getCalls());
try {
proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null));
fail("Should have thrown CannotGetJdbcConnectionException");
} catch (SpecializedUncheckedException ex) {
// expected
}
assertEquals(6, cca.getCalls());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor 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.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertFalse(pc.isFrozen());
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
try {
pc.addAdvice(0, new NopInterceptor());
fail("Shouldn't be able to add interceptor when frozen");
} catch (AopConfigException ex) {
assertTrue(ex.getMessage().indexOf("frozen") > -1);
}
// Check it still works: proxy factory state shouldn't have been corrupted
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, ((Advised) proxied).getAdvisors().length);
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.
@Test
public void testSerializationAdviceNotSerializable() throws Exception {
SerializablePerson sp = new SerializablePerson();
assertTrue(SerializationTestUtils.isSerializable(sp));
ProxyFactory pf = new ProxyFactory(sp);
// This isn't serializable
Advice i = new NopInterceptor();
pf.addAdvice(i);
assertFalse(SerializationTestUtils.isSerializable(i));
Object proxy = createAopProxy(pf).getProxy();
assertFalse(SerializationTestUtils.isSerializable(proxy));
}
Aggregations