test and fix for version number matching per issue #558

This commit is contained in:
Jeremy Long
2016-09-09 06:36:56 -04:00
parent 29d127303c
commit aef118d375
2 changed files with 40 additions and 16 deletions

View File

@@ -26,14 +26,15 @@ import org.apache.commons.lang3.StringUtils;
/** /**
* <p> * <p>
* Simple object to track the parts of a version number. The parts are contained in a List such that version 1.2.3 will * Simple object to track the parts of a version number. The parts are contained
* be stored as: <code>versionParts[0] = 1; * in a List such that version 1.2.3 will be stored as: <code>versionParts[0] = 1;
* versionParts[1] = 2; * versionParts[1] = 2;
* versionParts[2] = 3; * versionParts[2] = 3;
* </code></p> * </code></p>
* <p> * <p>
* Note, the parser contained in this class expects the version numbers to be separated by periods. If a different * Note, the parser contained in this class expects the version numbers to be
* separator is used the parser will likely fail.</p> * separated by periods. If a different separator is used the parser will likely
* fail.</p>
* *
* @author Jeremy Long * @author Jeremy Long
*/ */
@@ -47,8 +48,9 @@ public class DependencyVersion implements Iterable<String>, Comparable<Dependenc
/** /**
* Constructor for a DependencyVersion that will parse a version string. * Constructor for a DependencyVersion that will parse a version string.
* <b>Note</b>, this should only be used when the version passed in is already known to be a well formatted version * <b>Note</b>, this should only be used when the version passed in is
* number. Otherwise, DependencyVersionUtil.parseVersion() should be used instead. * already known to be a well formatted version number. Otherwise,
* DependencyVersionUtil.parseVersion() should be used instead.
* *
* @param version the well formatted version number to parse * @param version the well formatted version number to parse
*/ */
@@ -57,8 +59,9 @@ public class DependencyVersion implements Iterable<String>, Comparable<Dependenc
} }
/** /**
* Parses a version string into its sub parts: major, minor, revision, build, etc. <b>Note</b>, this should only be * Parses a version string into its sub parts: major, minor, revision,
* used to parse something that is already known to be a version number. * build, etc. <b>Note</b>, this should only be used to parse something that
* is already known to be a version number.
* *
* @param version the version string to parse * @param version the version string to parse
*/ */
@@ -133,26 +136,33 @@ public class DependencyVersion implements Iterable<String>, Comparable<Dependenc
return false; return false;
} }
final DependencyVersion other = (DependencyVersion) obj; final DependencyVersion other = (DependencyVersion) obj;
final int max = (this.versionParts.size() < other.versionParts.size()) final int minVersionMatchLength = (this.versionParts.size() < other.versionParts.size())
? this.versionParts.size() : other.versionParts.size(); ? this.versionParts.size() : other.versionParts.size();
final int maxVersionMatchLength = (this.versionParts.size() > other.versionParts.size())
? this.versionParts.size() : other.versionParts.size();
if (minVersionMatchLength==1 && maxVersionMatchLength>=3) {
return false;
}
//TODO steal better version of code from compareTo //TODO steal better version of code from compareTo
for (int i = 0; i < max; i++) { for (int i = 0; i < minVersionMatchLength; i++) {
final String thisPart = this.versionParts.get(i); final String thisPart = this.versionParts.get(i);
final String otherPart = other.versionParts.get(i); final String otherPart = other.versionParts.get(i);
if (!thisPart.equals(otherPart)) { if (!thisPart.equals(otherPart)) {
return false; return false;
} }
} }
if (this.versionParts.size() > max) { if (this.versionParts.size() > minVersionMatchLength) {
for (int i = max; i < this.versionParts.size(); i++) { for (int i = minVersionMatchLength; i < this.versionParts.size(); i++) {
if (!"0".equals(this.versionParts.get(i))) { if (!"0".equals(this.versionParts.get(i))) {
return false; return false;
} }
} }
} }
if (other.versionParts.size() > max) { if (other.versionParts.size() > minVersionMatchLength) {
for (int i = max; i < other.versionParts.size(); i++) { for (int i = minVersionMatchLength; i < other.versionParts.size(); i++) {
if (!"0".equals(other.versionParts.get(i))) { if (!"0".equals(other.versionParts.get(i))) {
return false; return false;
} }
@@ -180,8 +190,9 @@ public class DependencyVersion implements Iterable<String>, Comparable<Dependenc
} }
/** /**
* Determines if the three most major major version parts are identical. For instances, if version 1.2.3.4 was * Determines if the three most major major version parts are identical. For
* compared to 1.2.3 this function would return true. * instances, if version 1.2.3.4 was compared to 1.2.3 this function would
* return true.
* *
* @param version the version number to compare * @param version the version number to compare
* @return true if the first three major parts of the version are identical * @return true if the first three major parts of the version are identical

View File

@@ -96,6 +96,19 @@ public class DependencyVersionTest extends BaseTest {
expResult = true; expResult = true;
result = instance.equals(obj); result = instance.equals(obj);
assertEquals(expResult, result); assertEquals(expResult, result);
instance = new DependencyVersion("2.0.0");
obj = new DependencyVersion("2");
expResult = false;
result = instance.equals(obj);
assertEquals(expResult, result);
obj = new DependencyVersion("2.0");
expResult = true;
result = instance.equals(obj);
assertEquals(expResult, result);
} }
/** /**