aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j/src/test/java
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2024-02-04 00:56:27 +0200
committerDecebal Suiu <decebal.suiu@gmail.com>2024-02-04 00:56:27 +0200
commit30f415fe2d92d38ab84cac119e52fd54a4bc36ba (patch)
tree97e6cd4c3b988034d6a87eb6b5ebbcd6dc5f09b3 /pf4j/src/test/java
parentd351e52ab4db778593538a616bc684c3d830c889 (diff)
downloadpf4j-30f415fe2d92d38ab84cac119e52fd54a4bc36ba.tar.gz
pf4j-30f415fe2d92d38ab84cac119e52fd54a4bc36ba.zip
Fix #520
Diffstat (limited to 'pf4j/src/test/java')
-rw-r--r--pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java88
-rw-r--r--pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java25
2 files changed, 106 insertions, 7 deletions
diff --git a/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java b/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java
index c43bc63..70a9312 100644
--- a/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java
+++ b/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java
@@ -15,28 +15,54 @@
*/
package org.pf4j;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.pf4j.test.TestExtension;
import org.pf4j.test.TestExtensionPoint;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
/**
* @author Decebal Suiu
*/
public class AbstractPluginManagerTest {
+ private AbstractPluginManager pluginManager;
+
+ @BeforeEach
+ void setUp() {
+ pluginManager = mock(AbstractPluginManager.class, withSettings()
+ .useConstructor()
+ .defaultAnswer(CALLS_REAL_METHODS));
+ }
+
+ @AfterEach
+ void tearDown() {
+ pluginManager = null;
+ }
+
@Test
public void getExtensionsByType() {
- AbstractPluginManager pluginManager = mock(AbstractPluginManager.class, CALLS_REAL_METHODS);
-
ExtensionFinder extensionFinder = mock(ExtensionFinder.class);
List<ExtensionWrapper<TestExtensionPoint>> extensionList = new ArrayList<>(1);
extensionList.add(new ExtensionWrapper<>(new ExtensionDescriptor(0, TestExtension.class), new DefaultExtensionFactory()));
@@ -46,11 +72,65 @@ public class AbstractPluginManagerTest {
List<TestExtensionPoint> extensions = pluginManager.getExtensions(TestExtensionPoint.class);
assertEquals(1, extensions.size());
}
-
+
@Test
public void getVersion() {
- AbstractPluginManager pluginManager = mock(AbstractPluginManager.class, CALLS_REAL_METHODS);
assertNotEquals("0.0.0", pluginManager.getVersion());
}
+ @Test
+ void stopStartedPlugin() throws IOException {
+ String pluginId = "plugin1";
+ PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
+ pluginWrapper.setPluginState(PluginState.STARTED);
+
+ doReturn(pluginWrapper).when(pluginManager).getPlugin(pluginId);
+ doNothing().when(pluginManager).checkPluginId(pluginId);
+ doReturn(new ArrayList<>(Arrays.asList(pluginWrapper))).when(pluginManager).getStartedPlugins();
+
+ PluginState pluginState = pluginManager.stopPlugin(pluginId, false);
+ verify(pluginWrapper.getPlugin()).stop();
+ assertSame(PluginState.STOPPED, pluginState);
+ }
+
+ @Test
+ void stopCreatedPlugin() {
+ String pluginId = "plugin1";
+ PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
+
+ doReturn(pluginWrapper).when(pluginManager).getPlugin(pluginId);
+ doNothing().when(pluginManager).checkPluginId(pluginId);
+ doReturn(new ArrayList<>(Arrays.asList(pluginWrapper))).when(pluginManager).getStartedPlugins();
+
+ PluginState pluginState = pluginManager.stopPlugin(pluginId, false);
+ verify(pluginWrapper.getPlugin(), never()).stop();
+ assertSame(PluginState.CREATED, pluginState);
+ }
+
+ @Test
+ void checkExistedPluginId() {
+ String pluginId = "plugin1";
+ PluginWrapper pluginWrapper = createPluginWrapper(pluginId);
+
+ pluginManager.plugins.put("plugin1", pluginWrapper);
+ pluginManager.checkPluginId("plugin1");
+ }
+
+ @Test
+ void checkNotExistedPluginId() {
+ assertThrows(IllegalArgumentException.class, () -> pluginManager.checkPluginId("plugin1"));
+ }
+
+ private PluginWrapper createPluginWrapper(String pluginId) {
+ DefaultPluginDescriptor pluginDescriptor = new DefaultPluginDescriptor();
+ pluginDescriptor.setPluginId(pluginId);
+ pluginDescriptor.setPluginVersion("1.2.3");
+
+ PluginWrapper pluginWrapper = new PluginWrapper(pluginManager, pluginDescriptor, Paths.get("plugin1"), getClass().getClassLoader());
+ Plugin plugin= mock(Plugin.class);
+ pluginWrapper.setPluginFactory(wrapper -> plugin);
+
+ return pluginWrapper;
+ }
+
}
diff --git a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java
index fab1a02..3edb1c5 100644
--- a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java
+++ b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java
@@ -28,6 +28,8 @@ import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -90,7 +92,7 @@ public class DefaultPluginManagerTest {
@Test
public void isPluginValid() {
- // By default accept all since system version not given
+ // By default, accept all since system version not given
assertTrue(pluginManager.isPluginValid(pluginWrapper));
pluginManager.setSystemVersion("1.0.0");
@@ -107,7 +109,7 @@ public class DefaultPluginManagerTest {
public void isPluginValidAllowExact() {
pluginManager.setExactVersionAllowed(true);
- // By default accept all since system version not given
+ // By default, accept all since system version not given
assertTrue(pluginManager.isPluginValid(pluginWrapper));
pluginManager.setSystemVersion("1.0.0");
@@ -127,7 +129,7 @@ public class DefaultPluginManagerTest {
/**
* Test that a disabled plugin doesn't start.
- * See https://github.com/pf4j/pf4j/issues/223.
+ * See <a href="https://github.com/pf4j/pf4j/issues/223">#223</a>.
*/
@Test
public void testPluginDisabledNoStart() throws IOException {
@@ -190,4 +192,21 @@ public class DefaultPluginManagerTest {
assertFalse(pluginJar.file().exists());
}
+ @Test
+ public void loadedPluginWithMissingDependencyCanBeUnloaded() throws IOException {
+ PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.1.1.zip"), "myPlugin")
+ .pluginVersion("1.1.1")
+ .pluginDependencies("myBasePlugin")
+ .build();
+
+ pluginManager.loadPlugins();
+ pluginManager.startPlugins();
+
+ PluginWrapper myPlugin = pluginManager.getPlugin(pluginZip.pluginId());
+ assertNotNull(myPlugin);
+ assertNotEquals(PluginState.STARTED, myPlugin.getPluginState());
+
+ assertTrue(pluginManager.unloadPlugin(pluginZip.pluginId()));
+ }
+
}