Browse Source

Add VersionManager abstractization (breaking change) (#155)

pull/165/head
Decebal Suiu 7 years ago committed by GitHub
parent
commit
c9bdd98e6d
  1. 35
      pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java
  2. 6
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 19
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultVersionManager.java
  4. 19
      pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java
  5. 17
      pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java
  6. 8
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java
  7. 10
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
  8. 6
      pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java
  9. 34
      pf4j/src/main/java/ro/fortsoft/pf4j/VersionManager.java
  10. 17
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java
  11. 52
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultVersionManagerTest.java
  12. 31
      pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java
  13. 11
      pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java
  14. 17
      pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java
  15. 17
      pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java
  16. 2
      pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java
  17. 6
      pf4j/src/test/java/ro/fortsoft/pf4j/util/FileUtilsTest.java

35
pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.StringUtils; import ro.fortsoft.pf4j.util.StringUtils;
@ -25,7 +24,13 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* This class implements the boilerplate plugin code that any {@link PluginManager} * This class implements the boilerplate plugin code that any {@link PluginManager}
@ -83,7 +88,7 @@ public abstract class AbstractPluginManager implements PluginManager {
/* /*
* The system version used for comparisons to the plugin requires attribute. * The system version used for comparisons to the plugin requires attribute.
*/ */
private Version systemVersion = Version.forIntegers(0); private String systemVersion = "0.0.0";
private PluginRepository pluginRepository; private PluginRepository pluginRepository;
private PluginFactory pluginFactory; private PluginFactory pluginFactory;
@ -93,6 +98,8 @@ public abstract class AbstractPluginManager implements PluginManager {
private PluginLoader pluginLoader; private PluginLoader pluginLoader;
private boolean exactVersionAllowed = false; private boolean exactVersionAllowed = false;
private VersionManager versionManager;
/** /**
* The plugins root is supplied by {@code System.getProperty("pf4j.pluginsDir", "plugins")}. * The plugins root is supplied by {@code System.getProperty("pf4j.pluginsDir", "plugins")}.
*/ */
@ -112,12 +119,12 @@ public abstract class AbstractPluginManager implements PluginManager {
} }
@Override @Override
public void setSystemVersion(Version version) { public void setSystemVersion(String version) {
systemVersion = version; systemVersion = version;
} }
@Override @Override
public Version getSystemVersion() { public String getSystemVersion() {
return systemVersion; return systemVersion;
} }
@ -601,7 +608,7 @@ public abstract class AbstractPluginManager implements PluginManager {
pluginStateListeners.remove(listener); pluginStateListeners.remove(listener);
} }
public Version getVersion() { public String getVersion() {
String version = null; String version = null;
Package pf4jPackage = PluginManager.class.getPackage(); Package pf4jPackage = PluginManager.class.getPackage();
@ -612,7 +619,7 @@ public abstract class AbstractPluginManager implements PluginManager {
} }
} }
return (version != null) ? Version.valueOf(version) : Version.forIntegers(0); return (version != null) ? version : "0.0.0";
} }
protected abstract PluginRepository createPluginRepository(); protected abstract PluginRepository createPluginRepository();
@ -629,6 +636,8 @@ public abstract class AbstractPluginManager implements PluginManager {
protected abstract PluginLoader createPluginLoader(); protected abstract PluginLoader createPluginLoader();
protected abstract VersionManager createVersionManager();
protected PluginDescriptorFinder getPluginDescriptorFinder() { protected PluginDescriptorFinder getPluginDescriptorFinder() {
return pluginDescriptorFinder; return pluginDescriptorFinder;
} }
@ -656,8 +665,6 @@ public abstract class AbstractPluginManager implements PluginManager {
System.setProperty("pf4j.pluginsDir", pluginsRoot.toString()); System.setProperty("pf4j.pluginsDir", pluginsRoot.toString());
dependencyResolver = new DependencyResolver();
pluginRepository = createPluginRepository(); pluginRepository = createPluginRepository();
pluginFactory = createPluginFactory(); pluginFactory = createPluginFactory();
extensionFactory = createExtensionFactory(); extensionFactory = createExtensionFactory();
@ -665,6 +672,9 @@ public abstract class AbstractPluginManager implements PluginManager {
extensionFinder = createExtensionFinder(); extensionFinder = createExtensionFinder();
pluginStatusProvider = createPluginStatusProvider(); pluginStatusProvider = createPluginStatusProvider();
pluginLoader = createPluginLoader(); pluginLoader = createPluginLoader();
versionManager = createVersionManager();
dependencyResolver = new DependencyResolver(versionManager);
} }
/** /**
@ -700,7 +710,7 @@ public abstract class AbstractPluginManager implements PluginManager {
// If exact versions are not allowed in requires, rewrite to >= expression // If exact versions are not allowed in requires, rewrite to >= expression
requires = ">=" + requires; requires = ">=" + requires;
} }
if (systemVersion.equals(Version.forIntegers(0)) || systemVersion.satisfies(requires)) { if (systemVersion.equals("0.0.0") || versionManager.satisfies(requires, systemVersion)) {
return true; return true;
} }
@ -864,4 +874,9 @@ public abstract class AbstractPluginManager implements PluginManager {
this.exactVersionAllowed = exactVersionAllowed; this.exactVersionAllowed = exactVersionAllowed;
} }
@Override
public VersionManager getVersionManager() {
return versionManager;
}
} }

6
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java

@ -89,6 +89,11 @@ public class DefaultPluginManager extends AbstractPluginManager {
return new DefaultPluginLoader(this, pluginClasspath); return new DefaultPluginLoader(this, pluginClasspath);
} }
@Override
protected VersionManager createVersionManager() {
return new DefaultVersionManager();
}
/** /**
* By default if {@link DefaultPluginManager#isDevelopment()} returns true * By default if {@link DefaultPluginManager#isDevelopment()} returns true
* than a {@link DevelopmentPluginClasspath} is returned * than a {@link DevelopmentPluginClasspath} is returned
@ -125,4 +130,5 @@ public class DefaultPluginManager extends AbstractPluginManager {
return super.loadPluginFromPath(pluginPath); return super.loadPluginFromPath(pluginPath);
} }
} }

19
pf4j/src/test/java/ro/fortsoft/pf4j/util/SemVerUtils.java → pf4j/src/main/java/ro/fortsoft/pf4j/DefaultVersionManager.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -13,16 +13,21 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package ro.fortsoft.pf4j.util; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version; import com.github.zafarkhaja.semver.Version;
import com.github.zafarkhaja.semver.expr.ExpressionParser;
/** /**
* Utility for semantic version testing * Default implementation for {@link VersionManager}.
* This implementation uses jSemVer (a Java implementation of the SemVer Specification).
*
* @author Decebal Suiu
*/ */
public class SemVerUtils { public class DefaultVersionManager implements VersionManager {
public static boolean versionMatches(String expression, String systemVersion) {
return ExpressionParser.newInstance().parse(expression).interpret(Version.valueOf(systemVersion)); @Override
public boolean satisfies(String constraint, String version) {
return Version.valueOf(version).satisfies(constraint);
} }
} }

19
pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.DirectedGraph; import ro.fortsoft.pf4j.util.DirectedGraph;
@ -42,10 +41,16 @@ public class DependencyResolver {
private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class); private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class);
private VersionManager versionManager;
private DirectedGraph<String> dependenciesGraph; // the value is 'pluginId' private DirectedGraph<String> dependenciesGraph; // the value is 'pluginId'
private DirectedGraph<String> dependentsGraph; // the value is 'pluginId' private DirectedGraph<String> dependentsGraph; // the value is 'pluginId'
private boolean resolved; private boolean resolved;
public DependencyResolver(VersionManager versionManager) {
this.versionManager = versionManager;
}
public Result resolve(List<PluginDescriptor> plugins) { public Result resolve(List<PluginDescriptor> plugins) {
// create graphs // create graphs
dependenciesGraph = new DirectedGraph<>(); dependenciesGraph = new DirectedGraph<>();
@ -81,7 +86,7 @@ public class DependencyResolver {
// check dependencies versions // check dependencies versions
for (PluginDescriptor plugin : plugins) { for (PluginDescriptor plugin : plugins) {
String pluginId = plugin.getPluginId(); String pluginId = plugin.getPluginId();
Version existingVersion = plugin.getVersion(); String existingVersion = plugin.getVersion();
List<String> dependents = getDependents(pluginId); List<String> dependents = getDependents(pluginId);
while (!dependents.isEmpty()) { while (!dependents.isEmpty()) {
@ -127,8 +132,8 @@ public class DependencyResolver {
* @param existingVersion * @param existingVersion
* @return * @return
*/ */
protected boolean checkDependencyVersion(String requiredVersion, Version existingVersion) { protected boolean checkDependencyVersion(String requiredVersion, String existingVersion) {
return existingVersion.satisfies(requiredVersion); return versionManager.satisfies(requiredVersion, existingVersion);
} }
private void addPlugin(PluginDescriptor descriptor) { private void addPlugin(PluginDescriptor descriptor) {
@ -224,10 +229,10 @@ public class DependencyResolver {
private String dependencyId; // value is "pluginId" private String dependencyId; // value is "pluginId"
private String dependentId; // value is "pluginId" private String dependentId; // value is "pluginId"
private Version existingVersion; private String existingVersion;
private String requiredVersion; private String requiredVersion;
WrongDependencyVersion(String dependencyId, String dependentId, Version existingVersion, String requiredVersion) { WrongDependencyVersion(String dependencyId, String dependentId, String existingVersion, String requiredVersion) {
this.dependencyId = dependencyId; this.dependencyId = dependencyId;
this.dependentId = dependentId; this.dependentId = dependentId;
this.existingVersion = existingVersion; this.existingVersion = existingVersion;
@ -242,7 +247,7 @@ public class DependencyResolver {
return dependentId; return dependentId;
} }
public Version getExistingVersion() { public String getExistingVersion() {
return existingVersion; return existingVersion;
} }

17
pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import ro.fortsoft.pf4j.util.StringUtils; import ro.fortsoft.pf4j.util.StringUtils;
import java.nio.file.Path; import java.nio.file.Path;
@ -29,7 +28,7 @@ import java.util.jar.Manifest;
*/ */
public abstract class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { public abstract class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
@Override @Override
public PluginDescriptor find(Path pluginPath) throws PluginException { public PluginDescriptor find(Path pluginPath) throws PluginException {
Manifest manifest = readManifest(pluginPath); Manifest manifest = readManifest(pluginPath);
@ -58,7 +57,7 @@ public abstract class ManifestPluginDescriptorFinder implements PluginDescriptor
String version = attributes.getValue("Plugin-Version"); String version = attributes.getValue("Plugin-Version");
if (StringUtils.isNotEmpty(version)) { if (StringUtils.isNotEmpty(version)) {
pluginDescriptor.setPluginVersion(createPluginVersion(version)); pluginDescriptor.setPluginVersion(version);
} }
String provider = attributes.getValue("Plugin-Provider"); String provider = attributes.getValue("Plugin-Provider");
@ -76,18 +75,8 @@ public abstract class ManifestPluginDescriptorFinder implements PluginDescriptor
return pluginDescriptor; return pluginDescriptor;
} }
/**
* Parse version to semver {@link Version} object.
* Example : 1.1.1.RC1 -> 1.1.1-RC1.
* This may be override to use a custom parsing to semver Version.
* @param version given string to be the plugin version
* @return a semver version
*/
protected Version createPluginVersion(String version) {
return Version.valueOf(version);
}
protected PluginDescriptor createPluginDescriptorInstance() { protected PluginDescriptor createPluginDescriptorInstance() {
return new PluginDescriptor(); return new PluginDescriptor();
} }
} }

8
pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java

@ -15,8 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -32,7 +30,7 @@ public class PluginDescriptor {
private String pluginId; private String pluginId;
private String pluginDescription; private String pluginDescription;
private String pluginClass; private String pluginClass;
private Version version; private String version;
private String requires = "*"; // SemVer format private String requires = "*"; // SemVer format
private String provider; private String provider;
private List<PluginDependency> dependencies; private List<PluginDependency> dependencies;
@ -66,7 +64,7 @@ public class PluginDescriptor {
/** /**
* Returns the version of this plugin. * Returns the version of this plugin.
*/ */
public Version getVersion() { public String getVersion() {
return version; return version;
} }
@ -127,7 +125,7 @@ public class PluginDescriptor {
return this; return this;
} }
PluginDescriptor setPluginVersion(Version version) { PluginDescriptor setPluginVersion(String version) {
this.version = version; this.version = version;
return this; return this;

10
pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java

@ -15,8 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -165,14 +163,14 @@ public interface PluginManager {
* @default 0.0.0 * @default 0.0.0
* @param version * @param version
*/ */
void setSystemVersion(Version version); void setSystemVersion(String version);
/** /**
* Returns the system version. * Returns the system version.
* *
* * @return the system version * @return the system version
*/ */
Version getSystemVersion(); String getSystemVersion();
/** /**
* Gets the path of the folder where plugins are installed * Gets the path of the folder where plugins are installed
@ -180,4 +178,6 @@ public interface PluginManager {
*/ */
Path getPluginsRoot(); Path getPluginsRoot();
VersionManager getVersionManager();
} }

6
pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.StringUtils; import ro.fortsoft.pf4j.util.StringUtils;
@ -38,7 +37,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties"; private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties";
private String propertiesFileName; protected String propertiesFileName;
public PropertiesPluginDescriptorFinder() { public PropertiesPluginDescriptorFinder() {
this(DEFAULT_PROPERTIES_FILE_NAME); this(DEFAULT_PROPERTIES_FILE_NAME);
@ -91,7 +90,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
String version = properties.getProperty("plugin.version"); String version = properties.getProperty("plugin.version");
if (StringUtils.isNotEmpty(version)) { if (StringUtils.isNotEmpty(version)) {
pluginDescriptor.setPluginVersion(Version.valueOf(version)); pluginDescriptor.setPluginVersion(version);
} }
String provider = properties.getProperty("plugin.provider"); String provider = properties.getProperty("plugin.provider");
@ -113,4 +112,5 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
protected PluginDescriptor createPluginDescriptorInstance() { protected PluginDescriptor createPluginDescriptorInstance() {
return new PluginDescriptor(); return new PluginDescriptor();
} }
} }

34
pf4j/src/main/java/ro/fortsoft/pf4j/VersionManager.java

@ -0,0 +1,34 @@
/*
* Copyright 2017 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.fortsoft.pf4j;
/**
* Manager responsible for versions of plugins.
*
* @author Decebal Suiu
*/
public interface VersionManager {
/**
* Check if a {@code constraint} and a {@code version} match.
*
* @param constraint
* @param version
* @return
*/
boolean satisfies(String constraint, String version);
}

17
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -27,6 +26,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class DefaultPluginManagerTest { public class DefaultPluginManagerTest {
private PluginDescriptor pd1 = null; private PluginDescriptor pd1 = null;
private DefaultPluginManager pluginManager = new DefaultPluginManager(); private DefaultPluginManager pluginManager = new DefaultPluginManager();
private PluginWrapper pw1; private PluginWrapper pw1;
@ -35,7 +35,7 @@ public class DefaultPluginManagerTest {
public void init() throws IOException { public void init() throws IOException {
pd1 = new PluginDescriptor(); pd1 = new PluginDescriptor();
pd1.setPluginId("myPlugin"); pd1.setPluginId("myPlugin");
pd1.setPluginVersion(Version.valueOf("1.2.3")); pd1.setPluginVersion("1.2.3");
pd1.setPluginClass("foo"); pd1.setPluginClass("foo");
pd1.setPluginDescription("My plugin"); pd1.setPluginDescription("My plugin");
pd1.setDependencies("bar, baz"); pd1.setDependencies("bar, baz");
@ -73,13 +73,13 @@ public class DefaultPluginManagerTest {
// By default accept all since system version not given // By default accept all since system version not given
assertTrue(pluginManager.isPluginValid(pw1)); assertTrue(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("1.0.0")); pluginManager.setSystemVersion("1.0.0");
assertFalse(pluginManager.isPluginValid(pw1)); assertFalse(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("5.0.0")); pluginManager.setSystemVersion("5.0.0");
assertTrue(pluginManager.isPluginValid(pw1)); assertTrue(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("6.0.0")); pluginManager.setSystemVersion("6.0.0");
assertTrue(pluginManager.isPluginValid(pw1)); assertTrue(pluginManager.isPluginValid(pw1));
} }
@ -90,13 +90,13 @@ public class DefaultPluginManagerTest {
// By default accept all since system version not given // By default accept all since system version not given
assertTrue(pluginManager.isPluginValid(pw1)); assertTrue(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("1.0.0")); pluginManager.setSystemVersion("1.0.0");
assertFalse(pluginManager.isPluginValid(pw1)); assertFalse(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("5.0.0")); pluginManager.setSystemVersion("5.0.0");
assertTrue(pluginManager.isPluginValid(pw1)); assertTrue(pluginManager.isPluginValid(pw1));
pluginManager.setSystemVersion(Version.valueOf("6.0.0")); pluginManager.setSystemVersion("6.0.0");
assertFalse(pluginManager.isPluginValid(pw1)); assertFalse(pluginManager.isPluginValid(pw1));
} }
@ -104,4 +104,5 @@ public class DefaultPluginManagerTest {
public void testDefaultExactVersionAllowed() throws Exception { public void testDefaultExactVersionAllowed() throws Exception {
assertEquals(false, pluginManager.isExactVersionAllowed()); assertEquals(false, pluginManager.isExactVersionAllowed());
} }
} }

52
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultVersionManagerTest.java

@ -0,0 +1,52 @@
/*
* Copyright 2017 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.ParseException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Decebal Suiu
*/
public class DefaultVersionManagerTest {
private VersionManager versionManager;
@Before
public void init() {
versionManager = new DefaultVersionManager();
}
@Test
public void satisfies() {
assertFalse(versionManager.satisfies(">2.0.0", "1.4.3")); // simple
assertTrue(versionManager.satisfies(">=1.4.0 & <1.6.0", "1.4.3")); // range
}
@Test(expected = IllegalArgumentException.class)
public void nullOrEmptyVersion() {
assertFalse(versionManager.satisfies(">2.0.0", null));
}
@Test(expected = ParseException.class)
public void invalidVersion() {
assertFalse(versionManager.satisfies(">2.0.0", "1.0"));
}
}

31
pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java

@ -15,22 +15,28 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/** /**
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class DependencyResolverTest { public class DependencyResolverTest {
private DependencyResolver resolver;
@Before
public void init() {
VersionManager versionManager = new DefaultVersionManager();
resolver = new DependencyResolver(versionManager);
}
@Test @Test
public void sortedPlugins() { public void sortedPlugins() {
// create incomplete plugin descriptor (ignore some attributes) // create incomplete plugin descriptor (ignore some attributes)
@ -40,13 +46,12 @@ public class DependencyResolverTest {
PluginDescriptor pd2 = new PluginDescriptor() PluginDescriptor pd2 = new PluginDescriptor()
.setPluginId("p2") .setPluginId("p2")
.setPluginVersion(Version.forIntegers(0)); // needed in "checkDependencyVersion" method .setPluginVersion("0.0.0"); // needed in "checkDependencyVersion" method
List<PluginDescriptor> plugins = new ArrayList<>(); List<PluginDescriptor> plugins = new ArrayList<>();
plugins.add(pd1); plugins.add(pd1);
plugins.add(pd2); plugins.add(pd2);
DependencyResolver resolver = new DependencyResolver();
DependencyResolver.Result result = resolver.resolve(plugins); DependencyResolver.Result result = resolver.resolve(plugins);
assertTrue(result.getNotFoundDependencies().isEmpty()); assertTrue(result.getNotFoundDependencies().isEmpty());
@ -62,7 +67,6 @@ public class DependencyResolverTest {
List<PluginDescriptor> plugins = new ArrayList<>(); List<PluginDescriptor> plugins = new ArrayList<>();
plugins.add(pd1); plugins.add(pd1);
DependencyResolver resolver = new DependencyResolver();
DependencyResolver.Result result = resolver.resolve(plugins); DependencyResolver.Result result = resolver.resolve(plugins);
assertFalse(result.getNotFoundDependencies().isEmpty()); assertFalse(result.getNotFoundDependencies().isEmpty());
@ -73,17 +77,17 @@ public class DependencyResolverTest {
public void cyclicDependencies() { public void cyclicDependencies() {
PluginDescriptor pd1 = new PluginDescriptor() PluginDescriptor pd1 = new PluginDescriptor()
.setPluginId("p1") .setPluginId("p1")
.setPluginVersion(Version.forIntegers(0)) .setPluginVersion("0.0.0")
.setDependencies("p2"); .setDependencies("p2");
PluginDescriptor pd2 = new PluginDescriptor() PluginDescriptor pd2 = new PluginDescriptor()
.setPluginId("p2") .setPluginId("p2")
.setPluginVersion(Version.forIntegers(0)) .setPluginVersion("0.0.0")
.setDependencies("p3"); .setDependencies("p3");
PluginDescriptor pd3 = new PluginDescriptor() PluginDescriptor pd3 = new PluginDescriptor()
.setPluginId("p3") .setPluginId("p3")
.setPluginVersion(Version.forIntegers(0)) .setPluginVersion("0.0.0")
.setDependencies("p1"); .setDependencies("p1");
List<PluginDescriptor> plugins = new ArrayList<>(); List<PluginDescriptor> plugins = new ArrayList<>();
@ -91,7 +95,6 @@ public class DependencyResolverTest {
plugins.add(pd2); plugins.add(pd2);
plugins.add(pd3); plugins.add(pd3);
DependencyResolver resolver = new DependencyResolver();
DependencyResolver.Result result = resolver.resolve(plugins); DependencyResolver.Result result = resolver.resolve(plugins);
assertTrue(result.hasCyclicDependency()); assertTrue(result.hasCyclicDependency());
@ -106,13 +109,12 @@ public class DependencyResolverTest {
PluginDescriptor pd2 = new PluginDescriptor() PluginDescriptor pd2 = new PluginDescriptor()
.setPluginId("p2") .setPluginId("p2")
.setPluginVersion(Version.forIntegers(1, 4)); .setPluginVersion("1.4.0");
List<PluginDescriptor> plugins = new ArrayList<>(); List<PluginDescriptor> plugins = new ArrayList<>();
plugins.add(pd1); plugins.add(pd1);
plugins.add(pd2); plugins.add(pd2);
DependencyResolver resolver = new DependencyResolver();
DependencyResolver.Result result = resolver.resolve(plugins); DependencyResolver.Result result = resolver.resolve(plugins);
assertFalse(result.getWrongVersionDependencies().isEmpty()); assertFalse(result.getWrongVersionDependencies().isEmpty());
@ -126,13 +128,12 @@ public class DependencyResolverTest {
PluginDescriptor pd2 = new PluginDescriptor() PluginDescriptor pd2 = new PluginDescriptor()
.setPluginId("p2") .setPluginId("p2")
.setPluginVersion(Version.forIntegers(2)); .setPluginVersion("2.0.0");
List<PluginDescriptor> plugins = new ArrayList<>(); List<PluginDescriptor> plugins = new ArrayList<>();
plugins.add(pd1); plugins.add(pd1);
plugins.add(pd2); plugins.add(pd2);
DependencyResolver resolver = new DependencyResolver();
DependencyResolver.Result result = resolver.resolve(plugins); DependencyResolver.Result result = resolver.resolve(plugins);
assertTrue(result.getWrongVersionDependencies().isEmpty()); assertTrue(result.getWrongVersionDependencies().isEmpty());

11
pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import ro.fortsoft.pf4j.plugin.MockPluginManager; import ro.fortsoft.pf4j.plugin.MockPluginManager;
@ -31,6 +30,7 @@ import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class LoadPluginsTest { public class LoadPluginsTest {
private Path tmpDir; private Path tmpDir;
private MockPluginManager pluginManager; private MockPluginManager pluginManager;
private MockZipPlugin p1; private MockZipPlugin p1;
@ -92,14 +92,14 @@ public class LoadPluginsTest {
pluginManager.loadPlugins(); pluginManager.loadPlugins();
pluginManager.startPlugins(); pluginManager.startPlugins();
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
assertEquals(Version.valueOf("1.2.3"), pluginManager.getPlugin(p2.id).getDescriptor().getVersion()); assertEquals("1.2.3", pluginManager.getPlugin(p2.id).getDescriptor().getVersion());
assertEquals(1, pluginManager.getStartedPlugins().size()); assertEquals(1, pluginManager.getStartedPlugins().size());
p2.create(); p2.create();
pluginManager.loadPlugins(); pluginManager.loadPlugins();
pluginManager.startPlugin(p2.id); pluginManager.startPlugin(p2.id);
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
assertEquals(Version.valueOf("2.0.0"), pluginManager.getPlugin(p2.id).getDescriptor().getVersion()); assertEquals("2.0.0", pluginManager.getPlugin(p2.id).getDescriptor().getVersion());
assertEquals(Version.valueOf("2.0.0"), pluginManager.getStartedPlugins().get(1).getDescriptor().getVersion()); assertEquals("2.0.0", pluginManager.getStartedPlugins().get(1).getDescriptor().getVersion());
} }
@Test @Test
@ -131,6 +131,7 @@ public class LoadPluginsTest {
} }
private class MockZipPlugin { private class MockZipPlugin {
public final String id; public final String id;
public final String version; public final String version;
public final String filename; public final String filename;
@ -165,5 +166,7 @@ public class LoadPluginsTest {
Files.move(propsFile, propsInZip); Files.move(propsFile, propsInZip);
} }
} }
} }
} }

17
pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -28,9 +27,7 @@ import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches;
/** /**
* @author Mario Franco * @author Mario Franco
@ -38,6 +35,8 @@ import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches;
*/ */
public class ManifestPluginDescriptorFinderTest { public class ManifestPluginDescriptorFinderTest {
private VersionManager versionManager;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder testFolder = new TemporaryFolder();
@ -70,6 +69,8 @@ public class ManifestPluginDescriptorFinderTest {
pluginPath = testFolder.newFolder("test-plugin-6", "classes", "META-INF").toPath(); pluginPath = testFolder.newFolder("test-plugin-6", "classes", "META-INF").toPath();
Files.write(pluginPath.resolve("extensions.idx"), "ro.fortsoft.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes()); Files.write(pluginPath.resolve("extensions.idx"), "ro.fortsoft.pf4j.demo.hello.HelloPlugin$HelloGreeting".getBytes());
Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin6Manifest(), charset); Files.write(pluginPath.resolve("MANIFEST.MF"), getPlugin6Manifest(), charset);
versionManager = new DefaultVersionManager();
} }
/** /**
@ -85,22 +86,22 @@ public class ManifestPluginDescriptorFinderTest {
assertEquals("test-plugin-1", plugin1.getPluginId()); assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription()); assertEquals("Test Plugin 1", plugin1.getPluginDescription());
assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin1.getPluginClass()); assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
assertEquals(Version.valueOf("0.0.1"), plugin1.getVersion()); assertEquals("0.0.1", plugin1.getVersion());
assertEquals("Decebal Suiu", plugin1.getProvider()); assertEquals("Decebal Suiu", plugin1.getProvider());
assertEquals(2, plugin1.getDependencies().size()); assertEquals(2, plugin1.getDependencies().size());
assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId()); assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId()); assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId());
assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport()); assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
assertEquals("Apache-2.0", plugin1.getLicense()); assertEquals("Apache-2.0", plugin1.getLicense());
assertTrue(versionMatches(plugin1.getRequires(), "1.0.0")); assertTrue(versionManager.satisfies(plugin1.getRequires(), "1.0.0"));
assertEquals("test-plugin-2", plugin2.getPluginId()); assertEquals("test-plugin-2", plugin2.getPluginId());
assertEquals("", plugin2.getPluginDescription()); assertEquals("", plugin2.getPluginDescription());
assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin2.getPluginClass()); assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
assertEquals(Version.valueOf("0.0.1"), plugin2.getVersion()); assertEquals("0.0.1", plugin2.getVersion());
assertEquals("Decebal Suiu", plugin2.getProvider()); assertEquals("Decebal Suiu", plugin2.getProvider());
assertEquals(0, plugin2.getDependencies().size()); assertEquals(0, plugin2.getDependencies().size());
assertTrue(versionMatches(plugin2.getRequires(),"1.0.0")); assertTrue(versionManager.satisfies(plugin2.getRequires(), "1.0.0"));
} }
/** /**

17
pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java

@ -15,7 +15,6 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import com.github.zafarkhaja.semver.Version;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -29,10 +28,11 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches;
public class PropertiesPluginDescriptorFinderTest { public class PropertiesPluginDescriptorFinderTest {
private VersionManager versionManager;
@Rule @Rule
public TemporaryFolder testFolder = new TemporaryFolder(); public TemporaryFolder testFolder = new TemporaryFolder();
@ -60,6 +60,8 @@ public class PropertiesPluginDescriptorFinderTest {
// no plugin id // no plugin id
pluginPath = testFolder.newFolder("test-plugin-6").toPath(); pluginPath = testFolder.newFolder("test-plugin-6").toPath();
Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset); Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset);
versionManager = new DefaultVersionManager();
} }
@Test @Test
@ -72,7 +74,7 @@ public class PropertiesPluginDescriptorFinderTest {
assertEquals("test-plugin-1", plugin1.getPluginId()); assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription()); assertEquals("Test Plugin 1", plugin1.getPluginDescription());
assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin1.getPluginClass()); assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
assertEquals(Version.valueOf("0.0.1"), plugin1.getVersion()); assertEquals("0.0.1", plugin1.getVersion());
assertEquals("Decebal Suiu", plugin1.getProvider()); assertEquals("Decebal Suiu", plugin1.getProvider());
assertEquals(2, plugin1.getDependencies().size()); assertEquals(2, plugin1.getDependencies().size());
assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId()); assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
@ -80,17 +82,17 @@ public class PropertiesPluginDescriptorFinderTest {
assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport()); assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
assertEquals("Apache-2.0", plugin1.getLicense()); assertEquals("Apache-2.0", plugin1.getLicense());
assertEquals(">=1", plugin1.getRequires()); assertEquals(">=1", plugin1.getRequires());
assertTrue(versionMatches(plugin1.getRequires(),"1.0.0")); assertTrue(versionManager.satisfies(plugin1.getRequires(), "1.0.0"));
assertFalse(versionMatches(plugin1.getRequires(), "0.1.0")); assertFalse(versionManager.satisfies(plugin1.getRequires(), "0.1.0"));
assertEquals("test-plugin-2", plugin2.getPluginId()); assertEquals("test-plugin-2", plugin2.getPluginId());
assertEquals("", plugin2.getPluginDescription()); assertEquals("", plugin2.getPluginDescription());
assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin2.getPluginClass()); assertEquals("ro.fortsoft.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
assertEquals(Version.valueOf("0.0.1"), plugin2.getVersion()); assertEquals("0.0.1", plugin2.getVersion());
assertEquals("Decebal Suiu", plugin2.getProvider()); assertEquals("Decebal Suiu", plugin2.getProvider());
assertEquals(0, plugin2.getDependencies().size()); assertEquals(0, plugin2.getDependencies().size());
assertEquals("*", plugin2.getRequires()); // Default is * assertEquals("*", plugin2.getRequires()); // Default is *
assertTrue(versionMatches(plugin2.getRequires(),"1.0.0")); assertTrue(versionManager.satisfies(plugin2.getRequires(), "1.0.0"));
} }
@Test(expected = PluginException.class) @Test(expected = PluginException.class)
@ -171,6 +173,7 @@ public class PropertiesPluginDescriptorFinderTest {
return Arrays.asList(lines); return Arrays.asList(lines);
} }
private Path getPluginsRoot() { private Path getPluginsRoot() {
return testFolder.getRoot().toPath(); return testFolder.getRoot().toPath();
} }

2
pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java

@ -26,6 +26,7 @@ import java.nio.file.Path;
* Manager for testing * Manager for testing
*/ */
public class MockPluginManager extends DefaultPluginManager { public class MockPluginManager extends DefaultPluginManager {
private PluginDescriptorFinder finder = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath()); private PluginDescriptorFinder finder = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath());
public MockPluginManager() { public MockPluginManager() {
@ -46,4 +47,5 @@ public class MockPluginManager extends DefaultPluginManager {
protected PluginDescriptorFinder createPluginDescriptorFinder() { protected PluginDescriptorFinder createPluginDescriptorFinder() {
return finder; return finder;
} }
} }

6
pf4j/src/test/java/ro/fortsoft/pf4j/util/FileUtilsTest.java

@ -22,12 +22,16 @@ import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.file.*; import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class FileUtilsTest { public class FileUtilsTest {
private Path zipFile; private Path zipFile;
private Path tmpDir; private Path tmpDir;
private Path propsFile; private Path propsFile;

Loading…
Cancel
Save