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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.TestMain;
  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 java.util.ArrayList;
  32. import java.util.List;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import static org.junit.Assert.*;
  36. import static org.mockito.Mockito.*;
  37. public class AliasWrapperTest {
  38. private Action action;
  39. private ActionCondition condition;
  40. private List<ActionCondition> conditions;
  41. private AliasWrapper aliasWrapper;
  42. @Before
  43. public void setup() {
  44. final CommandController controller = mock(CommandController.class);
  45. when(controller.getCommandChar()).thenReturn('!');
  46. action = mock(Action.class);
  47. condition = mock(ActionCondition.class);
  48. conditions = new ArrayList<>();
  49. aliasWrapper = new AliasWrapper(controller, TestMain.getTestMain().getServerManager());
  50. when(condition.getArg()).thenReturn(1);
  51. when(condition.getComparison()).thenReturn(CoreActionComparison.STRING_EQUALS);
  52. when(condition.getComponent()).thenReturn(CoreActionComponent.STRING_STRING);
  53. when(condition.getTarget()).thenReturn("name");
  54. when(action.getConditions()).thenReturn(conditions);
  55. when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.UNKNOWN_COMMAND });
  56. }
  57. @Test
  58. public void testAddsAlias() {
  59. conditions.add(condition);
  60. aliasWrapper.add(action);
  61. assertFalse(aliasWrapper.getAliases().isEmpty());
  62. }
  63. @Test
  64. public void testDoesNotAddActionWithNoConditions() {
  65. aliasWrapper.add(action);
  66. assertTrue(aliasWrapper.getAliases().isEmpty());
  67. }
  68. @Test
  69. public void testDoesNotAddActionWithWrongTrigger() {
  70. when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.ACTION_CREATED });
  71. aliasWrapper.add(action);
  72. assertTrue(aliasWrapper.getAliases().isEmpty());
  73. }
  74. @Test
  75. public void testGetsCommandName() {
  76. conditions.add(condition);
  77. assertEquals("!name", aliasWrapper.getCommandName(action));
  78. }
  79. @Test
  80. public void testGetsCommandNameWithMultipleConditions() {
  81. final ActionCondition otherCondition = mock(ActionCondition.class);
  82. when(otherCondition.getArg()).thenReturn(7);
  83. conditions.add(otherCondition);
  84. conditions.add(condition);
  85. assertEquals("!name", aliasWrapper.getCommandName(action));
  86. }
  87. @Test
  88. public void testGetCommandNameReturnsNullIfUnknown() {
  89. assertNull(aliasWrapper.getCommandName(action));
  90. }
  91. }