Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ConditionalExecuteCommandTest.java 7.1KB

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