Browse Source

Implement JPQ plugin.

pull/400/head
Greg Holmes 9 years ago
parent
commit
0f169f8e78

+ 6
- 2
jpq/plugin.config View File

@@ -7,10 +7,11 @@ keysections:
7 7
   metadata
8 8
   updates
9 9
   version
10
+  defaults
10 11
 
11 12
 metadata:
12 13
   author=Greg <greg@dmdirc.com>
13
-  mainclass=com.dmdirc.addons.jpq.JPQPLugin
14
+  mainclass=com.dmdirc.addons.jpq.JPQPlugin
14 15
   description=Hides Joins, parts and quits from specified channels.
15 16
   name=jpq
16 17
   nicename=JPQ Hiding
@@ -19,4 +20,7 @@ updates:
19 20
   id=79
20 21
 
21 22
 version:
22
-  friendly=0.1
23
+  friendly=0.1
24
+
25
+defaults:
26
+  hidejpq=false

+ 102
- 0
jpq/src/com/dmdirc/addons/jpq/GroupChatHandler.java View File

@@ -0,0 +1,102 @@
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.jpq;
24
+
25
+import com.dmdirc.config.ConfigBinder;
26
+import com.dmdirc.config.ConfigBinding;
27
+import com.dmdirc.events.ChannelJoinEvent;
28
+import com.dmdirc.events.ChannelPartEvent;
29
+import com.dmdirc.events.ChannelQuitEvent;
30
+import com.dmdirc.events.DisplayProperty;
31
+import com.dmdirc.events.DisplayableEvent;
32
+import com.dmdirc.interfaces.GroupChat;
33
+
34
+import com.google.common.annotations.VisibleForTesting;
35
+
36
+import net.engio.mbassy.listener.Handler;
37
+
38
+/**
39
+ * Handles {@link ChannelJoinEvent}, {@link ChannelPartEvent}, {@link ChannelQuitEvent} events and
40
+ * hides them if the required.
41
+ */
42
+public class GroupChatHandler {
43
+
44
+    private final GroupChat groupChat;
45
+    private final ConfigBinder binder;
46
+    private boolean hideEvents;
47
+
48
+    public GroupChatHandler(final String domain, final GroupChat groupChat) {
49
+        this.groupChat = groupChat;
50
+        binder = groupChat.getWindowModel().getConfigManager().getBinder()
51
+                .withDefaultDomain(domain);
52
+    }
53
+
54
+    /**
55
+     * Loads this handler, adds required listeners and bindings.
56
+     */
57
+    public void load() {
58
+        groupChat.getEventBus().subscribe(this);
59
+        binder.bind(this, GroupChatHandler.class);
60
+    }
61
+
62
+    /**
63
+     * Unloads this handler, removes required listeners and bindings.
64
+     */
65
+    public void unload() {
66
+        groupChat.getEventBus().unsubscribe(this);
67
+        binder.unbind(this);
68
+    }
69
+
70
+    @VisibleForTesting
71
+    @ConfigBinding(key = "hidejpq")
72
+    void handleSettingChange(final boolean value) {
73
+        hideEvents = value;
74
+    }
75
+
76
+    @SuppressWarnings("TypeMayBeWeakened")
77
+    @VisibleForTesting
78
+    @Handler
79
+    void handleJoin(final ChannelJoinEvent event) {
80
+        hideEvent(event);
81
+    }
82
+
83
+    @SuppressWarnings("TypeMayBeWeakened")
84
+    @VisibleForTesting
85
+    @Handler
86
+    void handlePart(final ChannelPartEvent event) {
87
+        hideEvent(event);
88
+    }
89
+
90
+    @SuppressWarnings("TypeMayBeWeakened")
91
+    @VisibleForTesting
92
+    @Handler
93
+    void handleQuit(final ChannelQuitEvent event) {
94
+        hideEvent(event);
95
+    }
96
+
97
+    private void hideEvent(final DisplayableEvent event) {
98
+        if (hideEvents) {
99
+            event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
100
+        }
101
+    }
102
+}

