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.

ActionConditionTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config.IdentityManager;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. import static org.junit.Assert.*;
  27. public class ActionConditionTest {
  28. @BeforeClass
  29. public static void setUp() throws Exception {
  30. IdentityManager.load();
  31. ActionManager.init();
  32. }
  33. @Test
  34. public void testConstructor1() {
  35. final ActionCondition ac = new ActionCondition(1, CoreActionComponent.STRING_STRING,
  36. CoreActionComparison.STRING_STARTSWITH, "foo");
  37. assertEquals(1, ac.getArg());
  38. assertEquals(CoreActionComponent.STRING_STRING, ac.getComponent());
  39. assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
  40. assertEquals("foo", ac.getTarget());
  41. }
  42. @Test
  43. public void testConstructor2() {
  44. final ActionCondition ac = new ActionCondition("foobarbaz",
  45. CoreActionComparison.STRING_STARTSWITH, "foo");
  46. assertEquals("foobarbaz", ac.getStarget());
  47. assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
  48. assertEquals("foo", ac.getTarget());
  49. }
  50. @Test
  51. public void testTest1() {
  52. final ActionCondition ac = new ActionCondition(1, CoreActionComponent.STRING_STRING,
  53. CoreActionComparison.STRING_STARTSWITH, "foo");
  54. assertTrue(ac.test(new ActionSubstitutor(CoreActionType.CLIENT_USER_INPUT), null, "foo bar"));
  55. }
  56. @Test
  57. public void testTest2() {
  58. final ActionCondition ac = new ActionCondition("foobarbaz",
  59. CoreActionComparison.STRING_STARTSWITH, "foo");
  60. assertTrue(ac.test(new ActionSubstitutor(CoreActionType.CLIENT_CLOSED)));
  61. }
  62. @Test
  63. public void testSetters() {
  64. final ActionCondition ac = new ActionCondition("foobarbaz",
  65. CoreActionComparison.STRING_STARTSWITH, "foo");
  66. assertEquals("foo", ac.getTarget());
  67. ac.setTarget("bar");
  68. assertEquals("bar", ac.getTarget());
  69. assertEquals("foobarbaz", ac.getStarget());
  70. ac.setStarget("bar");
  71. assertEquals("bar", ac.getStarget());
  72. assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
  73. ac.setComparison(CoreActionComparison.STRING_EQUALS);
  74. assertEquals(CoreActionComparison.STRING_EQUALS, ac.getComparison());
  75. ac.setComponent(CoreActionComponent.STRING_STRING);
  76. assertEquals(CoreActionComponent.STRING_STRING, ac.getComponent());
  77. ac.setArg(0);
  78. assertEquals(0, ac.getArg());
  79. }
  80. @Test
  81. public void testToString() {
  82. final ActionCondition ac1 = new ActionCondition("foobarbaz",
  83. CoreActionComparison.STRING_STARTSWITH, "foo");
  84. assertTrue(ac1.toString().indexOf("foo") > -1);
  85. assertTrue(ac1.toString().indexOf("foobarbaz") > -1);
  86. assertTrue(ac1.toString().indexOf(CoreActionComparison.STRING_STARTSWITH.toString()) > -1);
  87. }
  88. @Test
  89. public void testEquals() {
  90. final ActionCondition ac1 = new ActionCondition("foobarbaz",
  91. CoreActionComparison.STRING_STARTSWITH, "foo");
  92. final ActionCondition ac2 = new ActionCondition("foobarbaz",
  93. CoreActionComparison.STRING_STARTSWITH, "foo");
  94. assertTrue(ac1.equals(ac2));
  95. assertTrue(ac2.equals(ac1));
  96. assertFalse(ac1.equals(null));
  97. assertFalse(ac1.equals("foo"));
  98. assertEquals(ac1.hashCode(), ac2.hashCode());
  99. ac2.setStarget("bar");
  100. assertFalse(ac2.equals(ac1));
  101. assertFalse(ac1.equals(ac2));
  102. ac2.setStarget("foobarbaz");
  103. ac2.setComponent(CoreActionComponent.STRING_STRING);
  104. ac2.setArg(1);
  105. assertFalse(ac2.equals(ac1));
  106. assertFalse(ac1.equals(ac2));
  107. ac1.setComponent(CoreActionComponent.STRING_STRING);
  108. ac1.setArg(1);
  109. assertTrue(ac1.equals(ac2));
  110. assertTrue(ac2.equals(ac1));
  111. assertEquals(ac1.hashCode(), ac2.hashCode());
  112. ac1.setComponent(CoreActionComponent.STRING_LENGTH);
  113. assertFalse(ac2.equals(ac1));
  114. assertFalse(ac1.equals(ac2));
  115. ac1.setComponent(CoreActionComponent.STRING_STRING);
  116. ac1.setComparison(CoreActionComparison.STRING_NCONTAINS);
  117. assertFalse(ac2.equals(ac1));
  118. assertFalse(ac1.equals(ac2));
  119. ac1.setComparison(CoreActionComparison.STRING_STARTSWITH);
  120. ac1.setTarget("flub");
  121. assertFalse(ac2.equals(ac1));
  122. assertFalse(ac1.equals(ac2));
  123. }
  124. }