Browse Source

Add pluginClass getter in PluginZip and PluginJar

async
Decebal Suiu 6 years ago
parent
commit
642f6343c9
  1. 50
      pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java
  2. 13
      pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java
  3. 17
      pf4j/src/test/java/org/pf4j/plugin/PluginJar.java
  4. 17
      pf4j/src/test/java/org/pf4j/plugin/PluginZip.java

50
pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java

@ -19,12 +19,18 @@ import org.junit.jupiter.api.Test;
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;
@ -49,9 +55,8 @@ public class CompoundPluginDescriptorFinderTest {
@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());
@ -68,12 +73,14 @@ public class CompoundPluginDescriptorFinderTest {
.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());
}
@ -95,21 +102,24 @@ public class CompoundPluginDescriptorFinderTest {
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, "");
}
}
}

13
pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java

@ -19,6 +19,7 @@ import org.junit.jupiter.api.BeforeEach;
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;
@ -78,7 +79,7 @@ public class PropertiesPluginDescriptorFinderTest {
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());
@ -92,7 +93,7 @@ public class PropertiesPluginDescriptorFinderTest {
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());
@ -109,10 +110,10 @@ public class PropertiesPluginDescriptorFinderTest {
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");
@ -123,9 +124,9 @@ public class PropertiesPluginDescriptorFinderTest {
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);
@ -145,8 +146,8 @@ public class PropertiesPluginDescriptorFinderTest {
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, "*");
@ -155,9 +156,9 @@ public class PropertiesPluginDescriptorFinderTest {
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, "*");

17
pf4j/src/test/java/org/pf4j/plugin/PluginJar.java

@ -37,11 +37,13 @@ public class PluginJar {
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;
}
@ -49,6 +51,10 @@ public class PluginJar {
return path;
}
public String pluginClass() {
return pluginClass;
}
public String pluginId() {
return pluginId;
}
@ -73,6 +79,7 @@ public class PluginJar {
private final Path path;
private final String pluginId;
private String pluginClass;
private String pluginVersion;
private Map<String, String> attributes;
@ -81,6 +88,12 @@ public class PluginJar {
this.pluginId = pluginId;
}
public Builder pluginClass(String pluginClass) {
this.pluginClass = pluginClass;
return this;
}
public Builder pluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
@ -106,7 +119,9 @@ public class PluginJar {
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);
}

17
pf4j/src/test/java/org/pf4j/plugin/PluginZip.java

@ -36,11 +36,13 @@ public class PluginZip {
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;
}
@ -52,6 +54,10 @@ public class PluginZip {
return pluginId;
}
public String pluginClass() {
return pluginClass;
}
public String pluginVersion() {
return pluginVersion;
}
@ -75,6 +81,7 @@ public class PluginZip {
private final Path path;
private final String pluginId;
private String pluginClass;
private String pluginVersion;
private Map<String, String> properties;
@ -83,6 +90,12 @@ public class PluginZip {
this.pluginId = pluginId;
}
public Builder pluginClass(String pluginClass) {
this.pluginClass = pluginClass;
return this;
}
public Builder pluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
@ -108,7 +121,9 @@ public class PluginZip {
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);
}

Loading…
Cancel
Save