diff options
author | Decebal Suiu <decebal.suiu@gmail.com> | 2024-06-24 16:26:34 +0300 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2024-06-24 16:26:34 +0300 |
commit | 7da511c29819802a9caef25580f9fac98edf6b78 (patch) | |
tree | 8f56e3a7c76b0cd2d73883a8c0f23e7efede61ec /pf4j | |
parent | b1db6c9a509cd7758345a5ca2e51a87c089cdb85 (diff) | |
download | pf4j-7da511c29819802a9caef25580f9fac98edf6b78.tar.gz pf4j-7da511c29819802a9caef25580f9fac98edf6b78.zip |
Add more tests
Diffstat (limited to 'pf4j')
3 files changed, 127 insertions, 28 deletions
diff --git a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java index 33f9db0..2562260 100644 --- a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java +++ b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java @@ -19,25 +19,32 @@ 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.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; @@ -75,30 +82,30 @@ public class DefaultPluginManagerTest { } @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)); @@ -113,7 +120,7 @@ public class DefaultPluginManagerTest { } @Test - public void isPluginValidAllowExact() { + void isPluginValidAllowExact() { pluginManager.setExactVersionAllowed(true); // By default, accept all since system version not given @@ -130,7 +137,7 @@ public class DefaultPluginManagerTest { } @Test - public void testDefaultExactVersionAllowed() { + void testDefaultExactVersionAllowed() { assertFalse(pluginManager.isExactVersionAllowed()); } @@ -139,7 +146,7 @@ public class DefaultPluginManagerTest { * 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(); @@ -180,7 +187,7 @@ public class DefaultPluginManagerTest { } @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(); @@ -203,7 +210,7 @@ public class DefaultPluginManagerTest { } @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(); @@ -226,7 +233,7 @@ public class DefaultPluginManagerTest { } @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") @@ -346,7 +353,7 @@ public class DefaultPluginManagerTest { } @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(); @@ -432,4 +439,36 @@ public class DefaultPluginManagerTest { 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()); + } + } diff --git a/pf4j/src/test/java/org/pf4j/DependencyResolverTest.java b/pf4j/src/test/java/org/pf4j/DependencyResolverTest.java index 6e35780..b960066 100644 --- a/pf4j/src/test/java/org/pf4j/DependencyResolverTest.java +++ b/pf4j/src/test/java/org/pf4j/DependencyResolverTest.java @@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Decebal Suiu */ -public class DependencyResolverTest { +class DependencyResolverTest { private DependencyResolver resolver; @@ -40,7 +40,25 @@ public class DependencyResolverTest { } @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") @@ -61,7 +79,7 @@ public class DependencyResolverTest { } @Test - public void notFoundDependencies() { + void notFoundDependencies() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2, p3"); @@ -72,11 +90,11 @@ public class DependencyResolverTest { 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") @@ -103,7 +121,7 @@ public class DependencyResolverTest { } @Test - public void wrongDependencyVersion() { + void wrongDependencyVersion() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") // .setDependencies("p2@2.0.0"); // simple version @@ -123,7 +141,7 @@ public class DependencyResolverTest { } @Test - public void goodDependencyVersion() { + void goodDependencyVersion() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2@2.0.0"); diff --git a/pf4j/src/test/java/org/pf4j/util/DirectedGraphTest.java b/pf4j/src/test/java/org/pf4j/util/DirectedGraphTest.java index 44b8c54..b1c0e53 100644 --- a/pf4j/src/test/java/org/pf4j/util/DirectedGraphTest.java +++ b/pf4j/src/test/java/org/pf4j/util/DirectedGraphTest.java @@ -15,7 +15,7 @@ */ 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; @@ -24,16 +24,18 @@ import java.util.List; 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 @@ -54,21 +56,21 @@ public class DirectedGraphTest { } @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); @@ -82,7 +84,7 @@ public class DirectedGraphTest { } @Test - public void outDegree() { + void outDegree() { Map<Character, Integer> result = graph.outDegree(); Map<Character, Integer> expected = new HashMap<>(7); expected.put('A', 1); @@ -95,4 +97,44 @@ public class DirectedGraphTest { 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')); + } + } |