Browse Source

Fix for #394 (#417)

pull/420/head
Decebal Suiu 4 years ago committed by GitHub
parent
commit
6f16cc47bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      pf4j/src/main/java/org/pf4j/DependencyResolver.java
  2. 71
      pf4j/src/test/java/org/pf4j/PluginDependencyTest.java
  3. 14
      pf4j/src/test/java/org/pf4j/test/PluginZip.java

10
pf4j/src/main/java/org/pf4j/DependencyResolver.java

@ -88,7 +88,7 @@ public class DependencyResolver {
String pluginId = plugin.getPluginId(); String pluginId = plugin.getPluginId();
String existingVersion = plugin.getVersion(); String existingVersion = plugin.getVersion();
List<String> dependents = new ArrayList<>(getDependents(pluginId)); List<String> dependents = getDependents(pluginId);
while (!dependents.isEmpty()) { while (!dependents.isEmpty()) {
String dependentId = dependents.remove(0); String dependentId = dependents.remove(0);
PluginDescriptor dependent = pluginByIds.get(dependentId); PluginDescriptor dependent = pluginByIds.get(dependentId);
@ -107,22 +107,22 @@ public class DependencyResolver {
* Retrieves the plugins ids that the given plugin id directly depends on. * Retrieves the plugins ids that the given plugin id directly depends on.
* *
* @param pluginId the unique plugin identifier, specified in its metadata * @param pluginId the unique plugin identifier, specified in its metadata
* @return * @return an immutable list of dependencies (new list for each call)
*/ */
public List<String> getDependencies(String pluginId) { public List<String> getDependencies(String pluginId) {
checkResolved(); checkResolved();
return dependenciesGraph.getNeighbors(pluginId); return new ArrayList<>(dependenciesGraph.getNeighbors(pluginId));
} }
/** /**
* Retrieves the plugins ids that the given content is a direct dependency of. * Retrieves the plugins ids that the given content is a direct dependency of.
* *
* @param pluginId the unique plugin identifier, specified in its metadata * @param pluginId the unique plugin identifier, specified in its metadata
* @return * @return an immutable list of dependents (new list for each call)
*/ */
public List<String> getDependents(String pluginId) { public List<String> getDependents(String pluginId) {
checkResolved(); checkResolved();
return dependentsGraph.getNeighbors(pluginId); return new ArrayList<>(dependentsGraph.getNeighbors(pluginId));
} }
/** /**

71
pf4j/src/test/java/org/pf4j/PluginDependencyTest.java

@ -15,8 +15,12 @@
*/ */
package org.pf4j; package org.pf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.pf4j.test.PluginZip;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -27,6 +31,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/ */
public class PluginDependencyTest { public class PluginDependencyTest {
private DefaultPluginManager pluginManager;
@TempDir
Path pluginsPath;
@BeforeEach
public void setUp() {
pluginManager = new DefaultPluginManager(pluginsPath);
}
/** /**
* Test of getPluginId method, of class PluginDependency. * Test of getPluginId method, of class PluginDependency.
*/ */
@ -77,4 +91,59 @@ public class PluginDependencyTest {
assertEquals("PluginDependency [pluginId=test, pluginVersionSupport=1.0, optional=true]", instance.toString()); assertEquals("PluginDependency [pluginId=test, pluginVersionSupport=1.0, optional=true]", instance.toString());
} }
@Test
public void dependentStop() throws Exception {
// B depends on A
PluginZip pluginA = new PluginZip.Builder(pluginsPath.resolve("A-plugin-1.2.3.zip"), "plugin.a")
.pluginVersion("1.2.3").build();
PluginZip pluginB = new PluginZip.Builder(pluginsPath.resolve("B-plugin-1.2.3.zip"), "plugin.b")
.pluginDependencies("plugin.a")
.pluginVersion("1.2.3").build();
pluginManager.loadPlugins();
assertEquals(2, pluginManager.getPlugins().size());
pluginManager.startPlugins();
assertEquals(2, pluginManager.getStartedPlugins().size());
// stop A, both A and B should be stopped
pluginManager.stopPlugin("plugin.a");
assertEquals(0, pluginManager.getStartedPlugins().size());
// start all again
pluginManager.startPlugins();
assertEquals(2, pluginManager.getStartedPlugins().size());
// dependent info should be kept. #394
pluginManager.stopPlugin("plugin.a");
assertEquals(0, pluginManager.getStartedPlugins().size());
}
@Test
public void dependentUnload() throws Exception {
// B depends on A
PluginZip pluginA = new PluginZip.Builder(pluginsPath.resolve("A-plugin-1.2.3.zip"), "plugin.a")
.pluginVersion("1.2.3").build();
PluginZip pluginB = new PluginZip.Builder(pluginsPath.resolve("B-plugin-1.2.3.zip"), "plugin.b")
.pluginDependencies("plugin.a")
.pluginVersion("1.2.3").build();
pluginManager.loadPlugins();
assertEquals(2, pluginManager.getPlugins().size());
pluginManager.startPlugins();
assertEquals(2, pluginManager.getStartedPlugins().size());
// stop A, both A and B should be stopped
pluginManager.stopPlugin("plugin.a");
assertEquals(0, pluginManager.getStartedPlugins().size());
// unload A, both A and B should be unloaded
pluginManager.unloadPlugin("plugin.a");
assertEquals(0, pluginManager.getResolvedPlugins().size());
assertEquals(0, pluginManager.getPlugins().size());
}
} }

14
pf4j/src/test/java/org/pf4j/test/PluginZip.java

@ -39,12 +39,14 @@ public class PluginZip {
private final String pluginId; private final String pluginId;
private final String pluginClass; private final String pluginClass;
private final String pluginVersion; private final String pluginVersion;
private final String pluginDependencies;
protected PluginZip(Builder builder) { protected PluginZip(Builder builder) {
this.path = builder.path; this.path = builder.path;
this.pluginId = builder.pluginId; this.pluginId = builder.pluginId;
this.pluginClass = builder.pluginClass; this.pluginClass = builder.pluginClass;
this.pluginVersion = builder.pluginVersion; this.pluginVersion = builder.pluginVersion;
this.pluginDependencies = builder.pluginDependencies;
} }
public Path path() { public Path path() {
@ -67,6 +69,8 @@ public class PluginZip {
return pluginVersion; return pluginVersion;
} }
public String pluginDependencies() { return pluginDependencies; }
public Path unzippedPath() { public Path unzippedPath() {
Path path = path(); Path path = path();
String fileName = path.getFileName().toString(); String fileName = path.getFileName().toString();
@ -88,6 +92,7 @@ public class PluginZip {
private String pluginClass; private String pluginClass;
private String pluginVersion; private String pluginVersion;
private String pluginDependencies;
private Map<String, String> properties = new LinkedHashMap<>(); private Map<String, String> properties = new LinkedHashMap<>();
private Map<Path, byte[]> files = new LinkedHashMap<>(); private Map<Path, byte[]> files = new LinkedHashMap<>();
@ -108,6 +113,12 @@ public class PluginZip {
return this; return this;
} }
public Builder pluginDependencies(String pluginDependencies) {
this.pluginDependencies = pluginDependencies;
return this;
}
/** /**
* Add extra properties to the {@code properties} file. * Add extra properties to the {@code properties} file.
* As possible attribute name please see {@link PropertiesPluginDescriptorFinder}. * As possible attribute name please see {@link PropertiesPluginDescriptorFinder}.
@ -162,6 +173,9 @@ public class PluginZip {
Map<String, String> map = new LinkedHashMap<>(); Map<String, String> map = new LinkedHashMap<>();
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId); map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion); map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
if (pluginDependencies != null) {
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, pluginDependencies);
}
if (pluginClass != null) { if (pluginClass != null) {
map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass); map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
} }

Loading…
Cancel
Save