Search in sources :

Example 1 with MyThrowsHandler

use of cn.taketoday.aop.testfixture.advice.MyThrowsHandler in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) MarshalException(java.rmi.MarshalException) MyThrowsHandler(cn.taketoday.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 2 with MyThrowsHandler

use of cn.taketoday.aop.testfixture.advice.MyThrowsHandler in project today-infrastructure by TAKETODAY.

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(cn.taketoday.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 3 with MyThrowsHandler

use of cn.taketoday.aop.testfixture.advice.MyThrowsHandler in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 4 with MyThrowsHandler

use of cn.taketoday.aop.testfixture.advice.MyThrowsHandler in project today-framework by TAKETODAY.

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(cn.taketoday.aop.testfixture.advice.MyThrowsHandler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 5 with MyThrowsHandler

use of cn.taketoday.aop.testfixture.advice.MyThrowsHandler in project today-framework by TAKETODAY.

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(cn.taketoday.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)

Aggregations

MyThrowsHandler (cn.taketoday.aop.testfixture.advice.MyThrowsHandler)16 Test (org.junit.jupiter.api.Test)16 FileNotFoundException (java.io.FileNotFoundException)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)10 ConnectException (java.rmi.ConnectException)6 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)6 LockedException (test.mixin.LockedException)6 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)4 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)4 IOException (java.io.IOException)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 MarshalException (java.rmi.MarshalException)4 RemoteException (java.rmi.RemoteException)4 SQLException (java.sql.SQLException)4 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)4 Advisor (cn.taketoday.aop.Advisor)2 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)2 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)2 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)2 CountingBeforeAdvice (cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice)2