/* * Copyright (c) 2006-2014 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.util.Date; 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 ErrorReporterTest { private static RavenFactory ravenFactory; @Mock private Raven raven; @Captor private ArgumentCaptor eventCaptor; private ErrorReporter errorReporter; @BeforeClass public static void setUpClass() { // Static calls are horrible. ravenFactory = mock(RavenFactory.class); RavenFactory.registerFactory(ravenFactory); } @Before public void setUp() { when(ravenFactory.createRavenInstance(Matchers.anyObject())).thenReturn(raven); errorReporter = new ErrorReporter(new ClientInfo()); } @Test public void testSendsMessage() { errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals("message 123", eventCaptor.getValue().getMessage()); } @Test public void testSendsTimestamp() { final Date date = new Date(561859200000l); errorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(date, eventCaptor.getValue().getTimestamp()); } @Test public void testSendsLowLevelAsInfo() { errorReporter.sendException("message 123", ErrorLevel.LOW, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel()); } @Test public void testSendsMediumLevelAsWarning() { errorReporter.sendException("message 123", ErrorLevel.MEDIUM, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel()); } @Test public void testSendsHighLevelAsError() { errorReporter.sendException("message 123", ErrorLevel.HIGH, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel()); } @Test public void testSendsFatalLevelAsFatal() { errorReporter.sendException("message 123", ErrorLevel.FATAL, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel()); } @Test public void testSendsUnknownLevelAsInfo() { errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, null); verify(raven).sendEvent(eventCaptor.capture()); assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel()); } @Test public void testAddsDetailsAsExtra() { errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), null, "details 456"); verify(raven).sendEvent(eventCaptor.capture()); assertEquals("details 456", eventCaptor.getValue().getExtra().get("details")); } @Test public void testAddsExceptionInterface() { final Exception exception = new IndexOutOfBoundsException("Message blah"); errorReporter.sendException("message 123", ErrorLevel.UNKNOWN, new Date(), exception, null); 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()); } }