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.

DefaultPluginRepositoryTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import org.junit.jupiter.api.AfterEach;
  18. import org.junit.jupiter.api.BeforeEach;
  19. import org.junit.jupiter.api.Test;
  20. import org.pf4j.test.PluginZip;
  21. import org.pf4j.util.FileUtils;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.List;
  26. import static org.junit.jupiter.api.Assertions.assertEquals;
  27. import static org.junit.jupiter.api.Assertions.assertFalse;
  28. import static org.junit.jupiter.api.Assertions.assertTrue;
  29. /**
  30. * @author Mario Franco
  31. * @author Decebal Suiu
  32. */
  33. public class DefaultPluginRepositoryTest {
  34. Path pluginsPath1;
  35. Path pluginsPath2;
  36. @BeforeEach
  37. public void setUp() throws IOException {
  38. pluginsPath1 = Files.createTempDirectory("junit-pf4j-");
  39. pluginsPath2 = Files.createTempDirectory("junit-pf4j-");
  40. Path plugin1Path = Files.createDirectory(pluginsPath1.resolve("plugin-1"));
  41. // Prove that we can delete a folder with a file inside
  42. Files.createFile(plugin1Path.resolve("myfile"));
  43. // Create a zip file for plugin-1 to test that it is deleted when plugin is deleted
  44. new PluginZip.Builder(pluginsPath1.resolve("plugin-1.zip"), "plugin-1").pluginVersion("1.0").build();
  45. Files.createDirectory(pluginsPath2.resolve("plugin-2"));
  46. Files.createDirectory(pluginsPath2.resolve("plugin-3"));
  47. }
  48. @AfterEach
  49. public void tearDown() throws IOException {
  50. FileUtils.delete(pluginsPath1);
  51. FileUtils.delete(pluginsPath2);
  52. }
  53. /**
  54. * Test of {@link DefaultPluginRepository#getPluginPaths()} method.
  55. */
  56. @Test
  57. public void testGetPluginArchivesFromSinglePath() {
  58. PluginRepository repository = new DefaultPluginRepository(pluginsPath2);
  59. List<Path> pluginPaths = repository.getPluginPaths();
  60. assertEquals(2, pluginPaths.size());
  61. assertPathExists(pluginPaths, pluginsPath2.resolve("plugin-2"));
  62. assertPathExists(pluginPaths, pluginsPath2.resolve("plugin-3"));
  63. }
  64. /**
  65. * Test of {@link DefaultPluginRepository#getPluginPaths()} method.
  66. */
  67. @Test
  68. public void testGetPluginArchives() {
  69. PluginRepository repository = new DefaultPluginRepository(pluginsPath1, pluginsPath2);
  70. List<Path> pluginPaths = repository.getPluginPaths();
  71. assertEquals(3, pluginPaths.size());
  72. assertPathExists(pluginPaths, pluginsPath1.resolve("plugin-1"));
  73. assertPathExists(pluginPaths, pluginsPath2.resolve("plugin-2"));
  74. assertPathExists(pluginPaths, pluginsPath2.resolve("plugin-3"));
  75. }
  76. /**
  77. * Test of {@link DefaultPluginRepository#deletePluginPath(Path)} method.
  78. */
  79. @Test
  80. public void testDeletePluginPath() {
  81. PluginRepository repository = new DefaultPluginRepository(pluginsPath1, pluginsPath2);
  82. assertTrue(Files.exists(pluginsPath1.resolve("plugin-1.zip")));
  83. assertTrue(repository.deletePluginPath(pluginsPath1.resolve("plugin-1")));
  84. assertFalse(Files.exists(pluginsPath1.resolve("plugin-1.zip")));
  85. assertTrue(repository.deletePluginPath(pluginsPath2.resolve("plugin-3")));
  86. assertFalse(repository.deletePluginPath(pluginsPath2.resolve("plugin-4")));
  87. List<Path> pluginPaths = repository.getPluginPaths();
  88. assertEquals(1, pluginPaths.size());
  89. assertEquals(pluginsPath2.relativize(pluginPaths.get(0)).toString(), "plugin-2");
  90. }
  91. private void assertPathExists(List<Path> paths, Path path) {
  92. assertTrue(paths.contains(path), "The directory must contain the file " + path);
  93. }
  94. }