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

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