From de2a2ecfbad573151c7bf19f77ab0710362edbb1 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Sat, 12 Oct 2024 18:45:35 +0300 Subject: [PATCH] Fix #590 --- .../java/org/pf4j/AbstractPluginManager.java | 2 +- .../main/java/org/pf4j/PluginStateEvent.java | 14 ++++++++++++++ .../org/pf4j/AbstractPluginManagerTest.java | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java index f576210..626dbcb 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java +++ b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java @@ -516,7 +516,7 @@ public abstract class AbstractPluginManager implements PluginManager { pluginWrapper.setPluginState(PluginState.STOPPED); getStartedPlugins().remove(pluginWrapper); - firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, PluginState.STOPPED)); + firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, PluginState.STARTED)); return PluginState.STOPPED; } diff --git a/pf4j/src/main/java/org/pf4j/PluginStateEvent.java b/pf4j/src/main/java/org/pf4j/PluginStateEvent.java index 8793abe..efcc66a 100644 --- a/pf4j/src/main/java/org/pf4j/PluginStateEvent.java +++ b/pf4j/src/main/java/org/pf4j/PluginStateEvent.java @@ -16,6 +16,7 @@ package org.pf4j; import java.util.EventObject; +import java.util.Objects; /** * Event object that indicates a change in the state of a plugin. @@ -83,4 +84,17 @@ public class PluginStateEvent extends EventObject { ']'; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PluginStateEvent that = (PluginStateEvent) o; + return Objects.equals(plugin, that.plugin) && oldState == that.oldState; + } + + @Override + public int hashCode() { + return Objects.hash(plugin, oldState); + } + } diff --git a/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java b/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java index 88be865..e67e081 100644 --- a/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java +++ b/pf4j/src/test/java/org/pf4j/AbstractPluginManagerTest.java @@ -138,6 +138,25 @@ public class AbstractPluginManagerTest { verify(pluginManager, times(1)).resolveDependencies(); } + @Test + void stopPluginFirePluginStateListeners() { + PluginStateListener pluginStateListener = mock(PluginStateListener.class); + pluginManager.addPluginStateListener(pluginStateListener); + + 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(); + + pluginManager.stopPlugin(pluginId, false); + + PluginStateEvent event = new PluginStateEvent(pluginManager, pluginWrapper, PluginState.STARTED); + verify(pluginStateListener).pluginStateChanged(event); + } + private PluginWrapper createPluginWrapper(String pluginId, String... dependencies) { PluginDescriptor pluginDescriptor = new DefaultPluginDescriptor() .setPluginId(pluginId) -- 2.39.5