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

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