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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2008 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.util.ConfigFile;
  25. import com.dmdirc.util.InvalidConfigFileException;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import org.junit.Test;
  31. import static org.junit.Assert.*;
  32. public class ActionTest extends junit.framework.TestCase {
  33. private static Action action;
  34. @Test
  35. public void testSave() {
  36. action = new Action("unit-test", "test1", new ActionType[0],
  37. new String[0], new ArrayList<ActionCondition>(), null);
  38. assertTrue("Action constructor must create new file",
  39. new File(ActionManager.getDirectory() + "unit-test"
  40. + File.separator + "test1").isFile());
  41. }
  42. @Test
  43. public void testSetGroup() {
  44. action.setGroup("unit-test-two");
  45. assertFalse("setGroup must remove old file",
  46. new File(ActionManager.getDirectory() + "unit-test"
  47. + File.separator + "test1").isFile());
  48. assertTrue("setGroup must create new file",
  49. new File(ActionManager.getDirectory() + "unit-test-two"
  50. + File.separator + "test1").isFile());
  51. }
  52. @Test
  53. public void testSetName() {
  54. action.setName("test2");
  55. assertFalse("setName must remove old file",
  56. new File(ActionManager.getDirectory() + "unit-test-two"
  57. + File.separator + "test1").isFile());
  58. assertTrue("setName must create new file",
  59. new File(ActionManager.getDirectory() + "unit-test-two"
  60. + File.separator + "test2").isFile());
  61. }
  62. @Test
  63. public void testDelete() {
  64. action.delete();
  65. assertFalse("delete must remove file",
  66. new File(ActionManager.getDirectory() + "unit-test-two"
  67. + File.separator + "test2").isFile());
  68. }
  69. @Test
  70. public void testRead() throws IOException, InvalidConfigFileException {
  71. ActionManager.init();
  72. final Action action = new Action("unit-test", "doesn't_exist");
  73. action.config = new ConfigFile(getClass().getResourceAsStream("action1"));
  74. action.config.read();
  75. action.loadActionFromConfig();
  76. assertTrue(Arrays.equals(action.getTriggers(),
  77. new ActionType[]{CoreActionType.SERVER_AWAY}));
  78. assertEquals("(0&1)", action.getConditionTree().toString());
  79. assertTrue(Arrays.equals(action.getResponse(), new String[]{"/away"}));
  80. assertEquals(new ActionCondition(1, CoreActionComponent.STRING_LENGTH,
  81. CoreActionComparison.INT_EQUALS, "0"), action.getConditions().get(0));
  82. assertEquals(new ActionCondition("foo", CoreActionComparison.STRING_CONTAINS,
  83. "bar"), action.getConditions().get(1));
  84. }
  85. }