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.

CompoundPluginDescriptorFinderTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.io.TempDir;
  19. import org.pf4j.test.PluginJar;
  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.assertNotNull;
  34. import static org.junit.jupiter.api.Assertions.assertThrows;
  35. /**
  36. * @author Decebal Suiu
  37. */
  38. public class CompoundPluginDescriptorFinderTest {
  39. @TempDir
  40. Path pluginsPath;
  41. @Test
  42. public void add() {
  43. CompoundPluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder();
  44. assertEquals(0, descriptorFinder.size());
  45. descriptorFinder.add(new PropertiesPluginDescriptorFinder());
  46. assertEquals(1, descriptorFinder.size());
  47. }
  48. @Test
  49. public void find() throws Exception {
  50. Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1"));
  51. storePropertiesToPath(getPlugin1Properties(), pluginPath);
  52. PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
  53. .add(new PropertiesPluginDescriptorFinder());
  54. PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginPath);
  55. assertNotNull(pluginDescriptor);
  56. assertEquals("test-plugin-1", pluginDescriptor.getPluginId());
  57. assertEquals("0.0.1", pluginDescriptor.getVersion());
  58. }
  59. @Test
  60. public void findInJar() throws Exception {
  61. PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
  62. .add(new ManifestPluginDescriptorFinder());
  63. PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("my-plugin-1.2.3.jar"), "myPlugin")
  64. .pluginClass(TestPlugin.class.getName())
  65. .pluginVersion("1.2.3")
  66. .build();
  67. PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
  68. assertNotNull(pluginDescriptor);
  69. assertEquals("myPlugin", pluginJar.pluginId());
  70. assertEquals(TestPlugin.class.getName(), pluginJar.pluginClass());
  71. assertEquals("1.2.3", pluginJar.pluginVersion());
  72. }
  73. @Test
  74. public void testNotFound() {
  75. PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder();
  76. assertThrows(PluginRuntimeException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3")));
  77. }
  78. @Test
  79. public void testSpaceCharacterInFileName() throws Exception {
  80. PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
  81. .add(new ManifestPluginDescriptorFinder());
  82. PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("my plugin-1.2.3.jar"), "myPlugin")
  83. .pluginVersion("1.2.3")
  84. .build();
  85. PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
  86. assertNotNull(pluginDescriptor);
  87. }
  88. private Properties getPlugin1Properties() {
  89. Map<String, String> map = new LinkedHashMap<>(7);
  90. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
  91. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
  92. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
  93. map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
  94. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
  95. map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1");
  96. map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
  97. return PluginZip.createProperties(map);
  98. }
  99. private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException {
  100. Path path = pluginPath.resolve(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
  101. try (Writer writer = new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
  102. properties.store(writer, "");
  103. }
  104. }
  105. }