|
@@ -0,0 +1,192 @@
|
|
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.ui.core.autocommands;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.commandparser.auto.AutoCommand;
|
|
26
|
+import com.dmdirc.commandparser.auto.AutoCommandManager;
|
|
27
|
+import com.dmdirc.interfaces.CommandController;
|
|
28
|
+
|
|
29
|
+import java.util.Optional;
|
|
30
|
+
|
|
31
|
+import org.junit.Before;
|
|
32
|
+import org.junit.Test;
|
|
33
|
+import org.junit.runner.RunWith;
|
|
34
|
+import org.mockito.Mock;
|
|
35
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
36
|
+
|
|
37
|
+import static org.junit.Assert.assertEquals;
|
|
38
|
+import static org.junit.Assert.assertFalse;
|
|
39
|
+import static org.junit.Assert.assertNotNull;
|
|
40
|
+import static org.junit.Assert.assertTrue;
|
|
41
|
+import static org.mockito.Matchers.eq;
|
|
42
|
+import static org.mockito.Mockito.verify;
|
|
43
|
+import static org.mockito.Mockito.when;
|
|
44
|
+
|
|
45
|
+@RunWith(MockitoJUnitRunner.class)
|
|
46
|
+public class CoreGlobalAutoCommandsDialogModelTest {
|
|
47
|
+
|
|
48
|
+ @Mock private AutoCommandManager autoCommandManager;
|
|
49
|
+ @Mock private CommandController commandController;
|
|
50
|
+ @Mock private AutoCommand autoCommand;
|
|
51
|
+ private CoreGlobalAutoCommandsDialogModel instance;
|
|
52
|
+
|
|
53
|
+ @Before
|
|
54
|
+ public void setUp() throws Exception {
|
|
55
|
+ when(autoCommand.getServer()).thenReturn(Optional.empty());
|
|
56
|
+ when(autoCommand.getNetwork()).thenReturn(Optional.empty());
|
|
57
|
+ when(autoCommand.getProfile()).thenReturn(Optional.empty());
|
|
58
|
+ when(autoCommand.getResponse()).thenReturn("response");
|
|
59
|
+ when(commandController.getCommandChar()).thenReturn('/');
|
|
60
|
+ when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.of(autoCommand));
|
|
61
|
+ instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
|
|
62
|
+ new ResponseValidator(commandController));
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ @Test(expected = IllegalStateException.class)
|
|
66
|
+ public void testLoad_NotCalled() throws Exception {
|
|
67
|
+ instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
|
|
68
|
+ new ResponseValidator(commandController));
|
|
69
|
+ instance.getResponse();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Test
|
|
73
|
+ public void testGetResponse_Empty() throws Exception {
|
|
74
|
+ when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
|
|
75
|
+ instance.load();
|
|
76
|
+ assertEquals("", instance.getResponse());
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ @Test
|
|
80
|
+ public void testGetResponse_Complete() throws Exception {
|
|
81
|
+ instance.load();
|
|
82
|
+ assertEquals("response", instance.getResponse());
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ @Test(expected = NullPointerException.class)
|
|
86
|
+ public void testSetResponse_Null() throws Exception {
|
|
87
|
+ instance.load();
|
|
88
|
+ assertEquals("response", instance.getResponse());
|
|
89
|
+ instance.setResponse(null);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ @Test
|
|
93
|
+ public void testSetResponse_empty() throws Exception {
|
|
94
|
+ instance.load();
|
|
95
|
+ assertEquals("response", instance.getResponse());
|
|
96
|
+ instance.setResponse("");
|
|
97
|
+ assertEquals("", instance.getResponse());
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ @Test
|
|
101
|
+ public void testSetResponse() throws Exception {
|
|
102
|
+ instance.load();
|
|
103
|
+ assertEquals("response", instance.getResponse());
|
|
104
|
+ instance.setResponse("test");
|
|
105
|
+ assertEquals("test", instance.getResponse());
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ @Test
|
|
109
|
+ public void testGetResponseValidator() throws Exception {
|
|
110
|
+ instance.load();
|
|
111
|
+ assertNotNull(instance.getResponseValidator());
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ @Test
|
|
115
|
+ public void testIsResponseValid_Initially() throws Exception {
|
|
116
|
+ instance.load();
|
|
117
|
+ assertTrue(instance.isResponseValid());
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @Test
|
|
121
|
+ public void testIsResponseValid_EmptyGlobal() throws Exception {
|
|
122
|
+ when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
|
|
123
|
+ instance.load();
|
|
124
|
+ assertFalse(instance.isResponseValid());
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ @Test
|
|
128
|
+ public void testIsResponseValid_Empty() throws Exception {
|
|
129
|
+ instance.load();
|
|
130
|
+ instance.setResponse("");
|
|
131
|
+ assertFalse(instance.isResponseValid());
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Test
|
|
135
|
+ public void testIsResponseValid_CommandChar() throws Exception {
|
|
136
|
+ instance.load();
|
|
137
|
+ instance.setResponse("/moo");
|
|
138
|
+ assertFalse(instance.isResponseValid());
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ @Test
|
|
142
|
+ public void testIsResponseValid() throws Exception {
|
|
143
|
+ instance.load();
|
|
144
|
+ instance.setResponse("moo");
|
|
145
|
+ assertTrue(instance.isResponseValid());
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ @Test
|
|
149
|
+ public void testIsSaveAllowed_InvalidResponse() throws Exception {
|
|
150
|
+ instance.load();
|
|
151
|
+ assertTrue(instance.isSaveAllowed());
|
|
152
|
+ instance.setResponse("");
|
|
153
|
+ assertFalse(instance.isSaveAllowed());
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @Test
|
|
157
|
+ public void testIsSaveAllowed() throws Exception {
|
|
158
|
+ instance.load();
|
|
159
|
+ assertTrue(instance.isSaveAllowed());
|
|
160
|
+ instance.setResponse("test");
|
|
161
|
+ assertTrue(instance.isSaveAllowed());
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ @Test(expected = IllegalStateException.class)
|
|
165
|
+ public void testSave_NotAllowed() throws Exception {
|
|
166
|
+ instance.load();
|
|
167
|
+ instance.setResponse("");
|
|
168
|
+ assertFalse(instance.isSaveAllowed());
|
|
169
|
+ instance.save();
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ @Test
|
|
173
|
+ public void testSave_EmptyGlobal() throws Exception {
|
|
174
|
+ when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
|
|
175
|
+ instance.load();
|
|
176
|
+ instance.setResponse("test");
|
|
177
|
+ assertTrue(instance.isSaveAllowed());
|
|
178
|
+ instance.save();
|
|
179
|
+ verify(autoCommandManager).addAutoCommand(eq(AutoCommand.create(Optional.empty(),
|
|
180
|
+ Optional.empty(), Optional.empty(), "test")));
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ @Test
|
|
184
|
+ public void testSave() throws Exception {
|
|
185
|
+ instance.load();
|
|
186
|
+ instance.setResponse("test");
|
|
187
|
+ assertTrue(instance.isSaveAllowed());
|
|
188
|
+ instance.save();
|
|
189
|
+ verify(autoCommandManager).replaceAutoCommand(eq(autoCommand), eq(AutoCommand
|
|
190
|
+ .create(Optional.empty(), Optional.empty(), Optional.empty(), "test")));
|
|
191
|
+ }
|
|
192
|
+}
|