Browse Source

Add some unit tests for ErrorReporter.

pull/82/head
Chris Smith 9 years ago
parent
commit
9b6133fa9a
1 changed files with 135 additions and 0 deletions
  1. 135
    0
      test/com/dmdirc/logger/ErrorReporterTest.java

+ 135
- 0
test/com/dmdirc/logger/ErrorReporterTest.java View File

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

Loading…
Cancel
Save