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.4KB

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.BeforeEach;
  18. import org.junit.jupiter.api.Test;
  19. import org.junit.jupiter.api.io.TempDir;
  20. import java.io.IOException;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.util.List;
  24. import static org.junit.jupiter.api.Assertions.assertEquals;
  25. import static org.junit.jupiter.api.Assertions.assertFalse;
  26. import static org.junit.jupiter.api.Assertions.assertTrue;
  27. /**
  28. * @author Mario Franco
  29. * @author Decebal Suiu
  30. */
  31. public class DefaultPluginRepositoryTest {
  32. @TempDir
  33. Path pluginsPath;
  34. @BeforeEach
  35. public void setUp() throws IOException {
  36. Path plugin1Path = Files.createDirectory(pluginsPath.resolve("plugin-1"));
  37. // Prove that we can delete a folder with a file inside
  38. Files.createFile(plugin1Path.resolve("myfile"));
  39. // Create a zip file for plugin-1 to test that it is deleted when plugin is deleted
  40. Files.createFile(pluginsPath.resolve("plugin-1.zip"));
  41. Files.createDirectory(pluginsPath.resolve("plugin-2"));
  42. Files.createDirectory(pluginsPath.resolve("plugin-3"));
  43. // standard maven/gradle bin folder - these should be skipped in development mode because the cause errors
  44. Files.createDirectory(pluginsPath.resolve("target"));
  45. Files.createDirectory(pluginsPath.resolve("build"));
  46. }
  47. /**
  48. * Test of {@link DefaultPluginRepository#getPluginPaths()} method.
  49. */
  50. @Test
  51. public void testGetPluginArchives() {
  52. PluginRepository repository = new DefaultPluginRepository(pluginsPath, false);
  53. List<Path> pluginPaths = repository.getPluginPaths();
  54. assertEquals(5, pluginPaths.size());
  55. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-1"));
  56. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-2"));
  57. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-3"));
  58. // when not in development mode we will honor these folders
  59. assertPathExists(pluginPaths, pluginsPath.resolve("target"));
  60. assertPathExists(pluginPaths, pluginsPath.resolve("build"));
  61. }
  62. @Test
  63. public void testGetPluginArchivesInDevelopmentMode() {
  64. PluginRepository repository = new DefaultPluginRepository(pluginsPath, true);
  65. List<Path> pluginPaths = repository.getPluginPaths();
  66. // target and build should be ignored
  67. assertEquals(3, pluginPaths.size());
  68. assertPathDoesNotExists(pluginPaths, pluginsPath.resolve("target"));
  69. assertPathDoesNotExists(pluginPaths, pluginsPath.resolve("build"));
  70. }
  71. /**
  72. * Test of {@link DefaultPluginRepository#deletePluginPath(Path)} method.
  73. */
  74. @Test
  75. public void testDeletePluginPath() {
  76. PluginRepository repository = new DefaultPluginRepository(pluginsPath, false);
  77. assertTrue(Files.exists(pluginsPath.resolve("plugin-1.zip")));
  78. assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-1")));
  79. assertFalse(Files.exists(pluginsPath.resolve("plugin-1.zip")));
  80. assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-3")));
  81. assertFalse(repository.deletePluginPath(pluginsPath.resolve("plugin-4")));
  82. assertTrue(repository.deletePluginPath(pluginsPath.resolve("target")));
  83. assertTrue(repository.deletePluginPath(pluginsPath.resolve("build")));
  84. List<Path> pluginPaths = repository.getPluginPaths();
  85. assertEquals(1, pluginPaths.size());
  86. assertEquals(pluginsPath.relativize(pluginPaths.get(0)).toString(), "plugin-2");
  87. }
  88. private void assertPathExists(List<Path> paths, Path path) {
  89. assertTrue(paths.contains(path), "The directory must contain the file " + path);
  90. }
  91. private void assertPathDoesNotExists(List<Path> paths, Path path) {
  92. assertFalse(paths.contains(path), "The directory must not contain the file " + path);
  93. }
  94. }