You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyExtensionFinderTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import org.junit.jupiter.api.Test;
  18. import org.junit.jupiter.api.condition.EnabledOnOs;
  19. import org.junit.jupiter.api.io.TempDir;
  20. import org.pf4j.test.PluginJar;
  21. import org.pf4j.test.TestExtension;
  22. import org.pf4j.test.TestPlugin;
  23. import java.nio.file.Path;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import static org.hamcrest.MatcherAssert.assertThat;
  27. import static org.hamcrest.Matchers.contains;
  28. import static org.junit.jupiter.api.Assertions.assertEquals;
  29. import static org.junit.jupiter.api.Assertions.assertFalse;
  30. import static org.junit.jupiter.api.Assertions.assertNotNull;
  31. import static org.junit.jupiter.api.Assertions.assertTrue;
  32. import static org.junit.jupiter.api.condition.OS.WINDOWS;
  33. public class LegacyExtensionFinderTest {
  34. @TempDir
  35. Path pluginsPath;
  36. @Test
  37. @EnabledOnOs(WINDOWS)
  38. public void shouldUnlockFileAfterReadingExtensionsFromPlugin() throws Exception {
  39. PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("test-plugin.jar"), "test-plugin")
  40. .pluginClass(TestPlugin.class.getName())
  41. .pluginVersion("1.2.3")
  42. .extension(TestExtension.class.getName())
  43. .build();
  44. assertTrue(pluginJar.file().exists());
  45. PluginManager pluginManager = new JarPluginManager(pluginsPath);
  46. pluginManager.loadPlugins();
  47. assertEquals(1, pluginManager.getPlugins().size());
  48. LegacyExtensionFinder extensionFinder = new LegacyExtensionFinder(pluginManager);
  49. Map<String, Set<String>> pluginsStorages = extensionFinder.readPluginsStorages();
  50. assertNotNull(pluginsStorages);
  51. pluginManager.unloadPlugin(pluginJar.pluginId());
  52. boolean fileDeleted = pluginJar.file().delete();
  53. Set<String> pluginStorages = pluginsStorages.get(pluginJar.pluginId());
  54. assertNotNull(pluginStorages);
  55. assertEquals(1, pluginStorages.size());
  56. assertThat(pluginStorages, contains(TestExtension.class.getName()));
  57. assertTrue(fileDeleted);
  58. assertFalse(pluginJar.file().exists());
  59. }
  60. @Test
  61. public void shouldFindExtensionsWithApdStrategy() throws Exception {
  62. PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("test-plugin.jar"), "test-plugin").pluginClass(TestPlugin.class.getName()).pluginVersion("1.2.3").extension(TestExtension.class.getName()).build();
  63. assertTrue(pluginJar.file().exists());
  64. PluginManager pluginManager = new JarPluginManager(pluginsPath) {
  65. @Override
  66. protected PluginLoader createPluginLoader() {
  67. return new JarPluginLoader(this) {
  68. @Override
  69. public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) {
  70. PluginClassLoader pluginClassLoader = new PluginClassLoader(pluginManager, pluginDescriptor, getClass().getClassLoader(), ClassLoadingStrategy.APD);
  71. pluginClassLoader.addFile(pluginPath.toFile());
  72. return pluginClassLoader;
  73. }
  74. };
  75. }
  76. };
  77. pluginManager.loadPlugins();
  78. pluginManager.startPlugins();
  79. assertEquals(1, pluginManager.getPlugins().size());
  80. LegacyExtensionFinder extensionFinder = new LegacyExtensionFinder(pluginManager);
  81. assertEquals(1, extensionFinder.find("test-plugin").size());
  82. pluginManager.unloadPlugin(pluginJar.pluginId());
  83. }
  84. }