Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ActionModelTest.java 6.0KB

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