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.5KB

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