Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SentryErrorReporterTest.java 5.5KB

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