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.

ManifestPluginDescriptorFinderTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.BeforeEach;
  18. import org.junit.jupiter.api.Test;
  19. import org.junit.jupiter.api.io.TempDir;
  20. import org.pf4j.plugin.PluginJar;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;
  26. import java.util.LinkedHashMap;
  27. import java.util.Map;
  28. import java.util.jar.Manifest;
  29. import static org.junit.jupiter.api.Assertions.assertEquals;
  30. import static org.junit.jupiter.api.Assertions.assertThrows;
  31. import static org.junit.jupiter.api.Assertions.assertTrue;
  32. /**
  33. * @author Mario Franco
  34. * @author Decebal Suiu
  35. */
  36. public class ManifestPluginDescriptorFinderTest {
  37. private VersionManager versionManager;
  38. @TempDir
  39. Path pluginsPath;
  40. @BeforeEach
  41. public void setUp() throws IOException {
  42. Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1"));
  43. storeManifestToPath(getPlugin1Manifest(), pluginPath);
  44. pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2"));
  45. storeManifestToPath(getPlugin2Manifest(), pluginPath);
  46. // empty plugin
  47. Files.createDirectories(pluginsPath.resolve("test-plugin-3"));
  48. // no plugin class
  49. pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4"));
  50. storeManifestToPath(getPlugin4Manifest(), pluginPath);
  51. // no plugin version
  52. pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5"));
  53. storeManifestToPath(getPlugin5Manifest(), pluginPath);
  54. // no plugin id
  55. pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6"));
  56. storeManifestToPath(getPlugin6Manifest(), pluginPath);
  57. versionManager = new DefaultVersionManager();
  58. }
  59. /**
  60. * Test of {@link ManifestPluginDescriptorFinder#find(Path)} method.
  61. */
  62. @Test
  63. public void testFind() throws Exception {
  64. PluginDescriptorFinder descriptorFinder = new ManifestPluginDescriptorFinder();
  65. PluginDescriptor plugin1 = descriptorFinder.find(pluginsPath.resolve("test-plugin-1"));
  66. PluginDescriptor plugin2 = descriptorFinder.find(pluginsPath.resolve("test-plugin-2"));
  67. assertEquals("test-plugin-1", plugin1.getPluginId());
  68. assertEquals("Test Plugin 1", plugin1.getPluginDescription());
  69. assertEquals("org.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
  70. assertEquals("0.0.1", plugin1.getVersion());
  71. assertEquals("Decebal Suiu", plugin1.getProvider());
  72. assertEquals(2, plugin1.getDependencies().size());
  73. assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
  74. assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId());
  75. assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
  76. assertEquals("Apache-2.0", plugin1.getLicense());
  77. assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin1.getRequires()));
  78. assertEquals("test-plugin-2", plugin2.getPluginId());
  79. assertEquals("", plugin2.getPluginDescription());
  80. assertEquals("org.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
  81. assertEquals("0.0.1", plugin2.getVersion());
  82. assertEquals("Decebal Suiu", plugin2.getProvider());
  83. assertEquals(0, plugin2.getDependencies().size());
  84. assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin2.getRequires()));
  85. }
  86. /**
  87. * Test of {@link ManifestPluginDescriptorFinder#find(Path)} method.
  88. */
  89. @Test
  90. public void testFindNotFound() {
  91. PluginDescriptorFinder descriptorFinder = new ManifestPluginDescriptorFinder();
  92. assertThrows(PluginRuntimeException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3")));
  93. }
  94. private Manifest getPlugin1Manifest() {
  95. Map<String, String> map = new LinkedHashMap<>(8);
  96. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
  97. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
  98. map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  99. map.put(ManifestPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1");
  100. map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  101. map.put(ManifestPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
  102. map.put(ManifestPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
  103. map.put(ManifestPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
  104. return PluginJar.createManifest(map);
  105. }
  106. private Manifest getPlugin2Manifest() {
  107. Map<String, String> map = new LinkedHashMap<>(5);
  108. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
  109. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
  110. map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  111. map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  112. map.put(ManifestPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
  113. return PluginJar.createManifest(map);
  114. }
  115. private Manifest getPlugin4Manifest() {
  116. Map<String, String> map = new LinkedHashMap<>(3);
  117. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
  118. map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  119. map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  120. return PluginJar.createManifest(map);
  121. }
  122. private Manifest getPlugin5Manifest() {
  123. Map<String, String> map = new LinkedHashMap<>(3);
  124. map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
  125. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
  126. map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  127. return PluginJar.createManifest(map);
  128. }
  129. private Manifest getPlugin6Manifest() {
  130. Map<String, String> map = new LinkedHashMap<>(2);
  131. map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
  132. map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  133. return PluginJar.createManifest(map);
  134. }
  135. private void storeManifestToPath(Manifest manifest, Path pluginPath) throws IOException {
  136. Path path = Files.createDirectory(pluginPath.resolve("META-INF"));
  137. try (OutputStream output = new FileOutputStream(path.resolve("MANIFEST.MF").toFile())) {
  138. manifest.write(output);
  139. }
  140. }
  141. }