Browse Source

Start adding some ServerEventHandler tests.

Change-Id: I84ee00fedc3096de4ac500bf63a54d2b13209fe9
Reviewed-on: http://gerrit.dmdirc.com/4077
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith 9 years ago
parent
commit
aa8437afbf
1 changed files with 246 additions and 0 deletions
  1. 246
    0
      test/com/dmdirc/ServerEventHandlerTest.java

+ 246
- 0
test/com/dmdirc/ServerEventHandlerTest.java View File

@@ -0,0 +1,246 @@
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;
24
+
25
+import com.dmdirc.events.AppErrorEvent;
26
+import com.dmdirc.events.DMDircEvent;
27
+import com.dmdirc.events.ServerCtcpEvent;
28
+import com.dmdirc.events.UserErrorEvent;
29
+import com.dmdirc.parser.common.CallbackManager;
30
+import com.dmdirc.parser.common.ParserError;
31
+import com.dmdirc.parser.interfaces.ChannelInfo;
32
+import com.dmdirc.parser.interfaces.ClientInfo;
33
+import com.dmdirc.parser.interfaces.Parser;
34
+import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
35
+import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
36
+import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
37
+import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
38
+import com.dmdirc.parser.interfaces.callbacks.PrivateCtcpListener;
39
+import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
40
+
41
+import com.google.common.collect.Lists;
42
+
43
+import java.util.Date;
44
+
45
+import org.junit.Before;
46
+import org.junit.Test;
47
+import org.junit.runner.RunWith;
48
+import org.mockito.ArgumentCaptor;
49
+import org.mockito.Mock;
50
+import org.mockito.runners.MockitoJUnitRunner;
51
+
52
+import static org.junit.Assert.assertEquals;
53
+import static org.junit.Assert.assertNotNull;
54
+import static org.junit.Assert.assertTrue;
55
+import static org.mockito.Matchers.eq;
56
+import static org.mockito.Mockito.mock;
57
+import static org.mockito.Mockito.verify;
58
+import static org.mockito.Mockito.when;
59
+
60
+@RunWith(MockitoJUnitRunner.class)
61
+public class ServerEventHandlerTest {
62
+
63
+    @Mock private Server server;
64
+    @Mock private Parser parser;
65
+    @Mock private CallbackManager callbackManager;
66
+    @Mock private DMDircMBassador eventBus;
67
+    @Mock private ChannelInfo channelInfo;
68
+    @Mock private ClientInfo clientInfo;
69
+    @Mock private Query query;
70
+    @Mock private Date date;
71
+
72
+    @Before
73
+    public void setUp() {
74
+        when(server.getParser()).thenReturn(parser);
75
+        when(server.getState()).thenReturn(ServerState.CONNECTED);
76
+        when(parser.getCallbackManager()).thenReturn(callbackManager);
77
+
78
+        final ServerEventHandler handler = new ServerEventHandler(server, eventBus);
79
+        handler.registerCallbacks();
80
+    }
81
+
82
+    @Test
83
+    public void testSelfJoinAddsChannel() {
84
+        final ChannelSelfJoinListener listener = getCallback(ChannelSelfJoinListener.class);
85
+        listener.onChannelSelfJoin(parser, date, channelInfo);
86
+        verify(server).addChannel(channelInfo);
87
+    }
88
+
89
+    @Test
90
+    public void testOnPrivateMessageCreatesQuery() {
91
+        when(server.hasQuery("ho!st@name")).thenReturn(false);
92
+        when(server.getQuery("ho!st@name")).thenReturn(query);
93
+
94
+        final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
95
+        listener.onPrivateMessage(parser, date, "message", "ho!st@name");
96
+        verify(server).getQuery("ho!st@name");
97
+    }
98
+
99
+    @Test
100
+    public void testOnPrivateMessageRelaysMessage() {
101
+        when(server.hasQuery("ho!st@name")).thenReturn(false);
102
+        when(server.getQuery("ho!st@name")).thenReturn(query);
103
+
104
+        final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
105
+        listener.onPrivateMessage(parser, date, "message", "ho!st@name");
106
+        verify(query).onPrivateMessage(parser, date, "message", "ho!st@name");
107
+    }
108
+
109
+    @Test
110
+    public void testOnPrivateActionCreatesQuery() {
111
+        when(server.hasQuery("ho!st@name")).thenReturn(false);
112
+        when(server.getQuery("ho!st@name")).thenReturn(query);
113
+
114
+        final PrivateActionListener listener = getCallback(PrivateActionListener.class);
115
+        listener.onPrivateAction(parser, date, "message", "ho!st@name");
116
+        verify(server).getQuery("ho!st@name");
117
+    }
118
+
119
+    @Test
120
+    public void testOnPrivateActionRelaysMessage() {
121
+        when(server.hasQuery("ho!st@name")).thenReturn(false);
122
+        when(server.getQuery("ho!st@name")).thenReturn(query);
123
+
124
+        final PrivateActionListener listener = getCallback(PrivateActionListener.class);
125
+        listener.onPrivateAction(parser, date, "message", "ho!st@name");
126
+        verify(query).onPrivateAction(parser, date, "message", "ho!st@name");
127
+    }
128
+
129
+    @Test
130
+    public void testOnErrorInfoEmitsUserErrorEvent() {
131
+        final ParserError error = mock(ParserError.class);
132
+        when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
133
+        when(error.getLastLine()).thenReturn("lastline");
134
+        when(server.getAddress()).thenReturn("serveraddress");
135
+        when(error.getData()).thenReturn("DATA");
136
+        when(error.isUserError()).thenReturn(true);
137
+
138
+        final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
139
+        listener.onErrorInfo(parser, date, error);
140
+
141
+        final UserErrorEvent event = getAsyncEvent(UserErrorEvent.class);
142
+        assertEquals("DATA", event.getMessage());
143
+    }
144
+
145
+    @Test
146
+    public void testOnErrorInfoEmitsAppErrorEvent() {
147
+        final ParserError error = mock(ParserError.class);
148
+        when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
149
+        when(error.getLastLine()).thenReturn("lastline");
150
+        when(server.getAddress()).thenReturn("serveraddress");
151
+        when(error.getData()).thenReturn("DATA");
152
+        when(error.isUserError()).thenReturn(false);
153
+
154
+        final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
155
+        listener.onErrorInfo(parser, date, error);
156
+
157
+        final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
158
+        assertEquals("DATA", event.getMessage());
159
+    }
160
+
161
+    @Test
162
+    public void testOnErrorInfoIncludesConnectionDetails() {
163
+        final ParserError error = mock(ParserError.class);
164
+        when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
165
+        when(error.getLastLine()).thenReturn("lastline");
166
+        when(server.getAddress()).thenReturn("serveraddress");
167
+        when(error.getData()).thenReturn("DATA");
168
+        when(error.isUserError()).thenReturn(false);
169
+
170
+        final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
171
+        listener.onErrorInfo(parser, date, error);
172
+
173
+        final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
174
+        assertNotNull(event.getThrowable());
175
+        assertTrue(event.getThrowable().getMessage().contains("lastline"));
176
+        assertTrue(event.getThrowable().getMessage().contains("iline1"));
177
+        assertTrue(event.getThrowable().getMessage().contains("iline2"));
178
+        assertTrue(event.getThrowable().getMessage().contains("serveraddress"));
179
+    }
180
+
181
+    @Test
182
+    public void testOnErrorInfoIncludesException() {
183
+        final ParserError error = mock(ParserError.class);
184
+        final Exception exception = mock(Exception.class);
185
+        when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
186
+        when(error.getLastLine()).thenReturn("lastline");
187
+        when(server.getAddress()).thenReturn("serveraddress");
188
+        when(error.getData()).thenReturn("DATA");
189
+        when(error.isException()).thenReturn(true);
190
+        when(error.getException()).thenReturn(exception);
191
+
192
+        final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
193
+        listener.onErrorInfo(parser, date, error);
194
+
195
+        final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
196
+        assertNotNull(event.getThrowable());
197
+        assertEquals(exception, event.getThrowable());
198
+    }
199
+
200
+    @Test
201
+    public void testOnPrivateCTCPRaisesEvent() {
202
+        when(parser.getClient("host!na@me")).thenReturn(clientInfo);
203
+        when(server.parseHostmask("host!na@me")).thenReturn(new String[]{"host", "na", "me"});
204
+
205
+        final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
206
+        listener.onPrivateCTCP(parser, date, "type", "message", "host!na@me");
207
+
208
+        final ServerCtcpEvent event = getEvent(ServerCtcpEvent.class);
209
+        assertEquals("type", event.getType());
210
+        assertEquals("message", event.getContent());
211
+        assertEquals(clientInfo, event.getClient());
212
+    }
213
+
214
+    @Test
215
+    public void testOnPrivateCTCPSendsReplyIfEventUnhandled() {
216
+        when(parser.getClient("host!na@me")).thenReturn(clientInfo);
217
+        when(server.parseHostmask("host!na@me")).thenReturn(new String[]{"host", "na", "me"});
218
+
219
+        final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
220
+        listener.onPrivateCTCP(parser, date, "type", "message", "host!na@me");
221
+
222
+        verify(server).sendCTCPReply("host", "type", "message");
223
+    }
224
+
225
+    private <T extends CallbackInterface> T getCallback(final Class<T> type) {
226
+        final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
227
+        verify(callbackManager).addCallback(eq(type), captor.capture());
228
+        assertNotNull(captor.getValue());
229
+        return captor.getValue();
230
+    }
231
+
232
+    private <T extends DMDircEvent> T getAsyncEvent(final Class<T> type) {
233
+        final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
234
+        verify(eventBus).publishAsync(captor.capture());
235
+        assertNotNull(captor.getValue());
236
+        return captor.getValue();
237
+    }
238
+
239
+    private <T extends DMDircEvent> T getEvent(final Class<T> type) {
240
+        final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
241
+        verify(eventBus).publish(captor.capture());
242
+        assertNotNull(captor.getValue());
243
+        return captor.getValue();
244
+    }
245
+
246
+}

Loading…
Cancel
Save