Search in sources :

Example 1 with MyThrowsHandler

use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class ThrowsAdviceInterceptorTests method testNoHandlerMethodForThrowable.

@Test
public void testNoHandlerMethodForThrowable() throws Throwable {
    MyThrowsHandler th = new MyThrowsHandler();
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    assertThat(ti.getHandlerMethodCount()).isEqualTo(2);
    Exception ex = new Exception();
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.proceed()).willThrow(ex);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
    assertThat(th.getCalls()).isEqualTo(0);
}
Also used : MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.rmi.ConnectException) RemoteException(java.rmi.RemoteException) Test(org.junit.jupiter.api.Test)

Example 2 with MyThrowsHandler

use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class ThrowsAdviceInterceptorTests method testHandlerMethodThrowsException.

@Test
public void testHandlerMethodThrowsException() throws Throwable {
    final Throwable t = new Throwable();
    @SuppressWarnings("serial") MyThrowsHandler th = new MyThrowsHandler() {

        @Override
        public void afterThrowing(RemoteException ex) throws Throwable {
            super.afterThrowing(ex);
            throw t;
        }
    };
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    // Extends RemoteException
    ConnectException ex = new ConnectException("");
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.proceed()).willThrow(ex);
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(t);
    assertThat(th.getCalls()).isEqualTo(1);
    assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
Also used : MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) RemoteException(java.rmi.RemoteException) ConnectException(java.rmi.ConnectException) Test(org.junit.jupiter.api.Test)

Example 3 with MyThrowsHandler

use of org.springframework.aop.testfixture.advice.MyThrowsHandler 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 4 with MyThrowsHandler

use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class ThrowsAdviceInterceptorTests method testCorrectHandlerUsedForSubclass.

@Test
public void testCorrectHandlerUsedForSubclass() throws Throwable {
    MyThrowsHandler th = new MyThrowsHandler();
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    // Extends RemoteException
    ConnectException ex = new ConnectException("");
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.proceed()).willThrow(ex);
    assertThatExceptionOfType(ConnectException.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
    assertThat(th.getCalls()).isEqualTo(1);
    assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
Also used : MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ConnectException(java.rmi.ConnectException) Test(org.junit.jupiter.api.Test)

Example 5 with MyThrowsHandler

use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class ThrowsAdviceInterceptorTests method testNotInvoked.

@Test
public void testNotInvoked() throws Throwable {
    MyThrowsHandler th = new MyThrowsHandler();
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    Object ret = new Object();
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.proceed()).willReturn(ret);
    assertThat(ti.invoke(mi)).isEqualTo(ret);
    assertThat(th.getCalls()).isEqualTo(0);
}
Also used : MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)8 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)8 FileNotFoundException (java.io.FileNotFoundException)5 MethodInvocation (org.aopalliance.intercept.MethodInvocation)5 ConnectException (java.rmi.ConnectException)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 LockedException (test.mixin.LockedException)3 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 MarshalException (java.rmi.MarshalException)2 RemoteException (java.rmi.RemoteException)2 SQLException (java.sql.SQLException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)2 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)2 Method (java.lang.reflect.Method)1 Assertions.assertThatIOException (org.assertj.core.api.Assertions.assertThatIOException)1 Advisor (org.springframework.aop.Advisor)1 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)1 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)1