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

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