|
@@ -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
|
|
-}
|