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.

ActionAliasMigratorTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.aliases;
  23. import com.dmdirc.tests.JimFsRule;
  24. import java.io.IOException;
  25. import java.nio.file.Files;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.junit.Assert.assertFalse;
  33. import static org.junit.Assert.assertTrue;
  34. import static org.mockito.Mockito.verify;
  35. @RunWith(MockitoJUnitRunner.class)
  36. public class ActionAliasMigratorTest {
  37. @Rule public final JimFsRule jimFsRule = new JimFsRule();
  38. @Mock private AliasFactory aliasFactory;
  39. @Mock private AliasManager aliasManager;
  40. private ActionAliasMigrator migrator1;
  41. private ActionAliasMigrator migrator2;
  42. private ActionAliasMigrator migrator3;
  43. @Before
  44. public void setup() throws IOException {
  45. Files.createDirectories(jimFsRule.getPath("test1/aliases"));
  46. Files.createDirectories(jimFsRule.getPath("test2/other-stuff/aliases"));
  47. Files.createDirectories(jimFsRule.getPath("test3/aliases"));
  48. Files.copy(getClass().getResource("op-greater-0").openStream(),
  49. jimFsRule.getPath("test1/aliases/op"));
  50. Files.copy(getClass().getResource("unset-equals-2").openStream(),
  51. jimFsRule.getPath("test1/aliases/unset"));
  52. Files.copy(getClass().getResource("no-trigger").openStream(),
  53. jimFsRule.getPath("test3/aliases/bad"));
  54. migrator1 = new ActionAliasMigrator(jimFsRule.getPath("test1"), aliasFactory, aliasManager);
  55. migrator2 = new ActionAliasMigrator(jimFsRule.getPath("test2"), aliasFactory, aliasManager);
  56. migrator3 = new ActionAliasMigrator(jimFsRule.getPath("test3"), aliasFactory, aliasManager);
  57. }
  58. @Test
  59. public void testNeedMigration() {
  60. assertTrue(migrator1.needsMigration());
  61. assertFalse(migrator2.needsMigration());
  62. }
  63. @Test
  64. public void testDeletesFilesAfterMigration() {
  65. migrator1.migrate();
  66. assertFalse(Files.exists(jimFsRule.getPath("test1/aliases/unset")));
  67. assertFalse(Files.exists(jimFsRule.getPath("test1/aliases/op")));
  68. assertFalse(Files.exists(jimFsRule.getPath("test1/aliases")));
  69. assertTrue(Files.exists(jimFsRule.getPath("test1")));
  70. }
  71. @Test
  72. public void testLeavesOtherFilesDuringMigration() {
  73. migrator2.migrate();
  74. assertTrue(Files.exists(jimFsRule.getPath("test2/other-stuff/aliases")));
  75. }
  76. @Test
  77. public void testMigrationCreatesAliases() {
  78. migrator1.migrate();
  79. verify(aliasFactory).createAlias("op", 1, "/mode ooooooooooooooo $1-");
  80. verify(aliasFactory).createAlias("unset", 2, "/set --unset $1-");
  81. }
  82. @Test
  83. public void testBadActionsLeftOnDisk() {
  84. migrator3.migrate();
  85. assertTrue(Files.exists(jimFsRule.getPath("test3/aliases/bad")));
  86. }
  87. }