use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
pc.addAdvisor(advisor);
assertFalse("Opaque defaults to false", pc.isOpaque());
pc.setOpaque(true);
assertTrue("Opaque now true for this config", pc.isOpaque());
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertEquals(10, proxied.getAge());
assertEquals(1, mba.getCalls());
assertFalse("Cannot be cast to Advised", proxied instanceof Advised);
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
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();
assertTrue(proxyConfigString.contains(advisor.toString()));
assertTrue(proxyConfigString.contains("1 interface"));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testProxyProtectedMethod.
@Test
public void testProxyProtectedMethod() throws Exception {
CountingBeforeAdvice advice = new CountingBeforeAdvice();
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
proxyFactory.addAdvice(advice);
proxyFactory.setProxyTargetClass(true);
MyBean proxy = (MyBean) proxyFactory.getProxy();
assertEquals(4, proxy.add(1, 3));
assertEquals(1, advice.getCalls("add"));
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class SelectivePrototypeTargetSourceCreator method testWithOptimizedProxy.
@Test
public void testWithOptimizedProxy() throws Exception {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);
ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
assertTrue(AopUtils.isAopProxy(testBean));
CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice");
testBean.setAge(23);
testBean.getAge();
assertEquals("Incorrect number of calls to proxy", 2, beforeAdvice.getCalls());
}
use of org.springframework.tests.aop.advice.CountingBeforeAdvice 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());
}
Aggregations