Skip to content

Commit 344b791

Browse files
committed
Fixed #215
o Added JenkinsVersion which is capable of comparing different versions correctly. The implementation is taken from the Maven project which contains a class ComparableVersion which handled everything I need here.
1 parent c596050 commit 344b791

File tree

9 files changed

+827
-26
lines changed

9 files changed

+827
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ target
44
.classpath
55
.project
66
.settings
7+
test-output

ReleaseNotes.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@
1616

1717
### API Changes
1818

19+
* [Fixed Issue 215][issue-215]
20+
21+
The JenkinsServer class will return `JenkinsVersion` instead of String if you
22+
call `getJenkinsVersion()`.
23+
24+
```java
25+
public class JenkinsVersion ... {
26+
public boolean isGreaterThan(String version);
27+
public boolean isGreaterOrEqual(String version);
28+
public boolean isLessThan(String version);
29+
public boolean isLessOrEqual(String version);
30+
public boolean isEqualTo(String version);
31+
}
32+
```
33+
34+
The `JenkinsVersion` class can be used to compare different versions with
35+
each other.
36+
37+
```java
38+
JenkinsVersion jv = new JenkinsVersion("1.651.1");
39+
assertThat(jv.isGreaterThan("1.651.1")).isFalse();
40+
```
41+
42+
1943
* [Fixed issue 184][issue-184]
2044

2145
Based on the differences between getting a TestReport for a MavenJob type and
@@ -916,6 +940,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
916940
[issue-207]: https://github.com/jenkinsci/java-client-api/issues/207
917941
[issue-209]: https://github.com/jenkinsci/java-client-api/issues/209
918942
[issue-211]: https://github.com/jenkinsci/java-client-api/issues/211
943+
[issue-215]: https://github.com/jenkinsci/java-client-api/issues/215
919944
[pull-123]: https://github.com/jenkinsci/java-client-api/pull/123
920945
[pull-149]: https://github.com/jenkinsci/java-client-api/pull/149
921946
[pull-158]: https://github.com/jenkinsci/java-client-api/pull/158

jenkins-client-it-docker/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
</plugins>
8383
</build>
8484
<properties>
85-
<docker.container.network>http://192.168.99.100:8080</docker.container.network>
85+
<docker.container.network>http://127.0.0.1:8080</docker.container.network>
8686
</properties>
8787
<profiles>
8888
<profile>

jenkins-client-it-docker/src/test/java/com/offbytwo/jenkins/integration/NoExecutorStartedGetJobXmlIT.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,31 @@ public void beforeMethod() throws IOException {
1919
jobXml = jenkinsServer.getJobXml("test");
2020
}
2121

22-
private static final String[] CONFIG_XML = { "<?xml version='1.0' encoding='UTF-8'?>", "<project>", " <actions/>",
23-
" <description>This is the description with umlauts äöü</description>",
24-
" <keepDependencies>false</keepDependencies>", " <properties/>", " <scm class=\"hudson.scm.NullSCM\"/>",
25-
" <canRoam>true</canRoam>", " <disabled>false</disabled>",
26-
" <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>",
27-
" <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>", " <triggers/>",
28-
" <concurrentBuild>false</concurrentBuild>", " <builders>", " <hudson.tasks.Shell>",
29-
" <command>echo &quot;test&quot;</command>", " </hudson.tasks.Shell>", " </builders>",
30-
" <publishers/>", " <buildWrappers/>", "</project>" };
22+
//@formatter:off
23+
private static final String[] CONFIG_XML = {
24+
"<?xml version='1.0' encoding='UTF-8'?>",
25+
"<project>",
26+
" <actions/>",
27+
" <description>This is the description with umlauts äöü</description>",
28+
" <keepDependencies>false</keepDependencies>",
29+
" <properties/>",
30+
" <scm class=\"hudson.scm.NullSCM\"/>",
31+
" <canRoam>true</canRoam>",
32+
" <disabled>false</disabled>",
33+
" <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>",
34+
" <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>",
35+
" <triggers/>",
36+
" <concurrentBuild>false</concurrentBuild>",
37+
" <builders>",
38+
" <hudson.tasks.Shell>",
39+
" <command>echo &quot;test&quot;</command>",
40+
" </hudson.tasks.Shell>",
41+
" </builders>",
42+
" <publishers/>",
43+
" <buildWrappers/>",
44+
"</project>"
45+
};
46+
//@formatter:on
3147

