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.

PropertiesPluginDescriptorFinderTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.test.PluginZip;
  21. import org.pf4j.test.TestPlugin;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25. import java.io.Writer;
  26. import java.nio.charset.StandardCharsets;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.util.LinkedHashMap;
  30. import java.util.Map;
  31. import java.util.Properties;
  32. import static org.junit.jupiter.api.Assertions.assertEquals;
  33. import static org.junit.jupiter.api.Assertions.assertFalse;
  34. import static org.junit.jupiter.api.Assertions.assertThrows;
  35. import static org.junit.jupiter.api.Assertions.assertTrue;
  36. public class PropertiesPluginDescriptorFinderTest {
  37. private VersionManager versionManager;
  38. @TempDir
  39. Path pluginsPath;
  40. @BeforeEach
  41. public void setUp() throws IOException {
  42. Path pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-1"));
  43. storePropertiesToPath(getPlugin1Properties(), pluginPath);
  44. pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-2"));
  45. storePropertiesToPath(getPlugin2Properties(), pluginPath);
  46. // empty plugin
  47. Files.createDirectories(pluginsPath.resolve("test-plugin-3"));
  48. // no plugin class
  49. pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-4"));
  50. storePropertiesToPath(getPlugin4Properties(), pluginPath);
  51. // no plugin version
  52. pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-5"));
  53. storePropertiesToPath(getPlugin5Properties(), pluginPath);
  54. // no plugin id
  55. pluginPath = Files.createDirectory(pluginsPath.resolve("test-plugin-6"));
  56. storePropertiesToPath(getPlugin6Properties(), pluginPath);
  57. versionManager = new DefaultVersionManager();
  58. }
  59. @Test
  60. public void testFind() throws Exception {
  61. PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
  62. PluginDescriptor plugin1 = descriptorFinder.find(pluginsPath.resolve("test-plugin-1"));
  63. PluginDescriptor plugin2 = descriptorFinder.find(pluginsPath.resolve("test-plugin-2"));
  64. assertEquals("test-plugin-1", plugin1.getPluginId());
  65. assertEquals("Test Plugin 1", plugin1.getPluginDescription());
  66. assertEquals(TestPlugin.class.getName(), plugin1.getPluginClass());
  67. assertEquals("0.0.1", plugin1.getVersion());
  68. assertEquals("Decebal Suiu", plugin1.getProvider());
  69. assertEquals(2, plugin1.getDependencies().size());
  70. assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
  71. assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId());
  72. assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
  73. assertEquals("Apache-2.0", plugin1.getLicense());
  74. assertEquals(">=1", plugin1.getRequires());
  75. assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin1.getRequires()));
  76. assertFalse(versionManager.checkVersionConstraint("0.1.0", plugin1.getRequires()));
  77. assertEquals("test-plugin-2", plugin2.getPluginId());
  78. assertEquals("", plugin2.getPluginDescription());
  79. assertEquals(TestPlugin.class.getName(), plugin2.getPluginClass());
  80. assertEquals("0.0.1", plugin2.getVersion());
  81. assertEquals("Decebal Suiu", plugin2.getProvider());
  82. assertEquals(0, plugin2.getDependencies().size());
  83. assertEquals("*", plugin2.getRequires()); // Default is *
  84. assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin2.getRequires()));
  85. }
  86. @Test
  87. public void testNotFound() {
  88. PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
  89. assertThrows(PluginRuntimeException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3")));
  90. }
  91. private Properties getPlugin1Properties() {
  92. Map<String, String> map = new LinkedHashMap<>(8);
  93. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
  94. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
  95. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  96. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1");
  97. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  98. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
  99. map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1");
  100. map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
  101. return PluginZip.createProperties(map);
  102. }
  103. private Properties getPlugin2Properties() {
  104. Map<String, String> map = new LinkedHashMap<>(5);
  105. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
  106. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
  107. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  108. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  109. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
  110. return PluginZip.createProperties(map);
  111. }
  112. private Properties getPlugin4Properties() {
  113. Map<String, String> map = new LinkedHashMap<>(5);
  114. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
  115. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  116. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  117. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
  118. map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
  119. return PluginZip.createProperties(map);
  120. }
  121. private Properties getPlugin5Properties() {
  122. Map<String, String> map = new LinkedHashMap<>(5);
  123. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
  124. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
  125. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  126. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
  127. map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
  128. return PluginZip.createProperties(map);
  129. }
  130. private Properties getPlugin6Properties() {
  131. Map<String, String> map = new LinkedHashMap<>(5);
  132. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
  133. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  134. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  135. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
  136. map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
  137. return PluginZip.createProperties(map);
  138. }
  139. private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException {
  140. Path path = pluginPath.resolve(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
  141. try (Writer writer = new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
  142. properties.store(writer, "");
  143. }
  144. }
  145. }