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.

ErrorReporterTest.java 5.6KB

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