Skip to content

Commit

Permalink
Merge branch 'master' into snapshots-JENKINS-51594
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 6, 2019
2 parents 0d14306 + 49bd478 commit 3edc207
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pipeline {
agent none
tools {
maven "mvn"
jdk 'jdk8'
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Parallel Stage') {
failFast true
parallel {
stage('Linux') {
agent {
label "linux"
}
steps {
sh 'mvn -V -B clean verify'
}
}
stage('Windows') {
agent {
label "windows"
}
steps {
bat 'mvn -V -B clean verify'
}
}
}
}
}
}
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.jenkins-ci</groupId>
<artifactId>jenkins</artifactId>
<version>1.46</version>
<version>1.50</version>
<relativePath />
</parent>

<artifactId>version-number</artifactId>
<version>1.5-SNAPSHOT</version>
<version>1.7-SNAPSHOT</version>
<name>Version number manipulation</name>

<dependencies>
Expand All @@ -28,6 +28,12 @@
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<scm>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/hudson/util/VersionNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public StringItem(String value, boolean followedByDigit) {
case 'm':
value = "milestone";
break;
default:
/* fall through? */
}
}
this.value = ALIASES.getProperty(value, value);
Expand Down Expand Up @@ -319,7 +321,16 @@ public int compareTo(Item item) {
Item r = right.hasNext() ? (Item) right.next() : null;

// if this is shorter, then invert the compare and mul with -1
int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r);
int result;
if (l == null) {
if (r == null) {
result = 0;
} else {
result = -1 * r.compareTo(null);
}
} else {
result = l.compareTo(r);
}

