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.

JarPluginManagerTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Assertions;
  19. import org.junit.jupiter.api.BeforeEach;
  20. import org.junit.jupiter.api.Test;
  21. import org.junit.jupiter.api.condition.EnabledOnOs;
  22. import org.junit.jupiter.api.condition.OS;
  23. import org.junit.jupiter.api.io.TempDir;
  24. import org.pf4j.test.PluginJar;
  25. import org.pf4j.test.TestExtension;
  26. import org.pf4j.test.TestExtensionPoint;
  27. import org.pf4j.test.TestPlugin;
  28. import java.io.IOException;
  29. import java.nio.file.FileSystemException;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.util.List;
  33. import java.util.concurrent.CompletableFuture;
  34. import static org.junit.jupiter.api.Assertions.assertEquals;
  35. import static org.junit.jupiter.api.Assertions.assertFalse;
  36. import static org.junit.jupiter.api.Assertions.assertTrue;
  37. public class JarPluginManagerTest {
  38. private PluginJar pluginJar;
  39. private JarPluginManager pluginManager;
  40. @TempDir
  41. Path pluginsPath;
  42. @BeforeEach
  43. public void setUp() throws IOException {
  44. pluginJar = new PluginJar.Builder(pluginsPath.resolve("test-plugin.jar"), "test-plugin")
  45. .pluginClass(TestPlugin.class.getName())
  46. .pluginVersion("1.2.3")
  47. .extension(TestExtension.class.getName())
  48. .build();
  49. pluginManager = new JarPluginManager(pluginsPath);
  50. }
  51. @AfterEach
  52. public void tearDown() {
  53. pluginManager.unloadPlugins();
  54. pluginJar = null;
  55. pluginManager = null;
  56. }
  57. @Test
  58. public void getExtensions() {
  59. pluginManager.loadPlugins();
  60. pluginManager.startPlugins();
  61. List<TestExtensionPoint> extensions = pluginManager.getExtensions(TestExtensionPoint.class);
  62. assertEquals(1, extensions.size());
  63. String something = extensions.get(0).saySomething();
  64. assertEquals(new TestExtension().saySomething(), something);
  65. }
  66. @Test
  67. public void unloadPlugin() throws Exception {
  68. pluginManager.loadPlugins();
  69. assertEquals(1, pluginManager.getPlugins().size());
  70. boolean unloaded = pluginManager.unloadPlugin(pluginJar.pluginId());
  71. assertTrue(unloaded);
  72. assertTrue(pluginJar.file().exists());
  73. }
  74. @Test
  75. public void deletePlugin() throws Exception {
  76. pluginManager.loadPlugins();
  77. assertEquals(1, pluginManager.getPlugins().size());
  78. boolean deleted = pluginManager.deletePlugin(pluginJar.pluginId());
  79. assertTrue(deleted);
  80. assertFalse(pluginJar.file().exists());
  81. }
  82. @Test
  83. @EnabledOnOs(OS.WINDOWS)
  84. public void releaseBrokenJarOnWindows() throws IOException {
  85. PluginJar pluginZip = new PluginJar.Builder(pluginsPath.resolve("test.jar"), "test")
  86. .pluginVersion("1.2.3")
  87. .pluginClass("invalidClass")
  88. .build();
  89. pluginManager.loadPlugins();
  90. Path pluginPath = pluginManager.getPlugin(pluginZip.pluginId()).getPluginPath();
  91. try {
  92. pluginManager.startPlugin(pluginZip.pluginId());
  93. } catch (Exception exceptionStartPlugin) {
  94. Assertions.assertThrows(FileSystemException.class, () -> Files.delete(pluginPath));
  95. // Try to remove the plugin if it cannot be started
  96. try {
  97. pluginManager.unloadPlugin(pluginZip.pluginId());
  98. } catch (Exception ignored2) {
  99. }
  100. Assertions.assertDoesNotThrow(() -> Files.delete(pluginPath));
  101. }
  102. }
  103. }