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.6KB

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