if (result != 0) {
return result;
Expand Down Expand Up @@ -509,6 +520,14 @@ public boolean isNewerThan(VersionNumber rhs) {
return compareTo(rhs) > 0;
}

public boolean isOlderThanOrEqualTo(VersionNumber rhs) {
return compareTo(rhs) <= 0;
}

public boolean isNewerThanOrEqualTo(VersionNumber rhs) {
return compareTo(rhs) >= 0;
}

/**
* @deprecated see {@link #getDigitAt(int)}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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 io.jenkins.lib.versionnumber;

import hudson.util.VersionNumber;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Java Version Specification.
* Implemented according to https://openjdk.java.net/jeps/223
* @author Oleg Nenashev
* @since 1.6
*/
public class JavaSpecificationVersion extends VersionNumber {

private static final String JAVA_SPEC_VERSION_PROPERTY_NAME = "java.specification.version";

public static final JavaSpecificationVersion JAVA_5 = new JavaSpecificationVersion("1.5");
public static final JavaSpecificationVersion JAVA_6 = new JavaSpecificationVersion("1.6");
public static final JavaSpecificationVersion JAVA_7 = new JavaSpecificationVersion("1.7");
public static final JavaSpecificationVersion JAVA_8 = new JavaSpecificationVersion("1.8");
public static final JavaSpecificationVersion JAVA_9 = new JavaSpecificationVersion("9");
public static final JavaSpecificationVersion JAVA_10 = new JavaSpecificationVersion("10");
public static final JavaSpecificationVersion JAVA_11 = new JavaSpecificationVersion("11");
public static final JavaSpecificationVersion JAVA_12 = new JavaSpecificationVersion("12");
public static final JavaSpecificationVersion JAVA_13 = new JavaSpecificationVersion("13");

/**
* Constructor which automatically normalizes version strings.
* @param version Java specification version, should follow JEP-223 or the previous format.
* @throws NumberFormatException Illegal Java specification version number
*/
public JavaSpecificationVersion(@Nonnull String version)
throws NumberFormatException {
super(normalizeVersion(version));
}

@Nonnull
private static String normalizeVersion(@Nonnull String input)
throws NumberFormatException {
input = input.trim();
if (input.startsWith("1.")) {
String[] split = input.split("\\.");
if (split.length != 2) {
throw new NumberFormatException("Malformed old Java Specification Version. " +
"There should be exactly one dot and something after it: " + input);
}
input = split[1];
}

int majorVersion = Integer.parseInt(input);
if (majorVersion > 8) {
return input;
} else {
return "1." + input;
}
}

/**
* Get the Java Specification version for the current JVM
* @return Java Specification version
* @throws NumberFormatException Version parsing error
* @throws IllegalStateException JVM does not specify the mandatory {@link #JAVA_SPEC_VERSION_PROPERTY_NAME} property.
*/
@Nonnull
public static JavaSpecificationVersion forCurrentJVM() throws NumberFormatException {
final String value = System.getProperty(JAVA_SPEC_VERSION_PROPERTY_NAME);
if (value == null) {
throw new IllegalStateException("Missing mandatory JVM system property: " + JAVA_SPEC_VERSION_PROPERTY_NAME);
}
return new JavaSpecificationVersion(value);
}
}
10 changes: 10 additions & 0 deletions src/test/java/hudson/util/VersionNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ public void mavenComparison() {
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")));
assertTrue(new VersionNumber("2").isNewerThanOrEqualTo(new VersionNumber("1.8")));

assertTrue(new VersionNumber("1.8").isOlderThanOrEqualTo(new VersionNumber("1.8")));
assertTrue(new VersionNumber("1.7").isOlderThanOrEqualTo(new VersionNumber("1.8")));
assertTrue(new VersionNumber("1").isOlderThanOrEqualTo(new VersionNumber("1.8")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* The MIT License
*
* Copyright (c) 2019 CloudBees, Inc.
*
* 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 io.jenkins.lib.versionnumber;

import org.junit.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.For;
import static io.jenkins.lib.versionnumber.JavaSpecificationVersion.*;

/**
* Tests for {@link JavaSpecificationVersion}.
*/
@For(JavaSpecificationVersion.class)
public class JavaSpecificationVersionTest {

@Test
public void shouldParseValidNumbersCorrectly() {
assertSpecEquals(JAVA_6, "1.6");
assertSpecEquals(JAVA_7, "1.7");
assertSpecEquals(JAVA_8, "1.8");
assertSpecEquals(JAVA_9, "9");
assertSpecEquals(JAVA_10, "10");
assertSpecEquals(JAVA_11, "11");
}

@Test
public void shouldParseOldSpecCorrectly() {
assertSpecEquals(JAVA_9, "1.9");
assertSpecEquals(JAVA_10, "1.10");
assertSpecEquals(JAVA_11, "1.11");
assertSpecEquals(JAVA_12, "1.12");
}

@Test
public void shouldResolveIncorrectSpecs() {
assertSpecEquals(JAVA_8, "8");
assertSpecEquals(JAVA_7, "7");
assertSpecEquals(JAVA_5, "5");
}

@Test
public void shouldCompareVersionsProperly() {
Assert.assertTrue(JAVA_5.isOlderThan(JAVA_6));
Assert.assertTrue(JAVA_6.isOlderThan(JAVA_7));
Assert.assertTrue(JAVA_7.isOlderThan(JAVA_8));
Assert.assertTrue(JAVA_8.isOlderThan(JAVA_9));
Assert.assertTrue(JAVA_8.isNewerThan(JAVA_7));
Assert.assertTrue(JAVA_9.isOlderThan(JAVA_10));
Assert.assertTrue(JAVA_10.isOlderThan(JAVA_11));
Assert.assertTrue(JAVA_10.isNewerThan(JAVA_8));
}

@Test
public void shouldRetrieveSpecVersionForTheCurrentJVM() {
System.out.println("Current JVM Specification Version: " + JavaSpecificationVersion.forCurrentJVM());
}

public void assertSpecEquals(JavaSpecificationVersion version, String value) {
JavaSpecificationVersion actualSpec = new JavaSpecificationVersion(value);
Assert.assertEquals("Wrong Java version", version, actualSpec);
}

public void assertOlder(JavaSpecificationVersion version1, JavaSpecificationVersion version2) {
Assert.assertTrue(String.format("Version %s should be older than %s", version1, version2),
version1.isOlderThan(version2));
}

public void assertNewer(JavaSpecificationVersion version1, JavaSpecificationVersion version2) {
Assert.assertTrue(String.format("Version %s should be newer than %s", version1, version2),
version1.isNewerThan(version2));
}
}

0 comments on commit 3edc207

Please sign in to comment.