Kaynağa Gözat

Add TimerCommandTest.

pull/182/head
Greg Holmes 9 yıl önce
ebeveyn
işleme
7d39517e3d

+ 2
- 3
time/src/com/dmdirc/addons/time/TimerCommand.java Dosyayı Görüntüle

@@ -136,7 +136,7 @@ public class TimerCommand extends Command implements IntelligentCommand {
136 136
     private void doList(final FrameContainer origin, final boolean isSilent) {
137 137
         final Set<Entry<Integer, TimedCommand>> timerList = manager.listTimers();
138 138
         if (timerList.isEmpty()) {
139
-            sendLine(origin, isSilent, FORMAT_ERROR, "There are " + "currently no active timers");
139
+            sendLine(origin, isSilent, FORMAT_ERROR, "There are currently no active timers");
140 140
         } else {
141 141
             for (Entry<Integer, TimedCommand> entry : timerList) {
142 142
                 sendLine(origin, isSilent, FORMAT_OUTPUT,
@@ -152,8 +152,7 @@ public class TimerCommand extends Command implements IntelligentCommand {
152 152
      * @param isSilent Whether this command is being silenced or not
153 153
      */
154 154
     private void doUsage(final FrameContainer origin, final boolean isSilent) {
155
-        showUsage(origin, isSilent, "timer", "[--list|--cancel <timer id> | "
156
-                + "<repetitions> <interval> <command>]");
155
+        showUsage(origin, isSilent, INFO.getName(), INFO.getHelp());
157 156
     }
158 157
 
159 158
     @Override

+ 227
- 0
time/test/com/dmdirc/addons/time/TimerCommandTest.java Dosyayı Görüntüle

@@ -0,0 +1,227 @@
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.addons.time;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.CommandArguments;
27
+import com.dmdirc.commandparser.commands.IntelligentCommand.IntelligentCommandContext;
28
+import com.dmdirc.commandparser.commands.context.CommandContext;
29
+import com.dmdirc.interfaces.CommandController;
30
+import com.dmdirc.ui.input.AdditionalTabTargets;
31
+
32
+import com.google.common.collect.ImmutableMap;
33
+import com.google.common.collect.Lists;
34
+import com.google.common.collect.Sets;
35
+
36
+import org.junit.Before;
37
+import org.junit.Test;
38
+import org.junit.runner.RunWith;
39
+import org.mockito.Mock;
40
+import org.mockito.runners.MockitoJUnitRunner;
41
+
42
+import static junit.framework.TestCase.assertEquals;
43
+import static org.mockito.Matchers.anyInt;
44
+import static org.mockito.Matchers.eq;
45
+import static org.mockito.Mockito.never;
46
+import static org.mockito.Mockito.times;
47
+import static org.mockito.Mockito.verify;
48
+import static org.mockito.Mockito.when;
49
+
50
+@RunWith(MockitoJUnitRunner.class)
51
+public class TimerCommandTest {
52
+
53
+    @Mock private TimerManager timerManager;
54
+    @Mock private CommandController commandController;
55
+    @Mock private FrameContainer frameContainer;
56
+    @Mock private CommandContext commandContext;
57
+    @Mock private IntelligentCommandContext intelligentCommandContext;
58
+    @Mock private CommandArguments commandArguments;
59
+    @Mock private TimedCommand timer;
60
+
61
+    private TimerCommand instance;
62
+
63
+    @Before
64
+    public void setUp() throws Exception {
65
+        instance = new TimerCommand(timerManager, commandController);
66
+        when(commandArguments.isSilent()).thenReturn(false);
67
+        when(timerManager.getTimerByID(anyInt())).thenReturn(timer);
68
+        when(commandController.getCommandChar()).thenReturn('/');
69
+    }
70
+
71
+    private void mockCommandArguments(final String one, final String two) {
72
+        when(commandArguments.getArguments()).thenReturn(new String[]{one, two});
73
+        when(commandArguments.getArgumentsAsString()).thenReturn((one + ' ' + two).trim());
74
+        when(commandArguments.getArgumentsAsString(1)).thenReturn(two.trim());
75
+    }
76
+
77
+    private void mockCommandArguments(final String one, final String two, final String three) {
78
+        when(commandArguments.getArguments()).thenReturn(new String[]{one, two, three});
79
+        when(commandArguments.getArgumentsAsString()).thenReturn((one + ' ' + two + ' ' + three).trim());
80
+        when(commandArguments.getArgumentsAsString(1)).thenReturn((two + ' ' + three).trim());
81
+        when(commandArguments.getArgumentsAsString(2)).thenReturn(three.trim());
82
+    }
83
+
84
+    private void mockCommandArguments(final String one, final String two, final String three,
85
+            final String four) {
86
+        when(commandArguments.getArguments())
87
+                .thenReturn(new String[]{one, two, three, four});
88
+        when(commandArguments.getArgumentsAsString())
89
+                .thenReturn((one + ' ' + two + ' ' + three + ' ' + four).trim());
90
+        when(commandArguments.getArgumentsAsString(1))
91
+                .thenReturn((two + ' ' + three + ' ' + four).trim());
92
+        when(commandArguments.getArgumentsAsString(2))
93
+                .thenReturn((three + ' ' + four).trim());
94
+    }
95
+
96
+    @Test
97
+    public void testExecute_cancel_not_number() throws Exception {
98
+        mockCommandArguments("--cancel", "one", "");
99
+        instance.execute(frameContainer, commandArguments, commandContext);
100
+        verify(timerManager, never()).hasTimerWithID(anyInt());
101
+        verify(timerManager, never()).getTimerByID(anyInt());
102
+        verify(timer, never()).cancelTimer();
103
+    }
104
+
105
+    @Test
106
+    public void testExecute_cancel_wrong_id() throws Exception {
107
+        mockCommandArguments("--cancel", "1", "");
108
+        when(timerManager.hasTimerWithID(1)).thenReturn(false);
109
+        instance.execute(frameContainer, commandArguments, commandContext);
110
+        verify(timerManager, times(1)).hasTimerWithID(1);
111
+        verify(timerManager, never()).getTimerByID(1);
112
+        verify(timer, never()).cancelTimer();
113
+    }
114
+
115
+    @Test
116
+    public void testExecute_cancel_right_id() throws Exception {
117
+        mockCommandArguments("--cancel", "1", "");
118
+        when(timerManager.hasTimerWithID(1)).thenReturn(true);
119
+        instance.execute(frameContainer, commandArguments, commandContext);
120
+        verify(timerManager, times(1)).hasTimerWithID(1);
121
+        verify(timerManager, times(1)).getTimerByID(1);
122
+        verify(timer, times(1)).cancelTimer();
123
+    }
124
+
125
+    @Test
126
+    public void testExecute_list_empty() throws Exception {
127
+        when(timerManager.listTimers()).thenReturn(Sets.newHashSet());
128
+        mockCommandArguments("--list", "", "");
129
+        instance.execute(frameContainer, commandArguments, commandContext);
130
+        verify(timerManager, times(1)).listTimers();
131
+        verify(frameContainer).addLine("commandError", "There are currently no active timers");
132
+    }
133
+
134
+    @Test
135
+    public void testExecute_list_not_empty() throws Exception {
136
+        when(timerManager.listTimers()).thenReturn(
137
+                ImmutableMap.<Integer, TimedCommand>builder().put(1, timer).build().entrySet());
138
+        mockCommandArguments("--list", "", "");
139
+        instance.execute(frameContainer, commandArguments, commandContext);
140
+        verify(timerManager, times(1)).listTimers();
141
+        verify(frameContainer).addLine("commandOutput", "Timer ID: 1 - null");
142
+    }
143
+
144
+    @Test
145
+    public void testExecute_incorrect() throws Exception {
146
+        mockCommandArguments("woop", "woop");
147
+        instance.execute(frameContainer, commandArguments, commandContext);
148
+        verify(frameContainer, times(1)).addLine(eq("commandUsage"), eq('/'),
149
+                eq(TimerCommand.INFO.getName()), eq(TimerCommand.INFO.getHelp()));
150
+    }
151
+
152
+    @Test
153
+    public void testExecute_no_args() throws Exception {
154
+        when(commandArguments.getArguments()).thenReturn(new String[0]);
155
+        instance.execute(frameContainer, commandArguments, commandContext);
156
+        verify(frameContainer, times(1)).addLine(eq("commandUsage"), eq('/'),
157
+                eq(TimerCommand.INFO.getName()), eq(TimerCommand.INFO.getHelp()));
158
+    }
159
+
160
+    @Test
161
+    public void testExecute_too_few_args() throws Exception {
162
+        mockCommandArguments("woop", "woop");
163
+        instance.execute(frameContainer, commandArguments, commandContext);
164
+        verify(frameContainer, times(1)).addLine(eq("commandUsage"), eq('/'),
165
+                eq(TimerCommand.INFO.getName()), eq(TimerCommand.INFO.getHelp()));
166
+    }
167
+
168
+    @Test
169
+    public void testExecute_repetitions_not_number() throws Exception {
170
+        mockCommandArguments("woop", "woop", "woop");
171
+        instance.execute(frameContainer, commandArguments, commandContext);
172
+        verify(frameContainer, times(1)).addLine(eq("commandUsage"), eq('/'),
173
+                eq(TimerCommand.INFO.getName()), eq(TimerCommand.INFO.getHelp()));
174
+    }
175
+
176
+    @Test
177
+    public void testExecute_interval_not_number() throws Exception {
178
+        mockCommandArguments("1", "woop", "woop");
179
+        instance.execute(frameContainer, commandArguments, commandContext);
180
+        verify(frameContainer, times(1)).addLine(eq("commandUsage"), eq('/'),
181
+                eq(TimerCommand.INFO.getName()), eq(TimerCommand.INFO.getHelp()));
182
+    }
183
+
184
+    @Test
185
+    public void testExecute_interval_invalid() throws Exception {
186
+        mockCommandArguments("1", "0", "woop");
187
+        instance.execute(frameContainer, commandArguments, commandContext);
188
+        verify(frameContainer, times(1))
189
+                .addLine(eq("commandError"), eq("Cannot use intervals below 1"));
190
+    }
191
+
192
+    @Test
193
+    public void testExecute_add() throws Exception {
194
+        mockCommandArguments("1", "1", "woop");
195
+        instance.execute(frameContainer, commandArguments, commandContext);
196
+        verify(timerManager).addTimer(1, 1, "woop", frameContainer);
197
+        verify(frameContainer, times(1)).addLine(eq("commandOutput"), eq("Command scheduled."));
198
+    }
199
+
200
+    @Test
201
+    public void testGetSuggestions_first() throws Exception {
202
+        final AdditionalTabTargets targets = instance.getSuggestions(0, intelligentCommandContext);
203
+        assertEquals(Lists.newArrayList("--list", "--cancel"), targets);
204
+    }
205
+
206
+    @Test
207
+    public void testGetSuggestions_not_cancel() throws Exception {
208
+        when(intelligentCommandContext.getPreviousArgs()).thenReturn(Lists.newArrayList("--list"));
209
+        final AdditionalTabTargets targets = instance.getSuggestions(1, intelligentCommandContext);
210
+        assertEquals(Lists.<String>newArrayList(), targets);
211
+    }
212
+
213
+    @Test
214
+    public void testGetSuggestions_second() throws Exception {
215
+        when(intelligentCommandContext.getPreviousArgs())
216
+                .thenReturn(Lists.newArrayList("--cancel"));
217
+        when(timerManager.getTimerIDs()).thenReturn(Sets.newHashSet(1, 2, 3));
218
+        final AdditionalTabTargets targets = instance.getSuggestions(1, intelligentCommandContext);
219
+        assertEquals(Lists.newArrayList("1", "2", "3"), targets);
220
+    }
221
+
222
+    @Test
223
+    public void testGetSuggestions_third() throws Exception {
224
+        final AdditionalTabTargets targets = instance.getSuggestions(3, intelligentCommandContext);
225
+        assertEquals(Lists.<String>newArrayList(), targets);
226
+    }
227
+}

Loading…
İptal
Kaydet