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 8.4KB

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