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.5KB

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