|
@@ -0,0 +1,283 @@
|
|
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
|
+
|
|
28
|
+import com.google.common.collect.Sets;
|
|
29
|
+
|
|
30
|
+import java.util.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.assertEquals;
|
|
39
|
+import static junit.framework.TestCase.assertFalse;
|
|
40
|
+import static junit.framework.TestCase.assertTrue;
|
|
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 AutoCommandsModelTest {
|
|
47
|
+
|
|
48
|
+ @Mock private AutoCommandManager manager;
|
|
49
|
+ private AutoCommandsModel instance;
|
|
50
|
+ private MutableAutoCommand mutableCommand1;
|
|
51
|
+ private MutableAutoCommand mutableCommand2;
|
|
52
|
+ private MutableAutoCommand mutableCommand3;
|
|
53
|
+ private MutableAutoCommand mutableCommand4;
|
|
54
|
+ private MutableAutoCommand mutableCommand5;
|
|
55
|
+
|
|
56
|
+ @Before
|
|
57
|
+ public void setUp() throws Exception {
|
|
58
|
+ final AutoCommand command1 = new AutoCommand(Optional.of("server1"),
|
|
59
|
+ Optional.of("network1"), Optional.of("profile1"), "response1");
|
|
60
|
+ final AutoCommand command2 = new AutoCommand(Optional.empty(),
|
|
61
|
+ Optional.empty(), Optional.of("profile2"), "response2");
|
|
62
|
+ final AutoCommand command3 = new AutoCommand(Optional.of("server3"),
|
|
63
|
+ Optional.of("network3"), Optional.of("profile3"), "response3");
|
|
64
|
+ final AutoCommand command4 = new AutoCommand(Optional.empty(),
|
|
65
|
+ Optional.empty(), Optional.of("profile4"), "response4");
|
|
66
|
+ final AutoCommand command5 = new AutoCommand(Optional.empty(),
|
|
67
|
+ Optional.empty(), Optional.of("profile5"), "response5");
|
|
68
|
+ mutableCommand1 = new MutableAutoCommand(command1);
|
|
69
|
+ mutableCommand2 = new MutableAutoCommand(command2);
|
|
70
|
+ mutableCommand3 = new MutableAutoCommand(command3);
|
|
71
|
+ mutableCommand4 = new MutableAutoCommand(command4);
|
|
72
|
+ mutableCommand5 = new MutableAutoCommand(command5);
|
|
73
|
+ when(manager.getAutoCommands())
|
|
74
|
+ .thenReturn(Sets.newHashSet(command1, command2, command3, command4));
|
|
75
|
+ when(manager.getConnectionAutoCommands()).thenReturn(Sets.newHashSet(command1, command3));
|
|
76
|
+ when(manager.getGlobalAutoCommands()).thenReturn(Sets.newHashSet(command2, command4));
|
|
77
|
+ instance = new AutoCommandsModel(manager, AutoCommandType.ALL);
|
|
78
|
+ instance.loadModel();
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ @Test
|
|
82
|
+ public void testLoadModel_All() throws Exception {
|
|
83
|
+ instance = new AutoCommandsModel(manager, AutoCommandType.ALL);
|
|
84
|
+ assertTrue(instance.getAutoCommands().isEmpty());
|
|
85
|
+ instance.loadModel();
|
|
86
|
+ assertFalse(instance.getAutoCommands().isEmpty());
|
|
87
|
+ assertEquals(4, instance.getAutoCommands().size());
|
|
88
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand1));
|
|
89
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand2));
|
|
90
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand3));
|
|
91
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand4));
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ @Test
|
|
95
|
+ public void testLoadModel_Global() throws Exception {
|
|
96
|
+ instance = new AutoCommandsModel(manager, AutoCommandType.GLOBAL);
|
|
97
|
+ assertTrue(instance.getAutoCommands().isEmpty());
|
|
98
|
+ instance.loadModel();
|
|
99
|
+ assertFalse(instance.getAutoCommands().isEmpty());
|
|
100
|
+ assertEquals(2, instance.getAutoCommands().size());
|
|
101
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand1));
|
|
102
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand2));
|
|
103
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand3));
|
|
104
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand4));
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ @Test
|
|
108
|
+ public void testLoadModel_Connection() throws Exception {
|
|
109
|
+ instance = new AutoCommandsModel(manager, AutoCommandType.CONNECTION);
|
|
110
|
+ assertTrue(instance.getAutoCommands().isEmpty());
|
|
111
|
+ instance.loadModel();
|
|
112
|
+ assertFalse(instance.getAutoCommands().isEmpty());
|
|
113
|
+ assertEquals(2, instance.getAutoCommands().size());
|
|
114
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand1));
|
|
115
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand2));
|
|
116
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand3));
|
|
117
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand4));
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @Test
|
|
121
|
+ public void testSetAutoCommands_Empty() throws Exception {
|
|
122
|
+ assertFalse(instance.getAutoCommands().isEmpty());
|
|
123
|
+ instance.setAutoCommands(Sets.newHashSet());
|
|
124
|
+ assertTrue(instance.getAutoCommands().isEmpty());
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ @Test
|
|
128
|
+ public void testSetAutoCommands_NotEmpty() throws Exception {
|
|
129
|
+ assertEquals(4, instance.getAutoCommands().size());
|
|
130
|
+ instance.setAutoCommands(Sets.newHashSet(mutableCommand1, mutableCommand2));
|
|
131
|
+ assertEquals(2, instance.getAutoCommands().size());
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Test
|
|
135
|
+ public void testGetSelectedCommand() throws Exception {
|
|
136
|
+ assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
|
|
137
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
138
|
+ assertEquals(Optional.of(mutableCommand1), instance.getSelectedCommand());
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ @Test(expected = IllegalArgumentException.class)
|
|
142
|
+ public void testSetSelectedCommand_UnknownCommand() {
|
|
143
|
+ assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
|
|
144
|
+ instance.setSelectedCommand(Optional.of(mutableCommand5));
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ @Test
|
|
148
|
+ public void testSetSelectedCommand_EmptyCommand() {
|
|
149
|
+ assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
|
|
150
|
+ instance.setSelectedCommand(Optional.of(mutableCommand4));
|
|
151
|
+ assertEquals(Optional.of(mutableCommand4), instance.getSelectedCommand());
|
|
152
|
+ instance.setSelectedCommand(Optional.empty());
|
|
153
|
+ assertEquals(Optional.<MutableAutoCommand>empty(), instance.getSelectedCommand());
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @Test
|
|
157
|
+ public void testGetSelectedCommandServer() throws Exception {
|
|
158
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
|
|
159
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
160
|
+ assertEquals(mutableCommand1.getServer(), instance.getSelectedCommandServer());
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ @Test
|
|
164
|
+ public void testGetSelectedCommandNetwork() throws Exception {
|
|
165
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
|
|
166
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
167
|
+ assertEquals(mutableCommand1.getNetwork(), instance.getSelectedCommandNetwork());
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ @Test
|
|
171
|
+ public void testGetSelectedCommandProfile() throws Exception {
|
|
172
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
|
|
173
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
174
|
+ assertEquals(mutableCommand1.getProfile(), instance.getSelectedCommandProfile());
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ @Test
|
|
178
|
+ public void testGetSelectedCommandResponse() throws Exception {
|
|
179
|
+ assertEquals("", instance.getSelectedCommandResponse());
|
|
180
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
181
|
+ assertEquals(mutableCommand1.getResponse(), instance.getSelectedCommandResponse());
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ @Test
|
|
185
|
+ public void testSetSelectedCommandServer_Empty() throws Exception {
|
|
186
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
|
|
187
|
+ instance.setSelectedCommandServer(Optional.of("server"));
|
|
188
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ @Test
|
|
192
|
+ public void testSetSelectedCommandServer() throws Exception {
|
|
193
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandServer());
|
|
194
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
195
|
+ instance.setSelectedCommandServer(Optional.of("server"));
|
|
196
|
+ assertEquals(Optional.of("server"), instance.getSelectedCommandServer());
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ @Test
|
|
200
|
+ public void testSetSelectedCommandNetwork_Empty() throws Exception {
|
|
201
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
|
|
202
|
+ instance.setSelectedCommandNetwork(Optional.of("network"));
|
|
203
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ @Test
|
|
207
|
+ public void testSetSelectedCommandNetwork() throws Exception {
|
|
208
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandNetwork());
|
|
209
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
210
|
+ instance.setSelectedCommandNetwork(Optional.of("network"));
|
|
211
|
+ assertEquals(Optional.of("network"), instance.getSelectedCommandNetwork());
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ @Test
|
|
215
|
+ public void testSetSelectedCommandProfile_Empty() throws Exception {
|
|
216
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
|
|
217
|
+ instance.setSelectedCommandProfile(Optional.of("profile"));
|
|
218
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
|
|
219
|
+ }
|
|
220
|
+
|
|
221
|
+ @Test
|
|
222
|
+ public void testSetSelectedCommandProfile() throws Exception {
|
|
223
|
+ assertEquals(Optional.<String>empty(), instance.getSelectedCommandProfile());
|
|
224
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
225
|
+ instance.setSelectedCommandProfile(Optional.of("profile"));
|
|
226
|
+ assertEquals(Optional.of("profile"), instance.getSelectedCommandProfile());
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ @Test
|
|
230
|
+ public void testSetSelectedCommandResponse_Empty() throws Exception {
|
|
231
|
+ assertEquals("", instance.getSelectedCommandResponse());
|
|
232
|
+ instance.setSelectedCommandResponse("response");
|
|
233
|
+ assertEquals("", instance.getSelectedCommandResponse());
|
|
234
|
+ }
|
|
235
|
+
|
|
236
|
+ @Test(expected = NullPointerException.class)
|
|
237
|
+ public void testSetSelectedCommandResponse_Null() throws Exception {
|
|
238
|
+ assertEquals("", instance.getSelectedCommandResponse());
|
|
239
|
+ instance.setSelectedCommandResponse(null);
|
|
240
|
+ }
|
|
241
|
+
|
|
242
|
+ @Test
|
|
243
|
+ public void testSetSelectedCommandResponse() throws Exception {
|
|
244
|
+ assertEquals("", instance.getSelectedCommandResponse());
|
|
245
|
+ instance.setSelectedCommand(Optional.of(mutableCommand1));
|
|
246
|
+ instance.setSelectedCommandResponse("response");
|
|
247
|
+ assertEquals("response", instance.getSelectedCommandResponse());
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ @Test
|
|
251
|
+ public void testAddCommand() throws Exception {
|
|
252
|
+ assertEquals(4, instance.getAutoCommands().size());
|
|
253
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand5));
|
|
254
|
+ instance.addCommand(mutableCommand5);
|
|
255
|
+ assertEquals(5, instance.getAutoCommands().size());
|
|
256
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand5));
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ @Test
|
|
260
|
+ public void testRemoveCommand() throws Exception {
|
|
261
|
+ assertEquals(4, instance.getAutoCommands().size());
|
|
262
|
+ assertTrue(instance.getAutoCommands().contains(mutableCommand4));
|
|
263
|
+ instance.removeCommand(mutableCommand4);
|
|
264
|
+ assertEquals(3, instance.getAutoCommands().size());
|
|
265
|
+ assertFalse(instance.getAutoCommands().contains(mutableCommand4));
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ @Test
|
|
269
|
+ public void testSave() throws Exception {
|
|
270
|
+ instance.addCommand(mutableCommand5);
|
|
271
|
+ instance.removeCommand(mutableCommand1);
|
|
272
|
+ instance.save();
|
|
273
|
+ verify(manager).removeAutoCommand(mutableCommand1.getAutoCommand());
|
|
274
|
+ verify(manager).removeAutoCommand(mutableCommand2.getAutoCommand());
|
|
275
|
+ verify(manager).removeAutoCommand(mutableCommand3.getAutoCommand());
|
|
276
|
+ verify(manager).removeAutoCommand(mutableCommand4.getAutoCommand());
|
|
277
|
+ verify(manager, never()).addAutoCommand(mutableCommand1.getAutoCommand());
|
|
278
|
+ verify(manager).addAutoCommand(mutableCommand2.getAutoCommand());
|
|
279
|
+ verify(manager).addAutoCommand(mutableCommand3.getAutoCommand());
|
|
280
|
+ verify(manager).addAutoCommand(mutableCommand4.getAutoCommand());
|
|
281
|
+ verify(manager).addAutoCommand(mutableCommand5.getAutoCommand());
|
|
282
|
+ }
|
|
283
|
+}
|