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.

ActionTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.config.prefs.PreferencesSetting;
  24. import com.dmdirc.config.prefs.PreferencesType;
  25. import com.dmdirc.interfaces.ActionController;
  26. import com.dmdirc.interfaces.actions.ActionType;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.dmdirc.interfaces.config.ConfigProvider;
  29. import com.dmdirc.interfaces.config.IdentityController;
  30. import com.dmdirc.logger.ErrorManager;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.util.io.ConfigFile;
  33. import com.dmdirc.util.io.InvalidConfigFileException;
  34. import java.io.File;
  35. import java.io.IOException;
  36. import java.util.ArrayList;
  37. import java.util.Arrays;
  38. import java.util.HashMap;
  39. import java.util.Map;
  40. import org.junit.After;
  41. import org.junit.Before;
  42. import org.junit.BeforeClass;
  43. import org.junit.Test;
  44. import org.junit.runner.RunWith;
  45. import org.mockito.Mock;
  46. import org.mockito.runners.MockitoJUnitRunner;
  47. import static org.junit.Assert.*;
  48. import static org.mockito.Mockito.*;
  49. @RunWith(MockitoJUnitRunner.class)
  50. public class ActionTest {
  51. @Mock private ActionController actionController;
  52. @Mock private ActionGroup actionGroup;
  53. @Mock private IdentityController identityController;
  54. @Mock private ConfigProvider configProvider;
  55. @Mock private AggregateConfigProvider aggregateConfigProvider;
  56. private Map<String, PreferencesSetting> prefs;
  57. private String testDirectory;
  58. @BeforeClass
  59. public static void stubLogger() {
  60. Logger.setErrorManager(mock(ErrorManager.class));
  61. }
  62. @Before
  63. public void makeTestDirectory() throws IOException {
  64. File tempFile = File.createTempFile("dmdirc", "test");
  65. tempFile.delete();
  66. tempFile.mkdir();
  67. testDirectory = tempFile.getAbsolutePath() + File.separator;
  68. }
  69. @Before
  70. public void setupActionController() {
  71. prefs = new HashMap<>();
  72. when(actionController.getOrCreateGroup(anyString())).thenReturn(actionGroup);
  73. when(actionGroup.getSettings()).thenReturn(prefs);
  74. when(actionController.getType("SERVER_AWAY")).thenReturn(CoreActionType.SERVER_AWAY);
  75. when(actionController.getComponent("STRING_LENGTH")).thenReturn(CoreActionComponent.STRING_LENGTH);
  76. when(actionController.getComparison("STRING_CONTAINS")).thenReturn(CoreActionComparison.STRING_CONTAINS);
  77. when(actionController.getComparison("INT_EQUALS")).thenReturn(CoreActionComparison.INT_EQUALS);
  78. }
  79. @Before
  80. public void setupIdentityController() {
  81. when(identityController.getGlobalConfiguration()).thenReturn(aggregateConfigProvider);
  82. when(identityController.getUserSettings()).thenReturn(configProvider);
  83. }
  84. @After
  85. public void killTestDirectory() {
  86. // TODO: This probably won't work - need to recursively delete files etc.
  87. new File(testDirectory).delete();
  88. }
  89. @Test
  90. public void testSave() {
  91. new Action(actionController, identityController, testDirectory,
  92. "unit-test", "test1", new ActionType[0],
  93. new String[0], new ArrayList<ActionCondition>(),
  94. ConditionTree.createConjunction(0), null);
  95. assertTrue("Action constructor must create new file",
  96. new File(testDirectory + "unit-test"
  97. + File.separator + "test1").isFile());
  98. }
  99. @Test
  100. public void testSetGroup() {
  101. Action action = new Action(actionController, identityController, testDirectory,
  102. "unit-test", "test1", new ActionType[0],
  103. new String[0], new ArrayList<ActionCondition>(),
  104. ConditionTree.createConjunction(0), null);
  105. action.setGroup("unit-test-two");
  106. assertFalse("setGroup must remove old file",
  107. new File(testDirectory + "unit-test" + File.separator + "test1").isFile());
  108. assertTrue("setGroup must create new file",
  109. new File(testDirectory + "unit-test-two" + File.separator + "test1").isFile());
  110. }
  111. @Test
  112. public void testSetName() {
  113. Action action = new Action(actionController, identityController, testDirectory,
  114. "unit-test", "test1", new ActionType[0],
  115. new String[0], new ArrayList<ActionCondition>(),
  116. ConditionTree.createConjunction(0), null);
  117. action.setName("test2");
  118. assertFalse("setName must remove old file",
  119. new File(testDirectory + "unit-test" + File.separator + "test1").isFile());
  120. assertTrue("setName must create new file",
  121. new File(testDirectory + "unit-test" + File.separator + "test2").isFile());
  122. }
  123. @Test
  124. public void testDelete() {
  125. Action action = new Action(actionController, identityController, testDirectory,
  126. "unit-test", "test1", new ActionType[0],
  127. new String[0], new ArrayList<ActionCondition>(),
  128. ConditionTree.createConjunction(0), null);
  129. action.delete();
  130. assertFalse("delete must remove file",
  131. new File(testDirectory + "unit-test"
  132. + File.separator + "test1").isFile());
  133. }
  134. @Test
  135. public void testRead() throws IOException, InvalidConfigFileException {
  136. Action action = new Action(actionController, identityController, testDirectory,
  137. "unit-test", "doesn't_exist");
  138. action.config = new ConfigFile(getClass().getResourceAsStream("action1"));
  139. action.config.read();
  140. action.loadActionFromConfig();
  141. assertTrue(Arrays.equals(action.getTriggers(),
  142. new ActionType[]{CoreActionType.SERVER_AWAY}));
  143. assertEquals("(0&1)", action.getConditionTree().toString());
  144. assertTrue(Arrays.equals(action.getResponse(), new String[]{"/away"}));
  145. assertEquals(new ActionCondition(1, CoreActionComponent.STRING_LENGTH,
  146. CoreActionComparison.INT_EQUALS, "0"), action.getConditions().get(0));
  147. assertEquals(new ActionCondition("foo", CoreActionComparison.STRING_CONTAINS,
  148. "bar"), action.getConditions().get(1));
  149. }
  150. @Test
  151. public void testMultipleGroups() throws IOException, InvalidConfigFileException {
  152. Action action = new Action(actionController, identityController, testDirectory,
  153. "unit-test", "doesn't_exist");
  154. action.config = new ConfigFile(getClass().getResourceAsStream("action_multisettings"));
  155. action.config.read();
  156. action.loadActionFromConfig();
  157. assertEquals(1, prefs.size());
  158. final PreferencesSetting setting = prefs.values().iterator().next();
  159. assertEquals(PreferencesType.TEXT, setting.getType());
  160. assertEquals("Highlight Regex", setting.getTitle());
  161. assertEquals("Regex to use to detect a highlight", setting.getHelptext());
  162. verify(actionController, atLeast(1)).registerSetting("highlightregex",
  163. "(?i).*(shane|dataforce|Q${SERVER_MYNICKNAME}E|(?<![#A-Z])DF).*");
  164. }
  165. }