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 6.9KB

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