diff options
author | Dmitry Timofeev <dmitry-timofeev@users.noreply.github.com> | 2019-03-02 22:10:05 +0200 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2019-03-02 22:10:05 +0200 |
commit | cd8e12c0bc7afe2d71855147fbffd54f32f5cc08 (patch) | |
tree | a009971416b8cd350467e7a75737c9486098d4d0 /pf4j/src/test | |
parent | f6ebda434d51a6e9708e6612f422eb88420ff99d (diff) | |
download | pf4j-cd8e12c0bc7afe2d71855147fbffd54f32f5cc08.tar.gz pf4j-cd8e12c0bc7afe2d71855147fbffd54f32f5cc08.zip |
Check no plugin with same pluginId is loaded (#287)
Diffstat (limited to 'pf4j/src/test')
-rw-r--r-- | pf4j/src/test/java/org/pf4j/LoadPluginsTest.java | 93 |
1 files changed, 84 insertions, 9 deletions
diff --git a/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java b/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java index 8b6af11..c2917e8 100644 --- a/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java +++ b/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java @@ -15,20 +15,26 @@ */ package org.pf4j; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.pf4j.plugin.PluginZip; - -import java.nio.file.Files; -import java.nio.file.Paths; - +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.pf4j.plugin.PluginZip; public class LoadPluginsTest { @@ -74,6 +80,75 @@ public class LoadPluginsTest { } @Test + public void loadPluginWithSameIdDifferentPathFails() throws Exception { + String pluginId = "myPlugin"; + String pluginVersion = "1.2.3"; + File plugin1Path = pluginsFolder.newFile("my-plugin-1.2.3.zip"); + PluginZip plugin1 = new PluginZip.Builder(plugin1Path, pluginId) + .pluginVersion(pluginVersion) + .build(); + + File plugin2Path = pluginsFolder.newFile("my-plugin-1.2.3-renamed.zip"); + PluginZip plugin2 = new PluginZip.Builder(plugin2Path, pluginId) + .pluginVersion(pluginVersion) + .build(); + + // Verify the first plugin with the given id is loaded + assertNotNull(pluginManager.loadPluginFromPath(plugin1.path())); + Path loadedPlugin1Path = pluginManager.getPlugin(pluginId).getPluginPath(); + + try { + // Verify the second plugin is not loaded as it has the same metadata + pluginManager.loadPluginFromPath(plugin2.path()); + fail("Expected loadPluginFromPath to fail"); + } catch (PluginException e) { + // Check the path of the loaded plugin remains the same + PluginWrapper loadedPlugin = pluginManager.getPlugin(pluginId); + assertThat(loadedPlugin.getPluginPath(), equalTo(loadedPlugin1Path)); + // Check the message includes relevant information + String message = e.getMessage(); + assertThat(message, startsWith("There is an already loaded plugin")); + assertThat(message, containsString(pluginId)); + assertThat(message, containsString("my-plugin-1.2.3-renamed")); + } + } + + /** + * This test verifies the behaviour as of PF4J 2.x, where plugins of different + * versions but with the pluginId cannot be loaded correctly because the API + * uses pluginId as the unique identifier of the loaded plugin. + */ + @Test + public void loadPluginWithSameIdDifferentVersionsFails() throws Exception { + String pluginId = "myPlugin"; + String plugin1Version = "1.2.3"; + File plugin1Path = pluginsFolder.newFile("my-plugin-1.2.3.zip"); + PluginZip plugin1 = new PluginZip.Builder(plugin1Path, pluginId) + .pluginVersion(plugin1Version) + .build(); + + String plugin2Version = "2.0.0"; + File plugin2Path = pluginsFolder.newFile("my-plugin-2.0.0.zip"); + PluginZip plugin2 = new PluginZip.Builder(plugin2Path, pluginId) + .pluginVersion(plugin2Version) + .build(); + + // Verify the first plugin with the given id is loaded + assertNotNull(pluginManager.loadPluginFromPath(plugin1.path())); + Path loadedPlugin1Path = pluginManager.getPlugin(pluginId).getPluginPath(); + try { + // Verify the second plugin is not loaded as it has the same pluginId + pluginManager.loadPluginFromPath(plugin2.path()); + fail("Expected loadPluginFromPath to fail"); + } catch (PluginException e) { + // Check the path and version of the loaded plugin remain the same + PluginWrapper loadedPlugin = pluginManager.getPlugin(pluginId); + assertThat(loadedPlugin.getPluginPath(), equalTo(loadedPlugin1Path)); + assertThat(loadedPlugin.getDescriptor().getVersion(), equalTo(plugin1Version)); + } + } + + @Test public void loadUnloadLoad() throws Exception { PluginZip pluginZip = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") .pluginVersion("1.2.3") |