hide constructor and throw exception if the version could not be parsed

This commit is contained in:
Sebastian Sdorra
2014-02-19 21:47:45 +01:00
parent b6c65e085b
commit 269178f0a2
11 changed files with 162 additions and 82 deletions

View File

@@ -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))

View File

@@ -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<Version>
{
/** 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<Version>
/**
* 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<Version>
}
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<Version>
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<Version>
@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<Version>
//~--- 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<Version>
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<Version>
}
/**
* 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<Version>
}
/**
* 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<Version>
*/
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<Version>
}
/**
* 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<Version>
*/
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<Version>
/** 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;

View File

@@ -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);
}
}

View File

@@ -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");
}
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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());
}
/**

View File

@@ -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)
{

View File

@@ -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"))
{

View File

@@ -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"))
{

View File

@@ -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<UpgradeHandler> upgradeHandlers = collectUpgradeHandlers();