Make major refactoring toward object-orientation
This commit is contained in:
parent
f7b8dce2af
commit
5d0aa683cb
2
pom.xml
2
pom.xml
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
<groupId>com.github.zafarkhaja</groupId>
|
<groupId>com.github.zafarkhaja</groupId>
|
||||||
<artifactId>semver</artifactId>
|
<artifactId>semver</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.2.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>semver</name>
|
<name>semver</name>
|
||||||
|
|
|
@ -0,0 +1,120 @@
|
||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright 2013 Zafar Khaja <zafarkhaja@gmail.com>.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
||||||
|
*/
|
||||||
|
class AlphaNumericVersion implements Comparable<AlphaNumericVersion> {
|
||||||
|
|
||||||
|
static final String FORMAT = "([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)";
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$");
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
AlphaNumericVersion(String value) {
|
||||||
|
if (value == null) {
|
||||||
|
throw new NullPointerException(
|
||||||
|
"Alpha-numeric version MUST NOT be NULL"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Matcher matcher = PATTERN.matcher(value);
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Alpha-numeric version MUST consist of dot separated identifiers [0-9A-Za-z-]"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.value = matcher.group(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (this == other) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(other instanceof AlphaNumericVersion)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return compareTo((AlphaNumericVersion) other) == 0 ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return value.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(AlphaNumericVersion other) {
|
||||||
|
String[] thisIds = value.split("\\.");
|
||||||
|
String[] otherIds = other.value.split("\\.");
|
||||||
|
|
||||||
|
int result = compareIdentifierArrays(thisIds, otherIds);
|
||||||
|
if (result == 0) {
|
||||||
|
result = thisIds.length - otherIds.length;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int compareIdentifierArrays(String[] ids1, String[] ids2) {
|
||||||
|
int result = 0;
|
||||||
|
int length = getLeastCommonArrayLength(ids1, ids2);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
result = compareIdentifiers(ids1[i], ids2[i]);
|
||||||
|
if (result != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getLeastCommonArrayLength(String[] arr1, String[] arr2) {
|
||||||
|
return arr1.length <= arr2.length ? arr1.length : arr2.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int compareIdentifiers(String id1, String id2) {
|
||||||
|
if (isInt(id1) && isInt(id2)) {
|
||||||
|
return Integer.parseInt(id1) - Integer.parseInt(id2);
|
||||||
|
} else {
|
||||||
|
return id1.compareTo(id2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isInt(String str) {
|
||||||
|
try {
|
||||||
|
Integer.parseInt(str);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
135
src/main/java/com/github/zafarkhaja/semver/NormalVersion.java
Normal file
135
src/main/java/com/github/zafarkhaja/semver/NormalVersion.java
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright 2013 Zafar Khaja <zafarkhaja@gmail.com>.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
||||||
|
*/
|
||||||
|
class NormalVersion implements Comparable<NormalVersion> {
|
||||||
|
|
||||||
|
private int major;
|
||||||
|
private int minor;
|
||||||
|
private int patch;
|
||||||
|
|
||||||
|
static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("^" + FORMAT + "$");
|
||||||
|
|
||||||
|
NormalVersion(int major, int minor, int patch) {
|
||||||
|
if (major < 0 || minor < 0 || patch < 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Major, minor and patch versions MUST be non-negative integers."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.major = major;
|
||||||
|
this.minor = minor;
|
||||||
|
this.patch = patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
static NormalVersion valueOf(String value) {
|
||||||
|
Matcher matcher = PATTERN.matcher(value);
|
||||||
|
if (!matcher.matches()) {
|
||||||
|
throw new IllegalArgumentException("Illegal normal version format");
|
||||||
|
}
|
||||||
|
return new NormalVersion(
|
||||||
|
Integer.parseInt(matcher.group(1)),
|
||||||
|
Integer.parseInt(matcher.group(2)),
|
||||||
|
Integer.parseInt(matcher.group(3))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getMajor() {
|
||||||
|
return major;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getMinor() {
|
||||||
|
return minor;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getPatch() {
|
||||||
|
return patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
void incrementMajor() {
|
||||||
|
major = major + 1;
|
||||||
|
minor = 0;
|
||||||
|
patch = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void incrementMinor() {
|
||||||
|
minor = minor + 1;
|
||||||
|
patch = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void incrementPatch() {
|
||||||
|
patch = patch + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(NormalVersion other) {
|
||||||
|
int result = major - other.major;
|
||||||
|
if (result == 0) {
|
||||||
|
result = minor - other.minor;
|
||||||
|
if (result == 0) {
|
||||||
|
result = patch - other.patch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (this == other) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(other instanceof NormalVersion)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return compareTo((NormalVersion) other) == 0 ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int hash = 17;
|
||||||
|
hash = 31 * hash + major;
|
||||||
|
hash = 31 * hash + minor;
|
||||||
|
hash = 31 * hash + patch;
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of this normal version.
|
||||||
|
*
|
||||||
|
* A normal version number MUST take the form X.Y.Z where X, Y, and Z are
|
||||||
|
* non-negative integers. X is the major version, Y is the minor version,
|
||||||
|
* and Z is the patch version. (SemVer p.2)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%d.%d.%d", major, minor, patch);
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,7 +21,6 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.github.zafarkhaja.semver;
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
@ -33,78 +32,93 @@ import java.util.regex.Pattern;
|
||||||
*/
|
*/
|
||||||
public class Version implements Comparable<Version> {
|
public class Version implements Comparable<Version> {
|
||||||
|
|
||||||
private String rawVersion;
|
private NormalVersion normal;
|
||||||
|
private AlphaNumericVersion preRelease;
|
||||||
|
private AlphaNumericVersion build;
|
||||||
|
|
||||||
private int majorVersion;
|
private static final String PRE_RELEASE_PREFIX = "-";
|
||||||
private int minorVersion;
|
private static final String BUILD_PREFIX = "+";
|
||||||
private int patchVersion;
|
|
||||||
|
|
||||||
private String preReleaseVersion;
|
private static final Pattern SEMVER_PATTERN;
|
||||||
private String buildVersion;
|
|
||||||
|
|
||||||
private static final String NORMAL_VERSION =
|
static {
|
||||||
"((?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+))";
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
private static final String PRE_RELEASE_VERSION =
|
sb.append("^");
|
||||||
"(?:-(?<preRelease>[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?";
|
sb.append(NormalVersion.FORMAT);
|
||||||
|
sb.append("(?:");
|
||||||
|
sb.append(PRE_RELEASE_PREFIX);
|
||||||
|
sb.append(AlphaNumericVersion.FORMAT);
|
||||||
|
sb.append(")?");
|
||||||
|
sb.append("(?:");
|
||||||
|
sb.append("\\");
|
||||||
|
sb.append(BUILD_PREFIX);
|
||||||
|
sb.append(AlphaNumericVersion.FORMAT);
|
||||||
|
sb.append(")?");
|
||||||
|
sb.append("$");
|
||||||
|
|
||||||
private static final String BUILD_VERSION =
|
SEMVER_PATTERN = Pattern.compile(sb.toString());
|
||||||
"(?:\\+(?<build>[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?";
|
}
|
||||||
|
|
||||||
private static final Pattern SEMVER_PATTERN = Pattern.compile(
|
Version(
|
||||||
"^" + NORMAL_VERSION + PRE_RELEASE_VERSION + BUILD_VERSION + "$"
|
NormalVersion normal,
|
||||||
);
|
AlphaNumericVersion preRelease,
|
||||||
|
AlphaNumericVersion build
|
||||||
|
) {
|
||||||
|
this.normal = normal;
|
||||||
|
this.preRelease = preRelease;
|
||||||
|
this.build = build;
|
||||||
|
}
|
||||||
|
|
||||||
public Version(String version) {
|
public static Version valueOf(String value) {
|
||||||
Matcher matcher = SEMVER_PATTERN.matcher(version);
|
Matcher matcher = SEMVER_PATTERN.matcher(value);
|
||||||
if (!matcher.matches()) {
|
if (!matcher.matches()) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Illegal version format"
|
"Illegal version format"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
rawVersion = version;
|
|
||||||
|
|
||||||
majorVersion = Integer.parseInt(matcher.group("major"));
|
NormalVersion normal = new NormalVersion(
|
||||||
minorVersion = Integer.parseInt(matcher.group("minor"));
|
Integer.parseInt(matcher.group(1)),
|
||||||
patchVersion = Integer.parseInt(matcher.group("patch"));
|
Integer.parseInt(matcher.group(2)),
|
||||||
|
Integer.parseInt(matcher.group(3))
|
||||||
|
);
|
||||||
|
|
||||||
preReleaseVersion = matcher.group("preRelease");
|
AlphaNumericVersion preRelease =
|
||||||
buildVersion = matcher.group("build");
|
(matcher.group(4) != null) ?
|
||||||
|
new AlphaNumericVersion(matcher.group(4)) :
|
||||||
|
null;
|
||||||
|
|
||||||
|
AlphaNumericVersion build =
|
||||||
|
(matcher.group(5) != null) ?
|
||||||
|
new AlphaNumericVersion(matcher.group(5)) :
|
||||||
|
null;
|
||||||
|
|
||||||
|
return new Version(normal, preRelease, build);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMajorVersion() {
|
public int getMajorVersion() {
|
||||||
return majorVersion;
|
return normal.getMajor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMinorVersion() {
|
public int getMinorVersion() {
|
||||||
return minorVersion;
|
return normal.getMinor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPatchVersion() {
|
public int getPatchVersion() {
|
||||||
return patchVersion;
|
return normal.getPatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNormalVersion() {
|
||||||
|
return normal.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPreReleaseVersion() {
|
public String getPreReleaseVersion() {
|
||||||
return preReleaseVersion;
|
return (preRelease != null) ? preRelease.toString() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBuildVersion() {
|
public String getBuildVersion() {
|
||||||
return buildVersion;
|
return (build != null) ? build.toString() : "";
|
||||||
}
|
|
||||||
|
|
||||||
public void bumpMajorVersion() {
|
|
||||||
majorVersion = majorVersion + 1;
|
|
||||||
minorVersion = 0;
|
|
||||||
patchVersion = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void bumpMinorVersion() {
|
|
||||||
minorVersion = minorVersion + 1;
|
|
||||||
patchVersion = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void bumpPatchVersion() {
|
|
||||||
patchVersion = patchVersion + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean greaterThan(Version other) {
|
public boolean greaterThan(Version other) {
|
||||||
|
@ -128,7 +142,7 @@ public class Version implements Comparable<Version> {
|
||||||
if (this == other) {
|
if (this == other) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (null == other || this.getClass() != other.getClass()) {
|
if (!(other instanceof Version)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return compareTo((Version) other) == 0 ? true : false;
|
return compareTo((Version) other) == 0 ? true : false;
|
||||||
|
@ -137,128 +151,55 @@ public class Version implements Comparable<Version> {
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 5;
|
int hash = 5;
|
||||||
hash = 97 * hash + majorVersion;
|
hash = 97 * hash + (normal != null ? normal.hashCode() : 0);
|
||||||
hash = 97 * hash + minorVersion;
|
hash = 97 * hash + (preRelease != null ? preRelease.hashCode() : 0);
|
||||||
hash = 97 * hash + patchVersion;
|
hash = 97 * hash + (build != null ? build.hashCode() : 0);
|
||||||
hash = 97 * hash + (
|
|
||||||
preReleaseVersion != null ? preReleaseVersion.hashCode(): 0
|
|
||||||
);
|
|
||||||
hash = 97 * hash + (
|
|
||||||
buildVersion != null ? buildVersion.hashCode() : 0
|
|
||||||
);
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return rawVersion;
|
StringBuilder sb = new StringBuilder(getNormalVersion());
|
||||||
|
if (preRelease != null) {
|
||||||
|
sb.append(PRE_RELEASE_PREFIX);
|
||||||
|
sb.append(getPreReleaseVersion());
|
||||||
|
}
|
||||||
|
if (build != null) {
|
||||||
|
sb.append(BUILD_PREFIX);
|
||||||
|
sb.append(getBuildVersion());
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Version other) {
|
public int compareTo(Version other) {
|
||||||
int result = compareNormalVersions(other);
|
int result = normal.compareTo(other.normal);
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
result = comparePreReleaseVersions(other);
|
result = comparePreReleases(other);
|
||||||
}
|
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
result = compareBuildVersions(other);
|
result = compareBuilds(other);
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int compareNormalVersions(Version other) {
|
|
||||||
int result = compareInts(majorVersion, other.getMajorVersion());
|
|
||||||
if (result == 0) {
|
|
||||||
result = compareInts(minorVersion, other.getMinorVersion());
|
|
||||||
if (result == 0) {
|
|
||||||
result = compareInts(patchVersion, other.getPatchVersion());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int compareInts(int thisOp, int otherOp) {
|
private int comparePreReleases(Version other) {
|
||||||
return (thisOp == otherOp) ? 0 : ((thisOp > otherOp) ? 1 : -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int comparePreReleaseVersions(Version other) {
|
|
||||||
if (preReleaseVersion == null ^ other.getPreReleaseVersion() == null) {
|
|
||||||
return preReleaseVersion == null ? 1 : -1;
|
|
||||||
} else {
|
|
||||||
return compareAlphaNumericVersions(
|
|
||||||
preReleaseVersion,
|
|
||||||
other.getPreReleaseVersion()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int compareBuildVersions(Version other) {
|
|
||||||
if (buildVersion == null ^ other.getBuildVersion() == null) {
|
|
||||||
return buildVersion == null ? -1 : 1;
|
|
||||||
} else {
|
|
||||||
return compareAlphaNumericVersions(
|
|
||||||
buildVersion,
|
|
||||||
other.getBuildVersion()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int compareAlphaNumericVersions(String thisOp, String otherOp) {
|
|
||||||
if (thisOp == null && otherOp == null) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
String[] thisIdents = thisOp.split("\\.");
|
|
||||||
String[] otherIdents = otherOp.split("\\.");
|
|
||||||
|
|
||||||
int result = compareIdentifierArrays(thisIdents, otherIdents);
|
|
||||||
if (result == 0 && thisIdents.length != otherIdents.length) {
|
|
||||||
result = (thisIdents.length > otherIdents.length) ? 1 : -1;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int compareIdentifierArrays(String[] thisArr, String[] otherArr) {
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < getSmallestArrayLength(thisArr, otherArr); i++) {
|
if (preRelease != null && other.preRelease != null) {
|
||||||
result = compareIdentifiers(thisArr[i], otherArr[i]);
|
result = preRelease.compareTo(other.preRelease);
|
||||||
if (result != 0) {
|
} else if (preRelease == null ^ other.preRelease == null) {
|
||||||
break;
|
result = preRelease == null ? 1 : -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getSmallestArrayLength(String[] thisArr, String[] otherArr) {
|
private int compareBuilds(Version other) {
|
||||||
if (thisArr.length <= otherArr.length) {
|
int result = 0;
|
||||||
return thisArr.length;
|
if (build != null && other.build != null) {
|
||||||
} else {
|
result = build.compareTo(other.build);
|
||||||
return otherArr.length;
|
} else if (build == null ^ other.build == null) {
|
||||||
|
result = build == null ? -1 : 1;
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
|
|
||||||
private int compareIdentifiers(String thisIdent, String otherIdent) {
|
|
||||||
if (isInt(thisIdent) && isInt(otherIdent)) {
|
|
||||||
return compareInts(
|
|
||||||
Integer.parseInt(thisIdent),
|
|
||||||
Integer.parseInt(otherIdent)
|
|
||||||
);
|
|
||||||
} else if (isInt(thisIdent) || isInt(otherIdent)) {
|
|
||||||
/**
|
|
||||||
* Numeric identifiers always have lower precedence
|
|
||||||
* than non-numeric identifiers.
|
|
||||||
*/
|
|
||||||
return isInt(thisIdent) ? -1 : 1;
|
|
||||||
} else {
|
|
||||||
return thisIdent.compareTo(otherIdent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isInt(String str) {
|
|
||||||
try {
|
|
||||||
Integer.parseInt(str);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,166 @@
|
||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright 2013 Zafar Khaja <zafarkhaja@gmail.com>.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.runners.Enclosed;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
||||||
|
*/
|
||||||
|
@RunWith(Enclosed.class)
|
||||||
|
public class AlphaNumericVersionTest {
|
||||||
|
|
||||||
|
public static class CoreFunctionalityTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustConsistOfDotSeparatedIdentifiersOfAlphaNumericsAndHyphen() {
|
||||||
|
String[] invalidVersions = {
|
||||||
|
null,
|
||||||
|
"",
|
||||||
|
"123!",
|
||||||
|
"1a:2b:3c",
|
||||||
|
"123,abc,123",
|
||||||
|
};
|
||||||
|
for (String ver : invalidVersions) {
|
||||||
|
try {
|
||||||
|
AlphaNumericVersion v = new AlphaNumericVersion(ver);
|
||||||
|
} catch (Exception e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fail("Alpha-numeric version MUST consist of dot separated identifiers [0-9A-Za-z-]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustCompareEachIdentifierSeparately() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("beta.2.abc");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("beta.1.edf");
|
||||||
|
assertTrue(0 < v1.compareTo(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCompareIdentifiersCountIfCommonIdentifiersAreEqual() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("beta.abc");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("beta.abc.def");
|
||||||
|
assertTrue(0 > v1.compareTo(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldComapareDigitsOnlyIdentifiersNumerically() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.321");
|
||||||
|
assertTrue(0 > v1.compareTo(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCompareMixedIdentifiersLexicallyInAsciiSortOrder() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("beta.abc");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("beta.111");
|
||||||
|
assertTrue(0 < v1.compareTo(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldOverrideEqualsMethod() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v3 = new AlphaNumericVersion("alpha.321");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertFalse(v1.equals(v3));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EqualsMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeReflexive() {
|
||||||
|
AlphaNumericVersion v = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertTrue(v.equals(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeSymmetric() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeTransitive() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v3 = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v3));
|
||||||
|
assertTrue(v1.equals(v3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeConsistent() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsOfDifferentType() {
|
||||||
|
AlphaNumericVersion v = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertFalse(v.equals(new String("alpha.123")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsNull() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = null;
|
||||||
|
assertFalse(v1.equals(v2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class HashCodeMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSameHashCodeIfVersionsAreEqual() {
|
||||||
|
AlphaNumericVersion v1 = new AlphaNumericVersion("alpha.123");
|
||||||
|
AlphaNumericVersion v2 = new AlphaNumericVersion("alpha.123");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertEquals(v1.hashCode(), v2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ToStringMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStringRepresentation() {
|
||||||
|
String value = "beta.abc.def";
|
||||||
|
AlphaNumericVersion v = new AlphaNumericVersion(value);
|
||||||
|
assertEquals(value, v.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,195 @@
|
||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright 2013 Zafar Khaja <zafarkhaja@gmail.com>.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.runners.Enclosed;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
||||||
|
*/
|
||||||
|
@RunWith(Enclosed.class)
|
||||||
|
public class NormalVersionTest {
|
||||||
|
|
||||||
|
public static class CoreFunctionalityTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustConsistOfMajorMinorAndPatchVersions() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertEquals(1, v.getMajor());
|
||||||
|
assertEquals(2, v.getMinor());
|
||||||
|
assertEquals(3, v.getPatch());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertEquals("1.2.3", v.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAcceptOnlyNonNegativeMajorMinorAndPatchVersions() {
|
||||||
|
int[][] invalidVersions = {{-1, 2, 3}, {1, -2, 3}, {1, 2, -3}};
|
||||||
|
for (int[] versionParts : invalidVersions) {
|
||||||
|
try {
|
||||||
|
NormalVersion v = new NormalVersion(
|
||||||
|
versionParts[0],
|
||||||
|
versionParts[1],
|
||||||
|
versionParts[2]
|
||||||
|
);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fail("Major, minor and patch versions MUST be non-negative integers.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustIncreaseEachElementNumericallyByIncrementsOfOne() {
|
||||||
|
int major = 1, minor = 2, patch = 3;
|
||||||
|
NormalVersion v = new NormalVersion(major, minor, patch);
|
||||||
|
v.incrementPatch();
|
||||||
|
assertEquals(patch + 1, v.getPatch());
|
||||||
|
v.incrementMinor();
|
||||||
|
assertEquals(minor + 1, v.getMinor());
|
||||||
|
v.incrementMajor();
|
||||||
|
assertEquals(major + 1, v.getMajor());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustResetMinorAndPatchToZeroWhenMajorIsIncremented() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
v.incrementMajor();
|
||||||
|
assertEquals(2, v.getMajor());
|
||||||
|
assertEquals(0, v.getMinor());
|
||||||
|
assertEquals(0, v.getPatch());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustResetPatchToZeroWhenMinorIsIncremented() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
v.incrementMinor();
|
||||||
|
assertEquals(1, v.getMajor());
|
||||||
|
assertEquals(3, v.getMinor());
|
||||||
|
assertEquals(0, v.getPatch());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mustCompareMajorMinorAndPatchNumerically() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(0 < v.compareTo(new NormalVersion(0, 2, 3)));
|
||||||
|
assertTrue(0 == v.compareTo(new NormalVersion(1, 2, 3)));
|
||||||
|
assertTrue(0 > v.compareTo(new NormalVersion(1, 2, 4)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldOverrideEqualsMethod() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v3 = new NormalVersion(3, 2, 1);
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertFalse(v1.equals(v3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHaveStaticFactoryMethod() {
|
||||||
|
NormalVersion v = NormalVersion.valueOf("1.2.3");
|
||||||
|
assertEquals(1, v.getMajor());
|
||||||
|
assertEquals(2, v.getMinor());
|
||||||
|
assertEquals(3, v.getPatch());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EqualsMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeReflexive() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(v.equals(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeSymmetric() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeTransitive() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v3 = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v3));
|
||||||
|
assertTrue(v1.equals(v3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeConsistent() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsOfDifferentType() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertFalse(v.equals(new String("1.2.3")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsNull() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = null;
|
||||||
|
assertFalse(v1.equals(v2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class HashCodeMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSameHashCodeIfVersionsAreEqual() {
|
||||||
|
NormalVersion v1 = new NormalVersion(1, 2, 3);
|
||||||
|
NormalVersion v2 = new NormalVersion(1, 2, 3);
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertEquals(v1.hashCode(), v2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ToStringMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStringRepresentation() {
|
||||||
|
NormalVersion v = new NormalVersion(1, 2, 3);
|
||||||
|
assertEquals("1.2.3", v.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,269 +21,106 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.github.zafarkhaja.semver;
|
package com.github.zafarkhaja.semver;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.experimental.runners.Enclosed;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
* @author Zafar Khaja <zafarkhaja@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
@RunWith(Enclosed.class)
|
||||||
public class VersionTest {
|
public class VersionTest {
|
||||||
|
|
||||||
@Test public void
|
public static class CoreFunctionalityTest {
|
||||||
mustConsistOfMajorMinorAndPatchVersions() {
|
|
||||||
Version version = new Version("1.2.3");
|
@Test
|
||||||
assertNotNull(version.getMajorVersion());
|
public void mayHavePreReleaseFollowingPatchAppendedWithHyphen() {
|
||||||
assertNotNull(version.getMinorVersion());
|
Version v = Version.valueOf("1.2.3-alpha");
|
||||||
assertNotNull(version.getPatchVersion());
|
assertEquals("alpha", v.getPreReleaseVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mustTakeTheFormOfXDotYDotZWhereXyzAreNonNegativeIntegers() {
|
public void preReleaseShouldHaveLowerPrecedenceThanAssociatedNormal() {
|
||||||
Version version = new Version("1.2.3");
|
Version v1 = Version.valueOf("1.3.7");
|
||||||
assertEquals(1, version.getMajorVersion());
|
Version v2 = Version.valueOf("1.3.7-alpha");
|
||||||
assertEquals(2, version.getMinorVersion());
|
assertTrue(0 < v1.compareTo(v2));
|
||||||
assertEquals(3, version.getPatchVersion());
|
assertTrue(0 > v2.compareTo(v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
shouldAcceptOnlyNonNegativeMajorMinorAndPatchVersions() {
|
public void mayHaveBuildFollowingPatchOrPreReleaseAppendedWithPlus() {
|
||||||
String[] versionStrings = {"-1.2.3", "1.-2.3", "1.2.-3"};
|
Version v = Version.valueOf("1.2.3+build");
|
||||||
for (String illegalVersion : versionStrings) {
|
assertEquals("build", v.getBuildVersion());
|
||||||
try {
|
|
||||||
Version version = new Version(illegalVersion);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
fail("An expected exception has not been thrown.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mustIncreaseEachElementNumericallyByIncrementsOfOne() {
|
public void shouldCompareBuildIfNormalAndPreReleaseAreEqual() {
|
||||||
Version version = new Version("1.2.3");
|
Version v1 = Version.valueOf("1.3.7-beta+build.1");
|
||||||
version.bumpPatchVersion();
|
Version v2 = Version.valueOf("1.3.7-beta+build.2");
|
||||||
assertEquals(4, version.getPatchVersion());
|
assertTrue(0 > v1.compareTo(v2));
|
||||||
version.bumpMinorVersion();
|
|
||||||
assertEquals(3, version.getMinorVersion());
|
|
||||||
version.bumpMajorVersion();
|
|
||||||
assertEquals(2, version.getMajorVersion());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mustResetToZeroMinorAndPatchVersionsWhenMajorVersionIsIncremented() {
|
public void buildShouldHaveHigherPrecedenceThanAssociatedNormal() {
|
||||||
Version version = new Version("1.2.3");
|
Version v1 = Version.valueOf("1.3.7");
|
||||||
version.bumpMajorVersion();
|
Version v2 = Version.valueOf("1.3.7+build");
|
||||||
assertEquals(2, version.getMajorVersion());
|
assertTrue(0 > v1.compareTo(v2));
|
||||||
assertEquals(0, version.getMinorVersion());
|
assertTrue(0 < v2.compareTo(v1));
|
||||||
assertEquals(0, version.getPatchVersion());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mustResetToZeroPatchVersionWhenMinorVersionIsIncremented() {
|
public void shouldHaveGreaterThanMethodReturningBoolean() {
|
||||||
Version version = new Version("1.2.3");
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
version.bumpMinorVersion();
|
Version v2 = Version.valueOf("1.3.7");
|
||||||
assertEquals(1, version.getMajorVersion());
|
assertTrue(v1.greaterThan(v2));
|
||||||
assertEquals(3, version.getMinorVersion());
|
assertFalse(v2.greaterThan(v1));
|
||||||
assertEquals(0, version.getPatchVersion());
|
assertFalse(v1.greaterThan(v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mayHavePreReleaseVersionFollowingPatchVersionAppendedWithDash() {
|
public void shouldHaveGreaterThanOrEqualsToMethodReturningBoolean() {
|
||||||
Version version = new Version("1.2.3-alpha");
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
assertEquals("alpha", version.getPreReleaseVersion());
|
Version v2 = Version.valueOf("1.3.7");
|
||||||
|
assertTrue(v1.greaterThanOrEqualsTo(v2));
|
||||||
|
assertFalse(v2.greaterThanOrEqualsTo(v1));
|
||||||
|
assertTrue(v1.greaterThanOrEqualsTo(v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
preReleaseVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() {
|
public void shouldHaveLessThanMethodReturningBoolean() {
|
||||||
Version version = new Version("1.0.0-x.7.z.92");
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
assertEquals("x.7.z.92", version.getPreReleaseVersion());
|
Version v2 = Version.valueOf("1.3.7");
|
||||||
|
assertFalse(v1.lessThan(v2));
|
||||||
|
assertTrue(v2.lessThan(v1));
|
||||||
|
assertFalse(v1.lessThan(v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mayHaveBuildVersionFollowingPatchOrPreReleaseVersionsAppendedWithPlus() {
|
public void shouldHaveLessThanOrEqualsToMethodReturningBoolean() {
|
||||||
Version version = new Version("1.2.3+build");
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
assertEquals("build", version.getBuildVersion());
|
Version v2 = Version.valueOf("1.3.7");
|
||||||
|
assertFalse(v1.lessThanOrEqualsTo(v2));
|
||||||
|
assertTrue(v2.lessThanOrEqualsTo(v1));
|
||||||
|
assertTrue(v1.lessThanOrEqualsTo(v1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
buildVersionMustCompriseDotSeparatedIdentifiersOfAlphaNumericsAndDash() {
|
public void shouldOverrideEqualsMethod() {
|
||||||
Version version = new Version("1.3.7+build.11.e0f985a");
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
assertEquals("build.11.e0f985a", version.getBuildVersion());
|
Version v2 = Version.valueOf("2.3.7");
|
||||||
|
Version v3 = Version.valueOf("1.3.7");
|
||||||
|
assertTrue(v1.equals(v1));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertFalse(v1.equals(v3));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void
|
@Test
|
||||||
mustCompareMajorMinorAndPatchVersionsNumerically() {
|
public void shouldCorrectlyCompareAllVersionsFromSpecification() {
|
||||||
Version version = new Version("1.2.3");
|
String[] versions = {
|
||||||
assertEquals(1, version.compareTo(new Version("0.2.3")));
|
|
||||||
assertEquals(0, version.compareTo(new Version("1.2.3")));
|
|
||||||
assertEquals(-1, version.compareTo(new Version("1.2.4")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
mustComparePreReleaseAndBuildVersionsByComparingEachIdentifierSeparately() {
|
|
||||||
Version version1 = new Version("1.3.7+build.2.b8f12d7");
|
|
||||||
Version version2 = new Version("1.3.7+build.11.e0f985a");
|
|
||||||
assertEquals(-1, version1.compareTo(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldComparePreReleaseVersionsIfNormalVersionsAreEqual() {
|
|
||||||
Version version1 = new Version("1.3.7-beta");
|
|
||||||
Version version2 = new Version("1.3.7-alpha");
|
|
||||||
assertEquals(1, version1.compareTo(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
preReleaseVersionShouldHaveLowerPrecedenceThanAssociatedNormalVersion() {
|
|
||||||
Version version1 = new Version("1.3.7");
|
|
||||||
Version version2 = new Version("1.3.7-alpha");
|
|
||||||
assertEquals(1, version1.compareTo(version2));
|
|
||||||
assertEquals(-1, version2.compareTo(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldCompareBuildVersionsIfNormalAndPreReleaseVersionsAreEqual() {
|
|
||||||
Version version1 = new Version("1.3.7-beta+build.1");
|
|
||||||
Version version2 = new Version("1.3.7-beta+build.2");
|
|
||||||
assertEquals(-1, version1.compareTo(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
buildVersionShouldHaveHigherPrecedenceThanAssociatedNormalVersion() {
|
|
||||||
Version version1 = new Version("1.3.7");
|
|
||||||
Version version2 = new Version("1.3.7+build");
|
|
||||||
assertEquals(-1, version1.compareTo(version2));
|
|
||||||
assertEquals(1, version2.compareTo(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldCompareAccordingToIdentifiersCountIfCommonIdentifiersAreEqual() {
|
|
||||||
Version version1 = new Version("1.3.7-beta+build.3");
|
|
||||||
Version version2 = new Version("1.3.7-beta+build");
|
|
||||||
assertEquals(1, version1.compareTo(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
numericIdentifiersShouldHaveLowerPrecedenceThanNonNumericIdentfiers() {
|
|
||||||
Version version1 = new Version("1.3.7-beta+build.3");
|
|
||||||
Version version2 = new Version("1.3.7-beta+build.a");
|
|
||||||
assertEquals(-1, version1.compareTo(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldHaveGreaterThanConvenienceMethodReturningBoolean() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("1.3.7");
|
|
||||||
assertTrue(version1.greaterThan(version2));
|
|
||||||
assertFalse(version2.greaterThan(version1));
|
|
||||||
assertFalse(version1.greaterThan(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldHaveGreaterThanOrEqualsToConvenienceMethodReturningBoolean() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("1.3.7");
|
|
||||||
assertTrue(version1.greaterThanOrEqualsTo(version2));
|
|
||||||
assertFalse(version2.greaterThanOrEqualsTo(version1));
|
|
||||||
assertTrue(version1.greaterThanOrEqualsTo(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldHaveLessThanConvenienceMethodReturningBoolean() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("1.3.7");
|
|
||||||
assertFalse(version1.lessThan(version2));
|
|
||||||
assertTrue(version2.lessThan(version1));
|
|
||||||
assertFalse(version1.lessThan(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldHaveLessThanOrEqualsToConvenienceMethodReturningBoolean() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("1.3.7");
|
|
||||||
assertFalse(version1.lessThanOrEqualsTo(version2));
|
|
||||||
assertTrue(version2.lessThanOrEqualsTo(version1));
|
|
||||||
assertTrue(version1.lessThanOrEqualsTo(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldOverrideEqualsMethodForConvenience() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("2.3.7");
|
|
||||||
Version version3 = new Version("1.3.7");
|
|
||||||
assertTrue(version1.equals(version1));
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertFalse(version1.equals(version3));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldBeReflexive() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
assertTrue(version1.equals(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldBeSymmetric() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("2.3.7");
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertTrue(version2.equals(version1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldBeTransitive() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("2.3.7");
|
|
||||||
Version version3 = new Version("2.3.7");
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertTrue(version2.equals(version3));
|
|
||||||
assertTrue(version1.equals(version3));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldBeConsistent() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("2.3.7");
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldReturnFalseIfOtherVersionIsNotInstanceOfVersion() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
assertFalse(version1.equals(new String("2.3.7")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
equalsMethodShouldReturnFalseIfOtherVersionIsNull() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = null;
|
|
||||||
assertFalse(version1.equals(version2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
hashCodeMethodShouldReturnSameHashCodeIfVersionsAreEqual() {
|
|
||||||
Version version1 = new Version("2.3.7");
|
|
||||||
Version version2 = new Version("2.3.7");
|
|
||||||
assertTrue(version1.equals(version2));
|
|
||||||
assertEquals(version1.hashCode(), version2.hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldOverrideToStringMethod() {
|
|
||||||
String versionString = "1.2.3-beta+build";
|
|
||||||
Version version = new Version(versionString);
|
|
||||||
assertEquals(versionString, version.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void
|
|
||||||
shouldCorrectlyCompareAllVersionsFromSpecification() {
|
|
||||||
String[] versionStrings = {
|
|
||||||
"1.0.0-alpha",
|
"1.0.0-alpha",
|
||||||
"1.0.0-alpha.1",
|
"1.0.0-alpha.1",
|
||||||
"1.0.0-beta.2",
|
"1.0.0-beta.2",
|
||||||
|
@ -296,10 +133,92 @@ public class VersionTest {
|
||||||
"1.3.7+build.2.b8f12d7",
|
"1.3.7+build.2.b8f12d7",
|
||||||
"1.3.7+build.11.e0f985a"
|
"1.3.7+build.11.e0f985a"
|
||||||
};
|
};
|
||||||
for (int i = 1; i < versionStrings.length; i++) {
|
for (int i = 1; i < versions.length; i++) {
|
||||||
Version version1 = new Version(versionStrings[i-1]);
|
Version v1 = Version.valueOf(versions[i-1]);
|
||||||
Version version2 = new Version(versionStrings[i]);
|
Version v2 = Version.valueOf(versions[i]);
|
||||||
assertTrue(version1.lessThan(version2));
|
assertTrue(v1.lessThan(v2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHaveStaticFactoryMethod() {
|
||||||
|
Version v = Version.valueOf("1.0.0-rc.1+build.1");
|
||||||
|
assertEquals(1, v.getMajorVersion());
|
||||||
|
assertEquals(0, v.getMinorVersion());
|
||||||
|
assertEquals(0, v.getPatchVersion());
|
||||||
|
assertEquals("1.0.0", v.getNormalVersion());
|
||||||
|
assertEquals("rc.1", v.getPreReleaseVersion());
|
||||||
|
assertEquals("build.1", v.getBuildVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EqualsMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeReflexive() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
assertTrue(v1.equals(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeSymmetric() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
Version v2 = Version.valueOf("2.3.7");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeTransitive() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
Version v2 = Version.valueOf("2.3.7");
|
||||||
|
Version v3 = Version.valueOf("2.3.7");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v2.equals(v3));
|
||||||
|
assertTrue(v1.equals(v3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldBeConsistent() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
Version v2 = Version.valueOf("2.3.7");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsOfDifferentType() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
assertFalse(v1.equals(new String("2.3.7")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfOtherVersionIsNull() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
Version v2 = null;
|
||||||
|
assertFalse(v1.equals(v2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class HashCodeMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSameHashCodeIfVersionsAreEqual() {
|
||||||
|
Version v1 = Version.valueOf("2.3.7");
|
||||||
|
Version v2 = Version.valueOf("2.3.7");
|
||||||
|
assertTrue(v1.equals(v2));
|
||||||
|
assertEquals(v1.hashCode(), v2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ToStringMethodTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnStringRepresentation() {
|
||||||
|
String value = "1.2.3-beta+build";
|
||||||
|
Version v = Version.valueOf(value);
|
||||||
|
assertEquals(value, v.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user