瀏覽代碼

Implement nick keep plugin.

pull/313/head
Greg Holmes 9 年之前
父節點
當前提交
a9286348f2

+ 44
- 1
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java 查看文件

@@ -22,20 +22,63 @@
22 22
 
23 23
 package com.dmdirc.addons.nickkeep;
24 24
 
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.profiles.Profile;
27
+import com.dmdirc.events.ChannelNickChangeEvent;
28
+import com.dmdirc.interfaces.Connection;
29
+
30
+import java.util.Optional;
31
+
25 32
 import javax.inject.Inject;
26 33
 
34
+import net.engio.mbassy.listener.Handler;
35
+
27 36
 /**
28 37
  * Provides Nick Keep support in DMDirc.
29 38
  */
30 39
 public class NickKeepManager {
31 40
 
41
+    private final DMDircMBassador eventBus;
42
+
32 43
     @Inject
33
-    public NickKeepManager() {
44
+    public NickKeepManager(final DMDircMBassador eventBus) {
45
+        this.eventBus = eventBus;
34 46
     }
35 47
 
36 48
     public void load() {
49
+        eventBus.subscribe(this);
37 50
     }
38 51
 
39 52
     public void unload() {
53
+        eventBus.unsubscribe(this);
54
+    }
55
+
56
+    @Handler
57
+    public void handleNickChange(final ChannelNickChangeEvent event) {
58
+        final Optional<Connection> connection = event.getChannel().getConnection();
59
+        if (!connection.isPresent()) {
60
+            //No point carrying on without a connection
61
+            return;
62
+        }
63
+        final Optional<String> currentNickname = connection.map(Connection::getNickname)
64
+                .orElse(Optional.empty());
65
+        if (!currentNickname.isPresent()) {
66
+            //No point carrying on if we don't know our own nickname
67
+            return;
68
+        }
69
+        final Optional<String> desiredNickname = connection.map(Connection::getProfile)
70
+                .map(Profile::getNicknames).map(c -> c.stream().findFirst())
71
+                .orElse(Optional.empty());
72
+        if (!desiredNickname.isPresent()) {
73
+            //No point carrying on without a desired nickname
74
+            return;
75
+        }
76
+        if (!event.getOldNick().equals(desiredNickname.get())) {
77
+            //This isnt the nickname we're looking for, move along
78
+            return;
79
+        }
80
+        if (!currentNickname.equals(desiredNickname)) {
81
+            connection.ifPresent(c -> c.setNickname(desiredNickname.get()));
82
+        }
40 83
     }
41 84
 }

+ 1
- 9
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java 查看文件

@@ -23,7 +23,6 @@
23 23
 package com.dmdirc.addons.nickkeep;
24 24
 
25 25
 import com.dmdirc.ClientModule;
26
-import com.dmdirc.plugins.PluginInfo;
27 26
 
28 27
 import dagger.Module;
29 28
 
@@ -31,11 +30,4 @@ import dagger.Module;
31 30
  * Dagger injection module for the Nick Keep plugin
32 31
  */
33 32
 @Module(injects = NickKeepModule.class, addsTo = ClientModule.class)
34
-public class NickKeepModule {
35
-
36
-    private final PluginInfo pluginInfo;
37
-
38
-    public NickKeepModule(final PluginInfo pluginInfo) {
39
-        this.pluginInfo = pluginInfo;
40
-    }
41
-}
33
+public class NickKeepModule {}

+ 1
- 1
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java 查看文件

@@ -38,7 +38,7 @@ public class NickKeepPlugin extends BasePlugin {
38 38
     public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
39 39
         super.load(pluginInfo, graph);
40 40
 
41
-        setObjectGraph(graph.plus(new NickKeepModule(pluginInfo)));
41
+        setObjectGraph(graph.plus(new NickKeepModule()));
42 42
         manager = getObjectGraph().get(NickKeepManager.class);
43 43
     }
44 44
 

+ 78
- 0
nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java 查看文件

@@ -22,14 +22,92 @@
22 22
 
23 23
 package com.dmdirc.addons.nickkeep;
24 24
 
25
+import com.dmdirc.Channel;
26
+import com.dmdirc.DMDircMBassador;
27
+import com.dmdirc.config.profiles.Profile;
28
+import com.dmdirc.events.ChannelNickChangeEvent;
29
+import com.dmdirc.interfaces.Connection;
30
+
31
+import com.google.common.collect.Lists;
32
+
33
+import java.util.Optional;
34
+
25 35
 import org.junit.Before;
36
+import org.junit.Test;
26 37
 import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
27 39
 import org.mockito.runners.MockitoJUnitRunner;
28 40
 
41
+import static org.mockito.Matchers.anyString;
42
+import static org.mockito.Mockito.never;
43
+import static org.mockito.Mockito.verify;
44
+import static org.mockito.Mockito.when;
45
+
29 46
 @RunWith(MockitoJUnitRunner.class)
30 47
 public class NickKeepManagerTest {
31 48
 
49
+    @Mock private DMDircMBassador eventBus;
50
+    @Mock private ChannelNickChangeEvent event;
51
+    @Mock private Channel channel;
52
+    @Mock private Connection connection;
53
+    @Mock private Profile profile;
54
+    private NickKeepManager instance;
55
+
32 56
     @Before
33 57
     public void setUp() throws Exception {
58
+        instance = new NickKeepManager(eventBus);
59
+        when(event.getChannel()).thenReturn(channel);
60
+        when(channel.getConnection()).thenReturn(Optional.of(connection));
61
+        when(connection.getProfile()).thenReturn(profile);
62
+        when(profile.getNicknames()).thenReturn(Lists.newArrayList("one", "two", "three"));
63
+    }
64
+
65
+    @Test
66
+    public void testLoad() throws Exception {
67
+        instance.load();
68
+        verify(eventBus).subscribe(instance);
69
+    }
70
+
71
+    @Test
72
+    public void testUnload() throws Exception {
73
+        instance.unload();
74
+        verify(eventBus).unsubscribe(instance);
75
+    }
76
+
77
+    @Test
78
+    public void testHandleNickChange_NoConnection() throws Exception {
79
+        when(channel.getConnection()).thenReturn(Optional.empty());
80
+        instance.handleNickChange(event);
81
+        verify(connection, never()).setNickname("one");
82
+    }
83
+
84
+    @Test
85
+    public void testHandleNickChange_NoCurrentNickname() throws Exception {
86
+        when(connection.getNickname()).thenReturn(Optional.empty());
87
+        instance.handleNickChange(event);
88
+        verify(connection, never()).setNickname("one");
89
+    }
90
+
91
+    @Test
92
+    public void testHandleNickChange_NoNicknames() throws Exception {
93
+        when(profile.getNicknames()).thenReturn(Lists.newArrayList());
94
+        instance.handleNickChange(event);
95
+        verify(connection, never()).setNickname(anyString());
96
+    }
97
+
98
+    @Test
99
+    public void testHandleNickChange_NotDesired() throws Exception {
100
+        when(event.getOldNick()).thenReturn("RAR");
101
+        when(connection.getNickname()).thenReturn(Optional.of("one"));
102
+        instance.handleNickChange(event);
103
+        verify(connection, never()).setNickname("one");
104
+    }
105
+
106
+    @Test
107
+    public void testHandleNickChange_Desired() throws Exception {
108
+        when(event.getOldNick()).thenReturn("one");
109
+        when(connection.getNickname()).thenReturn(Optional.of("RAR"));
110
+        instance.handleNickChange(event);
111
+        verify(connection).setNickname("one");
34 112
     }
35 113
 }

+ 55
- 0
nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java 查看文件

@@ -0,0 +1,55 @@
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.nickkeep;
24
+
25
+import org.junit.Before;
26
+import org.junit.Test;
27
+import org.junit.runner.RunWith;
28
+import org.mockito.Mock;
29
+import org.mockito.runners.MockitoJUnitRunner;
30
+
31
+import static org.mockito.Mockito.verify;
32
+
33
+@RunWith(MockitoJUnitRunner.class)
34
+public class NickKeepPluginTest {
35
+
36
+    @Mock private NickKeepManager nickKeepManager;
37
+    private NickKeepPlugin instance;
38
+
39
+    @Before
40
+    public void setUp() throws Exception {
41
+        instance = new NickKeepPlugin();
42
+    }
43
+
44
+    @Test
45
+    public void testOnLoad() throws Exception {
46
+        instance.onLoad();
47
+        verify(nickKeepManager).load();
48
+    }
49
+
50
+    @Test
51
+    public void testOnUnload() throws Exception {
52
+        instance.onUnload();
53
+        verify(nickKeepManager).unload();
54
+    }
55
+}

Loading…
取消
儲存