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

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