Search in sources :

Example 1 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
    assertThat(pf.indexOf(nop)).isEqualTo(0);
    assertThat(pf.indexOf(advisor)).isEqualTo(1);
    assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ProxyFactoryTests method testInterceptorInclusionMethods.

@Test
public void testInterceptorInclusionMethods() {
    class MyInterceptor implements MethodInterceptor {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw new UnsupportedOperationException();
        }
    }
    NopInterceptor di = new NopInterceptor();
    NopInterceptor diUnused = new NopInterceptor();
    ProxyFactory factory = new ProxyFactory(new TestBean());
    factory.addAdvice(0, di);
    assertThat(factory.getProxy()).isInstanceOf(ITestBean.class);
    assertThat(factory.adviceIncluded(di)).isTrue();
    assertThat(!factory.adviceIncluded(diUnused)).isTrue();
    assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 1).isTrue();
    assertThat(factory.countAdvicesOfType(MyInterceptor.class) == 0).isTrue();
    factory.addAdvice(0, diUnused);
    assertThat(factory.adviceIncluded(diUnused)).isTrue();
    assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 2).isTrue();
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 3 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ControlFlowPointcutTests method testMatches.

@Test
public void testMatches() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));
    // Not advised, not under One
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(nop.getCount()).isEqualTo(0);
    // Will be advised
    assertThat(new One().getAge(proxied)).isEqualTo(target.getAge());
    assertThat(nop.getCount()).isEqualTo(1);
    // Won't be advised
    assertThat(new One().nomatch(proxied)).isEqualTo(target.getAge());
    assertThat(nop.getCount()).isEqualTo(1);
    assertThat(cflow.getEvaluations()).isEqualTo(3);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 4 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class ControlFlowPointcutTests method testSelectiveApplication.

/**
 * Check that we can use a cflow pointcut only in conjunction with
 * a static pointcut: e.g. all setter methods that are invoked under
 * a particular class. This greatly reduces the number of calls
 * to the cflow pointcut, meaning that it's not so prohibitively
 * expensive.
 */
@Test
public void testSelectiveApplication() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
    Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
    // Not advised, not under One
    target.setAge(16);
    assertThat(nop.getCount()).isEqualTo(0);
    // Not advised; under One but not a setter
    assertThat(new One().getAge(proxied)).isEqualTo(16);
    assertThat(nop.getCount()).isEqualTo(0);
    // Won't be advised
    new One().set(proxied);
    assertThat(nop.getCount()).isEqualTo(1);
    // We saved most evaluations
    assertThat(cflow.getEvaluations()).isEqualTo(1);
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 5 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class RegexpMethodPointcutAdvisorIntegrationTests method testSinglePattern.

@Test
public void testSinglePattern() throws Throwable {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
    // Interceptor behind regexp advisor
    NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
    assertThat(nop.getCount()).isEqualTo(0);
    int newAge = 12;
    // Not advised
    advised.exceptional(null);
    assertThat(nop.getCount()).isEqualTo(0);
    advised.setAge(newAge);
    assertThat(advised.getAge()).isEqualTo(newAge);
    // Only setter fired
    assertThat(nop.getCount()).isEqualTo(1);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)60 Test (org.junit.jupiter.api.Test)57 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)39 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)33 TestBean (org.springframework.beans.testfixture.beans.TestBean)32 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)13 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)11 Advisor (org.springframework.aop.Advisor)10 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 Method (java.lang.reflect.Method)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 IOException (java.io.IOException)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 Person (org.springframework.beans.testfixture.beans.Person)4 Nullable (org.springframework.lang.Nullable)4