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

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