Search in sources :

Example 1 with EzyHandleExceptionImpl

use of com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl in project ezyfox-server by youngmonkeys.

the class EzyHandleExceptionImplTest method handleExceptionWithHandlers.

@Test
public void handleExceptionWithHandlers() {
    // given
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyHandleExceptionImpl sut = new EzyHandleExceptionImpl(fetcher);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(handlers, times(1)).isEmpty();
    verify(handlers, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(0)).info("there is no handler for exception: ", exception);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) Logger(org.slf4j.Logger) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 2 with EzyHandleExceptionImpl

use of com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl in project ezyfox-server by youngmonkeys.

the class EzyHandleExceptionImplTest method handleExceptionWithHandlersButException.

@Test
public void handleExceptionWithHandlersButException() {
    // given
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    Exception exception = new IllegalArgumentException("one");
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyHandleExceptionImpl sut = new EzyHandleExceptionImpl(fetcher);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    RuntimeException ex = new RuntimeException("just test");
    doThrow(ex).when(handlers).handleException(Thread.currentThread(), exception);
    // when
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(handlers, times(1)).isEmpty();
    verify(handlers, times(1)).handleException(Thread.currentThread(), exception);
    verify(logger, times(1)).warn("handle exception: {} error", exceptionToSimpleString(exception), ex);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) Logger(org.slf4j.Logger) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 3 with EzyHandleExceptionImpl

use of com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl in project ezyfox-server by youngmonkeys.

the class EzyAbstractContext method init.

@Override
public final void init() {
    this.commandSuppliers = defaultCommandSuppliers();
    this.handleException = new EzyHandleExceptionImpl(component);
    this.properties.put(EzyHandleException.class, handleException);
    this.properties.put(EzyAddCommand.class, new EzyAddCommandImpl(this));
    this.properties.put(EzyAddExceptionHandler.class, new EzyAddExceptionHandlerImpl(component));
    this.doInit();
}
Also used : EzyAddExceptionHandlerImpl(com.tvd12.ezyfoxserver.command.impl.EzyAddExceptionHandlerImpl) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyAddCommandImpl(com.tvd12.ezyfoxserver.command.impl.EzyAddCommandImpl)

Example 4 with EzyHandleExceptionImpl

use of com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl in project ezyfox-server by youngmonkeys.

the class EzyHandleExceptionImplTest method handleExceptionWithEmptyHandlers.

@Test
public void handleExceptionWithEmptyHandlers() {
    // given
    EzyExceptionHandlersFetcher fetcher = mock(EzyExceptionHandlersFetcher.class);
    EzyExceptionHandlers handlers = mock(EzyExceptionHandlers.class);
    when(handlers.isEmpty()).thenReturn(true);
    when(fetcher.getExceptionHandlers()).thenReturn(handlers);
    EzyHandleExceptionImpl sut = new EzyHandleExceptionImpl(fetcher);
    Logger logger = mock(Logger.class);
    FieldUtil.setFieldValue(sut, "logger", logger);
    // when
    Exception exception = new IllegalArgumentException("one");
    sut.handle(Thread.currentThread(), exception);
    // then
    verify(handlers, times(1)).isEmpty();
    verify(logger, times(1)).info("there is no handler for exception: ", exception);
}
Also used : EzyExceptionHandlers(com.tvd12.ezyfox.util.EzyExceptionHandlers) Logger(org.slf4j.Logger) EzyHandleExceptionImpl(com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl) EzyExceptionHandlersFetcher(com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Aggregations

EzyHandleExceptionImpl (com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl)4 EzyExceptionHandlers (com.tvd12.ezyfox.util.EzyExceptionHandlers)3 EzyExceptionHandlersFetcher (com.tvd12.ezyfox.util.EzyExceptionHandlersFetcher)3 BaseTest (com.tvd12.test.base.BaseTest)3 Logger (org.slf4j.Logger)3 Test (org.testng.annotations.Test)3 EzyAddCommandImpl (com.tvd12.ezyfoxserver.command.impl.EzyAddCommandImpl)1 EzyAddExceptionHandlerImpl (com.tvd12.ezyfoxserver.command.impl.EzyAddExceptionHandlerImpl)1