import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
+import org.pf4j.test.JavaFileObjectUtils;
+import org.pf4j.test.JavaSources;
import org.pf4j.test.PluginJar;
import org.pf4j.test.PluginZip;
+import javax.tools.JavaFileObject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.pf4j.test.JavaSources.Greeting;
-public class DefaultPluginManagerTest {
+class DefaultPluginManagerTest {
private DefaultPluginManager pluginManager;
private DefaultPluginDescriptor pluginDescriptor;
}
@Test
- public void validateOK() {
+ void validateOK() {
pluginManager.validatePluginDescriptor(pluginDescriptor);
}
@Test
- public void validateFailsOnId() {
+ void validateFailsOnId() {
pluginDescriptor.setPluginId("");
assertThrows(PluginRuntimeException.class, () -> pluginManager.validatePluginDescriptor(pluginDescriptor));
}
@Test
- public void validateFailsOnVersion() {
+ void validateFailsOnVersion() {
pluginDescriptor.setPluginVersion(null);
assertThrows(PluginRuntimeException.class, () -> pluginManager.validatePluginDescriptor(pluginDescriptor));
}
@Test
- public void validateNoPluginClass() {
+ void validateNoPluginClass() {
pluginManager.validatePluginDescriptor(pluginDescriptor);
assertEquals(Plugin.class.getName(), pluginDescriptor.getPluginClass());
}
@Test
- public void isPluginValid() {
+ void isPluginValid() {
// By default, accept all since system version not given
assertTrue(pluginManager.isPluginValid(pluginWrapper));
}
@Test
- public void isPluginValidAllowExact() {
+ void isPluginValidAllowExact() {
pluginManager.setExactVersionAllowed(true);
// By default, accept all since system version not given
}
@Test
- public void testDefaultExactVersionAllowed() {
+ void testDefaultExactVersionAllowed() {
assertFalse(pluginManager.isExactVersionAllowed());
}
* See <a href="https://github.com/pf4j/pf4j/issues/223">#223</a>.
*/
@Test
- public void testPluginDisabledNoStart() throws IOException {
+ void testPluginDisabledNoStart() throws IOException {
new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3")
.build();
}
@Test
- public void deleteZipPlugin() throws Exception {
+ void deleteZipPlugin() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3")
.build();
}
@Test
- public void deleteJarPlugin() throws Exception {
+ void deleteJarPlugin() throws Exception {
PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("my-plugin-1.2.3.jar"), "myPlugin")
.pluginVersion("1.2.3")
.build();
}
@Test
- public void loadedPluginWithMissingDependencyCanBeUnloaded() throws IOException {
+ void loadedPluginWithMissingDependencyCanBeUnloaded() throws IOException {
pluginManager.setResolveRecoveryStrategy(AbstractPluginManager.ResolveRecoveryStrategy.IGNORE_PLUGIN_AND_CONTINUE);
PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.1.1.zip"), "myPlugin")
}
@Test
- public void deleteZipPluginForPluginThatHasNotBeenStartedPostsUnloadedEvent() throws Exception {
+ void deleteZipPluginForPluginThatHasNotBeenStartedPostsUnloadedEvent() throws Exception {
PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
.pluginVersion("1.2.3")
.build();
assertEquals(PluginState.STOPPED, pluginManager.getPlugin(pluginZip1.pluginId()).getPluginState());
}
+ @Test
+ void unloadPlugin() throws IOException, ClassNotFoundException {
+ JavaFileObject object = JavaSources.compile(Greeting);
+ String pluginClassName = JavaFileObjectUtils.getClassName(object);
+ byte[] pluginBytes = JavaFileObjectUtils.getAllBytes(object);
+
+ PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
+ .pluginVersion("1.2.3")
+ .addFile(Paths.get("classes", "test", "Greeting.class"), pluginBytes)
+ .build();
+
+ pluginManager.loadPlugin(pluginZip.path());
+ pluginManager.startPlugin(pluginZip.pluginId());
+
+ assertEquals(1, pluginManager.getPlugins().size());
+
+ PluginWrapper plugin = pluginManager.getPlugin(pluginZip.pluginId());
+ ClassLoader classLoader = plugin.getPluginClassLoader();
+ assertNotNull(classLoader);
+ assertTrue(classLoader instanceof PluginClassLoader);
+ PluginClassLoader pluginClassLoader = (PluginClassLoader) classLoader;
+
+ Class<?> clazz = classLoader.loadClass(pluginClassName);
+ assertNotNull(clazz);
+
+ boolean unloaded = pluginManager.unloadPlugin(pluginZip.pluginId());
+ assertTrue(unloaded);
+ assertTrue(pluginManager.getPlugins().isEmpty());
+ assertNull(pluginManager.getPluginClassLoader(pluginZip.pluginId()));
+ assertTrue(pluginClassLoader.isClosed());
+ }
+
}
/**
* @author Decebal Suiu
*/
-public class DependencyResolverTest {
+class DependencyResolverTest {
private DependencyResolver resolver;
}
@Test
- public void sortedPlugins() {
+ void resolve() {
+ PluginDescriptor pd1 = new DefaultPluginDescriptor()
+ .setPluginId("p1")
+ .setDependencies("p2");
+
+ PluginDescriptor pd2 = new DefaultPluginDescriptor()
+ .setPluginId("p2");
+
+ List<PluginDescriptor> plugins = Arrays.asList(pd1, pd2);
+
+ DependencyResolver.Result result = resolver.resolve(plugins);
+
+ assertFalse(result.hasCyclicDependency());
+ assertTrue(result.getNotFoundDependencies().isEmpty());
+ assertTrue(result.getWrongVersionDependencies().isEmpty());
+ }
+
+ @Test
+ void sortedPlugins() {
// create incomplete plugin descriptor (ignore some attributes)
PluginDescriptor pd1 = new DefaultPluginDescriptor()
.setPluginId("p1")
}
@Test
- public void notFoundDependencies() {
+ void notFoundDependencies() {
PluginDescriptor pd1 = new DefaultPluginDescriptor()
.setPluginId("p1")
.setDependencies("p2, p3");
DependencyResolver.Result result = resolver.resolve(plugins);
assertFalse(result.getNotFoundDependencies().isEmpty());
- assertEquals(result.getNotFoundDependencies(), Arrays.asList("p2", "p3"));
+ assertEquals(Arrays.asList("p2", "p3"), result.getNotFoundDependencies());
}
@Test
- public void cyclicDependencies() {
+ void cyclicDependencies() {
PluginDescriptor pd1 = new DefaultPluginDescriptor()
.setPluginId("p1")
.setPluginVersion("0.0.0")
}
@Test
- public void wrongDependencyVersion() {
+ void wrongDependencyVersion() {
PluginDescriptor pd1 = new DefaultPluginDescriptor()
.setPluginId("p1")
// .setDependencies("p2@2.0.0"); // simple version
}
@Test
- public void goodDependencyVersion() {
+ void goodDependencyVersion() {
PluginDescriptor pd1 = new DefaultPluginDescriptor()
.setPluginId("p1")
.setDependencies("p2@2.0.0");
*/
package org.pf4j.util;
-import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Decebal Suiu
*/
-public class DirectedGraphTest {
+class DirectedGraphTest {
private static DirectedGraph<Character> graph;
- @BeforeAll
- public static void setUp() {
+ @BeforeEach
+ public void setUp() {
graph = new DirectedGraph<>();
// add vertex
}
@Test
- public void reverseTopologicalSort() {
+ void reverseTopologicalSort() {
List<Character> result = graph.reverseTopologicalSort();
List<Character> expected = Arrays.asList('C', 'G', 'F', 'B', 'A', 'E', 'D');
assertEquals(expected, result);
}
@Test
- public void topologicalSort() {
+ void topologicalSort() {
List<Character> result = graph.topologicalSort();
List<Character> expected = Arrays.asList('D', 'E', 'A', 'B', 'F', 'G', 'C');
assertEquals(expected, result);
}
@Test
- public void inDegree() {
+ void inDegree() {
Map<Character, Integer> result = graph.inDegree();
Map<Character, Integer> expected = new HashMap<>(7);
expected.put('A', 0);
}
@Test
- public void outDegree() {
+ void outDegree() {
Map<Character, Integer> result = graph.outDegree();
Map<Character, Integer> expected = new HashMap<>(7);
expected.put('A', 1);
assertEquals(expected, result);
}
+ @Test
+ void getNeighbors() {
+ List<Character> result = graph.getNeighbors('B');
+ List<Character> expected = Arrays.asList('C', 'F');
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void removeEdge() {
+ graph.removeEdge('B', 'F');
+ List<Character> result = graph.getNeighbors('B');
+ List<Character> expected = Arrays.asList('C');
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void removeVertex() {
+ graph.removeVertex('B');
+ assertFalse(graph.containsVertex('B'));
+ }
+
+ @Test
+ void addEdge() {
+ graph.addEdge('B', 'G');
+ List<Character> result = graph.getNeighbors('B');
+ List<Character> expected = Arrays.asList('C', 'F', 'G');
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void addVertex() {
+ graph.addVertex('H');
+ assertTrue(graph.containsVertex('H'));
+ }
+
+ @Test
+ void containsVertex() {
+ assertTrue(graph.containsVertex('A'));
+ }
+
}