+ 54
- 0
jpq/src/com/dmdirc/addons/jpq/GroupChatHandlerFactory.java View File

@@ -0,0 +1,54 @@
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.jpq;
24
+
25
+import com.dmdirc.interfaces.GroupChat;
26
+import com.dmdirc.plugins.PluginDomain;
27
+
28
+import javax.inject.Inject;
29
+
30
+/**
31
+ * Factory for creating {@link GroupChatHandler}s.
32
+ */
33
+public class GroupChatHandlerFactory {
34
+
35
+    private final String domain;
36
+
37
+    @Inject
38
+    public GroupChatHandlerFactory(@PluginDomain(JPQPlugin.class) final String domain) {
39
+        this.domain = domain;
40
+    }
41
+
42
+    /**
43
+     * Creates a new instance for the specified {@link GroupChat}.
44
+     *
45
+     * @param groupChat Group chat to handle
46
+     *
47
+     * @return Loaded instance
48
+     */
49
+    public GroupChatHandler get(final GroupChat groupChat) {
50
+        final GroupChatHandler groupChatHandler = new GroupChatHandler(domain, groupChat);
51
+        groupChatHandler.load();
52
+        return groupChatHandler;
53
+    }
54
+}

+ 123
- 0
jpq/src/com/dmdirc/addons/jpq/JPQManager.java View File

@@ -0,0 +1,123 @@
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.jpq;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.prefs.PreferencesSetting;
27
+import com.dmdirc.config.prefs.PreferencesType;
28
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
29
+import com.dmdirc.events.ServerConnectedEvent;
30
+import com.dmdirc.events.ServerDisconnectedEvent;
31
+import com.dmdirc.interfaces.Connection;
32
+import com.dmdirc.interfaces.ConnectionManager;
33
+import com.dmdirc.interfaces.GroupChat;
34
+import com.dmdirc.plugins.PluginDomain;
35
+
36
+import com.google.common.annotations.VisibleForTesting;
37
+
38
+import java.util.HashMap;
39
+import java.util.Map;
40
+
41
+import javax.inject.Inject;
42
+
43
+import net.engio.mbassy.listener.Handler;
44
+
45
+/**
46
+ * Provides the ability to hide joins, parts and quits from a {@link GroupChat}.
47
+ */
48
+public class JPQManager {
49
+
50
+    private final String domain;
51
+    private final ConnectionManager connectionManager;
52
+    private final GroupChatHandlerFactory groupChatHandlerFactory;
53
+    private final DMDircMBassador eventBus;
54
+    private final Map<GroupChat, GroupChatHandler> groupChatHandlers;
55
+
56
+    @Inject
57
+    public JPQManager(
58
+            @PluginDomain(JPQPlugin.class) final String domain,
59
+            final ConnectionManager connectionManager,
60
+            final GroupChatHandlerFactory groupChatHandlerFactory,
61
+            final DMDircMBassador eventBus) {
62
+        this.domain = domain;
63
+        this.connectionManager = connectionManager;
64
+        this.groupChatHandlerFactory = groupChatHandlerFactory;
65
+        this.eventBus = eventBus;
66
+        groupChatHandlers = new HashMap<>();
67
+    }
68
+
69
+    /**
70
+     * Unloads this manager, removing required listeners and bindings.
71
+     */
72
+    public void load() {
73
+        connectionManager.getConnections().forEach(this::addGroupChatHandler);
74
+        eventBus.subscribe(this);
75
+    }
76
+
77
+    /**
78
+     * Loads this manager, adding required listeners and bindings.
79
+     */
80
+    public void unload() {
81
+        connectionManager.getConnections().forEach(this::removeGroupChatHandler);
82
+        eventBus.unsubscribe(this);
83
+    }
84
+
85
+    @VisibleForTesting
86
+    @Handler
87
+    void handleConnectionAdded(final ServerConnectedEvent event) {
88
+        addGroupChatHandler(event.getConnection());
89
+    }
90
+
91
+    @VisibleForTesting
92
+    @Handler
93
+    void handleConnectionRemoved(final ServerDisconnectedEvent event) {
94
+        removeGroupChatHandler(event.getConnection());
95
+    }
96
+
97
+    @VisibleForTesting
98
+    @Handler
99
+    void handleGroupChatPrefs(final GroupChatPrefsRequestedEvent event) {
100
+        event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
101
+                domain, "hidejpq", "Hide J/P/Q", "Hides joins, parts and quits in a group chat.",
102
+                event.getConfig(), event.getIdentity()));
103
+    }
104
+
105
+    private void addGroupChatHandler(final Connection connection) {
106
+        connection.getGroupChatManager().getChannels().forEach(this::addGroupChatHandler);
107
+    }
108
+
109
+    private void addGroupChatHandler(final GroupChat groupChat) {
110
+        groupChatHandlers.computeIfAbsent(groupChat, groupChatHandlerFactory::get);
111
+    }
112
+
113
+    private void removeGroupChatHandler(final Connection connection) {
114
+        connection.getGroupChatManager().getChannels().forEach(this::removeGroupChatHandler);
115
+    }
116
+
117
+    private void removeGroupChatHandler(final GroupChat groupChat) {
118
+        final GroupChatHandler groupChatHandler = groupChatHandlers.remove(groupChat);
119
+        if (groupChatHandler != null) {
120
+            groupChatHandler.unload();
121
+        }
122
+    }
123
+}

