Kaynağa Gözat

Add getGlobal/ConnectionCommands method.

Also test.

Change-Id: Iea8cfe088b98782d7e7ccbdb3d862ab47dbe6273
Reviewed-on: http://gerrit.dmdirc.com/3955
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 yıl önce
ebeveyn
işleme
201a49c77d

+ 10
- 0
src/com/dmdirc/commandparser/auto/AutoCommand.java Dosyayı Görüntüle

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.auto;
24 24
 
25
+import com.google.common.base.Objects;
25 26
 import com.google.common.base.Optional;
26 27
 
27 28
 /**
@@ -66,4 +67,13 @@ public class AutoCommand {
66 67
         return response;
67 68
     }
68 69
 
70
+    public String toString() {
71
+        return Objects.toStringHelper(this)
72
+                .add("Connection", server)
73
+                .add("Network", network)
74
+                .add("Profile", profile)
75
+                .add("Response", response)
76
+                .toString();
77
+    }
78
+
69 79
 }

+ 43
- 0
src/com/dmdirc/commandparser/auto/AutoCommandManager.java Dosyayı Görüntüle

@@ -24,6 +24,9 @@ package com.dmdirc.commandparser.auto;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 
27
+import com.google.common.base.Predicate;
28
+import com.google.common.collect.Sets;
29
+
27 30
 import java.util.Collections;
28 31
 import java.util.Map;
29 32
 import java.util.Set;
@@ -122,4 +125,44 @@ public class AutoCommandManager {
122 125
         return Collections.unmodifiableSet(autoCommands.keySet());
123 126
     }
124 127
 
128
+    /**
129
+     * Gets a set of all registered global auto commands.
130
+     *
131
+     * @return The set of all known global auto commands.
132
+     */
133
+    public Set<AutoCommand> getGlobalAutoCommands() {
134
+        return Sets.filter(getAutoCommands(), new GlobalCommandPredicate());
135
+    }
136
+
137
+    /**
138
+     * Gets a set of all registered connection auto commands.
139
+     *
140
+     * @return The set of all known connection auto commands.
141
+     */
142
+    public Set<AutoCommand> getConnectionAutoCommands() {
143
+        return Sets.filter(getAutoCommands(), new ConnectionCommandPredicate());
144
+    }
145
+
146
+    /**
147
+     * Predicate to check if an {@link AutoCommand} is global.
148
+     */
149
+    private static class GlobalCommandPredicate implements Predicate<AutoCommand> {
150
+
151
+        @Override
152
+        public boolean apply(final AutoCommand autoCommand) {
153
+            return !autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent();
154
+        }
155
+    }
156
+
157
+    /**
158
+     * Predicate to check if an {@link AutoCommand} relates to a connection.
159
+     */
160
+    private static class ConnectionCommandPredicate implements Predicate<AutoCommand> {
161
+
162
+        @Override
163
+        public boolean apply(final AutoCommand autoCommand) {
164
+            return autoCommand.getServer().isPresent() || autoCommand.getNetwork().isPresent();
165
+        }
166
+    }
167
+
125 168
 }

+ 107
- 0
test/com/dmdirc/commandparser/auto/AutoCommandManagerTest.java Dosyayı Görüntüle

@@ -0,0 +1,107 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.commandparser.auto;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.GlobalWindow;
27
+import com.dmdirc.commandparser.parsers.GlobalCommandParser;
28
+import com.dmdirc.interfaces.CommandController;
29
+
30
+import com.google.common.base.Optional;
31
+
32
+import org.junit.Before;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import static junit.framework.TestCase.assertFalse;
39
+import static org.junit.Assert.assertEquals;
40
+import static org.junit.Assert.assertTrue;
41
+
42
+@RunWith(MockitoJUnitRunner.class)
43
+public class AutoCommandManagerTest {
44
+
45
+    @Mock GlobalCommandParser globalCommandParser;
46
+    @Mock GlobalWindow globalWindow;
47
+    @Mock CommandController commandController;
48
+    @Mock DMDircMBassador eventBus;
49
+    private AutoCommandManager autoCommandManager;
50
+
51
+    @Before
52
+    public void setup() {
53
+        final AutoCommandHandlerFactory factory = new AutoCommandHandlerFactory(
54
+                globalCommandParser, globalWindow, commandController);
55
+        autoCommandManager = new AutoCommandManager(eventBus, factory);
56
+    }
57
+
58
+    @Test
59
+    public void testRemoveAutoCommand() {
60
+        final AutoCommand global = new AutoCommand(Optional.<String>absent(),
61
+                Optional.<String>absent(), Optional.<String>absent(), "DO STUFF");
62
+        final AutoCommand connection = new AutoCommand(Optional.<String>absent(),
63
+                Optional.fromNullable("Quakenet"), Optional.<String>absent(), "DO STUFF");
64
+        autoCommandManager.addAutoCommand(global);
65
+        autoCommandManager.addAutoCommand(connection);
66
+        assertEquals(2, autoCommandManager.getAutoCommands().size());
67
+        assertTrue(autoCommandManager.getAutoCommands().contains(connection));
68
+        autoCommandManager.removeAutoCommand(connection);
69
+        assertEquals(1, autoCommandManager.getAutoCommands().size());
70
+        assertFalse(autoCommandManager.getAutoCommands().contains(connection));
71
+    }
72
+
73
+    @Test
74
+    public void testGetAutoCommands() {
75
+        final AutoCommand global = new AutoCommand(Optional.<String>absent(),
76
+                Optional.<String>absent(), Optional.<String>absent(), "DO STUFF");
77
+        final AutoCommand connection = new AutoCommand(Optional.<String>absent(),
78
+                Optional.fromNullable("Quakenet"), Optional.<String>absent(), "DO STUFF");
79
+        autoCommandManager.addAutoCommand(global);
80
+        autoCommandManager.addAutoCommand(connection);
81
+        assertEquals(2, autoCommandManager.getAutoCommands().size());
82
+    }
83
+
84
+    @Test
85
+    public void testGetGlobalAutoCommands() {
86
+        final AutoCommand global = new AutoCommand(Optional.<String>absent(),
87
+                Optional.<String>absent(), Optional.<String>absent(), "DO STUFF");
88
+        final AutoCommand connection = new AutoCommand(Optional.<String>absent(),
89
+                Optional.fromNullable("Quakenet"), Optional.<String>absent(), "DO STUFF");
90
+        autoCommandManager.addAutoCommand(global);
91
+        autoCommandManager.addAutoCommand(connection);
92
+        assertEquals(1, autoCommandManager.getGlobalAutoCommands().size());
93
+        assertTrue(autoCommandManager.getGlobalAutoCommands().contains(global));
94
+    }
95
+
96
+    @Test
97
+    public void testGetConnectionAutoCommands() {
98
+        final AutoCommand global = new AutoCommand(Optional.<String>absent(),
99
+                Optional.<String>absent(), Optional.<String>absent(), "DO STUFF");
100
+        final AutoCommand connection = new AutoCommand(Optional.<String>absent(),
101
+                Optional.fromNullable("Quakenet"), Optional.<String>absent(), "DO STUFF");
102
+        autoCommandManager.addAutoCommand(global);
103
+        autoCommandManager.addAutoCommand(connection);
104
+        assertEquals(1, autoCommandManager.getConnectionAutoCommands().size());
105
+        assertTrue(autoCommandManager.getConnectionAutoCommands().contains(connection));
106
+    }
107
+}

Loading…
İptal
Kaydet