You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimerCommandTest.java 10KB

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