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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.TestMain;
  24. import com.dmdirc.config.prefs.PreferencesSetting;
  25. import com.dmdirc.config.prefs.PreferencesType;
  26. import com.dmdirc.interfaces.actions.ActionType;
  27. import com.dmdirc.util.io.ConfigFile;
  28. import com.dmdirc.util.io.InvalidConfigFileException;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import org.junit.BeforeClass;
  34. import org.junit.Ignore;
  35. import org.junit.Test;
  36. import static org.junit.Assert.*;
  37. public class ActionTest {
  38. @BeforeClass
  39. public static void setUp() throws Exception {
  40. TestMain.getTestMain();
  41. }
  42. private static Action action;
  43. @Test
  44. public void testSave() {
  45. action = new Action("unit-test", "test1", new ActionType[0],
  46. new String[0], new ArrayList<ActionCondition>(), null);
  47. assertTrue("Action constructor must create new file",
  48. new File(ActionManager.getActionManager().getDirectory() + "unit-test"
  49. + File.separator + "test1").isFile());
  50. }
  51. @Ignore
  52. @Test
  53. public void testSetGroup() {
  54. action.setGroup("unit-test-two");
  55. assertFalse("setGroup must remove old file",
  56. new File(ActionManager.getActionManager().getDirectory() + "unit-test"
  57. + File.separator + "test1").isFile());
  58. assertTrue("setGroup must create new file",
  59. new File(ActionManager.getActionManager().getDirectory() + "unit-test-two"
  60. + File.separator + "test1").isFile());
  61. }
  62. @Ignore
  63. @Test
  64. public void testSetName() {
  65. action.setName("test2");
  66. assertFalse("setName must remove old file",
  67. new File(ActionManager.getActionManager().getDirectory() + "unit-test-two"
  68. + File.separator + "test1").isFile());
  69. assertTrue("setName must create new file",
  70. new File(ActionManager.getActionManager().getDirectory() + "unit-test-two"
  71. + File.separator + "test2").isFile());
  72. }
  73. @Test
  74. public void testDelete() {
  75. action.delete();
  76. assertFalse("delete must remove file",
  77. new File(ActionManager.getActionManager().getDirectory() + "unit-test-two"
  78. + File.separator + "test2").isFile());
  79. }
  80. @Test
  81. public void testRead() throws IOException, InvalidConfigFileException {
  82. action = new Action("unit-test", "doesn't_exist");
  83. action.config = new ConfigFile(getClass().getResourceAsStream("action1"));
  84. action.config.read();
  85. action.loadActionFromConfig();
  86. assertTrue(Arrays.equals(action.getTriggers(),
  87. new ActionType[]{CoreActionType.SERVER_AWAY}));
  88. assertEquals("(0&1)", action.getConditionTree().toString());
  89. assertTrue(Arrays.equals(action.getResponse(), new String[]{"/away"}));
  90. assertEquals(new ActionCondition(1, CoreActionComponent.STRING_LENGTH,
  91. CoreActionComparison.INT_EQUALS, "0"), action.getConditions().get(0));
  92. assertEquals(new ActionCondition("foo", CoreActionComparison.STRING_CONTAINS,
  93. "bar"), action.getConditions().get(1));
  94. }
  95. @Test
  96. public void testMultipleGroups() throws IOException, InvalidConfigFileException {
  97. action = new Action("unit-test", "doesn't_exist");
  98. action.config = new ConfigFile(getClass().getResourceAsStream("action_multisettings"));
  99. action.config.read();
  100. action.loadActionFromConfig();
  101. assertEquals(1, ActionManager.getActionManager().getOrCreateGroup("unit-test").getSettings().size());
  102. final PreferencesSetting setting = ActionManager.getActionManager().getOrCreateGroup("unit-test")
  103. .getSettings().values().iterator().next();
  104. assertEquals(PreferencesType.TEXT, setting.getType());
  105. assertEquals("Highlight Regex", setting.getTitle());
  106. assertEquals("Regex to use to detect a highlight", setting.getHelptext());
  107. assertEquals("(?i).*(shane|dataforce|Q${SERVER_MYNICKNAME}E|(?<![#A-Z])DF).*",
  108. setting.getValue());
  109. }
  110. }