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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.junit.jupiter.api.io.TempDir;
  21. import org.pf4j.test.PluginJar;
  22. import org.pf4j.test.TestExtension;
  23. import org.pf4j.test.TestExtensionPoint;
  24. import org.pf4j.test.TestPlugin;
  25. import java.io.IOException;
  26. import java.nio.file.Path;
  27. import java.util.List;
  28. import static org.junit.jupiter.api.Assertions.assertEquals;
  29. import static org.junit.jupiter.api.Assertions.assertFalse;
  30. import static org.junit.jupiter.api.Assertions.assertTrue;
  31. public class JarPluginManagerTest {
  32. private PluginJar pluginJar;
  33. private JarPluginManager pluginManager;
  34. @TempDir
  35. Path pluginsPath;
  36. @BeforeEach
  37. public void setUp() throws IOException {
  38. pluginJar = new PluginJar.Builder(pluginsPath.resolve("test-plugin.jar"), "test-plugin")
  39. .pluginClass(TestPlugin.class.getName())
  40. .pluginVersion("1.2.3")
  41. .extension(TestExtension.class.getName())
  42. .build();
  43. pluginManager = new JarPluginManager(pluginsPath);
  44. }
  45. @AfterEach
  46. public void tearDown() {
  47. pluginManager.unloadPlugins();
  48. pluginJar = null;
  49. pluginManager = null;
  50. }
  51. @Test
  52. public void getExtensions() {
  53. pluginManager.loadPlugins();
  54. pluginManager.startPlugins();
  55. List<TestExtensionPoint> extensions = pluginManager.getExtensions(TestExtensionPoint.class);
  56. assertEquals(1, extensions.size());
  57. String something = extensions.get(0).saySomething();
  58. assertEquals(new TestExtension().saySomething(), something);
  59. }
  60. @Test
  61. public void unloadPlugin() throws Exception {
  62. pluginManager.loadPlugins();
  63. assertEquals(1, pluginManager.getPlugins().size());
  64. boolean unloaded = pluginManager.unloadPlugin(pluginJar.pluginId());
  65. assertTrue(unloaded);
  66. assertTrue(pluginJar.file().exists());
  67. }
  68. @Test
  69. public void deletePlugin() throws Exception {
  70. pluginManager.loadPlugins();
  71. assertEquals(1, pluginManager.getPlugins().size());
  72. boolean deleted = pluginManager.deletePlugin(pluginJar.pluginId());
  73. assertTrue(deleted);
  74. assertFalse(pluginJar.file().exists());
  75. }
  76. }