Skip to content
Permalink
Browse files
Adding tests for VersionComparator.
Fixes JENKINS-26701.
  • Loading branch information
Radek Antoniuk committed Oct 8, 2015
1 parent 06fe2a2 commit dec02b839246bbe9a8da894ac27acfc9b9ea8dde
Showing 3 changed files with 107 additions and 6 deletions.
12 pom.xml
@@ -254,6 +254,18 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.mifmif</groupId>
<artifactId>generex</artifactId>
<version>0.0.4</version>
<scope>test</scope>
</dependency>

</dependencies>

@@ -21,7 +21,8 @@ public class VersionComparator implements Comparator<Version> {

public int compare(Version rev1, Version rev2) {
int result = 0;
boolean startWithoutLetters = true;
boolean rev1WithoutLetters = true;
boolean rev2WithoutLetters = true;

List<String> listRev1 = Arrays.asList(rev1.getName().split("\\."));
String oldRev1 = listRev1.get(0);
@@ -32,9 +33,13 @@ public int compare(Version rev1, Version rev2) {
listRev1.set(0, getNumberVersion(listRev1.get(0)));
listRev2.set(0, getNumberVersion(listRev2.get(0)));

if (oldRev1.equals(listRev1.get(0)) && oldRev2.equals(listRev2.get(0))) {
startWithoutLetters = false;
if (oldRev1.equals(listRev1.get(0))) {
rev1WithoutLetters = false;
}
if (oldRev2.equals(listRev2.get(0))) {
rev2WithoutLetters = false;
}


int lenRev1 = listRev1.size();
int lenRev2 = listRev2.size();
@@ -58,10 +63,15 @@ public int compare(Version rev1, Version rev2) {
if (result == 0) {
if (lenRev1 > lenRev2) {
result = -1;
} else if (lenRev2 > lenRev1) {
result = 1;
} else if (startWithoutLetters) {
} else if (lenRev1 < lenRev2) {
result = 1;
} else {
if (rev1WithoutLetters && ! rev2WithoutLetters)
result = -1;
else if (!rev1WithoutLetters && rev2WithoutLetters)
result = 1;
else
result = 0;
}

}
@@ -0,0 +1,79 @@
package hudson.plugins.jira.versionparameter

import com.atlassian.jira.rest.client.api.domain.Version
import spock.lang.*

class VersionComparatorTest extends Specification {

void testComplexCompare() {
def input = [
"9.9.9.9.9",
"V-5.2.3",
"PDFREPORT-2.3.4",
"PDFREPORT-2.3",
"1.12.2.3.4",
"1.3.4",
"1.1.1.2",
"1.1.1.1"
]
def expected = [
"1.1.1.1",
"1.1.1.2",
"1.3.4",
"1.12.2.3.4",
"9.9.9.9.9",
"PDFREPORT-2.3",
"PDFREPORT-2.3.4",
"V-5.2.3",
]

SortedSet<Version> sorted = new TreeSet<Version>();
sorted.addAll(input);

assert sorted == expected;
}

def singleComparisonsTests() {

given:
def comparator = new VersionComparator();

when:
def o1 = new Version(null, null, v1, null, v1arch, v1rel, null)
def o2 = new Version(null, null, v2, null, v2arch, v2rel, null)

then:
comparator.compare(o1, o2) == expected

where:
v1 | v1rel | v1arch | v2 | v2rel | v2arch | expected
"1.1.1.1" | false | false | "1.1.1.1" | false | false | 0
"A-1.1.1.1" | false | false | "1.1.1.1" | false | false | -1
"1.1.1.1" | false | false | "A-1.1.1.1" | false | false | 1
"1.1.1.1" | false | false | "1.1.1.1.1" | false | false | 1
"1.1.1.1" | false | false | "1.1.1" | false | false | -1
"1.1.1.2" | false | false | "1.1.1.3" | false | false | 1
"2.2.2.1" | false | false | "1.1.1.2" | false | false | -1
"2.2.2" | false | false | "1.1.1.2" | false | false | -1
"2.0" | false | false | "1.0.15.3" | false | false | -1
"2.0.5.4" | false | false | "4.0" | false | false | 1
"1.12.1.1" | false | false | "1.1.1.2" | false | false | -1
"PDFREPORT-2.3.4" | false | false | "1.2.3" | false | false | -1
"PDFREPORT-2.3.4" | false | false | "4.5.6" | false | false | 1
"PDFREPORT-2.3.4" | false | false | "x" | false | false | -1 //exception
}

def getNumberVersionTest() {
given:
def comp = new VersionComparator();

expect:
comp.getNumberVersion(v) == expect

where:
v | expect
"PDFREPORT-2.3.4" | "2.3.4"

}

}

0 comments on commit dec02b8

Please sign in to comment.