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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }
  44. /**
  45. * Test of {@link DefaultPluginRepository#getPluginPaths()} method.
  46. */
  47. @Test
  48. public void testGetPluginArchives() {
  49. PluginRepository repository = new DefaultPluginRepository(pluginsPath);
  50. List<Path> pluginPaths = repository.getPluginPaths();
  51. assertEquals(3, pluginPaths.size());
  52. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-1"));
  53. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-2"));
  54. assertPathExists(pluginPaths, pluginsPath.resolve("plugin-3"));
  55. }
  56. /**
  57. * Test of {@link DefaultPluginRepository#deletePluginPath(Path)} method.
  58. */
  59. @Test
  60. public void testDeletePluginPath() {
  61. PluginRepository repository = new DefaultPluginRepository(pluginsPath);
  62. assertTrue(Files.exists(pluginsPath.resolve("plugin-1.zip")));
  63. assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-1")));
  64. assertFalse(Files.exists(pluginsPath.resolve("plugin-1.zip")));
  65. assertTrue(repository.deletePluginPath(pluginsPath.resolve("plugin-3")));
  66. assertFalse(repository.deletePluginPath(pluginsPath.resolve("plugin-4")));
  67. List<Path> pluginPaths = repository.getPluginPaths();
  68. assertEquals(1, pluginPaths.size());
  69. assertEquals(pluginsPath.relativize(pluginPaths.get(0)).toString(), "plugin-2");
  70. }
  71. private void assertPathExists(List<Path> paths, Path path) {
  72. assertTrue(paths.contains(path), "The directory must contain the file " + path);
  73. }
  74. }