]> source.dussan.org Git - pf4j.git/commitdiff
Adding parsing of plugin.description and plugin.requires to the properties finder...
authorJan Høydahl <jan.git@cominvent.com>
Thu, 30 Mar 2017 08:37:14 +0000 (10:37 +0200)
committerJan Høydahl <jan.git@cominvent.com>
Thu, 30 Mar 2017 08:37:14 +0000 (10:37 +0200)
pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java
pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java [new file with mode: 0644]

index b8595bbf92195a6764faec1e908a81abba702cc0..b774cf93b85a1185998bfd6dfd3a87ffdc6b5fe5 100644 (file)
@@ -82,6 +82,13 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
         String id = properties.getProperty("plugin.id");
         pluginDescriptor.setPluginId(id);
 
+        String description = properties.getProperty("plugin.description");
+        if (StringUtils.isEmpty(description)) {
+            pluginDescriptor.setPluginDescription("");
+        } else {
+            pluginDescriptor.setPluginDescription(description);
+        }
+
         String clazz = properties.getProperty("plugin.class");
         pluginDescriptor.setPluginClass(clazz);
 
@@ -96,6 +103,11 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
         String dependencies = properties.getProperty("plugin.dependencies");
         pluginDescriptor.setDependencies(dependencies);
 
+        String requires = properties.getProperty("plugin.requires");
+        if (StringUtils.isNotEmpty(requires)) {
+            pluginDescriptor.setRequires(requires);
+        }
+
         return pluginDescriptor;
     }
 
diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java
new file mode 100644 (file)
index 0000000..8ee52a6
--- /dev/null
@@ -0,0 +1,177 @@
+package ro.fortsoft.pf4j;
+
+import com.github.zafarkhaja.semver.Version;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class PropertiesPluginDescriptorFinderTest {
+
+    @Rule
+    public TemporaryFolder testFolder = new TemporaryFolder();
+
+    @Before
+    public void setUp() throws IOException {
+        Charset charset = Charset.forName("UTF-8");
+
+        Path pluginPath = testFolder.newFolder("test-plugin-1").toPath();
+        Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), charset);
+
+        pluginPath = testFolder.newFolder("test-plugin-2").toPath();
+        Files.write(pluginPath.resolve("plugin.properties"), getPlugin2Properties(), charset);
+
+        // empty plugin
+        testFolder.newFolder("test-plugin-3");
+
+        // no plugin class
+        pluginPath = testFolder.newFolder("test-plugin-4").toPath();
+        Files.write(pluginPath.resolve("plugin.properties"), getPlugin4Properties(), charset);
+
+        // no plugin version
+        pluginPath = testFolder.newFolder("test-plugin-5").toPath();
+        Files.write(pluginPath.resolve("plugin.properties"), getPlugin5Properties(), charset);
+
+        // no plugin id
+        pluginPath = testFolder.newFolder("test-plugin-6").toPath();
+        Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset);
+    }
+
+    @Test
+    public void testFind() throws Exception {
+        PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder();
+
+        PluginDescriptor plugin1 = instance.find(getPluginsRoot().resolve("test-plugin-1"));
+        PluginDescriptor plugin2 = instance.find(getPluginsRoot().resolve("test-plugin-2"));
+
+        assertEquals("test-plugin-1", plugin1.getPluginId());
+        assertEquals("Test Plugin 1", plugin1.getPluginDescription());
+        assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
+        assertEquals(Version.valueOf("0.0.1"), plugin1.getVersion());
+        assertEquals("Decebal Suiu", plugin1.getProvider());
+        assertEquals(2, plugin1.getDependencies().size());
+        assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
+        assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId());
+        assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
+        assertTrue(plugin1.getRequires().interpret(Version.valueOf("1.0.0")));
+
+        assertEquals("test-plugin-2", plugin2.getPluginId());
+        assertEquals("", plugin2.getPluginDescription());
+        assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
+        assertEquals(Version.valueOf("0.0.1"), plugin2.getVersion());
+        assertEquals("Decebal Suiu", plugin2.getProvider());
+        assertEquals(0, plugin2.getDependencies().size());
+        assertTrue(plugin2.getRequires().interpret(Version.valueOf("1.0.0")));
+    }
+
+    @Test(expected = PluginException.class)
+    public void testFindNotFound() throws Exception {
+        PluginDescriptorFinder instance = new PropertiesPluginDescriptorFinder();
+        instance.find(getPluginsRoot().resolve("test-plugin-3"));
+    }
+
+    @Test(expected = PluginException.class)
+    public void testFindMissingPluginClass() throws Exception {
+        PluginDescriptorFinder instance = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath());
+        instance.find(getPluginsRoot().resolve("test-plugin-4"));
+    }
+
+    @Test(expected = PluginException.class)
+    public void testFindMissingPluginVersion() throws Exception {
+        PluginDescriptorFinder instance = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath());
+        instance.find(getPluginsRoot().resolve("test-plugin-5"));
+    }
+
+    @Test(expected = PluginException.class)
+    public void testFindMissingPluginId() throws Exception {
+        PluginDescriptorFinder instance = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath());
+        instance.find(getPluginsRoot().resolve("test-plugin-6"));
+    }
+
+    private List<String> getPlugin1Properties() {
+        String[] lines = new String[] {
+            "plugin.id=test-plugin-1\n"
+            + "plugin.version=0.0.1\n"
+            + "plugin.description=Test Plugin 1\n"
+            + "plugin.provider=Decebal Suiu\n"
+            + "plugin.class=ro.fortsoft.pf4j.plugin.TestPlugin\n"
+            + "plugin.dependencies=test-plugin-2,test-plugin-3@~1.0\n"
+            + "plugin.requires=*\n"
+            + "\n"
+            + ""
+        };
+
+        return Arrays.asList(lines);
+    }
+
+    private List<String> getPlugin2Properties() {
+        String[] lines = new String[] {
+            "plugin.id=test-plugin-2\n"
+            + "plugin.version=0.0.1\n"
+            + "plugin.provider=Decebal Suiu\n"
+            + "plugin.class=ro.fortsoft.pf4j.plugin.TestPlugin\n"
+            + "plugin.dependencies=\n"
+            + "plugin.requires=*\n"
+            + "\n"
+            + ""
+        };
+
+        return Arrays.asList(lines);
+    }
+
+    private List<String> getPlugin4Properties() {
+        String[] lines = new String[] {
+            "plugin.id=test-plugin-2\n"
+            + "plugin.version=0.0.1\n"
+            + "plugin.provider=Decebal Suiu\n"
+            + "plugin.dependencies=\n"
+            + "plugin.requires=*\n"
+            + "\n"
+            + ""
+        };
+
+        return Arrays.asList(lines);
+    }
+
+    private List<String> getPlugin5Properties() {
+        String[] lines = new String[] {
+            "plugin.id=test-plugin-2\n"
+            + "plugin.provider=Decebal Suiu\n"
+            + "plugin.class=ro.fortsoft.pf4j.plugin.TestPlugin\n"
+            + "plugin.dependencies=\n"
+            + "plugin.requires=*\n"
+            + "\n"
+            + ""
+        };
+
+        return Arrays.asList(lines);
+    }
+
+    private List<String> getPlugin6Properties() {
+        String[] lines = new String[] {
+            "plugin.version=0.0.1\n"
+            + "plugin.provider=Decebal Suiu\n"
+            + "plugin.class=ro.fortsoft.pf4j.plugin.TestPlugin\n"
+            + "plugin.dependencies=\n"
+            + "plugin.requires=*\n"
+            + "\n"
+            + ""
+        };
+
+        return Arrays.asList(lines);
+    }
+    private Path getPluginsRoot() {
+        return testFolder.getRoot().toPath();
+    }
+
+}