use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class CreatesTestBean method cglibAssertions.
/**
* Also has counting before advice.
*/
private void cglibAssertions(TestBean tb) {
CountingBeforeAdvice cba = (CountingBeforeAdvice) beanFactory.getBean("countingBeforeAdvice");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
assertEquals(0, cba.getCalls());
assertEquals(0, nop.getCount());
assertTrue(AopUtils.isCglibProxy(tb));
int age = 5;
tb.setAge(age);
assertEquals(age, tb.getAge());
assertEquals(2, nop.getCount());
assertEquals(2, cba.getCalls());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializationAdviceAndTargetNotSerializable.
@Test
public void testSerializationAdviceAndTargetNotSerializable() throws Exception {
TestBean tb = new TestBean();
assertFalse(SerializationTestUtils.isSerializable(tb));
ProxyFactory pf = new ProxyFactory(tb);
pf.addAdvice(new NopInterceptor());
ITestBean proxy = (ITestBean) createAopProxy(pf).getProxy();
assertFalse(SerializationTestUtils.isSerializable(proxy));
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.
@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 0;
}
});
NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
}
});
IOverloads proxy = (IOverloads) createProxy(pc);
assertEquals(0, overLoadInts.getCount());
assertEquals(0, overLoadVoids.getCount());
proxy.overload();
assertEquals(0, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
assertEquals(25, proxy.overload(25));
assertEquals(1, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
proxy.noAdvice();
assertEquals(1, overLoadInts.getCount());
assertEquals(1, overLoadVoids.getCount());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testManyProxies.
private void testManyProxies(int howMany) {
int age1 = 33;
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvice(new NopInterceptor());
pf1.addAdvice(new NopInterceptor());
ITestBean[] proxies = new ITestBean[howMany];
for (int i = 0; i < howMany; i++) {
proxies[i] = (ITestBean) createAopProxy(pf1).getProxy();
assertEquals(age1, proxies[i].getAge());
}
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAfterReturningAdvisorIsInvoked.
@Test
public void testAfterReturningAdvisorIsInvoked() {
class SummingAfterAdvice implements AfterReturningAdvice {
public int sum;
@Override
public void afterReturning(Object returnValue, Method m, Object[] args, Object target) throws Throwable {
sum += ((Integer) returnValue).intValue();
}
}
SummingAfterAdvice aa = new SummingAfterAdvice();
@SuppressWarnings("serial") Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getReturnType() == int.class;
}
};
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesInt);
assertEquals("Advisor was added", matchesInt, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, aa.sum);
int i1 = 12;
int i2 = 13;
// Won't be advised
proxied.setAge(i1);
assertEquals(i1, proxied.getAge());
assertEquals(i1, aa.sum);
proxied.setAge(i2);
assertEquals(i2, proxied.getAge());
assertEquals(i1 + i2, aa.sum);
assertEquals(i2, proxied.getAge());
}
Aggregations