Search in sources :

Example 6 with MyThrowsHandler

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

the class ThrowsAdviceInterceptorTests method testCorrectHandlerUsed.

@Test
public void testCorrectHandlerUsed() throws Throwable {
    MyThrowsHandler th = new MyThrowsHandler();
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    FileNotFoundException ex = new FileNotFoundException();
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode"));
    given(mi.getThis()).willReturn(new Object());
    given(mi.proceed()).willThrow(ex);
    assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
    assertThat(th.getCalls()).isEqualTo(1);
    assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Also used : MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) FileNotFoundException(java.io.FileNotFoundException) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 7 with MyThrowsHandler

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

the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.

@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvice(th);
    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);
    // Subclass of RemoteException
    MarshalException mex = new MarshalException("");
    assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex)).matches(mex::equals);
    assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) MarshalException(java.rmi.MarshalException) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) 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) Test(org.junit.jupiter.api.Test)

Example 8 with MyThrowsHandler

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

the class ProxyFactoryBeanTests method testCanAddThrowsAdviceWithoutAdvisor.

@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
    DefaultListableBeanFactory f = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
    MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
    CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(th.getCalls()).isEqualTo(0);
    IEcho echo = (IEcho) f.getBean("throwsAdvised");
    int i = 12;
    echo.setA(i);
    assertThat(echo.getA()).isEqualTo(i);
    assertThat(cba.getCalls()).isEqualTo(2);
    assertThat(th.getCalls()).isEqualTo(0);
    Exception expected = new Exception();
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echoException(1, expected)).matches(expected::equals);
    // No throws handler method: count should still be 0
    assertThat(th.getCalls()).isEqualTo(0);
    // Handler knows how to handle this exception
    FileNotFoundException expectedFileNotFound = new FileNotFoundException();
    assertThatIOException().isThrownBy(() -> echo.echoException(1, expectedFileNotFound)).matches(expectedFileNotFound::equals);
    // One match
    assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FileNotFoundException(java.io.FileNotFoundException) ClassPathResource(org.springframework.core.io.ClassPathResource) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) 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