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.

ActionModelTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2006-2014 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;
  23. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  24. import com.dmdirc.interfaces.ActionController;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.actions.ActionType;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import javax.inject.Provider;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.Mock;
  35. import org.mockito.runners.MockitoJUnitRunner;
  36. import static org.junit.Assert.*;
  37. @RunWith(MockitoJUnitRunner.class)
  38. public class ActionModelTest {
  39. @Mock private Provider<GlobalCommandParser> gcpProvider;
  40. @Mock private ActionSubstitutorFactory substitutorFactory;
  41. @Mock private ActionController actionController;
  42. @Mock private CommandController commandController;
  43. @Mock private AggregateConfigProvider configProvider;
  44. @Test
  45. public void testConditions() {
  46. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  47. assertTrue("ActionModel must start with no conditions",
  48. model.getConditions().isEmpty());
  49. final List<ActionCondition> conds = new ArrayList<>();
  50. model.setConditions(conds);
  51. assertEquals("setConditions must set conditions",
  52. conds, model.getConditions());
  53. }
  54. @Test
  55. public void testTriggers() {
  56. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  57. assertNull("ActionModel must start with null triggers",
  58. model.getTriggers());
  59. final ActionType[] triggers = {CoreActionType.CHANNEL_ACTION};
  60. model.setTriggers(triggers);
  61. assertEquals("setTriggers must set triggers",
  62. Arrays.asList(triggers), Arrays.asList(model.getTriggers()));
  63. }
  64. @Test
  65. public void testNewFormat() {
  66. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  67. assertNull("ActionModel must start with null format",
  68. model.getNewFormat());
  69. model.setNewFormat("");
  70. assertEquals("setNewFormat must set format (empty string)",
  71. "", model.getNewFormat());
  72. model.setNewFormat("newformat");
  73. assertEquals("setNewFormat must set format (non-empty string)",
  74. "newformat", model.getNewFormat());
  75. }
  76. @Test
  77. public void testResponse() {
  78. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  79. assertNull("ActionModel must start with null response",
  80. model.getResponse());
  81. final String[] newResponse = {"a", "b", "c"};
  82. model.setResponse(newResponse);
  83. assertEquals("setResponse must set response",
  84. Arrays.asList(newResponse), Arrays.asList(model.getResponse()));
  85. }
  86. @Test
  87. public void testGroup() {
  88. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  89. assertEquals("ActionModel constructor must set group",
  90. "group", model.getGroup());
  91. model.setGroup("group2");
  92. assertEquals("setGroup must set group",
  93. "group2", model.getGroup());
  94. }
  95. @Test
  96. public void testName() {
  97. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name");
  98. assertEquals("ActionModel constructor must set name",
  99. "name", model.getName());
  100. model.setName("name2");
  101. assertEquals("setName must set name",
  102. "name2", model.getName());
  103. }
  104. @Test
  105. public void testTest() {
  106. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name",
  107. new ActionType[]{CoreActionType.CHANNEL_ACTION},
  108. new String[0], Arrays.asList(new ActionCondition[]{
  109. new ActionCondition(2, CoreActionComponent.STRING_STRING,
  110. CoreActionComparison.STRING_REGEX, ".*e{5}.*"),
  111. new ActionCondition(2, CoreActionComponent.STRING_STRING,
  112. CoreActionComparison.STRING_STARTSWITH, "abc"),
  113. }), ConditionTree.parseString("0|1"), null);
  114. final ActionSubstitutor sub = new ActionSubstitutor(actionController,
  115. commandController, configProvider, CoreActionType.CHANNEL_ACTION);
  116. assertTrue("test must pass if one condition in disjunction passes (cond 1)",
  117. model.test(sub, null, null, "abcdef"));
  118. assertTrue("test must pass if one condition in disjunction passes (cond 2)",
  119. model.test(sub, null, null, "bcdeeeeeeeeef"));
  120. assertFalse("test must fail if all conditions fail",
  121. model.test(sub, null, null, "abeeef"));
  122. }
  123. @Test
  124. public void testTestNoCondTree() {
  125. final ActionModel model = new ActionModel(gcpProvider, substitutorFactory, "group", "name",
  126. new ActionType[]{CoreActionType.CHANNEL_ACTION},
  127. new String[0], Arrays.asList(new ActionCondition[]{
  128. new ActionCondition(2, CoreActionComponent.STRING_STRING,
  129. CoreActionComparison.STRING_REGEX, ".*e{5}.*"),
  130. new ActionCondition(2, CoreActionComponent.STRING_STRING,
  131. CoreActionComparison.STRING_STARTSWITH, "abc"),
  132. }), null, null);
  133. final ActionSubstitutor sub = new ActionSubstitutor(actionController, commandController,
  134. configProvider, CoreActionType.CHANNEL_ACTION);
  135. assertFalse("test must fail if one condition in conjunction fails (cond 1)",
  136. model.test(sub, null, null, "abcdef"));
  137. assertFalse("test must fail if one condition in conjunction fails (cond 2)",
  138. model.test(sub, null, null, "abdeeeeeeeeef"));
  139. assertTrue("test must pass if both conditions in conjunction pass",
  140. model.test(sub, null, null, "abcdeeeeeeeeef"));
  141. }
  142. }