You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SentryLoggingErrorManagerTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.logger;
  23. import com.dmdirc.config.binding.ConfigBinder;
  24. import com.dmdirc.events.ProgramErrorAddedEvent;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.config.provider.AggregateConfigProvider;
  27. import com.google.common.util.concurrent.MoreExecutors;
  28. import java.time.LocalDateTime;
  29. import java.util.Optional;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static org.mockito.ArgumentMatchers.isNull;
  36. import static org.mockito.ArgumentMatchers.any;
  37. import static org.mockito.ArgumentMatchers.anyString;
  38. import static org.mockito.ArgumentMatchers.eq;
  39. import static org.mockito.Mockito.never;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class SentryLoggingErrorManagerTest {
  44. @Mock private EventBus eventBus;
  45. @Mock private AggregateConfigProvider config;
  46. @Mock private ConfigBinder configBinder;
  47. @Mock private SentryErrorReporter sentryErrorReporter;
  48. @Mock private ProgramError appError;
  49. @Mock private ProgramErrorAddedEvent appErrorEvent;
  50. @Mock private ProgramError userError;
  51. @Mock private ProgramErrorAddedEvent userErrorEvent;
  52. private SentryLoggingErrorManager instance;
  53. @Before
  54. public void setUp() throws Exception {
  55. when(appErrorEvent.getError()).thenReturn(appError);
  56. when(appError.isAppError()).thenReturn(true);
  57. when(appError.getThrowable()).thenReturn(Optional.of(new IllegalStateException()));
  58. when(userErrorEvent.getError()).thenReturn(userError);
  59. when(userError.isAppError()).thenReturn(false);
  60. when(userError.getThrowable()).thenReturn(Optional.of(new IllegalStateException()));
  61. when(config.getBinder()).thenReturn(configBinder);
  62. instance = new SentryLoggingErrorManager(eventBus, sentryErrorReporter,
  63. MoreExecutors.newDirectExecutorService());
  64. instance.initialise(config);
  65. instance.handleSubmitErrors(true);
  66. instance.handleNoErrorReporting(false);
  67. }
  68. @Test
  69. public void testHandleErrorEvent() throws Exception {
  70. instance.handleErrorEvent(appErrorEvent);
  71. verify(sentryErrorReporter).sendException(isNull(), isNull(), isNull(), any());
  72. }
  73. @Test
  74. public void testHandledErrorEvent_UserError() throws Exception {
  75. instance.handleErrorEvent(userErrorEvent);
  76. verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
  77. any(LocalDateTime.class), any());
  78. }
  79. @Test
  80. public void testHandledErrorEvent_BannedException() throws Exception {
  81. final Throwable throwable = new OutOfMemoryError();
  82. when(appError.getThrowable()).thenReturn(Optional.of(throwable));
  83. instance.handleErrorEvent(appErrorEvent);
  84. verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
  85. any(LocalDateTime.class), eq(Optional.of(throwable)));
  86. }
  87. @Test
  88. public void testSendReports_NoSubmit_NoReporting() throws Exception {
  89. instance.handleSubmitErrors(false);
  90. instance.handleNoErrorReporting(false);
  91. instance.handleErrorEvent(appErrorEvent);
  92. verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
  93. any(LocalDateTime.class), any());
  94. }
  95. @Test
  96. public void testSendReports_Submit_NoError() throws Exception {
  97. instance.handleSubmitErrors(true);
  98. instance.handleNoErrorReporting(true);
  99. instance.handleErrorEvent(appErrorEvent);
  100. verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
  101. any(LocalDateTime.class), any());
  102. }
  103. @Test
  104. public void testSendReports_Submit_NoReporting() throws Exception {
  105. instance.handleSubmitErrors(true);
  106. instance.handleNoErrorReporting(false);
  107. instance.handleErrorEvent(appErrorEvent);
  108. verify(sentryErrorReporter).sendException(isNull(), isNull(), isNull(), any());
  109. }
  110. @Test
  111. public void testSendReports_Submit_Error() throws Exception {
  112. instance.handleSubmitErrors(false);
  113. instance.handleNoErrorReporting(true);
  114. instance.handleErrorEvent(appErrorEvent);
  115. verify(sentryErrorReporter, never()).sendException(anyString(), any(ErrorLevel.class),
  116. any(LocalDateTime.class), any());
  117. }
  118. }