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

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