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

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