Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ErrorReporterTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 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. @Captor private ArgumentCaptor<Event> eventCaptor;
  51. private ErrorReporter errorReporter;
  52. @BeforeClass
  53. public static void setUpClass() {
  54. // Static calls are horrible.
  55. ravenFactory = mock(RavenFactory.class);
  56. RavenFactory.registerFactory(ravenFactory);
  57. }
  58. @Before
  59. public void setUp() {
  60. when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
  61. errorReporter = new ErrorReporter(new ClientInfo());
  62. }
  63. @Test
  64. public void testSendsMessage() {
  65. errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
  66. verify(raven).sendEvent(eventCaptor.capture());
  67. assertEquals("message 123", eventCaptor.getValue().getMessage());
  68. }
  69. @Test
  70. public void testSendsTimestamp() {
  71. final Date date = new Date(561859200000l);
  72. errorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, null, null);
  73. verify(raven).sendEvent(eventCaptor.capture());
  74. assertEquals(date, eventCaptor.getValue().getTimestamp());
  75. }
  76. @Test
  77. public void testSendsLowLevelAsInfo() {
  78. errorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), null, null);
  79. verify(raven).sendEvent(eventCaptor.capture());
  80. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  81. }
  82. @Test
  83. public void testSendsMediumLevelAsWarning() {
  84. errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null);
  85. verify(raven).sendEvent(eventCaptor.capture());
  86. assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
  87. }
  88. @Test
  89. public void testSendsHighLevelAsError() {
  90. errorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), null, null);
  91. verify(raven).sendEvent(eventCaptor.capture());
  92. assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
  93. }
  94. @Test
  95. public void testSendsFatalLevelAsFatal() {
  96. errorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), null, null);
  97. verify(raven).sendEvent(eventCaptor.capture());
  98. assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
  99. }
  100. @Test
  101. public void testSendsUnknownLevelAsInfo() {
  102. errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, null);
  103. verify(raven).sendEvent(eventCaptor.capture());
  104. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  105. }
  106. @Test
  107. public void testAddsDetailsAsExtra() {
  108. errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null,
  109. "details 456");
  110. verify(raven).sendEvent(eventCaptor.capture());
  111. assertEquals("details 456", eventCaptor.getValue().getExtra().get("details"));
  112. }
  113. @Test
  114. public void testAddsExceptionInterface() {
  115. final Exception exception = new IndexOutOfBoundsException("Message blah");
  116. errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), exception, null);
  117. verify(raven).sendEvent(eventCaptor.capture());
  118. final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
  119. .get(ExceptionInterface.EXCEPTION_INTERFACE);
  120. assertNotNull(iface);
  121. assertTrue(iface instanceof ExceptionInterface);
  122. assertEquals(exception.getMessage(),
  123. ((ExceptionInterface) iface).getExceptions().getFirst().getExceptionMessage());
  124. }
  125. }