Search in sources :

Example 6 with NopInterceptor

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

the class RegexpMethodPointcutAdvisorIntegrationTests method testMultiplePatterns.

@Test
public void testMultiplePatterns() throws Throwable {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    // This is a CGLIB proxy, so we can proxy it to the target class
    TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
    // 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);
    // This is proxied
    advised.absquatulate();
    assertThat(nop.getCount()).isEqualTo(1);
    advised.setAge(newAge);
    assertThat(advised.getAge()).isEqualTo(newAge);
    // Only setter fired
    assertThat(nop.getCount()).isEqualTo(2);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 7 with NopInterceptor

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

the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.

@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory();
    pc.addInterface(ITestBean.class);
    TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
    pc.addAdvisor(dp);
    pc.setTarget(tb);
    ITestBean it = (ITestBean) createProxy(pc);
    assertThat(dp.count).isEqualTo(0);
    it.getAge();
    assertThat(dp.count).isEqualTo(1);
    it.setAge(11);
    assertThat(it.getAge()).isEqualTo(11);
    assertThat(dp.count).isEqualTo(2);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 8 with NopInterceptor

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

the class AbstractAopProxyTests method testThrowsAdvisorIsInvoked.

@Test
public void testThrowsAdvisorIsInvoked() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    @SuppressWarnings("serial") Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getName().startsWith("echo");
        }
    };
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesEchoInvocations);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesEchoInvocations);
    IEcho proxied = (IEcho) createProxy(pf);
    assertThat(th.getCalls()).isEqualTo(0);
    assertThat(proxied.getA()).isEqualTo(target.getA());
    assertThat(th.getCalls()).isEqualTo(0);
    Exception ex = new Exception();
    // Will be advised but doesn't match
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
    FileNotFoundException fex = new FileNotFoundException();
    assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> proxied.echoException(1, fex)).matches(fex::equals);
    assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) FileNotFoundException(java.io.FileNotFoundException) 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) Method(java.lang.reflect.Method) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) Nullable(org.springframework.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 9 with NopInterceptor

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

the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.

/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertThat(pc.isFrozen()).isFalse();
    pc.addAdvice(new NopInterceptor());
    ITestBean proxied = (ITestBean) createProxy(pc);
    pc.setFrozen(true);
    Advised advised = (Advised) proxied;
    assertThat(pc.isFrozen()).isTrue();
    assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(advised.getAdvisors().length).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) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 10 with NopInterceptor

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

the class AbstractAopProxyTests method testOneAdvisedObjectCallsAnother.

/**
 * Check that the two MethodInvocations necessary are independent and
 * don't conflict.
 * Check also proxy exposure.
 */
@Test
public void testOneAdvisedObjectCallsAnother() {
    int age1 = 33;
    int age2 = 37;
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    // Permit proxy and invocation checkers to get context from AopContext
    pf1.setExposeProxy(true);
    NopInterceptor di1 = new NopInterceptor();
    pf1.addAdvice(0, di1);
    pf1.addAdvice(1, new ProxyMatcherInterceptor());
    pf1.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
    pf1.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
    // Must be first
    pf1.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
    ITestBean advised1 = (ITestBean) pf1.getProxy();
    // = 1 invocation
    advised1.setAge(age1);
    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.setExposeProxy(true);
    NopInterceptor di2 = new NopInterceptor();
    pf2.addAdvice(0, di2);
    pf2.addAdvice(1, new ProxyMatcherInterceptor());
    pf2.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
    pf2.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
    pf2.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
    ITestBean advised2 = (ITestBean) createProxy(pf2);
    advised2.setAge(age2);
    // = 2 invocations
    advised1.setSpouse(advised2);
    // = 3 invocations
    assertThat(advised1.getAge()).as("Advised one has correct age").isEqualTo(age1);
    assertThat(advised2.getAge()).as("Advised two has correct age").isEqualTo(age2);
    // Means extra call on advised 2
    // = 4 invocations on 1 and another one on 2
    assertThat(advised1.getSpouse().getAge()).as("Advised one spouse has correct age").isEqualTo(age2);
    assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(4);
    // Got hit by call to advised1.getSpouse().getAge()
    assertThat(di2.getCount()).as("one was invoked correct number of times").isEqualTo(3);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) 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