import org.junit.jupiter.api.io.TempDir;
import org.pf4j.plugin.PluginJar;
import org.pf4j.plugin.PluginZip;
+import org.pf4j.plugin.TestPlugin;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.List;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Test
public void find() throws Exception {
- Path pluginPath = pluginsPath.resolve("test-plugin-1");
- Files.createDirectories(pluginPath);
- Files.write(pluginPath.resolve("plugin.properties"), getPlugin1Properties(), StandardCharsets.UTF_8);
+ Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1"));
+ storePropertiesToPath(getPlugin1Properties(), pluginPath);
PluginDescriptorFinder descriptorFinder = new CompoundPluginDescriptorFinder()
.add(new PropertiesPluginDescriptorFinder());
.add(new ManifestPluginDescriptorFinder());
PluginJar pluginJar = new PluginJar.Builder(pluginsPath.resolve("my-plugin-1.2.3.jar"), "myPlugin")
+ .pluginClass(TestPlugin.class.getName())
.pluginVersion("1.2.3")
.build();
PluginDescriptor pluginDescriptor = descriptorFinder.find(pluginJar.path());
assertNotNull(pluginDescriptor);
assertEquals("myPlugin", pluginJar.pluginId());
+ assertEquals(TestPlugin.class.getName(), pluginJar.pluginClass());
assertEquals("1.2.3", pluginJar.pluginVersion());
}
assertNotNull(pluginDescriptor);
}
- 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=org.pf4j.plugin.TestPlugin\n"
- + "plugin.dependencies=test-plugin-2,test-plugin-3@~1.0\n"
- + "plugin.requires=>=1\n"
- + "plugin.license=Apache-2.0\n"
- + "\n"
- + ""
- };
-
- return Arrays.asList(lines);
+ private Properties getPlugin1Properties() {
+ Map<String, String> map = new LinkedHashMap<>(7);
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
+
+ return PluginZip.createProperties(map);
+ }
+
+ private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException {
+ Path path = pluginPath.resolve(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
+ try (Writer writer = new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
+ properties.store(writer, "");
+ }
}
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.pf4j.plugin.PluginZip;
+import org.pf4j.plugin.TestPlugin;
import java.io.FileOutputStream;
import java.io.IOException;
assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription());
- assertEquals("org.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
+ assertEquals(TestPlugin.class.getName(), plugin1.getPluginClass());
assertEquals("0.0.1", plugin1.getVersion());
assertEquals("Decebal Suiu", plugin1.getProvider());
assertEquals(2, plugin1.getDependencies().size());
assertEquals("test-plugin-2", plugin2.getPluginId());
assertEquals("", plugin2.getPluginDescription());
- assertEquals("org.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
+ assertEquals(TestPlugin.class.getName(), plugin2.getPluginClass());
assertEquals("0.0.1", plugin2.getVersion());
assertEquals("Decebal Suiu", plugin2.getProvider());
assertEquals(0, plugin2.getDependencies().size());
private Properties getPlugin1Properties() {
Map<String, String> map = new LinkedHashMap<>(8);
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
- map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
private Properties getPlugin2Properties() {
Map<String, String> map = new LinkedHashMap<>(5);
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
- map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
return PluginZip.createProperties(map);
private Properties getPlugin5Properties() {
Map<String, String> map = new LinkedHashMap<>(5);
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
- map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
private Properties getPlugin6Properties() {
Map<String, String> map = new LinkedHashMap<>(5);
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, TestPlugin.class.getName());
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
- map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
private final Path path;
private final String pluginId;
+ private final String pluginClass;
private final String pluginVersion;
protected PluginJar(Builder builder) {
this.path = builder.path;
this.pluginId = builder.pluginId;
+ this.pluginClass = builder.pluginClass;
this.pluginVersion = builder.pluginVersion;
}
return path;
}
+ public String pluginClass() {
+ return pluginClass;
+ }
+
public String pluginId() {
return pluginId;
}
private final Path path;
private final String pluginId;
+ private String pluginClass;
private String pluginVersion;
private Map<String, String> attributes;
this.pluginId = pluginId;
}
+ public Builder pluginClass(String pluginClass) {
+ this.pluginClass = pluginClass;
+
+ return this;
+ }
+
public Builder pluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
Map<String, String> map = new LinkedHashMap<>();
map.put(ManifestPluginDescriptorFinder.PLUGIN_ID, pluginId);
map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
- map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
+ if (pluginClass != null) {
+ map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
+ }
if (attributes != null) {
map.putAll(attributes);
}
private final Path path;
private final String pluginId;
+ private final String pluginClass;
private final String pluginVersion;
protected PluginZip(Builder builder) {
this.path = builder.path;
this.pluginId = builder.pluginId;
+ this.pluginClass = builder.pluginClass;
this.pluginVersion = builder.pluginVersion;
}
return pluginId;
}
+ public String pluginClass() {
+ return pluginClass;
+ }
+
public String pluginVersion() {
return pluginVersion;
}
private final Path path;
private final String pluginId;
+ private String pluginClass;
private String pluginVersion;
private Map<String, String> properties;
this.pluginId = pluginId;
}
+ public Builder pluginClass(String pluginClass) {
+ this.pluginClass = pluginClass;
+
+ return this;
+ }
+
public Builder pluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
Map<String, String> map = new LinkedHashMap<>();
map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
- map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
+ if (pluginClass != null) {
+ map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
+ }
if (properties != null) {
map.putAll(properties);
}