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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.time;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.commands.IntelligentCommand.IntelligentCommandContext;
  20. import com.dmdirc.commandparser.commands.context.CommandContext;
  21. import com.dmdirc.events.CommandErrorEvent;
  22. import com.dmdirc.events.CommandOutputEvent;
  23. import com.dmdirc.interfaces.CommandController;
  24. import com.dmdirc.events.eventbus.EventBus;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import com.dmdirc.ui.input.AdditionalTabTargets;
  27. import com.google.common.collect.ImmutableMap;
  28. import com.google.common.collect.Lists;
  29. import com.google.common.collect.Sets;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.mockito.ArgumentCaptor;
  34. import org.mockito.Captor;
  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.Mockito.never;
  40. import static org.mockito.Mockito.times;
  41. import static org.mockito.Mockito.verify;
  42. import static org.mockito.Mockito.when;
  43. @RunWith(MockitoJUnitRunner.class)
  44. public class TimerCommandTest {
  45. @Mock private TimerManager timerManager;
  46. @Mock private CommandController commandController;
  47. @Mock private WindowModel frameContainer;
  48. @Mock private EventBus eventbus;
  49. @Mock private CommandContext commandContext;
  50. @Mock private IntelligentCommandContext intelligentCommandContext;
  51. @Mock private CommandArguments commandArguments;
  52. @Mock private TimedCommand timer;
  53. @Captor private ArgumentCaptor<CommandOutputEvent> outputEventCaptor;
  54. @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
  55. private TimerCommand instance;
  56. @Before
  57. public void setUp() throws Exception {
  58. instance = new TimerCommand(timerManager, commandController);
  59. when(commandArguments.isSilent()).thenReturn(false);
  60. when(timerManager.getTimerByID(anyInt())).thenReturn(timer);
  61. when(commandController.getCommandChar()).thenReturn('/');
  62. when(frameContainer.getEventBus()).thenReturn(eventbus);
  63. }
  64. private void mockCommandArguments(final String one, final String two) {
  65. when(commandArguments.getArguments()).thenReturn(new String[]{one, two});
  66. when(commandArguments.getArgumentsAsString()).thenReturn((one + ' ' + two).trim());
  67. when(commandArguments.getArgumentsAsString(1)).thenReturn(two.trim());
  68. }
  69. private void mockCommandArguments(final String one, final String two, final String three) {
  70. when(commandArguments.getArguments()).thenReturn(new String[]{one, two, three});
  71. when(commandArguments.getArgumentsAsString()).thenReturn((one + ' ' + two + ' ' + three).trim());
  72. when(commandArguments.getArgumentsAsString(1)).thenReturn((two + ' ' + three).trim());
  73. when(commandArguments.getArgumentsAsString(2)).thenReturn(three.trim());
  74. }
  75. private void mockCommandArguments(final String one, final String two, final String three,
  76. final String four) {
  77. when(commandArguments.getArguments())
  78. .thenReturn(new String[]{one, two, three, four});
  79. when(commandArguments.getArgumentsAsString())
  80. .thenReturn((one + ' ' + two + ' ' + three + ' ' + four).trim());
  81. when(commandArguments.getArgumentsAsString(1))
  82. .thenReturn((two + ' ' + three + ' ' + four).trim());
  83. when(commandArguments.getArgumentsAsString(2))
  84. .thenReturn((three + ' ' + four).trim());
  85. }
  86. @Test
  87. public void testExecute_cancel_not_number() throws Exception {
  88. mockCommandArguments("--cancel", "one", "");
  89. instance.execute(frameContainer, commandArguments, commandContext);
  90. verify(timerManager, never()).hasTimerWithID(anyInt());
  91. verify(timerManager, never()).getTimerByID(anyInt());
  92. verify(timer, never()).cancelTimer();
  93. }
  94. @Test
  95. public void testExecute_cancel_wrong_id() throws Exception {
  96. mockCommandArguments("--cancel", "1", "");
  97. when(timerManager.hasTimerWithID(1)).thenReturn(false);
  98. instance.execute(frameContainer, commandArguments, commandContext);
  99. verify(timerManager, times(1)).hasTimerWithID(1);
  100. verify(timerManager, never()).getTimerByID(1);
  101. verify(timer, never()).cancelTimer();
  102. }
  103. @Test
  104. public void testExecute_cancel_right_id() throws Exception {
  105. mockCommandArguments("--cancel", "1", "");
  106. when(timerManager.hasTimerWithID(1)).thenReturn(true);
  107. instance.execute(frameContainer, commandArguments, commandContext);
  108. verify(timerManager, times(1)).hasTimerWithID(1);
  109. verify(timerManager, times(1)).getTimerByID(1);
  110. verify(timer, times(1)).cancelTimer();
  111. }
  112. @Test
  113. public void testExecute_list_empty() throws Exception {
  114. when(timerManager.listTimers()).thenReturn(Sets.newHashSet());
  115. mockCommandArguments("--list", "", "");
  116. instance.execute(frameContainer, commandArguments, commandContext);
  117. verify(timerManager, times(1)).listTimers();
  118. verify(eventbus).publishAsync(errorEventCaptor.capture());
  119. assertEquals("There are currently no active timers", errorEventCaptor.getValue().getMessage());
  120. }
  121. @Test
  122. public void testExecute_list_not_empty() throws Exception {
  123. when(timerManager.listTimers()).thenReturn(
  124. ImmutableMap.<Integer, TimedCommand>builder().put(1, timer).build().entrySet());
  125. mockCommandArguments("--list", "", "");
  126. instance.execute(frameContainer, commandArguments, commandContext);
  127. verify(timerManager, times(1)).listTimers();
  128. verify(eventbus).publishAsync(outputEventCaptor.capture());
  129. assertEquals("Timer ID: 1 - null", outputEventCaptor.getValue().getMessage());
  130. }
  131. @Test
  132. public void testExecute_incorrect() throws Exception {
  133. mockCommandArguments("woop", "woop");
  134. instance.execute(frameContainer, commandArguments, commandContext);
  135. verify(eventbus).publishAsync(errorEventCaptor.capture());
  136. }
  137. @Test
  138. public void testExecute_no_args() throws Exception {
  139. when(commandArguments.getArguments()).thenReturn(new String[0]);
  140. instance.execute(frameContainer, commandArguments, commandContext);
  141. verify(eventbus).publishAsync(errorEventCaptor.capture());
  142. }
  143. @Test
  144. public void testExecute_too_few_args() throws Exception {
  145. mockCommandArguments("woop", "woop");
  146. instance.execute(frameContainer, commandArguments, commandContext);
  147. verify(eventbus).publishAsync(errorEventCaptor.capture());
  148. }
  149. @Test
  150. public void testExecute_repetitions_not_number() throws Exception {
  151. mockCommandArguments("woop", "woop", "woop");
  152. instance.execute(frameContainer, commandArguments, commandContext);
  153. verify(eventbus).publishAsync(errorEventCaptor.capture());
  154. }
  155. @Test
  156. public void testExecute_interval_not_number() throws Exception {
  157. mockCommandArguments("1", "woop", "woop");
  158. instance.execute(frameContainer, commandArguments, commandContext);
  159. verify(eventbus).publishAsync(errorEventCaptor.capture());
  160. }
  161. @Test
  162. public void testExecute_interval_invalid() throws Exception {
  163. mockCommandArguments("1", "0", "woop");
  164. instance.execute(frameContainer, commandArguments, commandContext);
  165. verify(eventbus).publishAsync(errorEventCaptor.capture());
  166. assertEquals("Cannot use intervals below 1", errorEventCaptor.getValue().getMessage());
  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(eventbus).publishAsync(outputEventCaptor.capture());
  174. assertEquals("Command scheduled.", outputEventCaptor.getValue().getMessage());
  175. }
  176. @Test
  177. public void testGetSuggestions_first() throws Exception {
  178. final AdditionalTabTargets targets = instance.getSuggestions(0, intelligentCommandContext);
  179. assertEquals(Lists.newArrayList("--list", "--cancel"), targets);
  180. }
  181. @Test
  182. public void testGetSuggestions_not_cancel() throws Exception {
  183. when(intelligentCommandContext.getPreviousArgs()).thenReturn(Lists.newArrayList("--list"));
  184. final AdditionalTabTargets targets = instance.getSuggestions(1, intelligentCommandContext);
  185. assertEquals(Lists.<String>newArrayList(), targets);
  186. }
  187. @Test
  188. public void testGetSuggestions_second() throws Exception {
  189. when(intelligentCommandContext.getPreviousArgs())
  190. .thenReturn(Lists.newArrayList("--cancel"));
  191. when(timerManager.getTimerIDs()).thenReturn(Sets.newHashSet(1, 2, 3));
  192. final AdditionalTabTargets targets = instance.getSuggestions(1, intelligentCommandContext);
  193. assertEquals(Lists.newArrayList("1", "2", "3"), targets);
  194. }
  195. @Test
  196. public void testGetSuggestions_third() throws Exception {
  197. final AdditionalTabTargets targets = instance.getSuggestions(3, intelligentCommandContext);
  198. assertEquals(Lists.<String>newArrayList(), targets);
  199. }
  200. }