+ 46
- 0
jpq/src/com/dmdirc/addons/jpq/JPQModule.java View File

@@ -0,0 +1,46 @@
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.jpq;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+@Module(injects = {JPQManager.class, GroupChatHandlerFactory.class}, addsTo = ClientModule.class)
33
+public class JPQModule {
34
+
35
+    private final PluginInfo pluginInfo;
36
+
37
+    public JPQModule(final PluginInfo pluginInfo) {
38
+        this.pluginInfo = pluginInfo;
39
+    }
40
+
41
+    @Provides
42
+    @PluginDomain(JPQPlugin.class)
43
+    public String getDomain() {
44
+        return pluginInfo.getDomain();
45
+    }
46
+}

+ 24
- 1
jpq/src/com/dmdirc/addons/jpq/JPQPlugin.java View File

@@ -22,10 +22,33 @@
22 22
 
23 23
 package com.dmdirc.addons.jpq;
24 24
 
25
+import com.dmdirc.interfaces.GroupChat;
26
+import com.dmdirc.plugins.PluginInfo;
25 27
 import com.dmdirc.plugins.implementations.BasePlugin;
26 28
 
29
+import dagger.ObjectGraph;
30
+
27 31
 /**
28
- *
32
+ * Provides the ability to hide joins, parts and quits from a {@link GroupChat}.
29 33
  */
30 34
 public class JPQPlugin extends BasePlugin {
35
+
36
+    private JPQManager manager;
37
+
38
+    @Override
39
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
40
+        super.load(pluginInfo, graph);
41
+        setObjectGraph(graph.plus(new JPQModule(pluginInfo)));
42
+        manager = getObjectGraph().get(JPQManager.class);
43
+    }
44
+
45
+    @Override
46
+    public void onLoad() {
47
+        manager.load();
48
+    }
49
+
50
+    @Override
51
+    public void onUnload() {
52
+        manager.unload();
53
+    }
31 54
 }

+ 129
- 0
jpq/test/com/dmdirc/addons/jpq/GroupChatHandlerTest.java View File

