Parcourir la source

Move invite handling into a separate class.

pull/404/head
Chris Smith il y a 9 ans
Parent
révision
4349f58de7

+ 98
- 0
src/com/dmdirc/InviteManagerImpl.java Voir le fichier

@@ -0,0 +1,98 @@
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;
24
+
25
+import com.dmdirc.events.ServerInviteExpiredEvent;
26
+import com.dmdirc.interfaces.Connection;
27
+import com.dmdirc.interfaces.InviteManager;
28
+import com.dmdirc.parser.common.ChannelJoinRequest;
29
+
30
+import java.util.ArrayList;
31
+import java.util.Collections;
32
+import java.util.List;
33
+import java.util.concurrent.CopyOnWriteArrayList;
34
+
35
+/**
36
+ * Manages invites on a {@link Connection}.
37
+ */
38
+public class InviteManagerImpl implements InviteManager {
39
+
40
+    /** A list of outstanding invites. */
41
+    private final List<Invite> invites = new CopyOnWriteArrayList<>();
42
+
43
+    /** The connection this manager works on. */
44
+    private final Connection connection;
45
+
46
+    public InviteManagerImpl(final Connection connection) {
47
+        this.connection = connection;
48
+    }
49
+
50
+    @Override
51
+    public void addInvite(final Invite invite) {
52
+        invites.stream()
53
+                .filter(oldInvite -> oldInvite.getChannel().equals(invite.getChannel()))
54
+                .forEach(this::removeInvite);
55
+
56
+        invites.add(invite);
57
+    }
58
+
59
+    @Override
60
+    public void acceptInvites(final Invite... invites) {
61
+        final ChannelJoinRequest[] requests = new ChannelJoinRequest[invites.length];
62
+
63
+        for (int i = 0; i < invites.length; i++) {
64
+            requests[i] = new ChannelJoinRequest(invites[i].getChannel());
65
+        }
66
+
67
+        connection.getGroupChatManager().join(requests);
68
+    }
69
+
70
+    @Override
71
+    public void acceptInvites() {
72
+        acceptInvites(invites.toArray(new Invite[invites.size()]));
73
+    }
74
+
75
+    @Override
76
+    public void removeInvites(final String channel) {
77
+        new ArrayList<>(invites).stream().filter(invite -> invite.getChannel().equals(channel))
78
+                .forEach(this::removeInvite);
79
+    }
80
+
81
+    @Override
82
+    public void removeInvites() {
83
+        new ArrayList<>(invites).forEach(this::removeInvite);
84
+    }
85
+
86
+    @Override
87
+    public void removeInvite(final Invite invite) {
88
+        invites.remove(invite);
89
+        connection.getWindowModel().getEventBus().publishAsync(
90
+                new ServerInviteExpiredEvent(connection, invite));
91
+    }
92
+
93
+    @Override
94
+    public List<Invite> getInvites() {
95
+        return Collections.unmodifiableList(invites);
96
+    }
97
+
98
+}

+ 7
- 63
src/com/dmdirc/Server.java Voir le fichier

@@ -30,7 +30,6 @@ import com.dmdirc.events.ServerConnectErrorEvent;
30 30
 import com.dmdirc.events.ServerConnectedEvent;
31 31
 import com.dmdirc.events.ServerConnectingEvent;
32 32
 import com.dmdirc.events.ServerDisconnectedEvent;
33
-import com.dmdirc.events.ServerInviteExpiredEvent;
34 33
 import com.dmdirc.interfaces.Connection;
35 34
 import com.dmdirc.interfaces.GroupChatManager;
36 35
 import com.dmdirc.interfaces.InviteManager;
@@ -40,7 +39,6 @@ import com.dmdirc.interfaces.config.ConfigProvider;
40 39
 import com.dmdirc.interfaces.config.ConfigProviderMigrator;
41 40
 import com.dmdirc.interfaces.config.IdentityFactory;
42 41
 import com.dmdirc.logger.ErrorLevel;
43
-import com.dmdirc.parser.common.ChannelJoinRequest;
44 42
 import com.dmdirc.parser.common.DefaultStringConverter;
45 43
 import com.dmdirc.parser.common.IgnoreList;
46 44
 import com.dmdirc.parser.common.ParserError;
@@ -97,7 +95,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
97 95
  * The Server class represents the client's view of a server. It maintains a list of all channels,
98 96
  * queries, etc, and handles parser callbacks pertaining to the server.
99 97
  */
