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.

ConditionalExecuteCommandTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.conditional_execute;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.commands.context.CommandContext;
  20. import com.dmdirc.commandparser.parsers.CommandParser;
  21. import com.dmdirc.events.CommandErrorEvent;
  22. import com.dmdirc.interfaces.CommandController;
  23. import com.dmdirc.interfaces.EventBus;
  24. import com.dmdirc.interfaces.InputModel;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import java.util.Optional;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.ArgumentCaptor;
  31. import org.mockito.Captor;
  32. import org.mockito.Mock;
  33. import org.mockito.runners.MockitoJUnitRunner;
  34. import static org.junit.Assert.assertTrue;
  35. import static org.mockito.Matchers.anyString;
  36. import static org.mockito.Matchers.eq;
  37. import static org.mockito.Matchers.same;
  38. import static org.mockito.Mockito.never;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. @RunWith(MockitoJUnitRunner.class)
  42. public class ConditionalExecuteCommandTest {
  43. @Mock private CommandController commandController;
  44. @Mock private CommandParser commandParser;
  45. @Mock private WindowModel container;
  46. @Mock private InputModel inputModel;
  47. @Mock private CommandContext context;
  48. @Mock private EventBus eventbus;
  49. @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
  50. private ConditionalExecuteCommand command;
  51. @Before
  52. public void setup() {
  53. when(commandController.getCommandChar()).thenReturn('/');
  54. when(commandController.getSilenceChar()).thenReturn('/');
  55. when(container.getInputModel()).thenReturn(Optional.of(inputModel));
  56. when(inputModel.getCommandParser()).thenReturn(commandParser);
  57. when(container.getEventBus()).thenReturn(eventbus);
  58. command = new ConditionalExecuteCommand(commandController);
  59. }
  60. /** Tests that {@code --namespace} with no args shows an error. */
  61. @Test
  62. public void testWithNoNamespace() {
  63. execute("/command --namespace");
  64. verifyErrorOutput("must specify a namespace");
  65. }
  66. /** Tests that passing a command without a namespace shows an error. */
  67. @Test
  68. public void testExecutingWithNoNamespace() {
  69. execute("/command /echo test");
  70. verifyErrorOutput("must specify a namespace");
  71. }
  72. /** Tests that passing a command with a previously unused namespace executes it. */
  73. @Test
  74. public void testExecutingWithNewNamespace() {
  75. execute("/command --namespace foo /echo test");
  76. verifyCommandExecuted("/echo test");
  77. }
  78. /** Tests that passing a command with an inhibited namespace does not execute it. */
  79. @Test
  80. public void testExecutingWithInhibitedNamespace() {
  81. execute("/command --namespace foo --inhibit");
  82. execute("/command --namespace foo /echo test");
  83. verifyNoCommandExecuted();
  84. }
  85. /** Tests that passing a command with a forced namespace executes it. */
  86. @Test
  87. public void testExecutingWithForcedNamespace() {
  88. execute("/command --namespace foo --force");
  89. execute("/command --namespace foo /echo test");
  90. verifyCommandExecuted("/echo test");
  91. }
  92. /** Tests that passing a command with an inhibited then forced namespace executes it. */
  93. @Test
  94. public void testExecutingWithInhibitedThenForcedNamespace() {
  95. execute("/command --namespace foo --inhibit");
  96. execute("/command --namespace foo --force");
  97. execute("/command --namespace foo /echo test");
  98. verifyCommandExecuted("/echo test");
  99. }
  100. /** Tests that passing a command with an inhibited then allowed namespace executes it. */
  101. @Test
  102. public void testExecutingWithInhibitedThenAllowedNamespace() {
  103. execute("/command --namespace foo --inhibit");
  104. execute("/command --namespace foo --allow");
  105. execute("/command --namespace foo /echo test");
  106. verifyCommandExecuted("/echo test");
  107. }
  108. /** Tests that passing a command with an inhibited then removed namespace executes it. */
  109. @Test
  110. public void testExecutingWithInhibitedThenRemovedNamespace() {
  111. execute("/command --namespace foo --inhibit");
  112. execute("/command --namespace foo --remove");
  113. execute("/command --namespace foo /echo test");
  114. verifyCommandExecuted("/echo test");
  115. }
  116. /** Tests that passing a command with a forced then inhibited namespace does not execute it. */
  117. @Test
  118. public void testExecutingWithForcedThenInhibitedNamespace() {
  119. execute("/command --namespace foo --force");
  120. execute("/command --namespace foo --inhibit");
  121. execute("/command --namespace foo /echo test");
  122. verifyNoCommandExecuted();
  123. }
  124. /**
  125. * Tests that passing a command with a new namespace and {@link --inverse} does not execute it.
  126. */
  127. @Test
  128. public void testInverseExecutingWithNewNamespace() {
  129. execute("/command --namespace foo --inverse /echo test");
  130. verifyNoCommandExecuted();
  131. }
  132. /**
  133. * Tests that passing a command with an inhibited namespace and {@link --inverse} does not
  134. * execute it. Inverse doesn't apply to inhibited namespaces.
  135. */
  136. @Test
  137. public void testInverseExecutingWithInhibitedNamespace() {
  138. execute("/command --namespace foo --inhibit");
  139. execute("/command --namespace foo --inverse /echo test");
  140. verifyNoCommandExecuted();
  141. }
  142. private void execute(final String line) {
  143. command.execute(container, new CommandArguments(commandController, line), context);
  144. }
  145. private void verifyCommandExecuted(final String command) {
  146. verify(commandParser).parseCommand(same(container), eq(command));
  147. }
  148. private void verifyNoCommandExecuted() {
  149. verify(commandParser, never()).parseCommand(same(container), anyString());
  150. }
  151. private void verifyErrorOutput(final CharSequence substring) {
  152. verify(eventbus).publishAsync(errorEventCaptor.capture());
  153. assertTrue(errorEventCaptor.getValue().getMessage().contains(substring));
  154. }
  155. }