From c9bdd98e6df7c146377f8b7d56146bfae7d5a797 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Wed, 12 Jul 2017 21:48:41 +0300 Subject: [PATCH] Add VersionManager abstractization (breaking change) (#155) --- .../fortsoft/pf4j/AbstractPluginManager.java | 35 +++++++++---- .../fortsoft/pf4j/DefaultPluginManager.java | 6 +++ .../fortsoft/pf4j/DefaultVersionManager.java} | 19 ++++--- .../ro/fortsoft/pf4j/DependencyResolver.java | 19 ++++--- .../pf4j/ManifestPluginDescriptorFinder.java | 17 ++---- .../ro/fortsoft/pf4j/PluginDescriptor.java | 8 ++- .../java/ro/fortsoft/pf4j/PluginManager.java | 10 ++-- .../PropertiesPluginDescriptorFinder.java | 6 +-- .../java/ro/fortsoft/pf4j/VersionManager.java | 34 ++++++++++++ .../pf4j/DefaultPluginManagerTest.java | 17 +++--- .../pf4j/DefaultVersionManagerTest.java | 52 +++++++++++++++++++ .../fortsoft/pf4j/DependencyResolverTest.java | 31 +++++------ .../ro/fortsoft/pf4j/LoadPluginsTest.java | 11 ++-- .../ManifestPluginDescriptorFinderTest.java | 17 +++--- .../PropertiesPluginDescriptorFinderTest.java | 17 +++--- .../pf4j/plugin/MockPluginManager.java | 2 + .../ro/fortsoft/pf4j/util/FileUtilsTest.java | 6 ++- 17 files changed, 213 insertions(+), 94 deletions(-) rename pf4j/src/{test/java/ro/fortsoft/pf4j/util/SemVerUtils.java => main/java/ro/fortsoft/pf4j/DefaultVersionManager.java} (57%) create mode 100644 pf4j/src/main/java/ro/fortsoft/pf4j/VersionManager.java create mode 100644 pf4j/src/test/java/ro/fortsoft/pf4j/DefaultVersionManagerTest.java diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java index 71bcb2e..1d8090a 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ro.fortsoft.pf4j.util.StringUtils; @@ -25,7 +24,13 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; 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} @@ -83,7 +88,7 @@ public abstract class AbstractPluginManager implements PluginManager { /* * 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 PluginFactory pluginFactory; @@ -93,6 +98,8 @@ public abstract class AbstractPluginManager implements PluginManager { private PluginLoader pluginLoader; private boolean exactVersionAllowed = false; + private VersionManager versionManager; + /** * The plugins root is supplied by {@code System.getProperty("pf4j.pluginsDir", "plugins")}. */ @@ -112,12 +119,12 @@ public abstract class AbstractPluginManager implements PluginManager { } @Override - public void setSystemVersion(Version version) { + public void setSystemVersion(String version) { systemVersion = version; } @Override - public Version getSystemVersion() { + public String getSystemVersion() { return systemVersion; } @@ -601,7 +608,7 @@ public abstract class AbstractPluginManager implements PluginManager { pluginStateListeners.remove(listener); } - public Version getVersion() { + public String getVersion() { String version = null; 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(); @@ -629,6 +636,8 @@ public abstract class AbstractPluginManager implements PluginManager { protected abstract PluginLoader createPluginLoader(); + protected abstract VersionManager createVersionManager(); + protected PluginDescriptorFinder getPluginDescriptorFinder() { return pluginDescriptorFinder; } @@ -656,8 +665,6 @@ public abstract class AbstractPluginManager implements PluginManager { System.setProperty("pf4j.pluginsDir", pluginsRoot.toString()); - dependencyResolver = new DependencyResolver(); - pluginRepository = createPluginRepository(); pluginFactory = createPluginFactory(); extensionFactory = createExtensionFactory(); @@ -665,6 +672,9 @@ public abstract class AbstractPluginManager implements PluginManager { extensionFinder = createExtensionFinder(); pluginStatusProvider = createPluginStatusProvider(); 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 requires = ">=" + requires; } - if (systemVersion.equals(Version.forIntegers(0)) || systemVersion.satisfies(requires)) { + if (systemVersion.equals("0.0.0") || versionManager.satisfies(requires, systemVersion)) { return true; } @@ -864,4 +874,9 @@ public abstract class AbstractPluginManager implements PluginManager { this.exactVersionAllowed = exactVersionAllowed; } + @Override + public VersionManager getVersionManager() { + return versionManager; + } + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index b9d6c67..9e24e01 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -89,6 +89,11 @@ public class DefaultPluginManager extends AbstractPluginManager { return new DefaultPluginLoader(this, pluginClasspath); } + @Override + protected VersionManager createVersionManager() { + return new DefaultVersionManager(); + } + /** * By default if {@link DefaultPluginManager#isDevelopment()} returns true * than a {@link DevelopmentPluginClasspath} is returned @@ -125,4 +130,5 @@ public class DefaultPluginManager extends AbstractPluginManager { return super.loadPluginFromPath(pluginPath); } + } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/util/SemVerUtils.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultVersionManager.java similarity index 57% rename from pf4j/src/test/java/ro/fortsoft/pf4j/util/SemVerUtils.java rename to pf4j/src/main/java/ro/fortsoft/pf4j/DefaultVersionManager.java index b5c94d1..0d11e90 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/util/SemVerUtils.java +++ b/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 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 * 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 * limitations under the License. */ -package ro.fortsoft.pf4j.util; +package ro.fortsoft.pf4j; 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 static boolean versionMatches(String expression, String systemVersion) { - return ExpressionParser.newInstance().parse(expression).interpret(Version.valueOf(systemVersion)); +public class DefaultVersionManager implements VersionManager { + + @Override + public boolean satisfies(String constraint, String version) { + return Version.valueOf(version).satisfies(constraint); } + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java index 3ab5526..0ea15a3 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ro.fortsoft.pf4j.util.DirectedGraph; @@ -42,10 +41,16 @@ public class DependencyResolver { private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class); + private VersionManager versionManager; + private DirectedGraph dependenciesGraph; // the value is 'pluginId' private DirectedGraph dependentsGraph; // the value is 'pluginId' private boolean resolved; + public DependencyResolver(VersionManager versionManager) { + this.versionManager = versionManager; + } + public Result resolve(List plugins) { // create graphs dependenciesGraph = new DirectedGraph<>(); @@ -81,7 +86,7 @@ public class DependencyResolver { // check dependencies versions for (PluginDescriptor plugin : plugins) { String pluginId = plugin.getPluginId(); - Version existingVersion = plugin.getVersion(); + String existingVersion = plugin.getVersion(); List dependents = getDependents(pluginId); while (!dependents.isEmpty()) { @@ -127,8 +132,8 @@ public class DependencyResolver { * @param existingVersion * @return */ - protected boolean checkDependencyVersion(String requiredVersion, Version existingVersion) { - return existingVersion.satisfies(requiredVersion); + protected boolean checkDependencyVersion(String requiredVersion, String existingVersion) { + return versionManager.satisfies(requiredVersion, existingVersion); } private void addPlugin(PluginDescriptor descriptor) { @@ -224,10 +229,10 @@ public class DependencyResolver { private String dependencyId; // value is "pluginId" private String dependentId; // value is "pluginId" - private Version existingVersion; + private String existingVersion; 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.dependentId = dependentId; this.existingVersion = existingVersion; @@ -242,7 +247,7 @@ public class DependencyResolver { return dependentId; } - public Version getExistingVersion() { + public String getExistingVersion() { return existingVersion; } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java index 394773c..a30fb67 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinder.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import ro.fortsoft.pf4j.util.StringUtils; import java.nio.file.Path; @@ -29,7 +28,7 @@ import java.util.jar.Manifest; */ public abstract class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { - @Override + @Override public PluginDescriptor find(Path pluginPath) throws PluginException { Manifest manifest = readManifest(pluginPath); @@ -58,7 +57,7 @@ public abstract class ManifestPluginDescriptorFinder implements PluginDescriptor String version = attributes.getValue("Plugin-Version"); if (StringUtils.isNotEmpty(version)) { - pluginDescriptor.setPluginVersion(createPluginVersion(version)); + pluginDescriptor.setPluginVersion(version); } String provider = attributes.getValue("Plugin-Provider"); @@ -76,18 +75,8 @@ public abstract class ManifestPluginDescriptorFinder implements 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() { return new PluginDescriptor(); } + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java index c6b7db8..e7bcb89 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginDescriptor.java @@ -15,8 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; - import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -32,7 +30,7 @@ public class PluginDescriptor { private String pluginId; private String pluginDescription; private String pluginClass; - private Version version; + private String version; private String requires = "*"; // SemVer format private String provider; private List dependencies; @@ -66,7 +64,7 @@ public class PluginDescriptor { /** * Returns the version of this plugin. */ - public Version getVersion() { + public String getVersion() { return version; } @@ -127,7 +125,7 @@ public class PluginDescriptor { return this; } - PluginDescriptor setPluginVersion(Version version) { + PluginDescriptor setPluginVersion(String version) { this.version = version; return this; diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java index e7f7f58..2efd9f3 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java @@ -15,8 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; - import java.nio.file.Path; import java.util.List; import java.util.Set; @@ -165,14 +163,14 @@ public interface PluginManager { * @default 0.0.0 * @param version */ - void setSystemVersion(Version version); + void setSystemVersion(String 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 @@ -180,4 +178,6 @@ public interface PluginManager { */ Path getPluginsRoot(); + VersionManager getVersionManager(); + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java index 7484b4d..259040c 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinder.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 String propertiesFileName; + protected String propertiesFileName; public PropertiesPluginDescriptorFinder() { this(DEFAULT_PROPERTIES_FILE_NAME); @@ -91,7 +90,7 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder String version = properties.getProperty("plugin.version"); if (StringUtils.isNotEmpty(version)) { - pluginDescriptor.setPluginVersion(Version.valueOf(version)); + pluginDescriptor.setPluginVersion(version); } String provider = properties.getProperty("plugin.provider"); @@ -113,4 +112,5 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder protected PluginDescriptor createPluginDescriptorInstance() { return new PluginDescriptor(); } + } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/VersionManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/VersionManager.java new file mode 100644 index 0000000..c3de913 --- /dev/null +++ b/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); + +} diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java index 5f235be..4c154d1 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginManagerTest.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.junit.Before; import org.junit.Test; @@ -27,6 +26,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class DefaultPluginManagerTest { + private PluginDescriptor pd1 = null; private DefaultPluginManager pluginManager = new DefaultPluginManager(); private PluginWrapper pw1; @@ -35,7 +35,7 @@ public class DefaultPluginManagerTest { public void init() throws IOException { pd1 = new PluginDescriptor(); pd1.setPluginId("myPlugin"); - pd1.setPluginVersion(Version.valueOf("1.2.3")); + pd1.setPluginVersion("1.2.3"); pd1.setPluginClass("foo"); pd1.setPluginDescription("My plugin"); pd1.setDependencies("bar, baz"); @@ -73,13 +73,13 @@ public class DefaultPluginManagerTest { // By default accept all since system version not given assertTrue(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("1.0.0")); + pluginManager.setSystemVersion("1.0.0"); assertFalse(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("5.0.0")); + pluginManager.setSystemVersion("5.0.0"); assertTrue(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("6.0.0")); + pluginManager.setSystemVersion("6.0.0"); assertTrue(pluginManager.isPluginValid(pw1)); } @@ -90,13 +90,13 @@ public class DefaultPluginManagerTest { // By default accept all since system version not given assertTrue(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("1.0.0")); + pluginManager.setSystemVersion("1.0.0"); assertFalse(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("5.0.0")); + pluginManager.setSystemVersion("5.0.0"); assertTrue(pluginManager.isPluginValid(pw1)); - pluginManager.setSystemVersion(Version.valueOf("6.0.0")); + pluginManager.setSystemVersion("6.0.0"); assertFalse(pluginManager.isPluginValid(pw1)); } @@ -104,4 +104,5 @@ public class DefaultPluginManagerTest { public void testDefaultExactVersionAllowed() throws Exception { assertEquals(false, pluginManager.isExactVersionAllowed()); } + } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultVersionManagerTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultVersionManagerTest.java new file mode 100644 index 0000000..802291d --- /dev/null +++ b/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")); + } + +} diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java index e8d1749..14aba39 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/DependencyResolverTest.java @@ -15,22 +15,28 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; /** * @author Decebal Suiu */ public class DependencyResolverTest { + private DependencyResolver resolver; + + @Before + public void init() { + VersionManager versionManager = new DefaultVersionManager(); + resolver = new DependencyResolver(versionManager); + } + @Test public void sortedPlugins() { // create incomplete plugin descriptor (ignore some attributes) @@ -40,13 +46,12 @@ public class DependencyResolverTest { PluginDescriptor pd2 = new PluginDescriptor() .setPluginId("p2") - .setPluginVersion(Version.forIntegers(0)); // needed in "checkDependencyVersion" method + .setPluginVersion("0.0.0"); // needed in "checkDependencyVersion" method List plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); - DependencyResolver resolver = new DependencyResolver(); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.getNotFoundDependencies().isEmpty()); @@ -62,7 +67,6 @@ public class DependencyResolverTest { List plugins = new ArrayList<>(); plugins.add(pd1); - DependencyResolver resolver = new DependencyResolver(); DependencyResolver.Result result = resolver.resolve(plugins); assertFalse(result.getNotFoundDependencies().isEmpty()); @@ -73,17 +77,17 @@ public class DependencyResolverTest { public void cyclicDependencies() { PluginDescriptor pd1 = new PluginDescriptor() .setPluginId("p1") - .setPluginVersion(Version.forIntegers(0)) + .setPluginVersion("0.0.0") .setDependencies("p2"); PluginDescriptor pd2 = new PluginDescriptor() .setPluginId("p2") - .setPluginVersion(Version.forIntegers(0)) + .setPluginVersion("0.0.0") .setDependencies("p3"); PluginDescriptor pd3 = new PluginDescriptor() .setPluginId("p3") - .setPluginVersion(Version.forIntegers(0)) + .setPluginVersion("0.0.0") .setDependencies("p1"); List plugins = new ArrayList<>(); @@ -91,7 +95,6 @@ public class DependencyResolverTest { plugins.add(pd2); plugins.add(pd3); - DependencyResolver resolver = new DependencyResolver(); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.hasCyclicDependency()); @@ -106,13 +109,12 @@ public class DependencyResolverTest { PluginDescriptor pd2 = new PluginDescriptor() .setPluginId("p2") - .setPluginVersion(Version.forIntegers(1, 4)); + .setPluginVersion("1.4.0"); List plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); - DependencyResolver resolver = new DependencyResolver(); DependencyResolver.Result result = resolver.resolve(plugins); assertFalse(result.getWrongVersionDependencies().isEmpty()); @@ -126,13 +128,12 @@ public class DependencyResolverTest { PluginDescriptor pd2 = new PluginDescriptor() .setPluginId("p2") - .setPluginVersion(Version.forIntegers(2)); + .setPluginVersion("2.0.0"); List plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); - DependencyResolver resolver = new DependencyResolver(); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.getWrongVersionDependencies().isEmpty()); diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java index 6806595..9ee9899 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/LoadPluginsTest.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.junit.Before; import org.junit.Test; import ro.fortsoft.pf4j.plugin.MockPluginManager; @@ -31,6 +30,7 @@ import static junit.framework.TestCase.assertNull; import static org.junit.Assert.*; public class LoadPluginsTest { + private Path tmpDir; private MockPluginManager pluginManager; private MockZipPlugin p1; @@ -92,14 +92,14 @@ public class LoadPluginsTest { pluginManager.loadPlugins(); pluginManager.startPlugins(); 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()); p2.create(); pluginManager.loadPlugins(); pluginManager.startPlugin(p2.id); assertEquals(1, pluginManager.getPlugins().size()); - assertEquals(Version.valueOf("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.getPlugin(p2.id).getDescriptor().getVersion()); + assertEquals("2.0.0", pluginManager.getStartedPlugins().get(1).getDescriptor().getVersion()); } @Test @@ -131,6 +131,7 @@ public class LoadPluginsTest { } private class MockZipPlugin { + public final String id; public final String version; public final String filename; @@ -165,5 +166,7 @@ public class LoadPluginsTest { Files.move(propsFile, propsInZip); } } + } + } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java index 1d1b621..c858767 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/ManifestPluginDescriptorFinderTest.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,9 +27,7 @@ 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; -import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches; +import static org.junit.Assert.*; /** * @author Mario Franco @@ -38,6 +35,8 @@ import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches; */ public class ManifestPluginDescriptorFinderTest { + private VersionManager versionManager; + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @@ -70,6 +69,8 @@ public class ManifestPluginDescriptorFinderTest { 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("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.getPluginDescription()); 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(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()); 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("", plugin2.getPluginDescription()); 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(0, plugin2.getDependencies().size()); - assertTrue(versionMatches(plugin2.getRequires(),"1.0.0")); + assertTrue(versionManager.satisfies(plugin2.getRequires(), "1.0.0")); } /** diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java index fa6666a..aa6d7c5 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/PropertiesPluginDescriptorFinderTest.java @@ -15,7 +15,6 @@ */ package ro.fortsoft.pf4j; -import com.github.zafarkhaja.semver.Version; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -29,10 +28,11 @@ import java.util.Arrays; import java.util.List; import static org.junit.Assert.*; -import static ro.fortsoft.pf4j.util.SemVerUtils.versionMatches; public class PropertiesPluginDescriptorFinderTest { + private VersionManager versionManager; + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @@ -60,6 +60,8 @@ public class PropertiesPluginDescriptorFinderTest { // no plugin id pluginPath = testFolder.newFolder("test-plugin-6").toPath(); Files.write(pluginPath.resolve("plugin.properties"), getPlugin6Properties(), charset); + + versionManager = new DefaultVersionManager(); } @Test @@ -72,7 +74,7 @@ public class PropertiesPluginDescriptorFinderTest { 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("0.0.1", plugin1.getVersion()); assertEquals("Decebal Suiu", plugin1.getProvider()); assertEquals(2, plugin1.getDependencies().size()); 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("Apache-2.0", plugin1.getLicense()); assertEquals(">=1", plugin1.getRequires()); - assertTrue(versionMatches(plugin1.getRequires(),"1.0.0")); - assertFalse(versionMatches(plugin1.getRequires(), "0.1.0")); + assertTrue(versionManager.satisfies(plugin1.getRequires(), "1.0.0")); + assertFalse(versionManager.satisfies(plugin1.getRequires(), "0.1.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("0.0.1", plugin2.getVersion()); assertEquals("Decebal Suiu", plugin2.getProvider()); assertEquals(0, plugin2.getDependencies().size()); assertEquals("*", plugin2.getRequires()); // Default is * - assertTrue(versionMatches(plugin2.getRequires(),"1.0.0")); + assertTrue(versionManager.satisfies(plugin2.getRequires(), "1.0.0")); } @Test(expected = PluginException.class) @@ -171,6 +173,7 @@ public class PropertiesPluginDescriptorFinderTest { return Arrays.asList(lines); } + private Path getPluginsRoot() { return testFolder.getRoot().toPath(); } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java b/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java index 23b1b34..139a0e8 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/plugin/MockPluginManager.java @@ -26,6 +26,7 @@ import java.nio.file.Path; * Manager for testing */ public class MockPluginManager extends DefaultPluginManager { + private PluginDescriptorFinder finder = new DefaultPluginDescriptorFinder(new DefaultPluginClasspath()); public MockPluginManager() { @@ -46,4 +47,5 @@ public class MockPluginManager extends DefaultPluginManager { protected PluginDescriptorFinder createPluginDescriptorFinder() { return finder; } + } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/util/FileUtilsTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/util/FileUtilsTest.java index 37f0d10..6b98549 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/util/FileUtilsTest.java +++ b/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.IOException; 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 static org.junit.Assert.*; public class FileUtilsTest { + private Path zipFile; private Path tmpDir; private Path propsFile;