@@ -0,0 +1,129 @@
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.jpq;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.ConfigBinder;
27
+import com.dmdirc.events.ChannelJoinEvent;
28
+import com.dmdirc.events.ChannelPartEvent;
29
+import com.dmdirc.events.ChannelQuitEvent;
30
+import com.dmdirc.events.DisplayProperty;
31
+import com.dmdirc.interfaces.GroupChat;
32
+import com.dmdirc.interfaces.WindowModel;
33
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
34
+
35
+import org.junit.Before;
36
+import org.junit.Test;
37
+import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
39
+import org.mockito.runners.MockitoJUnitRunner;
40
+
41
+import static org.mockito.Mockito.never;
42
+import static org.mockito.Mockito.verify;
43
+import static org.mockito.Mockito.when;
44
+
45
+@RunWith(MockitoJUnitRunner.class)
46
+public class GroupChatHandlerTest {
47
+
48
+    @Mock private GroupChat groupChat;
49
+    @Mock private WindowModel windowModel;
50
+    @Mock private AggregateConfigProvider configProvider;
51
+    @Mock private ConfigBinder configBinder;
52
+    @Mock private DMDircMBassador eventBus;
53
+    @Mock private ChannelJoinEvent channelJoinEvent;
54
+    @Mock private ChannelPartEvent channelPartEvent;
55
+    @Mock private ChannelQuitEvent channelQuitEvent;
56
+    private GroupChatHandler instance;
57
+
58
+    @Before
59
+    public void setUp() throws Exception {
60
+        when(groupChat.getEventBus()).thenReturn(eventBus);
61
+        when(groupChat.getWindowModel()).thenReturn(windowModel);
62
+        when(windowModel.getConfigManager()).thenReturn(configProvider);
63
+        when(configProvider.getBinder()).thenReturn(configBinder);
64
+        when(configBinder.withDefaultDomain("domain")).thenReturn(configBinder);
65
+        instance = new GroupChatHandler("domain", groupChat);
66
+    }
67
+
68
+    @Test
69
+    public void testLoad() throws Exception {
70
+        instance.load();
71
+        verify(configBinder).bind(instance, GroupChatHandler.class);
72
+        verify(eventBus).subscribe(instance);
73
+    }
74
+
75
+    @Test
76
+    public void testUnload() throws Exception {
77
+        instance.unload();
78
+        verify(configBinder).unbind(instance);
79
+        verify(eventBus).unsubscribe(instance);
80
+    }
81
+
82
+    @Test
83
+    public void testHandleJoin_On() throws Exception {
84
+        instance.load();
85
+        instance.handleSettingChange(true);
86
+        instance.handleJoin(channelJoinEvent);
87
+        verify(channelJoinEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
88
+    }
89
+
90
+    @Test
91
+    public void testHandlePart_On() throws Exception {
92
+        instance.load();
93
+        instance.handleSettingChange(true);
94
+        instance.handlePart(channelPartEvent);
95
+        verify(channelPartEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
96
+    }
97
+
98
+    @Test
99
+    public void testHandleQuit_On() throws Exception {
100
+        instance.load();
101
+        instance.handleSettingChange(true);
102
+        instance.handleQuit(channelQuitEvent);
103
+        verify(channelQuitEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
104
+    }
105
+
106
+    @Test
107
+    public void testHandleJoin_Off() throws Exception {
108
+        instance.load();
109
+        instance.handleSettingChange(false);
110
+        instance.handleJoin(channelJoinEvent);
111
+        verify(channelJoinEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
112
+    }
113
+
114
+    @Test
115
+    public void testHandlePart_Off() throws Exception {
116
+        instance.load();
117
+        instance.handleSettingChange(false);
118
+        instance.handlePart(channelPartEvent);
119
+        verify(channelPartEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
120
+    }
121
+
122
+    @Test
123
+    public void testHandleQuit_Off() throws Exception {
124
+        instance.load();
125
+        instance.handleSettingChange(false);
126
+        instance.handleJoin(channelJoinEvent);
127
+        verify(channelJoinEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
128
+    }
129
+}

+ 122
- 0
jpq/test/com/dmdirc/addons/jpq/JPQManagerTest.java View File

@@ -0,0 +1,122 @@
1
+package com.dmdirc.addons.jpq;
2
+
3
+import com.dmdirc.DMDircMBassador;
4
+import com.dmdirc.config.prefs.PreferencesCategory;
5
+import com.dmdirc.config.prefs.PreferencesSetting;
6
+import com.dmdirc.config.prefs.PreferencesType;
7
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
8
+import com.dmdirc.events.ServerConnectedEvent;
9
+import com.dmdirc.events.ServerDisconnectedEvent;
10
+import com.dmdirc.interfaces.Connection;
11
+import com.dmdirc.interfaces.ConnectionManager;
12
+import com.dmdirc.interfaces.GroupChat;
13
+import com.dmdirc.interfaces.GroupChatManager;
14
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
15
+
16
+import com.google.common.collect.Lists;
17
+
18
+import org.junit.Before;
19
+import org.junit.Test;
20
+import org.junit.runner.RunWith;
21
+import org.mockito.ArgumentCaptor;
22
+import org.mockito.Captor;
23
+import org.mockito.Mock;
24
+import org.mockito.runners.MockitoJUnitRunner;
25
+
26
+import static org.junit.Assert.assertEquals;
27
+import static org.mockito.Matchers.anyString;
28
+import static org.mockito.Mockito.verify;
29
+import static org.mockito.Mockito.when;
30
+
31
+@RunWith(MockitoJUnitRunner.class)
32
+public class JPQManagerTest {
33
+
34
+    @Mock private DMDircMBassador eventBus;
35
+    @Mock private ConnectionManager connectionManager;
36
+    @Mock private GroupChatHandlerFactory groupChatHandlerFactory;
37
+    @Mock private ServerConnectedEvent serverConnectedEvent;
38
+    @Mock private ServerDisconnectedEvent serverDisconnectedEvent;
39
+    @Mock private GroupChatPrefsRequestedEvent groupChatPrefsRequestedEvent;
40
+    @Mock private PreferencesCategory prefsCategory;
41
+    @Mock private AggregateConfigProvider configProvider;
42
+    @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
43
+    @Mock private Connection connection1;
44
+    @Mock private Connection connection2;
45
+    @Mock private GroupChat groupChat1;
46
+    @Mock private GroupChat groupChat2;
47
+    @Mock private GroupChat groupChat3;
48
+    @Mock private GroupChat groupChat4;
49
+    @Mock private GroupChatHandler groupChatHandler1;
50
+    @Mock private GroupChatHandler groupChatHandler2;
51
+    @Mock private GroupChatHandler groupChatHandler3;
52
+    @Mock private GroupChatHandler groupChatHandler4;
53
+    @Mock private GroupChatManager groupChatManager1;
54
+    @Mock private GroupChatManager groupChatManager2;
55
+    private JPQManager instance;
56
+
57
+    @Before
58
+    public void setUp() throws Exception {
59
+        when(groupChatHandlerFactory.get(groupChat1)).thenReturn(groupChatHandler1);
60
+        when(groupChatHandlerFactory.get(groupChat2)).thenReturn(groupChatHandler2);
61
+        when(groupChatHandlerFactory.get(groupChat3)).thenReturn(groupChatHandler3);
62
+        when(groupChatHandlerFactory.get(groupChat4)).thenReturn(groupChatHandler4);
63
+        when(serverConnectedEvent.getConnection()).thenReturn(connection1);
64
+        when(serverDisconnectedEvent.getConnection()).thenReturn(connection1);
65
+        when(connection1.getGroupChatManager()).thenReturn(groupChatManager1);
66
+        when(connection2.getGroupChatManager()).thenReturn(groupChatManager2);
67
+        when(groupChatManager1.getChannels())
68
+                .thenReturn(Lists.newArrayList(groupChat1, groupChat2));
69
+        when(groupChatManager2.getChannels())
70
+                .thenReturn(Lists.newArrayList(groupChat3, groupChat4));
71
+        when(connectionManager.getConnections())
72
+                .thenReturn(Lists.newArrayList(connection1, connection2));
73
+        when(groupChatPrefsRequestedEvent.getCategory()).thenReturn(prefsCategory);
74
+        when(groupChatPrefsRequestedEvent.getConfig()).thenReturn(configProvider);
75
+        when(configProvider.getOption(anyString(), anyString())).thenReturn("true");
76
+        instance = new JPQManager("domain", connectionManager, groupChatHandlerFactory, eventBus);
77
+    }
78
+
79
+    @Test
80
+    public void testLoad() throws Exception {
81
+        instance.load();
82
+        verify(groupChatHandlerFactory).get(groupChat1);
83
+        verify(groupChatHandlerFactory).get(groupChat2);
84
+        verify(groupChatHandlerFactory).get(groupChat3);
85
+        verify(groupChatHandlerFactory).get(groupChat4);
86
+        verify(eventBus).subscribe(instance);
87
+    }
88
+
89
+    @Test
90
+    public void testUnload() throws Exception {
91
+        instance.load();
92
+        instance.unload();
93
+        verify(groupChatHandler1).unload();
94
+        verify(groupChatHandler2).unload();
95
+        verify(groupChatHandler3).unload();
96
+        verify(groupChatHandler4).unload();
97
+        verify(eventBus).unsubscribe(instance);
98
+    }
99
+
100
+    @Test
101
+    public void testHandlePrefsEvent() throws Exception {
102
+        instance.handleGroupChatPrefs(groupChatPrefsRequestedEvent);
103
+        verify(prefsCategory).addSetting(preferencesSetting.capture());
104
+        assertEquals(PreferencesType.BOOLEAN, preferencesSetting.getValue().getType());
105
+        assertEquals("true", preferencesSetting.getValue().getValue());
106
+    }
107
+
108
+    @Test
109
+    public void testHandleConnectionAdded() throws Exception {
110
+        instance.handleConnectionAdded(serverConnectedEvent);
111
+        verify(groupChatHandlerFactory).get(groupChat1);
112
+        verify(groupChatHandlerFactory).get(groupChat2);
113
+    }
114
+
115
+    @Test
116
+    public void testHandleConnectionRemoved() throws Exception {
117
+        instance.handleConnectionAdded(serverConnectedEvent);
118
+        instance.handleConnectionRemoved(serverDisconnectedEvent);
119
+        verify(groupChatHandler1).unload();
120
+        verify(groupChatHandler2).unload();
121
+    }
122
+}

+ 34
- 1
jpq/test/com/dmdirc/addons/jpq/JPQPluginTest.java View File

@@ -22,14 +22,47 @@
22 22
 
23 23
 package com.dmdirc.addons.jpq;
24 24
 
25
+import com.dmdirc.plugins.PluginInfo;
26
+
27
+import org.junit.Before;
25 28
 import org.junit.Test;
26 29
 import org.junit.runner.RunWith;
30
+import org.mockito.Mock;
27 31
 import org.mockito.runners.MockitoJUnitRunner;
28 32
 
33
+import dagger.ObjectGraph;
34
+
35
+import static org.mockito.Matchers.any;
36
+import static org.mockito.Mockito.verify;
37
+import static org.mockito.Mockito.when;
38
+
29 39
 @RunWith(MockitoJUnitRunner.class)
30 40
 public class JPQPluginTest {
31 41
 
42
+    @Mock private ObjectGraph objectGraph;
43
+    @Mock private PluginInfo pluginInfo;
44
+    @Mock private JPQManager manager;
45
+    private JPQPlugin instance;
46
+
47
+    @Before
48
+    public void setup() {
49
+        when(objectGraph.plus(any(JPQModule.class))).thenReturn(objectGraph);
50
+        when(objectGraph.get(JPQManager.class)).thenReturn(manager);
51
+        instance = new JPQPlugin();
52
+    }
53
+
54
+    @Test
55
+    public void testLoad() {
56
+        instance.load(pluginInfo, objectGraph);
57
+        instance.onLoad();
58
+        verify(manager).load();
59
+    }
60
+
32 61
     @Test
33
-    public void testSomething() {}
62
+    public void testUnload() {
63
+        instance.load(pluginInfo, objectGraph);
64
+        instance.onUnload();
65
+        verify(manager).unload();
66
+    }
34 67
 
35 68
 }

Loading…
Cancel
Save