mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
improve PluginVersion api
This commit is contained in:
@@ -33,20 +33,26 @@
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.util.AssertUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class PluginVersion
|
||||
public class PluginVersion implements Comparable<PluginVersion>
|
||||
{
|
||||
|
||||
/** the logger for PluginVersion */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(PluginVersion.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -82,117 +88,93 @@ public class PluginVersion
|
||||
parsedVersion = createParsedVersion();
|
||||
}
|
||||
|
||||
//~--- enums ----------------------------------------------------------------
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Enum description
|
||||
*
|
||||
*
|
||||
* @param versionString
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static enum PluginVersionType
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param versionString
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static PluginVersion createVersion(String versionString)
|
||||
{
|
||||
EARLY_ACESS("ea", 0, "early", "earlyaccess"),
|
||||
MILESTONE("M", 1, "milestone"),
|
||||
ALPHA("alpha", 2), BETA("beta", 3),
|
||||
RELEASE_CANDIDAT("rc", 4, "releasecandidate"), RELEASE(10);
|
||||
PluginVersion version = null;
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
private PluginVersionType(int value)
|
||||
try
|
||||
{
|
||||
this(null, value);
|
||||
version = new PluginVersion(versionString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param value
|
||||
* @param aliases
|
||||
*/
|
||||
private PluginVersionType(String id, int value, String... aliases)
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
this.id = id;
|
||||
this.value = value;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
//~--- get methods --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String[] getAliases()
|
||||
{
|
||||
return aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<String> getNames()
|
||||
{
|
||||
List<String> names = new ArrayList<String>();
|
||||
|
||||
if (id != null)
|
||||
if (logger.isWarnEnabled())
|
||||
{
|
||||
names.add(id);
|
||||
logger.warn("could not parse version ".concat(versionString), ex);
|
||||
}
|
||||
|
||||
if (aliases != null)
|
||||
{
|
||||
names.addAll(Arrays.asList(aliases));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
public String[] aliases;
|
||||
|
||||
/** Field description */
|
||||
private String id;
|
||||
|
||||
/** Field description */
|
||||
private int value;
|
||||
return version;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(PluginVersion o)
|
||||
{
|
||||
AssertUtil.assertIsNotNull(o);
|
||||
|
||||
int result = o.major - major;
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
result = o.minor - minor;
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
result = o.maintenance - maintenance;
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
result = o.type.getValue() - type.getValue();
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
result = o.typeVersion - typeVersion;
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
if (o.snapshot &&!snapshot)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (!o.snapshot && snapshot)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -285,6 +267,62 @@ public class PluginVersion
|
||||
return unparsedVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isNewer(PluginVersion o)
|
||||
{
|
||||
return compareTo(o) < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param versionString
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isNewer(String versionString)
|
||||
{
|
||||
PluginVersion o = PluginVersion.createVersion(versionString);
|
||||
|
||||
return (o != null) && isNewer(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param o
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOlder(PluginVersion o)
|
||||
{
|
||||
return compareTo(o) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param versionString
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOlder(String versionString)
|
||||
{
|
||||
PluginVersion o = PluginVersion.createVersion(versionString);
|
||||
|
||||
return (o != null) && isOlder(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -347,6 +385,8 @@ public class PluginVersion
|
||||
{
|
||||
for (String name : versionType.getNames())
|
||||
{
|
||||
name = name.toLowerCase();
|
||||
|
||||
int index = qualifier.indexOf(name);
|
||||
|
||||
if (index > 0)
|
||||
|
||||
148
scm-core/src/main/java/sonia/scm/plugin/PluginVersionType.java
Normal file
148
scm-core/src/main/java/sonia/scm/plugin/PluginVersionType.java
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* 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.plugin;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public enum PluginVersionType
|
||||
{
|
||||
EARLY_ACESS("ea", 0, "early", "earlyaccess"), MILESTONE("M", 1, "milestone"),
|
||||
ALPHA("alpha", 2), BETA("beta", 3),
|
||||
RELEASE_CANDIDAT("RC", 4, "releasecandidate"), RELEASE(10);
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
private PluginVersionType(int value)
|
||||
{
|
||||
this(null, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param value
|
||||
* @param aliases
|
||||
*/
|
||||
private PluginVersionType(String id, int value, String... aliases)
|
||||
{
|
||||
this.id = id;
|
||||
this.value = value;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String[] getAliases()
|
||||
{
|
||||
return aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<String> getNames()
|
||||
{
|
||||
List<String> names = new ArrayList<String>();
|
||||
|
||||
if (id != null)
|
||||
{
|
||||
names.add(id);
|
||||
}
|
||||
|
||||
if (aliases != null)
|
||||
{
|
||||
names.addAll(Arrays.asList(aliases));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
public String[] aliases;
|
||||
|
||||
/** Field description */
|
||||
private String id;
|
||||
|
||||
/** Field description */
|
||||
private int value;
|
||||
}
|
||||
@@ -39,6 +39,10 @@ import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -59,7 +63,7 @@ public class PluginVersionTest
|
||||
assertTrue(v.getMinor() == 0);
|
||||
assertTrue(v.getMaintenance() == 0);
|
||||
assertFalse(v.isSnapshot());
|
||||
assertTrue(v.getType() == PluginVersion.PluginVersionType.RELEASE);
|
||||
assertTrue(v.getType() == PluginVersionType.RELEASE);
|
||||
assertEquals(v.getParsedVersion(), "1.0.0");
|
||||
|
||||
// test with snapshot
|
||||
@@ -68,7 +72,7 @@ public class PluginVersionTest
|
||||
assertTrue(v.getMinor() == 1);
|
||||
assertTrue(v.getMaintenance() == 0);
|
||||
assertTrue(v.isSnapshot());
|
||||
assertTrue(v.getType() == PluginVersion.PluginVersionType.RELEASE);
|
||||
assertTrue(v.getType() == PluginVersionType.RELEASE);
|
||||
assertEquals(v.getParsedVersion(), "1.1.0-SNAPSHOT");
|
||||
|
||||
// test with maintenance
|
||||
@@ -77,7 +81,7 @@ public class PluginVersionTest
|
||||
assertTrue(v.getMinor() == 3);
|
||||
assertTrue(v.getMaintenance() == 14);
|
||||
assertFalse(v.isSnapshot());
|
||||
assertTrue(v.getType() == PluginVersion.PluginVersionType.RELEASE);
|
||||
assertTrue(v.getType() == PluginVersionType.RELEASE);
|
||||
assertEquals(v.getParsedVersion(), "2.3.14");
|
||||
}
|
||||
|
||||
@@ -94,7 +98,7 @@ public class PluginVersionTest
|
||||
assertTrue(v.getMinor() == 0);
|
||||
assertTrue(v.getMaintenance() == 0);
|
||||
assertFalse(v.isSnapshot());
|
||||
assertTrue(v.getType() == PluginVersion.PluginVersionType.ALPHA);
|
||||
assertTrue(v.getType() == PluginVersionType.ALPHA);
|
||||
assertTrue(v.getTypeVersion() == 1);
|
||||
assertEquals(v.getParsedVersion(), "1.0.0-alpha1");
|
||||
|
||||
@@ -104,8 +108,50 @@ public class PluginVersionTest
|
||||
assertTrue(v.getMinor() == 1);
|
||||
assertTrue(v.getMaintenance() == 2);
|
||||
assertFalse(v.isSnapshot());
|
||||
assertTrue(v.getType() == PluginVersion.PluginVersionType.RELEASE_CANDIDAT);
|
||||
assertTrue(v.getType() == PluginVersionType.RELEASE_CANDIDAT);
|
||||
assertTrue(v.getTypeVersion() == 3);
|
||||
assertEquals(v.getParsedVersion(), "2.1.2-rc3");
|
||||
assertEquals(v.getParsedVersion(), "2.1.2-RC3");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testCompareTo()
|
||||
{
|
||||
PluginVersion[] versions = new PluginVersion[9];
|
||||
|
||||
versions[0] = PluginVersion.createVersion("2.3.1-SNAPSHOT");
|
||||
versions[1] = PluginVersion.createVersion("2.3.1-beta1");
|
||||
versions[2] = PluginVersion.createVersion("2.3.1-beta2");
|
||||
versions[3] = PluginVersion.createVersion("2.3.1-M1");
|
||||
versions[4] = PluginVersion.createVersion("2.3.1-alpha2");
|
||||
versions[5] = PluginVersion.createVersion("2.3.1-RC1");
|
||||
versions[6] = PluginVersion.createVersion("2.3.1");
|
||||
versions[7] = PluginVersion.createVersion("2.3");
|
||||
versions[8] = PluginVersion.createVersion("2.4.6");
|
||||
Arrays.sort(versions);
|
||||
assertEquals(versions[0].getParsedVersion(), "2.4.6");
|
||||
assertEquals(versions[1].getParsedVersion(), "2.3.1");
|
||||
assertEquals(versions[2].getParsedVersion(), "2.3.1-SNAPSHOT");
|
||||
assertEquals(versions[3].getParsedVersion(), "2.3.1-RC1");
|
||||
assertEquals(versions[4].getParsedVersion(), "2.3.1-beta2");
|
||||
assertEquals(versions[5].getParsedVersion(), "2.3.1-beta1");
|
||||
assertEquals(versions[6].getParsedVersion(), "2.3.1-alpha2");
|
||||
assertEquals(versions[7].getParsedVersion(), "2.3.1-M1");
|
||||
assertEquals(versions[8].getParsedVersion(), "2.3.0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testIsNewer()
|
||||
{
|
||||
assertFalse(PluginVersion.createVersion("1.0").isNewer("1.0.1"));
|
||||
assertTrue(PluginVersion.createVersion("1.1").isNewer("1.1-alpha1"));
|
||||
assertTrue(PluginVersion.createVersion("1.1").isNewer("1.1-RC5"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user