Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ActionTest.java 9.0KB

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