100
-public class Server extends FrameContainer implements Connection, InviteManager {
98
+public class Server extends FrameContainer implements Connection {
101 99
 
102 100
     private static final Logger LOG = LoggerFactory.getLogger(Server.class);
103 101
     /** The name of the general domain. */
@@ -106,7 +104,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
106 104
     /** Manager of group chats. */
107 105
     private final GroupChatManagerImpl groupChatManager;
108 106
     /** Manager of invites. */
109
-    private final InviteManager inviteManager = this;
107
+    private final InviteManager inviteManager;
110 108
     /** Open query windows on the server. */
111 109
     private final Map<String, Query> queries = new ConcurrentSkipListMap<>();
112 110
     /** The user manager to retrieve users from. */
@@ -148,8 +146,6 @@ public class Server extends FrameContainer implements Connection, InviteManager
148 146
     private Optional<String> awayMessage;
149 147
     /** Our event handler. */
150 148
     private final ServerEventHandler eventHandler;
151
-    /** A list of outstanding invites. */
152
-    private final List<Invite> invites = new ArrayList<>();
153 149
     /** Our ignore list. */
154 150
     private final IgnoreList ignoreList = new IgnoreList();
155 151
     /** Our string convertor. */
@@ -221,6 +217,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
221 217
         this.messageEncoderFactory = messageEncoderFactory;
222 218
         this.userManager = userManager;
223 219
         this.groupChatManager = groupChatManagerFactory.create(this, executorService);
220
+        this.inviteManager = new InviteManagerImpl(this);
224 221
 
225 222
         awayMessage = Optional.empty();
226 223
         eventHandler = new ServerEventHandler(this, groupChatManager, eventBus);
@@ -349,7 +346,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
349 346
             doCallbacks();
350 347
 
351 348
             updateAwayState(Optional.empty());
352
-            removeInvites();
349
+            inviteManager.removeInvites();
353 350
 
354 351
             newParser.connect();
355 352
             if (newParser instanceof ThreadedParser) {
@@ -412,7 +409,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
412 409
                 if (parser.isPresent()) {
413 410
                     myState.transition(ServerState.DISCONNECTING);
414 411
 
415
-                    removeInvites();
412
+                    inviteManager.removeInvites();
416 413
                     updateIcon();
417 414
 
418 415
                     parser.get().disconnect(reason);
@@ -756,7 +753,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
756 753
 
757 754
         groupChatManager.closeAll();
758 755
         closeQueries();
759
-        removeInvites();
756
+        inviteManager.removeInvites();
760 757
 
761 758
         super.close();
762 759
     }
@@ -882,7 +879,7 @@ public class Server extends FrameContainer implements Connection, InviteManager
882 879
                 closeQueries();
883 880
             }
884 881
 
885
-            removeInvites();
882
+            inviteManager.removeInvites();
886 883
             updateAwayState(Optional.empty());
887 884
 
888 885
             if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
@@ -1015,59 +1012,6 @@ public class Server extends FrameContainer implements Connection, InviteManager
1015 1012
         return identityFactory.createNetworkConfig(getNetwork());
1016 1013
     }
1017 1014
 
1018
-    @Override
1019
-    public void addInvite(final Invite invite) {
1020
-        synchronized (invites) {
1021
-            new ArrayList<>(invites).stream()
1022
-                    .filter(oldInvite -> oldInvite.getChannel().equals(invite.getChannel()))
1023
-                    .forEach(this::removeInvite);
1024
-
1025
-            invites.add(invite);
1026
-        }
1027
-    }
1028
-
1029
-    @Override
1030
-    public void acceptInvites(final Invite... invites) {
1031
-        final ChannelJoinRequest[] requests = new ChannelJoinRequest[invites.length];
1032
-
1033
-        for (int i = 0; i < invites.length; i++) {
1034
-            requests[i] = new ChannelJoinRequest(invites[i].getChannel());
1035
-        }
1036
-
1037
-        getGroupChatManager().join(requests);
1038
-    }
1039
-
1040
-    @Override
1041
-    public void acceptInvites() {
1042
-        synchronized (invites) {
1043
-            acceptInvites(invites.toArray(new Invite[invites.size()]));
1044
-        }
1045
-    }
1046
-
1047
-    @Override
1048
-    public void removeInvites(final String channel) {
1049
-        new ArrayList<>(invites).stream().filter(invite -> invite.getChannel().equals(channel))
1050
-                .forEach(this::removeInvite);
1051
-    }
1052
-
1053
-    @Override
1054
-    public void removeInvites() {
1055
-        new ArrayList<>(invites).forEach(this::removeInvite);
1056
-    }
1057
-
1058
-    @Override
1059
-    public void removeInvite(final Invite invite) {
1060
-        synchronized (invites) {
1061
-            invites.remove(invite);
1062
-            getEventBus().publishAsync(new ServerInviteExpiredEvent(this, invite));
1063
-        }
1064
-    }
1065
-
1066
-    @Override
1067
-    public List<Invite> getInvites() {
1068
-        return Collections.unmodifiableList(invites);
1069
-    }
1070
-
1071 1015
     @Override
1072 1016
     public void updateAwayState(final Optional<String> message) {
1073 1017
         checkNotNull(message);

+ 2
- 1
src/com/dmdirc/ServerEventHandler.java Voir le fichier

@@ -500,7 +500,8 @@ public class ServerEventHandler extends EventHandler implements
500 500
             final String channel) {
501 501
         checkParser(parser);
502 502
 
503
-        owner.getInviteManager().addInvite(new Invite(owner, channel, owner.getUser(userHost)));
503
+        owner.getInviteManager().addInvite(
504
+                new Invite(owner.getInviteManager(), channel, owner.getUser(userHost)));
504 505
         final ServerInviteReceivedEvent event = new ServerInviteReceivedEvent(owner,
505 506
                 owner.getUser(userHost), channel);
506 507
         final String format = EventUtils.postDisplayable(eventBus, event, "inviteReceived");

+ 75
- 0
test/com/dmdirc/InviteManagerImplTest.java Voir le fichier

@@ -0,0 +1,75 @@
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;
24
+
25
+import com.dmdirc.interfaces.Connection;
26
+import com.dmdirc.interfaces.User;
27
+
28
+import org.junit.Before;
29
+import org.junit.Test;
30
+import org.junit.runner.RunWith;
31
+import org.mockito.Mock;
32
+import org.mockito.runners.MockitoJUnitRunner;
33
+
34
+import static org.junit.Assert.assertEquals;
35
+import static org.mockito.Mockito.when;
36
+
37
+@RunWith(MockitoJUnitRunner.class)
38
+public class InviteManagerImplTest {
39
+
40
+    @Mock private Connection connection;
41
+    @Mock private FrameContainer frameContainer;
42
+    @Mock private DMDircMBassador eventBus;
43
+    @Mock private User user;
44
+
45
+    private InviteManagerImpl inviteManager;
46
+
47
+    @Before
48
+    public void setup() {
49
+        inviteManager = new InviteManagerImpl(connection);
50
+
51
+        when(connection.getWindowModel()).thenReturn(frameContainer);
52
+        when(frameContainer.getEventBus()).thenReturn(eventBus);
53
+    }
54
+
55
+    @Test
56
+    public void testDuplicateInviteRemoval() {
57
+        inviteManager.addInvite(new Invite(inviteManager, "#chan1", user));
58
+        inviteManager.addInvite(new Invite(inviteManager, "#chan1", user));
59
+
60
+        assertEquals(1, inviteManager.getInvites().size());
61
+    }
62
+
63
+    @Test
64
+    public void testRemoveInvites() {
65
+        inviteManager.addInvite(new Invite(inviteManager, "#chan1", user));
66
+        inviteManager.addInvite(new Invite(inviteManager, "#chan2", user));
67
+
68
+        inviteManager.removeInvites("#chan1");
69
+        assertEquals(1, inviteManager.getInvites().size());
70
+
71
+        inviteManager.removeInvites("#chan2");
72
+        assertEquals(0, inviteManager.getInvites().size());
73
+    }
74
+
75
+}

+ 0
- 103
test/com/dmdirc/ServerTest.java Voir le fichier

@@ -22,95 +22,12 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
-import com.dmdirc.commandparser.CommandType;
26
-import com.dmdirc.commandparser.parsers.CommandParser;
27
-import com.dmdirc.config.ConfigBinder;
28
-import com.dmdirc.config.profiles.Profile;
29
-import com.dmdirc.interfaces.User;
30
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
-import com.dmdirc.interfaces.config.ConfigProvider;
32
-import com.dmdirc.interfaces.config.ConfigProviderMigrator;
33
-import com.dmdirc.interfaces.config.IdentityFactory;
34
-import com.dmdirc.ui.input.TabCompleter;
35
-import com.dmdirc.ui.input.TabCompleterFactory;
36
-import com.dmdirc.ui.messages.BackBuffer;
37
-import com.dmdirc.ui.messages.BackBufferFactory;
38
-import com.dmdirc.ui.messages.sink.MessageSinkManager;
39
-import com.dmdirc.util.URLBuilder;
40
-
41
-import java.net.URI;
42
-import java.util.concurrent.ScheduledExecutorService;
43
-
44
-import org.junit.Before;
45 25
 import org.junit.Test;
46
-import org.mockito.Matchers;
47
-import org.mockito.Mock;
48
-import org.mockito.MockitoAnnotations;
49 26
 
50 27
 import static org.junit.Assert.assertEquals;
51
-import static org.mockito.Matchers.any;
52
-import static org.mockito.Matchers.anyString;
53
-import static org.mockito.Matchers.eq;
54
-import static org.mockito.Mockito.when;
55 28
 
56 29
 public class ServerTest {
57 30
 
58
-    @Mock private Profile profile;
59
-    @Mock private AggregateConfigProvider configManager;
60
-    @Mock private ConfigBinder configBinder;
61
-    @Mock private ConfigProvider userConfig;
62
-    @Mock private ConfigProviderMigrator configMigrator;
63
-    @Mock private CommandParser commandParser;
64
-    @Mock private ParserFactory parserFactory;
65
-    @Mock private IdentityFactory identityFactory;
66
-    @Mock private TabCompleterFactory tabCompleterFactory;
67
-    @Mock private TabCompleter tabCompleter;
68
-    @Mock private MessageSinkManager messageSinkManager;
69
-    @Mock private GroupChatManagerImplFactory groupChatManagerImplFactory;
70
-    @Mock private GroupChatManagerImpl groupChatManager;
71
-    @Mock private QueryFactory queryFactory;
72
-    @Mock private URLBuilder urlBuilder;
73
-    @Mock private DMDircMBassador eventBus;
74
-    @Mock private ScheduledExecutorService executorService;
75
-    @Mock private MessageEncoderFactory messageEncoderFactory;
76
-    @Mock private BackBufferFactory backBufferFactory;
77
-    @Mock private BackBuffer backBuffer;
78
-    @Mock private UserManager userManager;
79
-    @Mock private User user;
80
-
81
-    private Server server;
82
-
83
-    @Before
84
-    public void setUp() throws Exception {
85
-        MockitoAnnotations.initMocks(this);
86
-        when(configManager.getOptionInt(anyString(), anyString())).thenReturn(Integer.MAX_VALUE);
87
-        when(configManager.getBinder()).thenReturn(configBinder);
88
-        when(configMigrator.getConfigProvider()).thenReturn(configManager);
89
-        when(tabCompleterFactory.getTabCompleter(eq(configManager),
90
-                Matchers.<CommandType>anyVararg())).thenReturn(tabCompleter);
91
-        when(backBufferFactory.getBackBuffer(any())).thenReturn(backBuffer);
92
-        when(groupChatManagerImplFactory.create(any(), any())).thenReturn(groupChatManager);
93
-
94
-        server = new Server(
95
-                configMigrator,
96
-                commandParser,
97
-                parserFactory,
98
-                tabCompleterFactory,
99
-                identityFactory,
100
-                messageSinkManager,
101
-                queryFactory,
102
-                urlBuilder,
103
-                eventBus,
104
-                messageEncoderFactory,
105
-                userConfig,
106
-                groupChatManagerImplFactory,
107
-                executorService,
108
-                new URI("irc-test://255.255.255.255"),
109
-                profile,
110
-                backBufferFactory,
111
-                userManager);
112
-    }
113
-
114 31
     @Test
115 32
     public void testGetNetworkFromServerName() {
116 33
         final String[][] tests = {
@@ -128,24 +45,4 @@ public class ServerTest {
128 45
         }
129 46
     }
130 47
 
131
-    @Test
132
-    public void testDuplicateInviteRemoval() {
133
-        server.addInvite(new Invite(server, "#chan1", user));
134
-        server.addInvite(new Invite(server, "#chan1", user));
135
-
136
-        assertEquals(1, server.getInvites().size());
137
-    }
138
-
139
-    @Test
140
-    public void testRemoveInvites() {
141
-        server.addInvite(new Invite(server, "#chan1", user));
142
-        server.addInvite(new Invite(server, "#chan2", user));
143
-
144
-        server.removeInvites("#chan1");
145
-        assertEquals(1, server.getInvites().size());
146
-
147
-        server.removeInvites("#chan2");
148
-        assertEquals(0, server.getInvites().size());
149
-    }
150
-
151 48
 }

Chargement…
Annuler
Enregistrer