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.

LoadPluginsTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 org.pf4j.test.PluginZip;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.Collections;
  27. import java.util.zip.ZipEntry;
  28. import java.util.zip.ZipOutputStream;
  29. import static org.hamcrest.CoreMatchers.containsString;
  30. import static org.hamcrest.CoreMatchers.equalTo;
  31. import static org.hamcrest.CoreMatchers.startsWith;
  32. import static org.hamcrest.MatcherAssert.assertThat;
  33. import static org.junit.jupiter.api.Assertions.assertEquals;
  34. import static org.junit.jupiter.api.Assertions.assertFalse;
  35. import static org.junit.jupiter.api.Assertions.assertNotNull;
  36. import static org.junit.jupiter.api.Assertions.assertNull;
  37. import static org.junit.jupiter.api.Assertions.assertThrows;
  38. import static org.junit.jupiter.api.Assertions.assertTrue;
  39. import static org.junit.jupiter.api.Assertions.fail;
  40. import static org.pf4j.util.FileUtils.expandIfZip;
  41. public class LoadPluginsTest {
  42. private DefaultPluginManager pluginManager;
  43. @TempDir
  44. Path pluginsPath;
  45. @BeforeEach
  46. public void setUp() {
  47. pluginManager = new DefaultPluginManager(pluginsPath);
  48. }
  49. @Test
  50. public void load() throws Exception {
  51. PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
  52. .pluginVersion("1.2.3")
  53. .build();
  54. assertTrue(Files.exists(pluginZip.path()));
  55. assertEquals(0, pluginManager.getPlugins().size());
  56. pluginManager.loadPlugins();
  57. assertTrue(Files.exists(pluginZip.path()));
  58. assertTrue(Files.exists(pluginZip.unzippedPath()));
  59. assertEquals(1, pluginManager.getPlugins().size());
  60. assertEquals(pluginZip.pluginId(), pluginManager.idForPath(pluginZip.unzippedPath()));
  61. }
  62. @Test
  63. public void loadNonExisting() {
  64. assertThrows(IllegalArgumentException.class, () -> pluginManager.loadPlugin(Paths.get("nonexisting")));
  65. }
  66. @Test
  67. public void loadTwiceFails() throws Exception {
  68. PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
  69. .pluginVersion("1.2.3")
  70. .build();
  71. assertNotNull(pluginManager.loadPluginFromPath(pluginZip.path()));
  72. assertThrows(PluginAlreadyLoadedException.class, () -> pluginManager.loadPluginFromPath(pluginZip.path()));
  73. }
  74. @Test
  75. public void loadPluginWithSameIdDifferentPathFails() throws Exception {
  76. String pluginId = "myPlugin";
  77. String pluginVersion = "1.2.3";
  78. Path plugin1Path = pluginsPath.resolve("my-plugin-1.2.3.zip");
  79. PluginZip plugin1 = new PluginZip.Builder(plugin1Path, pluginId)
  80. .pluginVersion(pluginVersion)
  81. .build();
  82. Path plugin2Path = pluginsPath.resolve("my-plugin-1.2.3-renamed.zip");
  83. PluginZip plugin2 = new PluginZip.Builder(plugin2Path, pluginId)
  84. .pluginVersion(pluginVersion)
  85. .build();
  86. // Verify the first plugin with the given id is loaded
  87. assertNotNull(pluginManager.loadPluginFromPath(plugin1.path()));
  88. Path loadedPlugin1Path = pluginManager.getPlugin(pluginId).getPluginPath();
  89. try {
  90. // Verify the second plugin is not loaded as it has the same metadata
  91. pluginManager.loadPluginFromPath(plugin2.path());
  92. fail("Expected loadPluginFromPath to fail");
  93. } catch (PluginRuntimeException e) {
  94. // Check the path of the loaded plugin remains the same
  95. PluginWrapper loadedPlugin = pluginManager.getPlugin(pluginId);
  96. assertThat(loadedPlugin.getPluginPath(), equalTo(loadedPlugin1Path));
  97. // Check the message includes relevant information
  98. String message = e.getMessage();
  99. assertThat(message, startsWith("There is an already loaded plugin"));
  100. assertThat(message, containsString(pluginId));
  101. assertThat(message, containsString("my-plugin-1.2.3-renamed"));
  102. }
  103. }
  104. /**
  105. * This test verifies the behaviour as of PF4J 2.x, where plugins of different
  106. * versions but with the pluginId cannot be loaded correctly because the API
  107. * uses pluginId as the unique identifier of the loaded plugin.
  108. */
  109. @Test
  110. public void loadPluginWithSameIdDifferentVersionsFails() throws Exception {
  111. String pluginId = "myPlugin";
  112. String plugin1Version = "1.2.3";
  113. Path plugin1Path = pluginsPath.resolve("my-plugin-1.2.3.zip");
  114. PluginZip plugin1 = new PluginZip.Builder(plugin1Path, pluginId)
  115. .pluginVersion(plugin1Version)
  116. .build();
  117. String plugin2Version = "2.0.0";
  118. Path plugin2Path = pluginsPath.resolve("my-plugin-2.0.0.zip");
  119. PluginZip plugin2 = new PluginZip.Builder(plugin2Path, pluginId)
  120. .pluginVersion(plugin2Version)
  121. .build();
  122. // Verify the first plugin with the given id is loaded
  123. assertNotNull(pluginManager.loadPluginFromPath(plugin1.path()));
  124. Path loadedPlugin1Path = pluginManager.getPlugin(pluginId).getPluginPath();
  125. try {
  126. // Verify the second plugin is not loaded as it has the same pluginId
  127. pluginManager.loadPluginFromPath(plugin2.path());
  128. fail("Expected loadPluginFromPath to fail");
  129. } catch (PluginRuntimeException e) {
  130. // Check the path and version of the loaded plugin remain the same
  131. PluginWrapper loadedPlugin = pluginManager.getPlugin(pluginId);
  132. assertThat(loadedPlugin.getPluginPath(), equalTo(loadedPlugin1Path));
  133. assertThat(loadedPlugin.getDescriptor().getVersion(), equalTo(plugin1Version));
  134. }
  135. }
  136. @Test
  137. public void loadUnloadLoad() throws Exception {
  138. PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
  139. .pluginVersion("1.2.3")
  140. .build();
  141. pluginManager.loadPlugins();
  142. assertEquals(1, pluginManager.getPlugins().size());
  143. assertTrue(pluginManager.unloadPlugin(pluginManager.idForPath(pluginZip.unzippedPath())));
  144. // duplicate check
  145. assertNull(pluginManager.idForPath(pluginZip.unzippedPath()));
  146. // Double unload ok
  147. assertFalse(pluginManager.unloadPlugin(pluginManager.idForPath(pluginZip.unzippedPath())));
  148. assertNotNull(pluginManager.loadPlugin(pluginZip.unzippedPath()));
  149. }
  150. @Test
  151. public void upgrade() throws Exception {
  152. String pluginId = "myPlugin";
  153. new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), pluginId)
  154. .pluginVersion("1.2.3")
  155. .build();
  156. pluginManager.loadPlugins();
  157. pluginManager.startPlugins();
  158. assertEquals(1, pluginManager.getPlugins().size());
  159. assertEquals(1, pluginManager.getStartedPlugins().size());
  160. PluginZip pluginZip2 = new PluginZip.Builder(pluginsPath.resolve("my-plugin-2.0.0.ZIP"), pluginId)
  161. .pluginVersion("2.0.0")
  162. .build();
  163. assertEquals("1.2.3", pluginManager.getPlugin(pluginId).getDescriptor().getVersion());
  164. pluginManager.unloadPlugin(pluginId);
  165. pluginManager.loadPlugin(pluginZip2.path()); // or `pluginManager.loadPlugins();`
  166. pluginManager.startPlugin(pluginId);
  167. assertEquals(1, pluginManager.getPlugins().size());
  168. assertEquals("2.0.0", pluginManager.getPlugin(pluginId).getDescriptor().getVersion());
  169. assertEquals("2.0.0", pluginManager.getStartedPlugins().get(0).getDescriptor().getVersion());
  170. }
  171. @Test
  172. public void getRoot() {
  173. assertEquals(pluginsPath, pluginManager.getPluginsRoot());
  174. }
  175. @Test
  176. public void getRoots() {
  177. assertEquals(Collections.singletonList(pluginsPath), pluginManager.getPluginsRoots());
  178. }
  179. @Test
  180. public void notAPlugin() {
  181. pluginsPath.resolve("not-a-zip");
  182. pluginManager.loadPlugins();
  183. assertEquals(0, pluginManager.getPlugins().size());
  184. }
  185. @Test
  186. public void deletePlugin() throws Exception {
  187. PluginZip pluginZip1 = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
  188. .pluginVersion("1.2.3")
  189. .build();
  190. PluginZip pluginZip3 = new PluginZip.Builder(pluginsPath.resolve("other-3.0.0.Zip"), "other")
  191. .pluginVersion("3.0.0")
  192. .build();
  193. pluginManager.loadPlugins();
  194. pluginManager.startPlugins();
  195. assertEquals(2, pluginManager.getPlugins().size());
  196. pluginManager.deletePlugin(pluginZip1.pluginId());
  197. assertEquals(1, pluginManager.getPlugins().size());
  198. assertFalse(Files.exists(pluginZip1.path()));
  199. assertFalse(Files.exists(pluginZip1.unzippedPath()));
  200. assertTrue(Files.exists(pluginZip3.path()));
  201. assertTrue(Files.exists(pluginZip3.unzippedPath()));
  202. }
  203. }