From 1bd6b801d45193b660d59656fafe6ea6cde77975 Mon Sep 17 00:00:00 2001 From: hazemkmammu Date: Tue, 4 Jun 2019 15:02:55 +0530 Subject: [PATCH] Recreate FileSystemException in windows on plugin delete (#321) --- .../org/pf4j/LegacyExtensionFinderTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pf4j/src/test/java/org/pf4j/LegacyExtensionFinderTest.java diff --git a/pf4j/src/test/java/org/pf4j/LegacyExtensionFinderTest.java b/pf4j/src/test/java/org/pf4j/LegacyExtensionFinderTest.java new file mode 100644 index 0000000..fe2cac2 --- /dev/null +++ b/pf4j/src/test/java/org/pf4j/LegacyExtensionFinderTest.java @@ -0,0 +1,69 @@ +package org.pf4j; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.pf4j.plugin.PluginJar; + +public class LegacyExtensionFinderTest { + + @TempDir + public Path pluginsPath; + + public static class TestPlugin extends Plugin { + + public TestPlugin(PluginWrapper wrapper) { + super(wrapper); + } + } + + @Extension + public static class TestExtension implements ExtensionPoint { + + } + + private static boolean systemIsWindows() { + String os = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); + return os.startsWith("win"); + } + + @Test + public void shouldUnlockFileAfterReadingExtensionsFromPlugin() throws IOException, PluginException { + + assumeTrue(systemIsWindows()); + final Path pluginJarPath = pluginsPath.resolve("test-plugin.jar"); + PluginJar pluginJar = new PluginJar.Builder(pluginJarPath, "test-plugin") + .pluginClass(TestPlugin.class.getName()) + .pluginVersion("1.2.3") + .extension(TestExtension.class.getName()) + .build(); + + JarPluginManager pluginManager = new JarPluginManager(pluginsPath); + + pluginManager.loadPlugins(); + LegacyExtensionFinder extensionFinder = new LegacyExtensionFinder(pluginManager); + Map> pluginStorages = extensionFinder.readPluginsStorages(); + pluginManager.unloadPlugin(pluginJar.pluginId()); + boolean fileDeleted = pluginJarPath.toFile().delete(); + + assertThat(pluginStorages, is(notNullValue())); + assertThat(pluginStorages.get(pluginJar.pluginId()), is(notNullValue())); + assertThat(pluginStorages.get(pluginJar.pluginId()).size(), is(equalTo(1))); + assertThat(pluginStorages.get(pluginJar.pluginId()), + contains("org.pf4j.LegacyExtensionFinderTest$TestExtension")); + assertThat(fileDeleted, is(equalTo(true))); + } + +} -- 2.39.5