Search in sources :

Example 1 with StaticMethodMatcherPointcutAdvisor

use of org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor 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 2 with StaticMethodMatcherPointcutAdvisor

use of org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCloneInvocationToProceedThreeTimes.

/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    pc.addInterface(ITestBean.class);
    MethodInterceptor twoBirthdayInterceptor = mi -> {
        // Clone the invocation to proceed three times
        // "The Moor's Last Sigh": this technology can cause premature aging
        MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
        MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
        clone1.proceed();
        clone2.proceed();
        return mi.proceed();
    };
    @SuppressWarnings("serial") StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return "haveBirthday".equals(m.getName());
        }
    };
    pc.addAdvisor(advisor);
    ITestBean it = (ITestBean) createProxy(pc);
    final int age = 20;
    it.setAge(age);
    assertThat(it.getAge()).isEqualTo(age);
    // Should return the age before the third, AOP-induced birthday
    assertThat(it.haveBirthday()).isEqualTo((age + 2));
    // Return the final age produced by 3 birthdays
    assertThat(it.getAge()).isEqualTo((age + 3));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Nullable(org.springframework.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 3 with StaticMethodMatcherPointcutAdvisor

use of org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testMultiAdvice.

@Test
public void testMultiAdvice() throws Throwable {
    CountingMultiAdvice cca = new CountingMultiAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cca.getCalls()).isEqualTo(0);
    assertThat(cca.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cca.getCalls()).isEqualTo(2);
    assertThat(cca.getCalls("getAge")).isEqualTo(2);
    assertThat(cca.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cca.getCalls()).isEqualTo(2);
    assertThat(proxied.getAge()).isEqualTo(26);
    assertThat(cca.getCalls()).isEqualTo(4);
    assertThatExceptionOfType(SpecializedUncheckedException.class).as("Should have thrown CannotGetJdbcConnectionException").isThrownBy(() -> proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException) null)));
    assertThat(cca.getCalls()).isEqualTo(6);
}
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) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) 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) Nullable(org.springframework.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 4 with StaticMethodMatcherPointcutAdvisor

use of org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.

@Test
public void testBeforeAdvisorIsInvoked() {
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getParameterCount() == 0;
        }
    };
    TestBean target = new TestBean();
    target.setAge(80);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesNoArgs);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(cba.getCalls("getAge")).isEqualTo(0);
    assertThat(proxied.getAge()).isEqualTo(target.getAge());
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(cba.getCalls("getAge")).isEqualTo(1);
    assertThat(cba.getCalls("setAge")).isEqualTo(0);
    // Won't be advised
    proxied.setAge(26);
    assertThat(cba.getCalls()).isEqualTo(1);
    assertThat(proxied.getAge()).isEqualTo(26);
}
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) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) 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) Nullable(org.springframework.lang.Nullable) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 5 with StaticMethodMatcherPointcutAdvisor

use of org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor 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, @Nullable 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, @Nullable Class<?> targetClass) {
            return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
        }
    });
    IOverloads proxy = (IOverloads) createProxy(pc);
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(0);
    proxy.overload();
    assertThat(overLoadInts.getCount()).isEqualTo(0);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    assertThat(proxy.overload(25)).isEqualTo(25);
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
    proxy.noAdvice();
    assertThat(overLoadInts.getCount()).isEqualTo(1);
    assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

Method (java.lang.reflect.Method)6 Test (org.junit.jupiter.api.Test)6 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)6 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)6 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)6 Advisor (org.springframework.aop.Advisor)5 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)5 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)5 Nullable (org.springframework.lang.Nullable)5 LockMixinAdvisor (test.mixin.LockMixinAdvisor)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)4 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 MarshalException (java.rmi.MarshalException)2 SQLException (java.sql.SQLException)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 AfterReturningAdvice (org.springframework.aop.AfterReturningAdvice)2