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.

AutoCommandHandlerTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.commandparser.auto;
  23. import com.dmdirc.GlobalWindow;
  24. import com.dmdirc.commandparser.parsers.CommandParser;
  25. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  26. import com.dmdirc.config.profiles.Profile;
  27. import com.dmdirc.events.ClientOpenedEvent;
  28. import com.dmdirc.events.ServerConnectedEvent;
  29. import com.dmdirc.interfaces.CommandController;
  30. import com.dmdirc.interfaces.Connection;
  31. import com.dmdirc.interfaces.InputModel;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import java.util.Optional;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.mockito.Mock;
  38. import org.mockito.runners.MockitoJUnitRunner;
  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 AutoCommandHandlerTest {
  45. @Mock private CommandController commandController;
  46. @Mock private GlobalCommandParser globalCommandParser;
  47. @Mock private GlobalWindow globalWindow;
  48. @Mock private AutoCommand autoCommand;
  49. @Mock private ClientOpenedEvent clientOpenedEvent;
  50. @Mock private ServerConnectedEvent serverConnectedEvent;
  51. @Mock private Connection connection;
  52. @Mock private Profile profile;
  53. @Mock private WindowModel container;
  54. @Mock private InputModel inputModel;
  55. @Mock private CommandParser commandParser;
  56. private AutoCommandHandler autoCommandHandler;
  57. @Before
  58. public void setup() {
  59. when(autoCommand.getProfile()).thenReturn(Optional.of("profile"));
  60. when(autoCommand.getResponse()).thenReturn("Testing123");
  61. when(autoCommand.getServer()).thenReturn(Optional.<String>empty());
  62. when(autoCommand.getNetwork()).thenReturn(Optional.<String>empty());
  63. when(serverConnectedEvent.getConnection()).thenReturn(connection);
  64. when(connection.getProfile()).thenReturn(profile);
  65. when(connection.getWindowModel()).thenReturn(container);
  66. when(connection.getAddress()).thenReturn("irc.quakenet.org");
  67. when(connection.getNetwork()).thenReturn("Quakenet");
  68. when(profile.getName()).thenReturn("profile");
  69. when(container.getInputModel()).thenReturn(Optional.of(inputModel));
  70. when(inputModel.getCommandParser()).thenReturn(commandParser);
  71. autoCommandHandler = new AutoCommandHandler(commandController, globalCommandParser,
  72. globalWindow, autoCommand);
  73. }
  74. @Test
  75. public void testCheckAutoCommandWithGlobal() {
  76. autoCommandHandler.checkAutoCommand(clientOpenedEvent);
  77. verify(globalCommandParser).parseCommand(globalWindow,
  78. commandController.getCommandChar() + autoCommand.getResponse());
  79. }
  80. @Test
  81. public void testCheckAutoCommandWithoutGlobal() {
  82. when(autoCommand.getNetwork()).thenReturn(Optional.of("Quakenet"));
  83. autoCommandHandler.checkAutoCommand(clientOpenedEvent);
  84. verify(globalCommandParser, never()).parseCommand(globalWindow,
  85. commandController.getCommandChar() + autoCommand.getResponse());
  86. }
  87. @Test
  88. public void testCheckAutoCommandNoServerOrNetwork() {
  89. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  90. verify(globalCommandParser, never()).parseCommand(globalWindow,
  91. commandController.getCommandChar() + autoCommand.getResponse());
  92. verify(commandParser, never()).parseCommand(container,
  93. commandController.getCommandChar() + autoCommand.getResponse());
  94. }
  95. @Test
  96. public void testCheckAutoCommandNoProfile() {
  97. when(autoCommand.getNetwork()).thenReturn(Optional.of("Quakenet"));
  98. when(autoCommand.getProfile()).thenReturn(Optional.of("profile1"));
  99. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  100. verify(globalCommandParser, never()).parseCommand(globalWindow,
  101. commandController.getCommandChar() + autoCommand.getResponse());
  102. verify(commandParser, never()).parseCommand(container,
  103. commandController.getCommandChar() + autoCommand.getResponse());
  104. }
  105. @Test
  106. public void testCheckAutoCommandWithProfile() {
  107. when(autoCommand.getNetwork()).thenReturn(Optional.of("Quakenet"));
  108. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  109. verify(globalCommandParser, never()).parseCommand(globalWindow,
  110. commandController.getCommandChar() + autoCommand.getResponse());
  111. verify(commandParser, times(1)).parseCommand(container,
  112. commandController.getCommandChar() + autoCommand.getResponse());
  113. }
  114. @Test
  115. public void testCheckAutoCommandWithProfileNoServer() {
  116. when(autoCommand.getServer()).thenReturn(Optional.of("server"));
  117. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  118. verify(globalCommandParser, never()).parseCommand(globalWindow,
  119. commandController.getCommandChar() + autoCommand.getResponse());
  120. verify(commandParser, never()).parseCommand(container,
  121. commandController.getCommandChar() + autoCommand.getResponse());
  122. }
  123. @Test
  124. public void testCheckAutoCommandWithProfileNoServerOrNetwork() {
  125. when(autoCommand.getNetwork()).thenReturn(Optional.of("network"));
  126. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  127. verify(globalCommandParser, never()).parseCommand(globalWindow,
  128. commandController.getCommandChar() + autoCommand.getResponse());
  129. verify(commandParser, never()).parseCommand(container,
  130. commandController.getCommandChar() + autoCommand.getResponse());
  131. }
  132. @Test
  133. public void testCheckAutoCommandWithProfileWithServer() {
  134. when(autoCommand.getNetwork()).thenReturn(Optional.of("Quakenet"));
  135. autoCommandHandler.checkAutoCommand(serverConnectedEvent);
  136. verify(globalCommandParser, never()).parseCommand(globalWindow,
  137. commandController.getCommandChar() + autoCommand.getResponse());
  138. verify(commandParser, times(1)).parseCommand(container,
  139. commandController.getCommandChar() + autoCommand.getResponse());
  140. }
  141. @Test
  142. public void testCheckAutoCommandMultipleLines() {
  143. when(autoCommand.getResponse()).thenReturn("Testing\n123");
  144. autoCommandHandler.checkAutoCommand(clientOpenedEvent);
  145. verify(globalCommandParser, times(1)).parseCommand(globalWindow,
  146. commandController.getCommandChar() + "Testing");
  147. verify(globalCommandParser, times(1)).parseCommand(globalWindow,
  148. commandController.getCommandChar() + "123");
  149. }
  150. }