|
@@ -0,0 +1,192 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 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.addons.channelwho;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.DMDircMBassador;
|
|
26
|
+import com.dmdirc.config.ConfigBinder;
|
|
27
|
+import com.dmdirc.events.ChannelUserAwayEvent;
|
|
28
|
+import com.dmdirc.events.DisplayProperty;
|
|
29
|
+import com.dmdirc.events.ServerNumericEvent;
|
|
30
|
+import com.dmdirc.interfaces.Connection;
|
|
31
|
+import com.dmdirc.interfaces.ConnectionManager;
|
|
32
|
+import com.dmdirc.interfaces.GroupChat;
|
|
33
|
+import com.dmdirc.interfaces.GroupChatManager;
|
|
34
|
+import com.dmdirc.interfaces.GroupChatUser;
|
|
35
|
+import com.dmdirc.interfaces.User;
|
|
36
|
+import com.dmdirc.interfaces.WindowModel;
|
|
37
|
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
|
38
|
+
|
|
39
|
+import com.google.common.collect.Lists;
|
|
40
|
+
|
|
41
|
+import java.util.Optional;
|
|
42
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
43
|
+import java.util.concurrent.ScheduledFuture;
|
|
44
|
+import java.util.concurrent.TimeUnit;
|
|
45
|
+
|
|
46
|
+import org.junit.Before;
|
|
47
|
+import org.junit.Test;
|
|
48
|
+import org.junit.runner.RunWith;
|
|
49
|
+import org.mockito.ArgumentCaptor;
|
|
50
|
+import org.mockito.Captor;
|
|
51
|
+import org.mockito.Mock;
|
|
52
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
53
|
+
|
|
54
|
+import static org.junit.Assert.assertEquals;
|
|
55
|
+import static org.mockito.Matchers.any;
|
|
56
|
+import static org.mockito.Matchers.anyLong;
|
|
57
|
+import static org.mockito.Matchers.eq;
|
|
58
|
+import static org.mockito.Mockito.never;
|
|
59
|
+import static org.mockito.Mockito.verify;
|
|
60
|
+import static org.mockito.Mockito.when;
|
|
61
|
+
|
|
62
|
+@RunWith(MockitoJUnitRunner.class)
|
|
63
|
+public class ConnectionHandlerTest {
|
|
64
|
+
|
|
65
|
+ @Mock private AggregateConfigProvider config;
|
|
66
|
+ @Mock private ConfigBinder configBinder;
|
|
67
|
+ @Mock private WindowModel windowModel;
|
|
68
|
+ @Mock private DMDircMBassador eventBus;
|
|
69
|
+ @Mock private ScheduledExecutorService scheduledExecutorService;
|
|
70
|
+ @Mock private ScheduledFuture scheduledFuture;
|
|
71
|
+ @Mock private ConnectionManager connectionManager;
|
|
72
|
+ @Mock private Connection connection;
|
|
73
|
+ @Mock private GroupChat groupChat;
|
|
74
|
+ @Mock private GroupChatUser groupChatUser;
|
|
75
|
+ @Mock private User user;
|
|
76
|
+ @Mock private GroupChatManager groupChatManager;
|
|
77
|
+ @Mock private ServerNumericEvent serverNumericEvent;
|
|
78
|
+ @Mock private ChannelUserAwayEvent channelUserAwayEvent;
|
|
79
|
+ @Captor private ArgumentCaptor<ChannelUserAwayEvent> eventArgumentCaptor;
|
|
80
|
+ private ConnectionHandler instance;
|
|
81
|
+
|
|
82
|
+ @Before
|
|
83
|
+ public void setUp() throws Exception {
|
|
84
|
+ when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any()))
|
|
85
|
+ .thenReturn(scheduledFuture);
|
|
86
|
+ when(windowModel.getEventBus()).thenReturn(eventBus);
|
|
87
|
+ when(connection.getWindowModel()).thenReturn(windowModel);
|
|
88
|
+ when(config.getBinder()).thenReturn(configBinder);
|
|
89
|
+ when(connectionManager.getConnections()).thenReturn(Lists.newArrayList(connection));
|
|
90
|
+ when(connection.getGroupChatManager()).thenReturn(groupChatManager);
|
|
91
|
+ when(groupChatManager.getChannels()).thenReturn(Lists.newArrayList(groupChat));
|
|
92
|
+ when(groupChat.getWindowModel()).thenReturn(windowModel);
|
|
93
|
+ when(groupChat.getConnection()).thenReturn(Optional.of(connection));
|
|
94
|
+ when(configBinder.withDefaultDomain("domain")).thenReturn(configBinder);
|
|
95
|
+ when(windowModel.getConfigManager()).thenReturn(config);
|
|
96
|
+ when(groupChatUser.getNickname()).thenReturn("nickname");
|
|
97
|
+ when(channelUserAwayEvent.getUser()).thenReturn(groupChatUser);
|
|
98
|
+ when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
|
|
99
|
+ when(groupChatUser.getUser()).thenReturn(user);
|
|
100
|
+ instance = new ConnectionHandler(config, scheduledExecutorService, connectionManager,
|
|
101
|
+ "domain", connection);
|
|
102
|
+ instance.handleWhoInterval(5);
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ @Test
|
|
106
|
+ public void testLoad() throws Exception {
|
|
107
|
+ instance.load();
|
|
108
|
+ verify(configBinder).bind(instance, ConnectionHandler.class);
|
|
109
|
+ verify(eventBus).subscribe(instance);
|
|
110
|
+ verify(scheduledExecutorService).schedule(any(Runnable.class), eq(5l),
|
|
111
|
+ eq(TimeUnit.MILLISECONDS));
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ @Test
|
|
115
|
+ public void testUnload() throws Exception {
|
|
116
|
+ instance.unload();
|
|
117
|
+ verify(configBinder).unbind(instance);
|
|
118
|
+ verify(scheduledExecutorService).shutdown();
|
|
119
|
+ verify(eventBus).unsubscribe(instance);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @Test
|
|
123
|
+ public void testHandleWhoInterval() throws Exception {
|
|
124
|
+ instance.handleWhoInterval(10);
|
|
125
|
+ verify(scheduledFuture).cancel(true);
|
|
126
|
+ verify(scheduledExecutorService).schedule(any(Runnable.class), eq(5l),
|
|
127
|
+ eq(TimeUnit.MILLISECONDS));
|
|
128
|
+ verify(scheduledExecutorService).schedule(any(Runnable.class), eq(10l),
|
|
129
|
+ eq(TimeUnit.MILLISECONDS));
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ @Test
|
|
133
|
+ public void testCheckWho_True() throws Exception {
|
|
134
|
+ when(config.getOptionBool("domain", "sendWho")).thenReturn(true);
|
|
135
|
+ instance.checkWho();
|
|
136
|
+ verify(config).getOptionBool("domain", "sendWho");
|
|
137
|
+ verify(groupChat).requestUsersInfo();
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ @Test
|
|
141
|
+ public void testCheckWho_False() throws Exception {
|
|
142
|
+ when(config.getOptionBool("domain", "sendWho")).thenReturn(false);
|
|
143
|
+ instance.checkWho();
|
|
144
|
+ verify(config).getOptionBool("domain", "sendWho");
|
|
145
|
+ verify(groupChat, never()).requestUsersInfo();
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ @Test
|
|
149
|
+ public void testHandleAwayEvent_WithReason() throws Exception {
|
|
150
|
+ when(channelUserAwayEvent.getReason()).thenReturn(Optional.ofNullable("reason"));
|
|
151
|
+ instance.load();
|
|
152
|
+ instance.handleAwayEvent(channelUserAwayEvent);
|
|
153
|
+ verify(channelUserAwayEvent, never())
|
|
154
|
+ .setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
|
|
155
|
+ verify(connection, never()).requestUserInfo(any());
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ @Test
|
|
159
|
+ public void testHandleAwayEvent_WithoutReason() throws Exception {
|
|
160
|
+ when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
|
|
161
|
+ instance.load();
|
|
162
|
+ instance.handleAwayEvent(channelUserAwayEvent);
|
|
163
|
+ verify(channelUserAwayEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
|
|
164
|
+ verify(connection).requestUserInfo(any());
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ @Test
|
|
168
|
+ public void testHandleServerNumericEvent_301() throws Exception {
|
|
169
|
+ when(serverNumericEvent.getNumeric()).thenReturn(301);
|
|
170
|
+ when(serverNumericEvent.getArgs()).thenReturn(
|
|
171
|
+ new String[]{"", "", "", "", "nickname", "reason"});
|
|
172
|
+ instance.load();
|
|
173
|
+ when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
|
|
174
|
+ when(groupChatUser.getGroupChat()).thenReturn(groupChat);
|
|
175
|
+ instance.handleAwayEvent(channelUserAwayEvent);
|
|
176
|
+ instance.handleServerNumericEvent(serverNumericEvent);
|
|
177
|
+ verify(eventBus).publishAsync(eventArgumentCaptor.capture());
|
|
178
|
+ assertEquals("nickname", eventArgumentCaptor.getValue().getUser().getNickname());
|
|
179
|
+ assertEquals("reason", eventArgumentCaptor.getValue().getReason().get());
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ @Test
|
|
183
|
+ public void testHandleServerNumericEvent_101() throws Exception {
|
|
184
|
+ when(serverNumericEvent.getNumeric()).thenReturn(101);
|
|
185
|
+ when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
|
|
186
|
+ instance.load();
|
|
187
|
+ instance.handleAwayEvent(channelUserAwayEvent);
|
|
188
|
+ instance.handleServerNumericEvent(serverNumericEvent);
|
|
189
|
+ verify(serverNumericEvent, never()).getArgs();
|
|
190
|
+ verify(eventBus, never()).publishAsync(any());
|
|
191
|
+ }
|
|
192
|
+}
|