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

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