Browse Source

Add constants for properties names

async
Decebal Suiu 6 years ago
parent
commit
44ed78f1c2
  1. 27
      pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java
  2. 69
      pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java

27
pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java

@ -36,7 +36,16 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
private static final Logger log = LoggerFactory.getLogger(PropertiesPluginDescriptorFinder.class); private static final Logger log = LoggerFactory.getLogger(PropertiesPluginDescriptorFinder.class);
private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties"; public static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties";
public static final String PLUGIN_ID = "plugin.id";
public static final String PLUGIN_DESCRIPTION = "plugin.description";
public static final String PLUGIN_CLASS = "plugin.class";
public static final String PLUGIN_VERSION = "plugin.version";
public static final String PLUGIN_PROVIDER = "plugin.provider";
public static final String PLUGIN_DEPENDENCIES = "plugin.dependencies";
public static final String PLUGIN_REQUIRES = "plugin.requires";
public static final String PLUGIN_LICENSE = "plugin.license";
protected String propertiesFileName; protected String propertiesFileName;
@ -98,38 +107,38 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance(); DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
// TODO validate !!! // TODO validate !!!
String id = properties.getProperty("plugin.id"); String id = properties.getProperty(PLUGIN_ID);
pluginDescriptor.setPluginId(id); pluginDescriptor.setPluginId(id);
String description = properties.getProperty("plugin.description"); String description = properties.getProperty(PLUGIN_DESCRIPTION);
if (StringUtils.isNullOrEmpty(description)) { if (StringUtils.isNullOrEmpty(description)) {
pluginDescriptor.setPluginDescription(""); pluginDescriptor.setPluginDescription("");
} else { } else {
pluginDescriptor.setPluginDescription(description); pluginDescriptor.setPluginDescription(description);
} }
String clazz = properties.getProperty("plugin.class"); String clazz = properties.getProperty(PLUGIN_CLASS);
if (StringUtils.isNotNullOrEmpty(clazz)) { if (StringUtils.isNotNullOrEmpty(clazz)) {
pluginDescriptor.setPluginClass(clazz); pluginDescriptor.setPluginClass(clazz);
} }
String version = properties.getProperty("plugin.version"); String version = properties.getProperty(PLUGIN_VERSION);
if (StringUtils.isNotNullOrEmpty(version)) { if (StringUtils.isNotNullOrEmpty(version)) {
pluginDescriptor.setPluginVersion(version); pluginDescriptor.setPluginVersion(version);
} }
String provider = properties.getProperty("plugin.provider"); String provider = properties.getProperty(PLUGIN_PROVIDER);
pluginDescriptor.setProvider(provider); pluginDescriptor.setProvider(provider);
String dependencies = properties.getProperty("plugin.dependencies"); String dependencies = properties.getProperty(PLUGIN_DEPENDENCIES);
pluginDescriptor.setDependencies(dependencies); pluginDescriptor.setDependencies(dependencies);
String requires = properties.getProperty("plugin.requires"); String requires = properties.getProperty(PLUGIN_REQUIRES);
if (StringUtils.isNotNullOrEmpty(requires)) { if (StringUtils.isNotNullOrEmpty(requires)) {
pluginDescriptor.setRequires(requires); pluginDescriptor.setRequires(requires);
} }
pluginDescriptor.setLicense(properties.getProperty("plugin.license")); pluginDescriptor.setLicense(properties.getProperty(PLUGIN_LICENSE));
return pluginDescriptor; return pluginDescriptor;
} }

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

@ -43,25 +43,25 @@ public class PropertiesPluginDescriptorFinderTest {
@BeforeEach @BeforeEach
public void setUp() throws IOException { public void setUp() throws IOException {
Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1")); Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1"));
storePropertiesToPath(getPlugin1Properties(), pluginPath.resolve("plugin.properties")); storePropertiesToPath(getPlugin1Properties(), pluginPath);
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2")); pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2"));
storePropertiesToPath(getPlugin2Properties(), pluginPath.resolve("plugin.properties")); storePropertiesToPath(getPlugin2Properties(), pluginPath);
// empty plugin // empty plugin
Files.createDirectories(pluginsPath.resolve("test-plugin-3")); Files.createDirectories(pluginsPath.resolve("test-plugin-3"));
// no plugin class // no plugin class
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4")); pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4"));
storePropertiesToPath(getPlugin4Properties(), pluginPath.resolve("plugin.properties")); storePropertiesToPath(getPlugin4Properties(), pluginPath);
// no plugin version // no plugin version
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5")); pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5"));
storePropertiesToPath(getPlugin5Properties(), pluginPath.resolve("plugin.properties")); storePropertiesToPath(getPlugin5Properties(), pluginPath);
// no plugin id // no plugin id
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6")); pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6"));
storePropertiesToPath(getPlugin6Properties(), pluginPath.resolve("plugin.properties")); storePropertiesToPath(getPlugin6Properties(), pluginPath);
versionManager = new DefaultVersionManager(); versionManager = new DefaultVersionManager();
} }
@ -105,63 +105,64 @@ public class PropertiesPluginDescriptorFinderTest {
private Properties getPlugin1Properties() { private Properties getPlugin1Properties() {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-1");
properties.setProperty("plugin.version", "0.0.1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
properties.setProperty("plugin.description", "Test Plugin 1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DESCRIPTION, "Test Plugin 1");
properties.setProperty("plugin.provider", "Decebal Suiu"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", "test-plugin-2,test-plugin-3@~1.0"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "test-plugin-2,test-plugin-3@~1.0");
properties.setProperty("plugin.requires", ">=1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1");
properties.setProperty("plugin.license", "Apache-2.0"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0");
return properties; return properties;
} }
private Properties getPlugin2Properties() { private Properties getPlugin2Properties() {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
properties.setProperty("plugin.version", "0.0.1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", ""); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
return properties; return properties;
} }
private Properties getPlugin4Properties() { private Properties getPlugin4Properties() {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
properties.setProperty("plugin.version", "0.0.1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
properties.setProperty("plugin.dependencies", ""); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
properties.setProperty("plugin.requires", "*"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
return properties; return properties;
} }
private Properties getPlugin5Properties() { private Properties getPlugin5Properties() {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_ID, "test-plugin-2");
properties.setProperty("plugin.provider", "Decebal Suiu"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", ""); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
properties.setProperty("plugin.requires", "*"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
return properties; return properties;
} }
private Properties getPlugin6Properties() { private Properties getPlugin6Properties() {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("plugin.version", "0.0.1"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", ""); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, "");
properties.setProperty("plugin.requires", "*"); properties.setProperty(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*");
return properties; return properties;
} }
private void storePropertiesToPath(Properties properties, Path path) throws IOException { 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)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
properties.store(writer, ""); properties.store(writer, "");
} }

Loading…
Cancel
Save