123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.logger;
-
- import com.dmdirc.util.ClientInfo;
-
- import java.time.LocalDateTime;
- import java.time.ZoneOffset;
- import java.util.Optional;
-
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.ArgumentCaptor;
- import org.mockito.Captor;
- import org.mockito.Matchers;
- import org.mockito.Mock;
- import org.mockito.runners.MockitoJUnitRunner;
-
- import net.kencochrane.raven.Raven;
- import net.kencochrane.raven.RavenFactory;
- import net.kencochrane.raven.dsn.Dsn;
- import net.kencochrane.raven.event.Event;
- import net.kencochrane.raven.event.interfaces.ExceptionInterface;
- import net.kencochrane.raven.event.interfaces.SentryInterface;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertTrue;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
-
- @RunWith(MockitoJUnitRunner.class)
- public class SentryErrorReporterTest {
-
- private static RavenFactory ravenFactory;
- @Mock private Raven raven;
- @Mock private ClientInfo clientInfo;
- @Captor private ArgumentCaptor<Event> eventCaptor;
- private Optional<Throwable> throwable;
- private SentryErrorReporter sentryErrorReporter;
-
- @BeforeClass
- public static void setUpClass() {
- // Static calls are horrible.
- ravenFactory = mock(RavenFactory.class);
- RavenFactory.registerFactory(ravenFactory);
- }
-
- @Before
- public void setUp() {
- when(ravenFactory.createRavenInstance(Matchers.<Dsn>anyObject())).thenReturn(raven);
- throwable = Optional.of(new IllegalArgumentException());
- sentryErrorReporter = new SentryErrorReporter(clientInfo);
- }
-
- @Test
- public void testSendsMessage() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals("message 123", eventCaptor.getValue().getMessage());
- }
-
- @Test
- public void testSendsTimestamp() {
- final LocalDateTime date = LocalDateTime.ofEpochSecond(56185920L, 0, ZoneOffset.UTC);
- sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(56185920000L, eventCaptor.getValue().getTimestamp().getTime());
- }
-
- @Test
- public void testSendsLowLevelAsInfo() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
- }
-
- @Test
- public void testSendsMediumLevelAsWarning() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
- }
-
- @Test
- public void testSendsHighLevelAsError() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
- }
-
- @Test
- public void testSendsFatalLevelAsFatal() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
- }
-
- @Test
- public void testSendsUnknownLevelAsInfo() {
- sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
- throwable);
- verify(raven).sendEvent(eventCaptor.capture());
- assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
- }
-
- @Test
- public void testAddsExceptionInterface() {
- final Exception exception = new IndexOutOfBoundsException("Message blah");
- sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
- Optional.of(exception));
- verify(raven).sendEvent(eventCaptor.capture());
- final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
- .get(ExceptionInterface.EXCEPTION_INTERFACE);
- assertNotNull(iface);
- assertTrue(iface instanceof ExceptionInterface);
- assertEquals(exception.getMessage(),
- ((ExceptionInterface) iface).getExceptions().getFirst().getExceptionMessage());
- }
-
- }
|