Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-51594] Test behavior of snapshots vs. RCs, JEP-229 #6

Merged
merged 9 commits into from
Apr 14, 2021
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>test-annotations</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/hudson/util/VersionNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
* <p>
* 'SNAPSHOT' is also allowed as a component, and "N.SNAPSHOT" is interpreted as "N-1.*"
*
* <pre>
* 2.0.* &lt; 2.0.1 &lt; 2.0.1-SNAPSHOT &lt; 2.0.0.99 &lt; 2.0.0 &lt; 2.0.ea &lt; 2.0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just so far off that I did not see how to rescue it. 2.0.1 < 2.0??

* </pre>
*
* This class is re-implemented in 1.415. The class was originally introduced in 1.139
*
* @since 1.139
Expand Down Expand Up @@ -180,7 +176,7 @@ public String toString() {
* Represents a string in the version item list, usually a qualifier.
*/
private static class StringItem implements Item {
private final static String[] QUALIFIERS = {"snapshot", "alpha", "beta", "milestone", "rc", "", "sp"};
private final static String[] QUALIFIERS = {"alpha", "beta", "milestone", "rc", "snapshot", "", "sp"};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically from Maven's PoV -SNAPSHOT happens before -alpha-SNAPSHOT which happens before the release.

IOW your version starts off as 1.1-SNAPSHOT, then goes to 1.1-alpha-1-SNAPSHOT, 1.1-alpha-1, 1.1-alpha-2-SNAPSHOT, 1.1-alpha-2, 1.1-beta-1-SNAPSHOT, 1.1-beta-1, 1.1-rc-1-SNAPSHOT, 1.1-rc-1, ..., 1.1, 1.1-sp-1-SNAPSHOT, 1.1-sp-1, 1.1-sp-2-SNAPSHOT

So if you want to stay consistent with Maven's version handling, this is not the change you want

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm…I am wondering whether we should just be making this library a shim for ComparableVersion. It is not entirely clear to me why it differs at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some "magic" that KK had for * as a version component

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw a comment to that effect…but is this “feature” actually being used anywhere that you know of?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know not of any usage of that...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm milestones are usually pre-alpha thus milestone -> alpha -> beta -> rc -> ga but yeah maven ¯_(ツ)_/¯

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private final static List<String> _QUALIFIERS = Arrays.asList(QUALIFIERS);

Expand Down
63 changes: 53 additions & 10 deletions src/test/java/hudson/util/VersionNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
*/
package hudson.util;

import junit.framework.TestCase;
import java.util.Arrays;
import org.apache.maven.artifact.versioning.ComparableVersion;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author Xavier Le Vourch
*/
public class VersionNumberTest extends TestCase {
public class VersionNumberTest {

public void testIsNewerThan() {
@Test
public void isNewerThan() {
assertTrue(new VersionNumber("2.0.*").isNewerThan(new VersionNumber("2.0")));
assertTrue(new VersionNumber("2.1-SNAPSHOT").isNewerThan(new VersionNumber("2.0.*")));
assertTrue(new VersionNumber("2.1").isNewerThan(new VersionNumber("2.1-SNAPSHOT")));
Expand All @@ -44,23 +47,34 @@ public void testIsNewerThan() {
// which makes more sense than before
assertTrue(new VersionNumber("2.0.0").equals(new VersionNumber("2.0")));
}

@Issue("https://gitter.im/jenkinsci/configuration-as-code-plugin?at=5b4f2fc455a7e23c014da2af")
@Test
public void alpha() {
assertTrue(new VersionNumber("2.0").isNewerThan(new VersionNumber("2.0-alpha-1")));
assertTrue(new VersionNumber("2.0-alpha-1").isNewerThan(new VersionNumber("2.0-alpha-1-rc9999.abc123def456")));
}

public void testEarlyAccess() {
@Test
public void earlyAccess() {
assertTrue(new VersionNumber("2.0.ea2").isNewerThan(new VersionNumber("2.0.ea1")));
assertTrue(new VersionNumber("2.0.ea1").isNewerThan(new VersionNumber("2.0.ea")));
assertEquals(new VersionNumber("2.0.ea"), new VersionNumber("2.0.ea0"));
}

public void testSnapshots() {
@Test
public void snapshots() {
assertTrue(new VersionNumber("1.12").isNewerThan(new VersionNumber("1.12-SNAPSHOT (private-08/24/2008 12:13-hudson)")));
assertTrue(new VersionNumber("1.12-SNAPSHOT (private-08/24/2008 12:13-hudson)").isNewerThan(new VersionNumber("1.11")));
assertTrue(new VersionNumber("1.12-SNAPSHOT (private-08/24/2008 12:13-hudson)").equals(new VersionNumber("1.12-SNAPSHOT")));
// This is changed from the old impl because snapshots are no longer a "magic" number
assertFalse(new VersionNumber("1.12-SNAPSHOT").equals(new VersionNumber("1.11.*")));
assertTrue(new VersionNumber("1.11.*").isNewerThan(new VersionNumber("1.11.9")));
assertTrue(new VersionNumber("1.12-SNAPSHOT").isNewerThan(new VersionNumber("1.12-rc9999.abc123def456")));
}

public void testTimestamps() {
@Test
public void timestamps() {
assertTrue(new VersionNumber("2.0.3-20170207.105042-1").isNewerThan(new VersionNumber("2.0.2")));
assertTrue(new VersionNumber("2.0.3").isNewerThan(new VersionNumber("2.0.3-20170207.105042-1")));
assertTrue(new VersionNumber("2.0.3-20170207.105042-1").equals(new VersionNumber("2.0.3-SNAPSHOT")));
Expand All @@ -72,7 +86,8 @@ public void testTimestamps() {
assertFalse(new VersionNumber("2.0.3-20170207.105042-1").isOlderThan(new VersionNumber("2.0.3-SNAPSHOT")));
}

public void testDigit() {
@Test
public void digit() {
assertEquals(32, new VersionNumber("2.32.3.1-SNAPSHOT").getDigitAt(1));
assertEquals(3, new VersionNumber("2.32.3.1-SNAPSHOT").getDigitAt(2));
assertEquals(1, new VersionNumber("2.32.3.1-SNAPSHOT").getDigitAt(3));
Expand All @@ -88,6 +103,33 @@ public void testDigit() {
assertEquals(-1, new VersionNumber("").getDigitAt(0));
}

@Ignore("TODO still pretty divergent: expected:<[2.0.0, 2.0, 2.0.*, 2.0.ea, 2.0.0.99, 2.0.1-alpha-1-rc9999.abc123def456, 2.0.1-alpha-1, 2.0.1-rc9999.abc123def456, 2.0.1-SNAPSHOT, 2.0.1]> but was:<[2.0.ea, 2.0.0, 2.0, 2.0.1-alpha-1-rc9999.abc123def456, 2.0.1-alpha-1, 2.0.1-rc9999.abc123def456, 2.0.1-SNAPSHOT, 2.0.*, 2.0.0.99, 2.0.1]>")
@Issue("JENKINS-51594")
@Test
public void mavenComparison() {
String[] versions = {"2.0.*", "2.0.1", "2.0.1-SNAPSHOT", "2.0.0.99", "2.0.0", "2.0.ea", "2.0", "2.0.1-rc9999.abc123def456", "2.0.1-alpha-1-rc9999.abc123def456", "2.0.1-alpha-1"};
// Much more easily expressed in java.level=8:
ComparableVersion[] control = new ComparableVersion[versions.length];
for (int i = 0; i < versions.length; i++) {
control[i] = new ComparableVersion(versions[i]);
}
Arrays.sort(control);
String[] expected = new String[versions.length];
for (int i = 0; i < versions.length; i++) {
expected[i] = control[i].toString();
}
VersionNumber[] test = new VersionNumber[versions.length];
for (int i = 0; i < versions.length; i++) {
test[i] = new VersionNumber(versions[i]);
}
Arrays.sort(test);
String[] actual = new String[versions.length];
for (int i = 0; i < versions.length; i++) {
actual[i] = test[i].toString();
}
assertEquals(Arrays.asList(expected), Arrays.asList(actual));
}

public void testOrEqualTo() {
assertTrue(new VersionNumber("1.8").isNewerThanOrEqualTo(new VersionNumber("1.8")));
assertTrue(new VersionNumber("1.9").isNewerThanOrEqualTo(new VersionNumber("1.8")));
Expand All @@ -97,4 +139,5 @@ public void testOrEqualTo() {
assertTrue(new VersionNumber("1.7").isOlderThanOrEqualTo(new VersionNumber("1.8")));
assertTrue(new VersionNumber("1").isOlderThanOrEqualTo(new VersionNumber("1.8")));
}

}