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

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