3248
@Test
3349
public void getJobXmlShouldReturnTheExpectedConfigXml() {

jenkins-client-it-docker/src/test/java/com/offbytwo/jenkins/integration/NoExecutorStartedPluginManagerIT.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.io.IOException;
6-
import java.util.Arrays;
76
import java.util.List;
87

98
import org.testng.SkipException;
109
import org.testng.annotations.BeforeMethod;
1110
import org.testng.annotations.Test;
1211

12+
import com.offbytwo.jenkins.helper.JenkinsVersion;
1313
import com.offbytwo.jenkins.model.Plugin;
1414
import com.offbytwo.jenkins.model.PluginManager;
1515

@@ -26,18 +26,17 @@ public void beforeMethod() throws IOException {
2626
@Test
2727
public void getPluginsShouldReturn9ForJenkins20() {
2828
// TODO: Check why there is such a difference in the number of Plugins?
29-
if (!jenkinsServer.getVersion().equals("2.0")) {
30-
throw new SkipException("Not Version 2.0");
29+
if (jenkinsServer.getVersion().isLessThan("2.0")) {
30+
throw new SkipException("Not Version 2.0 (" + jenkinsServer.getVersion() + ")");
3131
}
3232
assertThat(pluginManager.getPlugins()).hasSize(9);
3333
}
3434

3535
@Test
3636
public void getPluginsShouldReturn27ForJenkins1651() {
37-
List<String> asList = Arrays.asList("1.651", "1.651.1", "1.651.2", "1.651.3");
38-
// TODO: Check why there is such a difference in the number of Plugins?
39-
if (!asList.contains(jenkinsServer.getVersion())) {
40-
throw new SkipException("Not Version 1.651");
37+
JenkinsVersion jv = jenkinsServer.getVersion();
38+
if (jv.isLessThan("1.651") && jv.isGreaterThan("1.651.3")) {
39+
throw new SkipException("Not Version 1.651 (" + jv.toString() + ")");
4140
}
4241
assertThat(pluginManager.getPlugins()).hasSize(27);
4342
}
@@ -52,8 +51,8 @@ private Plugin createPlugin(String shortName, String version) {
5251
@Test
5352
public void getPluginsShouldReturnTheListOfInstalledPluginsForJenkins20() {
5453

55-
if (!jenkinsServer.getVersion().equals("2.0")) {
56-
throw new SkipException("Not Version 2.0");
54+
if (jenkinsServer.getVersion().isLessThan("2.0")) {
55+
throw new SkipException("Not Version 2.0 (" + jenkinsServer.getVersion() + ")");
5756
}
5857

5958
// TODO: The list of plugins is contained in the plugin.txt
@@ -91,12 +90,12 @@ public void getPluginsShouldReturnTheListOfInstalledPluginsForJenkins20() {
9190

9291
@Test
9392
public void getPluginsShouldReturnTheListOfInstalledPluginsFor1651() {
94-
List<String> asList = Arrays.asList("1.651", "1.651.1", "1.651.2", "1.651.3");
93+
JenkinsVersion jv = jenkinsServer.getVersion();
94+
if (jv.isLessThan("1.651") && jv.isGreaterThan("1.651.3")) {
95+
throw new SkipException("Not Version 1.651 (" + jv + ")");
96+
}
9597

9698
// TODO: Check why there is such a difference in the number of Plugins?
97-
if (!asList.contains(jenkinsServer.getVersion())) {
98-
throw new SkipException("Not Version 1.651");
99-
}
10099
// TODO: The list of plugins is contained in the plugin.txt
101100
// which should be read and used as base of comparison.
102101
// instead of maintaining at two locations.

jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.collect.Maps;
2727
import com.offbytwo.jenkins.client.JenkinsHttpClient;
2828
import com.offbytwo.jenkins.client.util.EncodingUtils;
29+
import com.offbytwo.jenkins.helper.JenkinsVersion;
2930
import com.offbytwo.jenkins.model.Build;
3031
import com.offbytwo.jenkins.model.Computer;
3132
import com.offbytwo.jenkins.model.ComputerSet;
@@ -100,15 +101,16 @@ public boolean isRunning() {
100101
}
101102

102103
/**
103-
* @return The Jenkins version.
104+
* @return {@link JenkinsVersion}
104105
*/
105-
public String getVersion() {
106+
public JenkinsVersion getVersion() {
106107
if (client.getJenkinsVersion().isEmpty()) {
107108
// Force a request to get at least once
108109
// HttpHeader
109110
isRunning();
110111
}
111-
return client.getJenkinsVersion();
112+
JenkinsVersion jv = new JenkinsVersion(client.getJenkinsVersion());
113+
return jv;
112114
}
113115

114116
/**

0 commit comments

Comments
 (0)