123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.conditional_execute;
-
- import com.dmdirc.commandparser.CommandArguments;
- import com.dmdirc.commandparser.commands.context.CommandContext;
- import com.dmdirc.commandparser.parsers.CommandParser;
- import com.dmdirc.events.CommandErrorEvent;
- import com.dmdirc.interfaces.CommandController;
- import com.dmdirc.events.eventbus.EventBus;
- import com.dmdirc.interfaces.InputModel;
- import com.dmdirc.interfaces.WindowModel;
-
- import java.util.Optional;
-
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.ArgumentCaptor;
- import org.mockito.Captor;
- import org.mockito.Mock;
- import org.mockito.runners.MockitoJUnitRunner;
-
- import static org.junit.Assert.assertTrue;
- import static org.mockito.Matchers.anyString;
- import static org.mockito.Matchers.eq;
- import static org.mockito.Matchers.same;
- import static org.mockito.Mockito.never;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
-
- @RunWith(MockitoJUnitRunner.class)
- public class ConditionalExecuteCommandTest {
-
- @Mock private CommandController commandController;
- @Mock private CommandParser commandParser;
- @Mock private WindowModel container;
- @Mock private InputModel inputModel;
- @Mock private CommandContext context;
- @Mock private EventBus eventbus;
- @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
- private ConditionalExecuteCommand command;
-
- @Before
- public void setup() {
- when(commandController.getCommandChar()).thenReturn('/');
- when(commandController.getSilenceChar()).thenReturn('/');
- when(container.getInputModel()).thenReturn(Optional.of(inputModel));
- when(inputModel.getCommandParser()).thenReturn(commandParser);
- when(container.getEventBus()).thenReturn(eventbus);
-
- command = new ConditionalExecuteCommand(commandController);
- }
-
- /** Tests that {@code --namespace} with no args shows an error. */
- @Test
- public void testWithNoNamespace() {
- execute("/command --namespace");
- verifyErrorOutput("must specify a namespace");
- }
-
- /** Tests that passing a command without a namespace shows an error. */
- @Test
- public void testExecutingWithNoNamespace() {
- execute("/command /echo test");
- verifyErrorOutput("must specify a namespace");
- }
-
- /** Tests that passing a command with a previously unused namespace executes it. */
- @Test
- public void testExecutingWithNewNamespace() {
- execute("/command --namespace foo /echo test");
- verifyCommandExecuted("/echo test");
- }
-
- /** Tests that passing a command with an inhibited namespace does not execute it. */
- @Test
- public void testExecutingWithInhibitedNamespace() {
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo /echo test");
- verifyNoCommandExecuted();
- }
-
- /** Tests that passing a command with a forced namespace executes it. */
- @Test
- public void testExecutingWithForcedNamespace() {
- execute("/command --namespace foo --force");
- execute("/command --namespace foo /echo test");
- verifyCommandExecuted("/echo test");
- }
-
- /** Tests that passing a command with an inhibited then forced namespace executes it. */
- @Test
- public void testExecutingWithInhibitedThenForcedNamespace() {
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo --force");
- execute("/command --namespace foo /echo test");
- verifyCommandExecuted("/echo test");
- }
-
- /** Tests that passing a command with an inhibited then allowed namespace executes it. */
- @Test
- public void testExecutingWithInhibitedThenAllowedNamespace() {
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo --allow");
- execute("/command --namespace foo /echo test");
- verifyCommandExecuted("/echo test");
- }
-
- /** Tests that passing a command with an inhibited then removed namespace executes it. */
- @Test
- public void testExecutingWithInhibitedThenRemovedNamespace() {
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo --remove");
- execute("/command --namespace foo /echo test");
- verifyCommandExecuted("/echo test");
- }
-
- /** Tests that passing a command with a forced then inhibited namespace does not execute it. */
- @Test
- public void testExecutingWithForcedThenInhibitedNamespace() {
- execute("/command --namespace foo --force");
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo /echo test");
- verifyNoCommandExecuted();
- }
-
- /**
- * Tests that passing a command with a new namespace and {@link --inverse} does not execute it.
- */
- @Test
- public void testInverseExecutingWithNewNamespace() {
- execute("/command --namespace foo --inverse /echo test");
- verifyNoCommandExecuted();
- }
-
- /**
- * Tests that passing a command with an inhibited namespace and {@link --inverse} does not
- * execute it. Inverse doesn't apply to inhibited namespaces.
- */
- @Test
- public void testInverseExecutingWithInhibitedNamespace() {
- execute("/command --namespace foo --inhibit");
- execute("/command --namespace foo --inverse /echo test");
- verifyNoCommandExecuted();
- }
-
- private void execute(final String line) {
- command.execute(container, new CommandArguments(commandController, line), context);
- }
-
- private void verifyCommandExecuted(final String command) {
- verify(commandParser).parseCommand(same(container), eq(command));
- }
-
- private void verifyNoCommandExecuted() {
- verify(commandParser, never()).parseCommand(same(container), anyString());
- }
-
- private void verifyErrorOutput(final CharSequence substring) {
- verify(eventbus).publishAsync(errorEventCaptor.capture());
- assertTrue(errorEventCaptor.getValue().getMessage().contains(substring));
- }
-
- }
|