您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TimerCommandTest.java 10KB

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