您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ActionTest.java 5.1KB

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