Search in sources :

Example 11 with CountingBeforeAdvice

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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) Test(org.junit.Test)

Example 12 with CountingBeforeAdvice

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"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) Test(org.junit.Test)

Example 13 with CountingBeforeAdvice

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"));
}
Also used : CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 14 with CountingBeforeAdvice

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());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 15 with CountingBeforeAdvice

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());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice)

Aggregations

CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)17 Test (org.junit.Test)16 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)11 TestBean (org.springframework.tests.sample.beans.TestBean)11 Advisor (org.springframework.aop.Advisor)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)7 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)6 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)3 LockMixinAdvisor (test.mixin.LockMixinAdvisor)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 NameMatchMethodPointcut (org.springframework.aop.support.NameMatchMethodPointcut)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileNotFoundException (java.io.FileNotFoundException)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1