diff --git a/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java b/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java index 479c6be9a5..ad6ba84875 100644 --- a/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java +++ b/scm-core/src/main/java/sonia/scm/plugin/PluginCondition.java @@ -236,7 +236,7 @@ public class PluginCondition implements Cloneable, Serializable if (Util.isNotEmpty(minVersion) && Util.isNotEmpty(version)) { supported = (minVersion.equalsIgnoreCase(version) - || new Version(version).isNewer(minVersion)); + || Version.parse(version).isNewer(minVersion)); } if (supported && Util.isNotEmpty(this.os) && Util.isNotEmpty(os)) diff --git a/scm-core/src/main/java/sonia/scm/version/Version.java b/scm-core/src/main/java/sonia/scm/version/Version.java index 09efe75cb8..96f7a9ff07 100644 --- a/scm-core/src/main/java/sonia/scm/version/Version.java +++ b/scm-core/src/main/java/sonia/scm/version/Version.java @@ -39,35 +39,26 @@ import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.ComparisonChain; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - //~--- JDK imports ------------------------------------------------------------ import java.util.Locale; /** * Version object for comparing and parsing versions. - * + * * * @author Sebastian Sdorra */ public final class Version implements Comparable { - /** the logger for Version */ - private static final Logger logger = - LoggerFactory.getLogger(Version.class); - - //~--- constructors --------------------------------------------------------- - /** * Constructs a new version object * * * @param versionString string representation of the version */ - public Version(String versionString) + private Version(String versionString) { this.unparsedVersion = versionString; @@ -101,14 +92,11 @@ public final class Version implements Comparable /** * Creates a new version of the given string. * - * TODO throw exception if not parseable - * - * * @param versionString string representation of the version * * @return version object */ - public static Version createVersion(String versionString) + public static Version parse(String versionString) { Version version = null; @@ -118,10 +106,8 @@ public final class Version implements Comparable } catch (NumberFormatException ex) { - if (logger.isWarnEnabled()) - { - logger.warn("could not parse version ".concat(versionString), ex); - } + throw new VersionParseException( + "could not parse version ".concat(versionString), ex); } return version; @@ -166,8 +152,7 @@ public final class Version implements Comparable final Version other = (Version) obj; return Objects.equal(major, other.major) - && Objects.equal(minor, other.minor) - && Objects.equal(patch, other.patch) + && Objects.equal(minor, other.minor) && Objects.equal(patch, other.patch) && Objects.equal(type, other.type) && Objects.equal(typeVersion, other.typeVersion) && Objects.equal(snapshot, other.snapshot) @@ -180,8 +165,8 @@ public final class Version implements Comparable @Override public int hashCode() { - return Objects.hashCode(major, minor, patch, type, typeVersion, - snapshot, parsedVersion); + return Objects.hashCode(major, minor, patch, type, typeVersion, snapshot, + parsedVersion); } /** @@ -195,17 +180,6 @@ public final class Version implements Comparable //~--- get methods ---------------------------------------------------------- - /** - * Returns the patch part of the version. - * - * - * @return patch part - */ - public int getPatch() - { - return patch; - } - /** * Returns the major part of the version. * @@ -239,6 +213,17 @@ public final class Version implements Comparable return parsedVersion; } + /** + * Returns the patch part of the version. + * + * + * @return patch part + */ + public int getPatch() + { + return patch; + } + /** * Returns the version type (alpha, beta, milestone, etc.) of the version. * @@ -273,7 +258,7 @@ public final class Version implements Comparable } /** - * Returns true if the current version is newer than the given version. + * Returns true if the given version is newer. * * @param o other version * @@ -285,7 +270,7 @@ public final class Version implements Comparable } /** - * Returns true if the current version is newer than the given version. + * Returns true if the given version is newer. * * * @param versionString other version @@ -294,13 +279,13 @@ public final class Version implements Comparable */ public boolean isNewer(String versionString) { - Version o = Version.createVersion(versionString); + Version o = Version.parse(versionString); return (o != null) && isNewer(o); } /** - * Returns true if the current version is older than the given version. + * Returns true if the given version is older. * * * @param o other version @@ -313,7 +298,7 @@ public final class Version implements Comparable } /** - * Returns true if the current version is older than the given version. + * Returns true if the given version is older. * * * @param versionString other version @@ -322,7 +307,7 @@ public final class Version implements Comparable */ public boolean isOlder(String versionString) { - Version o = Version.createVersion(versionString); + Version o = Version.parse(versionString); return (o != null) && isOlder(o); } @@ -478,15 +463,15 @@ public final class Version implements Comparable /** unparsed version */ private final String unparsedVersion; - /** patch part */ - private int patch = 0; - /** major part */ private int major = 0; /** minor part */ private int minor = 0; + /** patch part */ + private int patch = 0; + /** is a snapshot */ private boolean snapshot; diff --git a/scm-core/src/main/java/sonia/scm/version/VersionParseException.java b/scm-core/src/main/java/sonia/scm/version/VersionParseException.java new file mode 100644 index 0000000000..9db6013dfb --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/version/VersionParseException.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.version; + +/** + * The VersionParseException is thrown if a version could not be parsed. + * + * @author Sebastian Sdorra + * + * @since 2.0.0 + */ +public class VersionParseException extends RuntimeException +{ + + /** + * Constructs a new VersionParseException. + * + */ + public VersionParseException() {} + + /** + * Constructs a new VersionParseException. + * + * + * @param message message of the exception + */ + public VersionParseException(String message) + { + super(message); + } + + /** + * Constructs a new VersionParseException. + * + * + * @param cause cause of the exception + */ + public VersionParseException(Throwable cause) + { + super(cause); + } + + /** + * Constructs a new VersionParseException. + * + * + * @param message message of the exception + * @param cause cause of the exception + */ + public VersionParseException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/scm-core/src/test/java/sonia/scm/version/VersionTest.java b/scm-core/src/test/java/sonia/scm/version/VersionTest.java index eb9b79dd6c..a0f3af85d1 100644 --- a/scm-core/src/test/java/sonia/scm/version/VersionTest.java +++ b/scm-core/src/test/java/sonia/scm/version/VersionTest.java @@ -57,7 +57,7 @@ public class VersionTest @Test public void parseSimpleVersion() { - Version v = new Version("1.0"); + Version v = Version.parse("1.0"); assertTrue(v.getMajor() == 1); assertTrue(v.getMinor() == 0); @@ -67,7 +67,7 @@ public class VersionTest assertEquals(v.getParsedVersion(), "1.0.0"); // test with snapshot - v = new Version("1.1-SNAPSHOT"); + v = Version.parse("1.1-SNAPSHOT"); assertTrue(v.getMajor() == 1); assertTrue(v.getMinor() == 1); assertTrue(v.getPatch() == 0); @@ -76,7 +76,7 @@ public class VersionTest assertEquals(v.getParsedVersion(), "1.1.0-SNAPSHOT"); // test with maintenance - v = new Version("2.3.14"); + v = Version.parse("2.3.14"); assertTrue(v.getMajor() == 2); assertTrue(v.getMinor() == 3); assertTrue(v.getPatch() == 14); @@ -92,7 +92,7 @@ public class VersionTest @Test public void parseTypeVersions() { - Version v = new Version("1.0-alpha"); + Version v = Version.parse("1.0-alpha"); assertTrue(v.getMajor() == 1); assertTrue(v.getMinor() == 0); @@ -103,7 +103,7 @@ public class VersionTest assertEquals(v.getParsedVersion(), "1.0.0-alpha1"); // Test release candidate - v = new Version("2.1.2-RC3"); + v = Version.parse("2.1.2-RC3"); assertTrue(v.getMajor() == 2); assertTrue(v.getMinor() == 1); assertTrue(v.getPatch() == 2); @@ -122,15 +122,15 @@ public class VersionTest { Version[] versions = new Version[9]; - versions[0] = Version.createVersion("2.3.1-SNAPSHOT"); - versions[1] = Version.createVersion("2.3.1-beta1"); - versions[2] = Version.createVersion("2.3.1-beta2"); - versions[3] = Version.createVersion("2.3.1-M1"); - versions[4] = Version.createVersion("2.3.1-alpha2"); - versions[5] = Version.createVersion("2.3.1-RC1"); - versions[6] = Version.createVersion("2.3.1"); - versions[7] = Version.createVersion("2.3"); - versions[8] = Version.createVersion("2.4.6"); + versions[0] = Version.parse("2.3.1-SNAPSHOT"); + versions[1] = Version.parse("2.3.1-beta1"); + versions[2] = Version.parse("2.3.1-beta2"); + versions[3] = Version.parse("2.3.1-M1"); + versions[4] = Version.parse("2.3.1-alpha2"); + versions[5] = Version.parse("2.3.1-RC1"); + versions[6] = Version.parse("2.3.1"); + versions[7] = Version.parse("2.3"); + versions[8] = Version.parse("2.4.6"); Arrays.sort(versions); assertEquals(versions[0].getParsedVersion(), "2.4.6"); assertEquals(versions[1].getParsedVersion(), "2.3.1"); @@ -150,9 +150,9 @@ public class VersionTest @Test public void testIsNewer() { - assertFalse(Version.createVersion("1.0").isNewer("1.0.1")); - assertTrue(Version.createVersion("1.1").isNewer("1.1-alpha1")); - assertTrue(Version.createVersion("1.1").isNewer("1.1-RC5")); + assertFalse(Version.parse("1.0").isNewer("1.0.1")); + assertTrue(Version.parse("1.1").isNewer("1.1-alpha1")); + assertTrue(Version.parse("1.1").isNewer("1.1-RC5")); } /** @@ -162,8 +162,18 @@ public class VersionTest @Test public void testIsOlder() { - assertFalse(Version.createVersion("1.0.1").isOlder("1.0")); - assertTrue(Version.createVersion("1.1-alpha1").isOlder("1.1")); - assertTrue(Version.createVersion("1.1-RC5").isOlder("1.1")); + assertFalse(Version.parse("1.0.1").isOlder("1.0")); + assertTrue(Version.parse("1.1-alpha1").isOlder("1.1")); + assertTrue(Version.parse("1.1-RC5").isOlder("1.1")); + } + + /** + * Method description + * + */ + @Test(expected = VersionParseException.class) + public void testUnparseable() + { + Version.parse("aaaa"); } } diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginInformationVersionComparator.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginInformationVersionComparator.java index 75ace83a9b..4d8af895aa 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginInformationVersionComparator.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/PluginInformationVersionComparator.java @@ -35,6 +35,7 @@ package sonia.scm.plugin; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.version.Version; import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -73,8 +74,8 @@ public class PluginInformationVersionComparator if (Util.isNotEmpty(v1) && Util.isNotEmpty(v2)) { - if (PluginVersion.createVersion(v1).isNewer( - PluginVersion.createVersion(v2))) + if (Version.parse(v1).isNewer( + Version.parse(v2))) { result = -1; } diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginInformationComparator.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginInformationComparator.java index 3f2d72d42b..487dfb98e8 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginInformationComparator.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/PluginInformationComparator.java @@ -36,7 +36,7 @@ package sonia.scm.plugin.rest; //~--- non-JDK imports -------------------------------------------------------- import sonia.scm.plugin.PluginInformation; -import sonia.scm.plugin.PluginVersion; +import sonia.scm.version.Version; //~--- JDK imports ------------------------------------------------------------ @@ -76,10 +76,10 @@ public class PluginInformationComparator if (c == 0) { - PluginVersion version = - PluginVersion.createVersion(plugin.getVersion()); - PluginVersion otherVersion = - PluginVersion.createVersion(otherPlugin.getVersion()); + Version version = + Version.parse(plugin.getVersion()); + Version otherVersion = + Version.parse(otherPlugin.getVersion()); c = version.compareTo(otherVersion); } diff --git a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/api/PluginResource.java b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/api/PluginResource.java index 01ad3aea12..94e4043c74 100644 --- a/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/api/PluginResource.java +++ b/scm-plugin-backend/src/main/java/sonia/scm/plugin/rest/api/PluginResource.java @@ -50,7 +50,7 @@ import sonia.scm.plugin.PluginBackend; import sonia.scm.plugin.PluginBackendListener; import sonia.scm.plugin.PluginCenter; import sonia.scm.plugin.PluginInformation; -import sonia.scm.plugin.PluginVersion; +import sonia.scm.version.Version; import sonia.scm.plugin.rest.PluginInformationComparator; //~--- JDK imports ------------------------------------------------------------ @@ -264,7 +264,7 @@ public class PluginResource implements PluginBackendListener */ private boolean isNewer(PluginInformation plugin, PluginInformation newest) { - return new PluginVersion(plugin.getVersion()).isNewer(newest.getVersion()); + return Version.parse(plugin.getVersion()).isNewer(newest.getVersion()); } /** diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java index 94ef21fd3c..d317155a2a 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java @@ -35,6 +35,7 @@ package sonia.scm.plugin; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.version.Version; import com.github.legman.Subscribe; import com.google.common.collect.Sets; @@ -721,7 +722,7 @@ public class DefaultPluginManager implements PluginManager PluginInformation installed) { boolean result = false; - PluginVersion version = PluginVersion.createVersion(available.getVersion()); + Version version = Version.parse(available.getVersion()); if (version != null) { diff --git a/scm-webapp/src/main/java/sonia/scm/upgrade/ClientDateFormatUpgradeHandler.java b/scm-webapp/src/main/java/sonia/scm/upgrade/ClientDateFormatUpgradeHandler.java index 6e4d4f7e03..5b8301960e 100644 --- a/scm-webapp/src/main/java/sonia/scm/upgrade/ClientDateFormatUpgradeHandler.java +++ b/scm-webapp/src/main/java/sonia/scm/upgrade/ClientDateFormatUpgradeHandler.java @@ -44,7 +44,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import sonia.scm.SCMContext; -import sonia.scm.plugin.PluginVersion; +import sonia.scm.version.Version; //~--- JDK imports ------------------------------------------------------------ @@ -79,7 +79,7 @@ public class ClientDateFormatUpgradeHandler extends XmlUpgradeHandler */ @Override public void doUpgrade(File homeDirectory, File configDirectory, - PluginVersion oldVersion, PluginVersion newVersion) + Version oldVersion, Version newVersion) { if (oldVersion.isOlder("1.23")) { diff --git a/scm-webapp/src/main/java/sonia/scm/upgrade/TimestampUpgradeHandler.java b/scm-webapp/src/main/java/sonia/scm/upgrade/TimestampUpgradeHandler.java index 66ed6d78a4..85328111fa 100644 --- a/scm-webapp/src/main/java/sonia/scm/upgrade/TimestampUpgradeHandler.java +++ b/scm-webapp/src/main/java/sonia/scm/upgrade/TimestampUpgradeHandler.java @@ -46,7 +46,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import sonia.scm.SCMContext; -import sonia.scm.plugin.PluginVersion; +import sonia.scm.version.Version; import sonia.scm.util.IOUtil; import sonia.scm.util.Util; @@ -90,7 +90,7 @@ public class TimestampUpgradeHandler extends XmlUpgradeHandler */ @Override public void doUpgrade(File homeDirectory, File configDirectory, - PluginVersion oldVersion, PluginVersion newVersion) + Version oldVersion, Version newVersion) { if (oldVersion.isOlder("1.2")) { diff --git a/scm-webapp/src/main/java/sonia/scm/upgrade/UpgradeManager.java b/scm-webapp/src/main/java/sonia/scm/upgrade/UpgradeManager.java index bbc0268388..9b34091f25 100644 --- a/scm-webapp/src/main/java/sonia/scm/upgrade/UpgradeManager.java +++ b/scm-webapp/src/main/java/sonia/scm/upgrade/UpgradeManager.java @@ -44,7 +44,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.SCMContext; -import sonia.scm.plugin.PluginVersion; +import sonia.scm.version.Version; import sonia.scm.util.IOUtil; //~--- JDK imports ------------------------------------------------------------ @@ -164,8 +164,8 @@ public class UpgradeManager try { - PluginVersion oldVersion = PluginVersion.createVersion(oldVersionString); - PluginVersion newVersion = PluginVersion.createVersion(newVersionString); + Version oldVersion = Version.parse(oldVersionString); + Version newVersion = Version.parse(newVersionString); doUpgradesForOldVersion(baseDirectory, configDirectory, oldVersion, newVersion); @@ -190,7 +190,7 @@ public class UpgradeManager * @param newVersion */ private void doUpgradesForOldVersion(File baseDirectory, - File configDirectory, PluginVersion oldVersion, PluginVersion newVersion) + File configDirectory, Version oldVersion, Version newVersion) { List upgradeHandlers = collectUpgradeHandlers();