Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ActionTest.java 5.1KB

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