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.

SentryErrorReporterTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.util.ClientInfo;
  24. import java.time.LocalDateTime;
  25. import java.time.ZoneOffset;
  26. import java.util.Optional;
  27. import org.junit.Before;
  28. import org.junit.BeforeClass;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.ArgumentCaptor;
  32. import org.mockito.Captor;
  33. import org.mockito.Matchers;
  34. import org.mockito.Mock;
  35. import org.mockito.runners.MockitoJUnitRunner;
  36. import net.kencochrane.raven.Raven;
  37. import net.kencochrane.raven.RavenFactory;
  38. import net.kencochrane.raven.dsn.Dsn;
  39. import net.kencochrane.raven.event.Event;
  40. import net.kencochrane.raven.event.interfaces.ExceptionInterface;
  41. import net.kencochrane.raven.event.interfaces.SentryInterface;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertNotNull;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.verify;
  47. import static org.mockito.Mockito.when;
  48. @RunWith(MockitoJUnitRunner.class)
  49. public class SentryErrorReporterTest {
  50. private static RavenFactory ravenFactory;
  51. @Mock private Raven raven;
  52. @Mock private ClientInfo clientInfo;
  53. @Captor private ArgumentCaptor<Event> eventCaptor;
  54. private Optional<Throwable> throwable;
  55. private SentryErrorReporter sentryErrorReporter;
  56. @BeforeClass
  57. public static void setUpClass() {
  58. // Static calls are horrible.
  59. ravenFactory = mock(RavenFactory.class);
  60. RavenFactory.registerFactory(ravenFactory);
  61. }
  62. @Before
  63. public void setUp() {
  64. when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
  65. throwable = Optional.of(new IllegalArgumentException());
  66. sentryErrorReporter = new SentryErrorReporter(clientInfo);
  67. }
  68. @Test
  69. public void testSendsMessage() {
  70. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
  71. throwable);
  72. verify(raven).sendEvent(eventCaptor.capture());
  73. assertEquals("message 123", eventCaptor.getValue().getMessage());
  74. }
  75. @Test
  76. public void testSendsTimestamp() {
  77. final LocalDateTime date = LocalDateTime.ofEpochSecond(56185920L, 0, ZoneOffset.UTC);
  78. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, throwable);
  79. verify(raven).sendEvent(eventCaptor.capture());
  80. assertEquals(56185920000L, eventCaptor.getValue().getTimestamp().getTime());
  81. }
  82. @Test
  83. public void testSendsLowLevelAsInfo() {
  84. sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, LocalDateTime.now(),
  85. throwable);
  86. verify(raven).sendEvent(eventCaptor.capture());
  87. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  88. }
  89. @Test
  90. public void testSendsMediumLevelAsWarning() {
  91. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
  92. throwable);
  93. verify(raven).sendEvent(eventCaptor.capture());
  94. assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
  95. }
  96. @Test
  97. public void testSendsHighLevelAsError() {
  98. sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, LocalDateTime.now(),
  99. throwable);
  100. verify(raven).sendEvent(eventCaptor.capture());
  101. assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
  102. }
  103. @Test
  104. public void testSendsFatalLevelAsFatal() {
  105. sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, LocalDateTime.now(),
  106. throwable);
  107. verify(raven).sendEvent(eventCaptor.capture());
  108. assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
  109. }
  110. @Test
  111. public void testSendsUnknownLevelAsInfo() {
  112. sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
  113. throwable);
  114. verify(raven).sendEvent(eventCaptor.capture());
  115. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  116. }
  117. @Test
  118. public void testAddsExceptionInterface() {
  119. final Exception exception = new IndexOutOfBoundsException("Message blah");
  120. sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
  121. Optional.of(exception));
  122. verify(raven).sendEvent(eventCaptor.capture());
  123. final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
  124. .get(ExceptionInterface.EXCEPTION_INTERFACE);
  125. assertNotNull(iface);
  126. assertTrue(iface instanceof ExceptionInterface);
  127. assertEquals(exception.getMessage(),
  128. ((ExceptionInterface) iface).getExceptions().getFirst().getExceptionMessage());
  129. }
  130. }