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.

TimedCommandTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.parsers.CommandParser;
  24. import com.dmdirc.interfaces.InputModel;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import java.util.Optional;
  27. import java.util.Timer;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.Mock;
  32. import org.mockito.runners.MockitoJUnitRunner;
  33. import static junit.framework.TestCase.assertEquals;
  34. import static org.mockito.Matchers.anyString;
  35. import static org.mockito.Mockito.never;
  36. import static org.mockito.Mockito.times;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. @RunWith(MockitoJUnitRunner.class)
  40. public class TimedCommandTest {
  41. @Mock private TimerManager timerManager;
  42. @Mock private WindowModel origin;
  43. @Mock private InputModel inputModel;
  44. @Mock private CommandParser commandParser;
  45. @Mock private TimerFactory timerFactory;
  46. @Mock private Timer timer;
  47. private TimedCommand instance;
  48. @Before
  49. public void setUp() throws Exception {
  50. when(timerFactory.getTimer(anyString())).thenReturn(timer);
  51. when(origin.getInputModel()).thenReturn(Optional.of(inputModel));
  52. when(inputModel.getCommandParser()).thenReturn(commandParser);
  53. instance = new TimedCommand(timerManager, 1, 2, 3, "command", origin);
  54. }
  55. @Test
  56. public void testSchedule() throws Exception {
  57. instance.schedule(timerFactory);
  58. verify(timer).schedule(instance, 3000, 3000);
  59. }
  60. @Test
  61. public void testGetCommand() throws Exception {
  62. assertEquals("command", instance.getCommand());
  63. }
  64. @Test
  65. public void testCancelTimer_WithoutSchedule() throws Exception {
  66. instance.cancelTimer();
  67. verify(timerManager).removeTimer(1);
  68. }
  69. @Test
  70. public void testCancelTimer() throws Exception {
  71. instance.schedule(timerFactory);
  72. instance.cancelTimer();
  73. verify(timerManager).removeTimer(1);
  74. verify(timer).cancel();
  75. }
  76. @Test
  77. public void testRun_withoutSchedule() throws Exception {
  78. instance.run();
  79. verify(commandParser, never()).parseCommand(origin, "command");
  80. verify(timerManager, never()).removeTimer(1);
  81. verify(timer, never()).cancel();
  82. }
  83. @Test
  84. public void testRun_LessThanRepetitions() throws Exception {
  85. instance.schedule(timerFactory);
  86. instance.run();
  87. verify(commandParser, times(1)).parseCommand(origin, "command");
  88. verify(timerManager, never()).removeTimer(1);
  89. verify(timer, never()).cancel();
  90. }
  91. @Test
  92. public void testRun_AllRepetitions() throws Exception {
  93. instance.schedule(timerFactory);
  94. instance.run();
  95. instance.run();
  96. verify(commandParser, times(2)).parseCommand(origin, "command");
  97. verify(timerManager, times(1)).removeTimer(1);
  98. verify(timer, times(1)).cancel();
  99. }
  100. }