Pārlūkot izejas kodu

Merge pull request #364 from greboid/dev4

Add a test for ChannelWhoManager.
pull/366/head
Chris Smith 9 gadus atpakaļ
vecāks
revīzija
575e33c3e2

+ 6
- 2
channelwho/src/com/dmdirc/addons/channelwho/ChannelWhoManager.java Parādīt failu

@@ -28,6 +28,8 @@ import com.dmdirc.events.ServerDisconnectedEvent;
28 28
 import com.dmdirc.interfaces.Connection;
29 29
 import com.dmdirc.interfaces.ConnectionManager;
30 30
 
31
+import com.google.common.annotations.VisibleForTesting;
32
+
31 33
 import java.util.HashMap;
32 34
 import java.util.Map;
33 35
 
@@ -77,13 +79,15 @@ public class ChannelWhoManager {
77 79
         }
78 80
     }
79 81
 
82
+    @VisibleForTesting
80 83
     @Handler
81
-    private void handleServerConnectingEvent(final ServerConnectingEvent event) {
84
+    void handleServerConnectingEvent(final ServerConnectingEvent event) {
82 85
         addConnectionHandler(event.getConnection());
83 86
     }
84 87
 
88
+    @VisibleForTesting
85 89
     @Handler
86
-    private void handleServerDisconnectedEvent(final ServerDisconnectedEvent event) {
90
+    void handleServerDisconnectedEvent(final ServerDisconnectedEvent event) {
87 91
         removeConnectionHandler(event.getConnection());
88 92
     }
89 93
 

+ 37
- 1
channelwho/test/com/dmdirc/addons/channelwho/ChannelWhoManagerTest.java Parādīt failu

@@ -23,35 +23,71 @@
23 23
 package com.dmdirc.addons.channelwho;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.ServerConnectingEvent;
27
+import com.dmdirc.events.ServerDisconnectedEvent;
28
+import com.dmdirc.interfaces.Connection;
26 29
 import com.dmdirc.interfaces.ConnectionManager;
27 30
 
31
+import com.google.common.collect.Lists;
32
+
28 33
 import org.junit.Before;
29 34
 import org.junit.Test;
30 35
 import org.junit.runner.RunWith;
31 36
 import org.mockito.Mock;
32 37
 import org.mockito.runners.MockitoJUnitRunner;
33 38
 
39
+import static org.mockito.Mockito.never;
40
+import static org.mockito.Mockito.verify;
41
+import static org.mockito.Mockito.when;
42
+
34 43
 @RunWith(MockitoJUnitRunner.class)
35 44
 public class ChannelWhoManagerTest {
36 45
 
37 46
     @Mock private ConnectionHandlerFactory connectionHandlerFactory;
47
+    @Mock private ConnectionHandler connectionHandler;
38 48
     @Mock private ConnectionManager connectionManager;
49
+    @Mock private Connection connection;
50
+    @Mock private Connection connection2;
39 51
     @Mock private DMDircMBassador eventBus;
40 52
 
41 53
     private ChannelWhoManager instance;
42 54
 
43 55
     @Before
44 56
     public void setUp() throws Exception {
57
+        when(connectionManager.getConnections()).thenReturn(Lists.newArrayList(connection));
58
+        when(connectionHandlerFactory.get(connection)).thenReturn(connectionHandler);
45 59
         instance = new ChannelWhoManager(connectionHandlerFactory, connectionManager, eventBus);
60
+        instance.load();
46 61
     }
47 62
 
48 63
     @Test
49 64
     public void testLoad() throws Exception {
50
-
65
+        verify(eventBus).subscribe(instance);
66
+        verify(connectionHandlerFactory).get(connection);
51 67
     }
52 68
 
53 69
     @Test
54 70
     public void testUnload() throws Exception {
71
+        instance.unload();
72
+        verify(eventBus).unsubscribe(instance);
73
+        verify(connectionHandler).unload();
74
+    }
55 75
 
76
+    @Test
77
+    public void testServerConnectionEvent() throws Exception {
78
+        instance.handleServerConnectingEvent(new ServerConnectingEvent(connection2));
79
+        verify(connectionHandlerFactory).get(connection);
80
+    }
81
+
82
+    @Test
83
+    public void testServerDisconnectionEvent_Existing() throws Exception {
84
+        instance.handleServerDisconnectedEvent(new ServerDisconnectedEvent(connection));
85
+        verify(connectionHandler).unload();
86
+    }
87
+
88
+    @Test
89
+    public void testServerDisconnectionEvent_Unknown() throws Exception {
90
+        instance.handleServerDisconnectedEvent(new ServerDisconnectedEvent(connection2));
91
+        verify(connectionHandler, never()).unload();
56 92
     }
57 93
 }

Notiek ielāde…
Atcelt
Saglabāt