Procházet zdrojové kódy

Add AutoCommandType.

Remove existing auto commands dialog model.
pull/154/head
Greg Holmes před 9 roky
rodič
revize
27aed2d417

+ 15
- 0
src/com/dmdirc/commandparser/auto/AutoCommand.java Zobrazit soubor

@@ -44,6 +44,8 @@ public class AutoCommand {
44 44
     private final Optional<String> profile;
45 45
     /** The commands to execute. */
46 46
     private final String response;
47
+    /** The type of auto command. */
48
+    private final AutoCommandType type;
47 49
 
48 50
     public AutoCommand(
49 51
             final Optional<String> server,
@@ -54,6 +56,15 @@ public class AutoCommand {
54 56
         this.network = network;
55 57
         this.profile = profile;
56 58
         this.response = response;
59
+        if (!server.isPresent() && !network.isPresent()) {
60
+            type = AutoCommandType.GLOBAL;
61
+        } else if (server.isPresent() && !network.isPresent()) {
62
+            type = AutoCommandType.SERVER;
63
+        } else if (!server.isPresent()) {
64
+            type = AutoCommandType.NETWORK;
65
+        } else {
66
+            type = AutoCommandType.UNKNOWN;
67
+        }
57 68
     }
58 69
 
59 70
     public Optional<String> getServer() {
@@ -73,6 +84,10 @@ public class AutoCommand {
73 84
         return response;
74 85
     }
75 86
 
87
+    public AutoCommandType getType() {
88
+        return type;
89
+    }
90
+
76 91
     @Override
77 92
     public boolean equals(final Object object) {
78 93
         if (object == null) {

+ 4
- 4
src/com/dmdirc/commandparser/auto/AutoCommandHandler.java Zobrazit soubor

@@ -85,16 +85,16 @@ public class AutoCommandHandler {
85 85
     private boolean appliesToServer(final String network, final String server,
86 86
             final String profile) {
87 87
         return !isGlobalCommand()
88
-                && matchesIfPreset(autoCommand.getNetwork(), network)
89
-                && matchesIfPreset(autoCommand.getServer(), server)
90
-                && matchesIfPreset(autoCommand.getProfile(), profile);
88
+                && matchesIfPresent(autoCommand.getNetwork(), network)
89
+                && matchesIfPresent(autoCommand.getServer(), server)
90
+                && matchesIfPresent(autoCommand.getProfile(), profile);
91 91
     }
92 92
 
93 93
     private boolean isGlobalCommand() {
94 94
         return !autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent();
95 95
     }
96 96
 
97
-    private boolean matchesIfPreset(final Optional<String> target, final String value) {
97
+    private boolean matchesIfPresent(final Optional<String> target, final String value) {
98 98
         return !target.isPresent() || target.get().equalsIgnoreCase(value);
99 99
     }
100 100
 

+ 7
- 9
src/com/dmdirc/commandparser/auto/AutoCommandManager.java Zobrazit soubor

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.commandparser.auto;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.config.profiles.Profile;
27 27
 
28 28
 import java.util.Collections;
29 29
 import java.util.Map;
@@ -166,13 +166,12 @@ public class AutoCommandManager {
166 166
      *
167 167
      * @return The set of all known connection auto commands.
168 168
      */
169
-    public Set<AutoCommand> getConnectionAutoCommands(final String server,
170
-            final ConfigProvider profile) {
169
+    public Set<AutoCommand> getConnectionAutoCommands(final String server, final Profile profile) {
171 170
         return getAutoCommands().parallelStream()
172 171
                 .filter(c -> c.getServer().isPresent() &&
173 172
                         c.getServer().get().equalsIgnoreCase(server))
174
-                .filter(c -> c.getProfile().isPresent() && c.getProfile().get().equalsIgnoreCase(
175
-                        profile.getName()))
173
+                .filter(c -> c.getProfile().isPresent()
174
+                        && c.getProfile().get().equalsIgnoreCase(profile.getName()))
176 175
                 .collect(Collectors.toSet());
177 176
     }
178 177
 
@@ -199,13 +198,12 @@ public class AutoCommandManager {
199 198
      *
200 199
      * @return The set of all known connection auto commands.
201 200
      */
202
-    public Set<AutoCommand> getNetworkAutoCommands(final String network,
203
-            final ConfigProvider profile) {
201
+    public Set<AutoCommand> getNetworkAutoCommands(final String network, final Profile profile) {
204 202
         return getAutoCommands().parallelStream()
205 203
                 .filter(c -> c.getNetwork().isPresent() &&
206 204
                         c.getNetwork().get().equalsIgnoreCase(network))
207
-                .filter(c -> c.getProfile().isPresent() && c.getProfile().get().equalsIgnoreCase
208
-                        (profile.getName()))
205
+                .filter(c -> c.getProfile().isPresent()
206
+                        && c.getProfile().get().equalsIgnoreCase(profile.getName()))
209 207
                 .collect(Collectors.toSet());
210 208
     }
211 209
 

src/com/dmdirc/ui/core/autocommands/AutoCommandType.java → src/com/dmdirc/commandparser/auto/AutoCommandType.java Zobrazit soubor

@@ -20,10 +20,14 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.ui.core.autocommands;
23
+package com.dmdirc.commandparser.auto;
24 24
 
25
+/**
26
+ * Specifies the type of auto command.
27
+ */
25 28
 public enum AutoCommandType {
29
+    SERVER,
30
+    NETWORK,
26 31
     GLOBAL,
27
-    CONNECTION,
28
-    ALL
32
+    UNKNOWN
29 33
 }

+ 1
- 1
src/com/dmdirc/interfaces/ui/AutoCommandsModel.java Zobrazit soubor

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.interfaces.ui;
24 24
 
25
-import com.dmdirc.ui.core.autocommands.AutoCommandType;
25
+import com.dmdirc.commandparser.auto.AutoCommandType;
26 26
 import com.dmdirc.ui.core.autocommands.MutableAutoCommand;
27 27
 
28 28
 import java.util.Collection;

+ 0
- 239
src/com/dmdirc/ui/core/autocommands/CoreAutoCommandsModel.java Zobrazit soubor

@@ -1,239 +0,0 @@
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.ui.core.autocommands;
24
-
25
-import com.dmdirc.commandparser.auto.AutoCommand;
26
-import com.dmdirc.commandparser.auto.AutoCommandManager;
27
-import com.dmdirc.interfaces.ui.AutoCommandsModel;
28
-import com.dmdirc.interfaces.ui.AutoCommandsModelListener;
29
-import com.dmdirc.util.collections.ListenerList;
30
-
31
-import com.google.common.collect.Sets;
32
-
33
-import java.util.Collection;
34
-import java.util.Collections;
35
-import java.util.Optional;
36
-import java.util.Set;
37
-
38
-import javax.annotation.Nonnull;
39
-import javax.inject.Inject;
40
-
41
-import static com.google.common.base.Preconditions.checkArgument;
42
-import static com.google.common.base.Preconditions.checkNotNull;
43
-import static com.google.common.base.Preconditions.checkState;
44
-
45
-public class CoreAutoCommandsModel implements AutoCommandsModel {
46
-
47
-    private final AutoCommandManager manager;
48
-    private final Set<AutoCommand> originalCommands;
49
-    private final Set<MutableAutoCommand> commands;
50
-    private AutoCommandType type;
51
-    private boolean loaded;
52
-    private Optional<MutableAutoCommand> selectedCommand;
53
-    private ListenerList listeners;
54
-
55
-    @Inject
56
-    public CoreAutoCommandsModel(final AutoCommandManager manager) {
57
-        this.manager = manager;
58
-        originalCommands = Sets.newHashSet();
59
-        commands = Sets.newHashSet();
60
-    }
61
-
62
-    @Override
63
-    public void addListener(@Nonnull final AutoCommandsModelListener listener) {
64
-        checkNotNull(listener);
65
-        listeners.add(AutoCommandsModelListener.class, listener);
66
-    }
67
-
68
-    @Override
69
-    public void removeListener(@Nonnull final AutoCommandsModelListener listener) {
70
-        checkNotNull(listener);
71
-        listeners.remove(AutoCommandsModelListener.class, listener);
72
-    }
73
-
74
-    @Override
75
-    public void setType(@Nonnull final AutoCommandType type) {
76
-        checkNotNull(type);
77
-        checkState(this.type == null);
78
-        this.type = type;
79
-    }
80
-
81
-    @Override
82
-    public void loadModel() {
83
-        checkNotNull(type);
84
-        checkState(!loaded);
85
-        loaded = true;
86
-        listeners = new ListenerList();
87
-        originalCommands.clear();
88
-        commands.clear();
89
-        selectedCommand = Optional.empty();
90
-        switch(type) {
91
-            case ALL:
92
-                manager.getAutoCommands().stream().map(c -> {
93
-                    originalCommands.add(c);
94
-                    return new MutableAutoCommand(c);
95
-                }).forEach(commands::add);
96
-                break;
97
-            case CONNECTION:
98
-                manager.getConnectionAutoCommands().stream().map(c -> {
99
-                    originalCommands.add(c);
100
-                    return new MutableAutoCommand(c);
101
-                }).forEach(commands::add);
102
-                break;
103
-            case GLOBAL:
104
-                manager.getGlobalAutoCommands().stream().map(c -> {
105
-                    originalCommands.add(c);
106
-                    return new MutableAutoCommand(c);
107
-                }).forEach(commands::add);
108
-                break;
109
-        }
110
-    }
111
-
112
-    @Override
113
-    @Nonnull
114
-    public Collection<MutableAutoCommand> getAutoCommands() {
115
-        return Collections.unmodifiableCollection(commands);
116
-    }
117
-
118
-    @Override
119
-    public void setAutoCommands(@Nonnull final Collection<MutableAutoCommand> commands) {
120
-        checkNotNull(commands);
121
-        this.commands.clear();
122
-        this.commands.addAll(commands);
123
-        listeners.getCallable(AutoCommandsModelListener.class).setAutoCommands(this.commands);
124
-    }
125
-
126
-    @Override
127
-    @Nonnull
128
-    public Optional<MutableAutoCommand> getSelectedCommand() {
129
-        return selectedCommand;
130
-    }
131
-
132
-    @Override
133
-    public void setSelectedCommand(@Nonnull final Optional<MutableAutoCommand> selectedCommand) {
134
-        checkNotNull(selectedCommand);
135
-        selectedCommand.ifPresent(s -> checkArgument(commands.contains(s)));
136
-        if (!this.selectedCommand.equals(selectedCommand)) {
137
-            this.selectedCommand = selectedCommand;
138
-            listeners.getCallable(AutoCommandsModelListener.class)
139
-                    .selectedCommandChanged(selectedCommand);
140
-        }
141
-    }
142
-
143
-    @Override
144
-    @Nonnull
145
-    public Optional<String> getSelectedCommandServer() {
146
-        if (selectedCommand.isPresent()) {
147
-            return selectedCommand.get().getServer();
148
-        }
149
-        return Optional.empty();
150
-    }
151
-
152
-    @Override
153
-    @Nonnull
154
-    public Optional<String> getSelectedCommandNetwork() {
155
-        if (selectedCommand.isPresent()) {
156
-            return selectedCommand.get().getNetwork();
157
-        }
158
-        return Optional.empty();
159
-    }
160
-
161
-    @Override
162
-    @Nonnull
163
-    public Optional<String> getSelectedCommandProfile() {
164
-        if (selectedCommand.isPresent()) {
165
-            return selectedCommand.get().getProfile();
166
-        }
167
-        return Optional.empty();
168
-    }
169
-
170
-    @Override
171
-    @Nonnull
172
-    public String getSelectedCommandResponse() {
173
-        if (selectedCommand.isPresent()) {
174
-            return selectedCommand.get().getResponse();
175
-        }
176
-        return "";
177
-    }
178
-
179
-    @Override
180
-    public void setSelectedCommandServer(@Nonnull final Optional<String> server) {
181
-        checkNotNull(server);
182
-        selectedCommand.ifPresent(s -> {
183
-            s.setServer(server);
184
-            listeners.getCallable(AutoCommandsModelListener.class)
185
-                    .commandEdited(selectedCommand.get());
186
-        });
187
-    }
188
-
189
-    @Override
190
-    public void setSelectedCommandNetwork(@Nonnull final Optional<String> network) {
191
-        checkNotNull(network);
192
-        selectedCommand.ifPresent(s -> {
193
-            s.setNetwork(network);
194
-            listeners.getCallable(AutoCommandsModelListener.class)
195
-                    .commandEdited(selectedCommand.get());
196
-        });
197
-    }
198
-
199
-    @Override
200
-    public void setSelectedCommandProfile(@Nonnull final Optional<String> profile) {
201
-        checkNotNull(profile);
202
-        selectedCommand.ifPresent(s -> {
203
-            s.setProfile(profile);
204
-            listeners.getCallable(AutoCommandsModelListener.class)
205
-                    .commandEdited(selectedCommand.get());});
206
-    }
207
-
208
-    @Override
209
-    public void setSelectedCommandResponse(@Nonnull final String response) {
210
-        checkNotNull(response);
211
-        checkArgument(!response.isEmpty());
212
-        selectedCommand.ifPresent(s -> {
213
-            s.setResponse(response);
214
-            listeners.getCallable(AutoCommandsModelListener.class)
215
-                    .commandEdited(selectedCommand.get());});
216
-    }
217
-
218
-    @Override
219
-    public void addCommand(@Nonnull final MutableAutoCommand command) {
220
-        checkNotNull(command);
221
-        checkArgument(!commands.contains(command));
222
-        commands.add(command);
223
-        listeners.getCallable(AutoCommandsModelListener.class).commandAdded(command);
224
-    }
225
-
226
-    @Override
227
-    public void removeCommand(@Nonnull final MutableAutoCommand command) {
228
-        checkNotNull(command);
229
-        checkArgument(commands.contains(command));
230
-        commands.remove(command);
231
-        listeners.getCallable(AutoCommandsModelListener.class).commandRemoved(command);
232
-    }
233
-
234
-    @Override
235
-    public void save() {
236
-        originalCommands.forEach(manager::removeAutoCommand);
237
-        commands.stream().map(MutableAutoCommand::getAutoCommand).forEach(manager::addAutoCommand);
238
-    }
239
-}

+ 0
- 1
src/com/dmdirc/ui/core/autocommands/MutableAutoCommand.java Zobrazit soubor

@@ -23,7 +23,6 @@
23 23
 package com.dmdirc.ui.core.autocommands;
24 24
 
25 25
 import com.dmdirc.commandparser.auto.AutoCommand;
26
-import com.dmdirc.ui.core.profiles.MutableProfile;
27 26
 
28 27
 import com.google.common.base.MoreObjects;
29 28
 

+ 2
- 2
test/com/dmdirc/commandparser/auto/AutoCommandManagerTest.java Zobrazit soubor

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.commandparser.auto;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.config.profiles.Profile;
27 27
 
28 28
 import java.util.Optional;
29 29
 
@@ -49,7 +49,7 @@ public class AutoCommandManagerTest {
49 49
     @Mock private AutoCommandHandler ircquakenetHandler;
50 50
     @Mock private AutoCommandHandler ukquakenetHandler;
51 51
     @Mock private AutoCommandHandler testnetHandler;
52
-    @Mock private ConfigProvider testProfile;
52
+    @Mock private Profile testProfile;
53 53
     private AutoCommandManager autoCommandManager;
54 54
     private AutoCommand global;
55 55
     private AutoCommand ircquakenet;

+ 116
- 0
test/com/dmdirc/commandparser/auto/AutoCommandTest.java Zobrazit soubor

@@ -0,0 +1,116 @@
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 java.util.Optional;
26
+
27
+import org.junit.Before;
28
+import org.junit.Test;
29
+
30
+import static junit.framework.TestCase.assertEquals;
31
+
32
+public class AutoCommandTest {
33
+
34
+    private AutoCommand command;
35
+
36
+    @Before
37
+    public void setUp() throws Exception {
38
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
39
+    }
40
+
41
+    @Test
42
+    public void testGetServer_Empty() throws Exception {
43
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
44
+        assertEquals(Optional.<String>empty(), command.getServer());
45
+    }
46
+
47
+    @Test
48
+    public void testGetServer_NotEmpty() throws Exception {
49
+        command = new AutoCommand(Optional.of("server"), Optional.empty(), Optional.empty(), "");
50
+        assertEquals(Optional.of("server"), command.getServer());
51
+    }
52
+
53
+    @Test
54
+    public void testGetNetwork_Empty() throws Exception {
55
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
56
+        assertEquals(Optional.<String>empty(), command.getNetwork());
57
+    }
58
+
59
+    @Test
60
+    public void testGetNetwork_NotEmpty() throws Exception {
61
+        command = new AutoCommand(Optional.empty(), Optional.of("network"), Optional.empty(), "");
62
+        assertEquals(Optional.of("network"), command.getNetwork());
63
+    }
64
+
65
+    @Test
66
+    public void testGetProfile_Empty() throws Exception {
67
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
68
+        assertEquals(Optional.<String>empty(), command.getProfile());
69
+    }
70
+
71
+    @Test
72
+    public void testGetProfile_NotEmpty() throws Exception {
73
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.of("profile"), "");
74
+        assertEquals(Optional.of("profile"), command.getProfile());
75
+    }
76
+
77
+    @Test
78
+    public void testGetResponse_Empty() throws Exception {
79
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
80
+        assertEquals("", command.getResponse());
81
+    }
82
+
83
+    @Test
84
+    public void testGetResponse_NotEmpty() throws Exception {
85
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "response");
86
+        assertEquals("response", command.getResponse());
87
+    }
88
+
89
+    @Test
90
+    public void testGetType_Server() throws Exception {
91
+        command = new AutoCommand(Optional.of("server"), Optional.empty(), Optional.empty(),
92
+                "response");
93
+        assertEquals(AutoCommandType.SERVER, command.getType());
94
+    }
95
+
96
+    @Test
97
+    public void testGetType_Network() throws Exception {
98
+        command = new AutoCommand(Optional.empty(), Optional.of("network"), Optional.empty(),
99
+                "response");
100
+        assertEquals(AutoCommandType.NETWORK, command.getType());
101
+    }
102
+
103
+    @Test
104
+    public void testGetType_Global() throws Exception {
105
+        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.of("profile"),
106
+                "response");
107
+        assertEquals(AutoCommandType.GLOBAL, command.getType());
108
+    }
109
+
110
+    @Test
111
+    public void testGetType_Unknown() throws Exception {
112
+        command = new AutoCommand(Optional.of("server"), Optional.of("network"), Optional.empty(),
113
+                "response");
114
+        assertEquals(AutoCommandType.UNKNOWN, command.getType());
115
+    }
116
+}

+ 0
- 359
test/com/dmdirc/ui/core/autocommands/CoreAutoCommandsModelTest.java Zobrazit soubor

@@ -1,359 +0,0 @@
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.ui.core.autocommands;
24
-
25
-import com.dmdirc.commandparser.auto.AutoCommand;
26
-import com.dmdirc.commandparser.auto.AutoCommandManager;
27
-import com.dmdirc.interfaces.ui.AutoCommandsModel;
28
-import com.dmdirc.interfaces.ui.AutoCommandsModelListener;
29
-
30
-import com.google.common.collect.Sets;
31
-
32
-import java.util.Optional;
33
-
34
-import org.junit.Before;
35
-import org.junit.Test;
36
-import org.junit.runner.RunWith;
37
-import org.mockito.Mock;
38
-import org.mockito.runners.MockitoJUnitRunner;
39
-
40
-import static junit.framework.TestCase.assertEquals;
41
-import static junit.framework.TestCase.assertFalse;
42
-import static junit.framework.TestCase.assertTrue;
43
-import static org.mockito.Matchers.any;
44
-import static org.mockito.Mockito.never;
45
-import static org.mockito.Mockito.verify;
46
-import static org.mockito.Mockito.when;
47
-
48
-@RunWith(MockitoJUnitRunner.class)
49
-public class CoreAutoCommandsModelTest {
50
-
51
-    @Mock private AutoCommandManager manager;
52
-    @Mock private AutoCommandsModelListener listener;
53
-    private AutoCommandsModel instance;
54
-    private MutableAutoCommand mutableCommand1;
55
-    private MutableAutoCommand mutableCommand2;
56
-    private MutableAutoCommand mutableCommand3;
57
-    private MutableAutoCommand mutableCommand4;
58
-    private MutableAutoCommand mutableCommand5;
59
-
60
-    @Before
61
-    public void setUp() throws Exception {
62
-        final AutoCommand command1 = new AutoCommand(Optional.of("server1"),
63
-                Optional.of("network1"), Optional.of("profile1"), "response1");
64
-        final AutoCommand command2 = new AutoCommand(Optional.empty(),
65
-                Optional.empty(), Optional.of("profile2"), "response2");
66
-        final AutoCommand command3 = new AutoCommand(Optional.of("server3"),
67
-                Optional.of("network3"), Optional.of("profile3"), "response3");
68
-        final AutoCommand command4 = new AutoCommand(Optional.empty(),
69
-                Optional.empty(), Optional.of("profile4"), "response4");
70
-        final AutoCommand command5 = new AutoCommand(Optional.empty(),
71
-                Optional.empty(), Optional.of("profile5"), "response5");
72
-        mutableCommand1 = new MutableAutoCommand(command1);
73
-        mutableCommand2 = new MutableAutoCommand(command2);
74
-        mutableCommand3 = new MutableAutoCommand(command3);
75
-        mutableCommand4 = new MutableAutoCommand(command4);
76
-        mutableCommand5 = new MutableAutoCommand(command5);
77
-        when(manager.getAutoCommands())
78
-                .thenReturn(Sets.newHashSet(command1, command2, command3, command4));
79
-        when(manager.getConnectionAutoCommands()).thenReturn(Sets.newHashSet(command1, command3));
80
-        when(manager.getGlobalAutoCommands()).thenReturn(Sets.newHashSet(command2, command4));
81
-        instance = new CoreAutoCommandsModel(manager);
82
-        instance.setType(AutoCommandType.ALL);
83
-        instance.loadModel();
84
-        instance.addListener(listener);
85
-    }
86
-
87
-    @Test
88
-    public void testAddListener() throws Exception {
89
-        instance = new CoreAutoCommandsModel(manager);
90
-        instance.setType(AutoCommandType.ALL);
91
-        instance.loadModel();
92
-        instance.setSelectedCommand(Optional.of(mutableCommand2));
93
-        verify(listener, never()).selectedCommandChanged(Optional.of(mutableCommand2));
94
-        instance.addListener(listener);
95
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
96
-        verify(listener).selectedCommandChanged(Optional.of(mutableCommand1));
97
-    }
98
-
99
-    @Test
100
-    public void testRemoveListener() throws Exception {
101
-        instance = new CoreAutoCommandsModel(manager);
102
-        instance.setType(AutoCommandType.ALL);
103
-        instance.loadModel();
104
-        instance.setSelectedCommand(Optional.of(mutableCommand2));
105
-        verify(listener, never()).selectedCommandChanged(Optional.of(mutableCommand2));
106
-        instance.addListener(listener);
107
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
108
-        verify(listener).selectedCommandChanged(Optional.of(mutableCommand1));
109
-        instance.removeListener(listener);
110
-        instance.setSelectedCommand(Optional.of(mutableCommand2));
111
-        verify(listener, never()).selectedCommandChanged(Optional.of(mutableCommand2));
112
-    }
113
-
114
-    @Test
115
-    public void testLoadModel_All() throws Exception {
116
-        instance = new CoreAutoCommandsModel(manager);
117
-        instance.setType(AutoCommandType.ALL);
118
-        assertTrue(instance.getAutoCommands().isEmpty());
119
-        instance.loadModel();
120
-        assertFalse(instance.getAutoCommands().isEmpty());
121
-        assertEquals(4, instance.getAutoCommands().size());
122
-        assertTrue(instance.getAutoCommands().contains(mutableCommand1));
123
-        assertTrue(instance.getAutoCommands().contains(mutableCommand2));
124
-        assertTrue(instance.getAutoCommands().contains(mutableCommand3));
125
-        assertTrue(instance.getAutoCommands().contains(mutableCommand4));
126
-    }
127
-
128
-    @Test
129
-    public void testLoadModel_Global() throws Exception {
130
-        instance = new CoreAutoCommandsModel(manager);
131
-        instance.setType(AutoCommandType.GLOBAL);
132
-        assertTrue(instance.getAutoCommands().isEmpty());
133
-        instance.loadModel();
134
-        assertFalse(instance.getAutoCommands().isEmpty());
135
-        assertEquals(2, instance.getAutoCommands().size());
136
-        assertFalse(instance.getAutoCommands().contains(mutableCommand1));
137
-        assertTrue(instance.getAutoCommands().contains(mutableCommand2));
138
-        assertFalse(instance.getAutoCommands().contains(mutableCommand3));
139
-        assertTrue(instance.getAutoCommands().contains(mutableCommand4));
140
-    }
141
-
142
-    @Test
143
-    public void testLoadModel_Connection() throws Exception {
144
-        instance = new CoreAutoCommandsModel(manager);
145
-        instance.setType(AutoCommandType.CONNECTION);
146
-        assertTrue(instance.getAutoCommands().isEmpty());
147
-        instance.loadModel();
148
-        assertFalse(instance.getAutoCommands().isEmpty());
149
-        assertEquals(2, instance.getAutoCommands().size());
150
-        assertTrue(instance.getAutoCommands().contains(mutableCommand1));
151
-        assertFalse(instance.getAutoCommands().contains(mutableCommand2));
152
-        assertTrue(instance.getAutoCommands().contains(mutableCommand3));
153
-        assertFalse(instance.getAutoCommands().contains(mutableCommand4));
154
-    }
155
-
156
-    @Test
157
-    public void testSetAutoCommands_Empty() throws Exception {
158
-        assertFalse(instance.getAutoCommands().isEmpty());
159
-        instance.setAutoCommands(Sets.newHashSet());
160
-        assertTrue(instance.getAutoCommands().isEmpty());
161
-        verify(listener).setAutoCommands(Sets.newHashSet());
162
-    }
163
-
164
-    @Test
165
-    public void testSetAutoCommands_NotEmpty() throws Exception {
166
-        assertEquals(4, instance.getAutoCommands().size());
167
-        instance.setAutoCommands(Sets.newHashSet(mutableCommand1, mutableCommand2));
168
-        assertEquals(2, instance.getAutoCommands().size());
169
-        verify(listener).setAutoCommands(Sets.newHashSet(mutableCommand1, mutableCommand2));
170
-    }
171
-
172
-    @Test
173
-    public void testGetSelectedCommand() throws Exception {
174
-        assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
175
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
176
-        assertEquals(Optional.of(mutableCommand1), instance.getSelectedCommand());
177
-    }
178
-
179
-    @Test(expected = IllegalArgumentException.class)
180
-    public void testSetSelectedCommand_UnknownCommand() {
181
-        assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
182
-        instance.setSelectedCommand(Optional.of(mutableCommand5));
183
-        verify(listener, never()).selectedCommandChanged(any());
184
-    }
185
-
186
-    @Test
187
-    public void testSetSelectedCommand_EmptyCommand() {
188
-        assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
189
-        instance.setSelectedCommand(Optional.of(mutableCommand4));
190
-        assertEquals(Optional.of(mutableCommand4), instance.getSelectedCommand());
191
-        instance.setSelectedCommand(Optional.empty());
192
-        assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
193
-        verify(listener).selectedCommandChanged(Optional.empty());
194
-    }
195
-
196
-    @Test
197
-    public void testGetSelectedCommandServer() throws Exception {
198
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
199
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
200
-        assertEquals(mutableCommand1.getServer(), instance.getSelectedCommandServer());
201
-    }
202
-
203
-    @Test
204
-    public void testGetSelectedCommandNetwork() throws Exception {
205
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
206
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
207
-        assertEquals(mutableCommand1.getNetwork(), instance.getSelectedCommandNetwork());
208
-    }
209
-
210
-    @Test
211
-    public void testGetSelectedCommandProfile() throws Exception {
212
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
213
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
214
-        assertEquals(mutableCommand1.getProfile(), instance.getSelectedCommandProfile());
215
-    }
216
-
217
-    @Test
218
-    public void testGetSelectedCommandResponse() throws Exception {
219
-        assertEquals("", instance.getSelectedCommandResponse());
220
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
221
-        assertEquals(mutableCommand1.getResponse(), instance.getSelectedCommandResponse());
222
-    }
223
-
224
-    @Test
225
-    public void testSetSelectedCommandServer_Empty() throws Exception {
226
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
227
-        instance.setSelectedCommandServer(Optional.of("server"));
228
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
229
-        verify(listener, never()).commandEdited(any());
230
-    }
231
-
232
-    @Test
233
-    public void testSetSelectedCommandServer() throws Exception {
234
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
235
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
236
-        instance.setSelectedCommandServer(Optional.of("server"));
237
-        assertEquals(Optional.of("server"), instance.getSelectedCommandServer());
238
-        verify(listener).commandEdited(mutableCommand1);
239
-    }
240
-
241
-    @Test
242
-    public void testSetSelectedCommandNetwork_Empty() throws Exception {
243
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
244
-        instance.setSelectedCommandNetwork(Optional.of("network"));
245
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
246
-        verify(listener, never()).commandEdited(any());
247
-    }
248
-
249
-    @Test
250
-    public void testSetSelectedCommandNetwork() throws Exception {
251
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
252
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
253
-        instance.setSelectedCommandNetwork(Optional.of("network"));
254
-        assertEquals(Optional.of("network"), instance.getSelectedCommandNetwork());
255
-        verify(listener).commandEdited(mutableCommand1);
256
-    }
257
-
258
-    @Test
259
-    public void testSetSelectedCommandProfile_Empty() throws Exception {
260
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
261
-        instance.setSelectedCommandProfile(Optional.of("profile"));
262
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
263
-        verify(listener, never()).commandEdited(any());
264
-    }
265
-
266
-    @Test
267
-    public void testSetSelectedCommandProfile() throws Exception {
268
-        assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
269
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
270
-        instance.setSelectedCommandProfile(Optional.of("profile"));
271
-        assertEquals(Optional.of("profile"), instance.getSelectedCommandProfile());
272
-        verify(listener).commandEdited(mutableCommand1);
273
-    }
274
-
275
-    @Test
276
-    public void testSetSelectedCommandResponse_Empty() throws Exception {
277
-        assertEquals("", instance.getSelectedCommandResponse());
278
-        instance.setSelectedCommandResponse("response");
279
-        assertEquals("", instance.getSelectedCommandResponse());
280
-        verify(listener, never()).commandEdited(any());
281
-    }
282
-
283
-    @Test(expected = NullPointerException.class)
284
-    public void testSetSelectedCommandResponse_Null() throws Exception {
285
-        assertEquals("", instance.getSelectedCommandResponse());
286
-        instance.setSelectedCommandResponse(null);
287
-        verify(listener, never()).commandEdited(any());
288
-    }
289
-
290
-    @Test
291
-    public void testSetSelectedCommandResponse() throws Exception {
292
-        assertEquals("", instance.getSelectedCommandResponse());
293
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
294
-        instance.setSelectedCommandResponse("response");
295
-        assertEquals("response", instance.getSelectedCommandResponse());
296
-        verify(listener).commandEdited(mutableCommand1);
297
-    }
298
-
299
-    @Test(expected = IllegalArgumentException.class)
300
-    public void testSetSelectedCommandResponse_EmptyResponse() throws Exception {
301
-        assertEquals("", instance.getSelectedCommandResponse());
302
-        instance.setSelectedCommand(Optional.of(mutableCommand1));
303
-        instance.setSelectedCommandResponse("");
304
-        assertEquals("response", instance.getSelectedCommandResponse());
305
-        verify(listener, never()).commandEdited(any());
306
-    }
307
-
308
-    @Test
309
-    public void testAddCommand() throws Exception {
310
-        assertEquals(4, instance.getAutoCommands().size());
311
-        assertFalse(instance.getAutoCommands().contains(mutableCommand5));
312
-        instance.addCommand(mutableCommand5);
313
-        assertEquals(5, instance.getAutoCommands().size());
314
-        assertTrue(instance.getAutoCommands().contains(mutableCommand5));
315
-        verify(listener).commandAdded(mutableCommand5);
316
-    }
317
-
318
-    @Test(expected = IllegalArgumentException.class)
319
-    public void testAddCommand_exists() throws Exception {
320
-        assertEquals(4, instance.getAutoCommands().size());
321
-        assertTrue(instance.getAutoCommands().contains(mutableCommand4));
322
-        instance.addCommand(mutableCommand4);
323
-        verify(listener, never()).commandAdded(any());
324
-    }
325
-
326
-    @Test
327
-    public void testRemoveCommand() throws Exception {
328
-        assertEquals(4, instance.getAutoCommands().size());
329
-        assertTrue(instance.getAutoCommands().contains(mutableCommand4));
330
-        instance.removeCommand(mutableCommand4);
331
-        assertEquals(3, instance.getAutoCommands().size());
332
-        assertFalse(instance.getAutoCommands().contains(mutableCommand4));
333
-        verify(listener).commandRemoved(mutableCommand4);
334
-    }
335
-
336
-    @Test(expected = IllegalArgumentException.class)
337
-    public void testRemoveCommand_NotContains() throws Exception {
338
-        assertEquals(4, instance.getAutoCommands().size());
339
-        assertFalse(instance.getAutoCommands().contains(mutableCommand5));
340
-        instance.removeCommand(mutableCommand5);
341
-        verify(listener, never()).commandRemoved(any());
342
-    }
343
-
344
-    @Test
345
-    public void testSave() throws Exception {
346
-        instance.addCommand(mutableCommand5);
347
-        instance.removeCommand(mutableCommand1);
348
-        instance.save();
349
-        verify(manager).removeAutoCommand(mutableCommand1.getAutoCommand());
350
-        verify(manager).removeAutoCommand(mutableCommand2.getAutoCommand());
351
-        verify(manager).removeAutoCommand(mutableCommand3.getAutoCommand());
352
-        verify(manager).removeAutoCommand(mutableCommand4.getAutoCommand());
353
-        verify(manager, never()).addAutoCommand(mutableCommand1.getAutoCommand());
354
-        verify(manager).addAutoCommand(mutableCommand2.getAutoCommand());
355
-        verify(manager).addAutoCommand(mutableCommand3.getAutoCommand());
356
-        verify(manager).addAutoCommand(mutableCommand4.getAutoCommand());
357
-        verify(manager).addAutoCommand(mutableCommand5.getAutoCommand());
358
-    }
359
-}

Načítá se…
Zrušit
Uložit