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.

AliasWrapperTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2013 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.actions.wrappers;
  23. import com.dmdirc.ServerManager;
  24. import com.dmdirc.actions.Action;
  25. import com.dmdirc.actions.ActionCondition;
  26. import com.dmdirc.actions.CoreActionComparison;
  27. import com.dmdirc.actions.CoreActionComponent;
  28. import com.dmdirc.actions.CoreActionType;
  29. import com.dmdirc.interfaces.CommandController;
  30. import com.dmdirc.interfaces.actions.ActionType;
  31. import com.dmdirc.logger.ErrorManager;
  32. import com.dmdirc.logger.Logger;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import org.junit.Before;
  36. import org.junit.BeforeClass;
  37. import org.junit.Test;
  38. import org.junit.runner.RunWith;
  39. import org.mockito.Mock;
  40. import org.mockito.runners.MockitoJUnitRunner;
  41. import static org.junit.Assert.*;
  42. import static org.mockito.Mockito.*;
  43. @RunWith(MockitoJUnitRunner.class)
  44. public class AliasWrapperTest {
  45. @Mock private Action action;
  46. @Mock private ActionCondition condition;
  47. @Mock private ServerManager serverManager;
  48. @Mock private CommandController commandController;
  49. private List<ActionCondition> conditions;
  50. private AliasWrapper aliasWrapper;
  51. @BeforeClass
  52. public static void overrideLogger() {
  53. // We expect errors to be generated, so mock out the error manager.
  54. Logger.setErrorManager(mock(ErrorManager.class));
  55. }
  56. @Before
  57. public void setup() {
  58. when(commandController.getCommandChar()).thenReturn('!');
  59. conditions = new ArrayList<>();
  60. aliasWrapper = new AliasWrapper(commandController, serverManager);
  61. when(condition.getArg()).thenReturn(1);
  62. when(condition.getComparison()).thenReturn(CoreActionComparison.STRING_EQUALS);
  63. when(condition.getComponent()).thenReturn(CoreActionComponent.STRING_STRING);
  64. when(condition.getTarget()).thenReturn("name");
  65. when(action.getConditions()).thenReturn(conditions);
  66. when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.UNKNOWN_COMMAND });
  67. }
  68. @Test
  69. public void testAddsAlias() {
  70. conditions.add(condition);
  71. aliasWrapper.add(action);
  72. assertFalse(aliasWrapper.getAliases().isEmpty());
  73. }
  74. @Test
  75. public void testDoesNotAddActionWithNoConditions() {
  76. aliasWrapper.add(action);
  77. assertTrue(aliasWrapper.getAliases().isEmpty());
  78. }
  79. @Test
  80. public void testDoesNotAddActionWithWrongTrigger() {
  81. when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.ACTION_CREATED });
  82. aliasWrapper.add(action);
  83. assertTrue(aliasWrapper.getAliases().isEmpty());
  84. }
  85. @Test
  86. public void testGetsCommandName() {
  87. conditions.add(condition);
  88. assertEquals("!name", aliasWrapper.getCommandName(action));
  89. }
  90. @Test
  91. public void testGetsCommandNameWithMultipleConditions() {
  92. final ActionCondition otherCondition = mock(ActionCondition.class);
  93. when(otherCondition.getArg()).thenReturn(7);
  94. conditions.add(otherCondition);
  95. conditions.add(condition);
  96. assertEquals("!name", aliasWrapper.getCommandName(action));
  97. }
  98. @Test
  99. public void testGetCommandNameReturnsNullIfUnknown() {
  100. assertNull(aliasWrapper.getCommandName(action));
  101. }
  102. }