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