Skip to content

Commit b2fa40b

Browse files
committed
Fixed jenkinsci#90
o Added getVersion() to JenkinsServer. o Fixed the appropriate IT's o Fixed Skipping of IT in MethodListener.
1 parent eb2acf1 commit b2fa40b

5 files changed

Lines changed: 115 additions & 5 deletions

File tree

ReleaseNotes.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
## Release 0.3.6 (NOT RELEASED YET)
44

5+
### General Changes
6+
7+
[Changed the structure and integrated Docker IT][issue-160]
8+
9+
### API Changes
10+
11+
[Get Jenkins Version from http header][issue-90]
12+
13+
```java
14+
public class JenkinsServer {
15+
public String getVersion();
16+
.
17+
}
18+
```
19+
20+
521
## Release 0.3.5
622

723
### API Changes
@@ -401,6 +417,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
401417
[issue-67]: https://github.com/RisingOak/jenkins-client/issues/67
402418
[issue-82]: https://github.com/RisingOak/jenkins-client/issues/82
403419
[issue-89]: https://github.com/RisingOak/jenkins-client/issues/89
420+
[issue-90]: https://github.com/RisingOak/jenkins-client/issues/90
404421
[issue-91]: https://github.com/RisingOak/jenkins-client/issues/91
405422
[issue-104]: https://github.com/RisingOak/jenkins-client/issues/104
406423
[issue-111]: https://github.com/RisingOak/jenkins-client/issues/111

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.testng.IInvokedMethodListener;
55
import org.testng.ITestNGMethod;
66
import org.testng.ITestResult;
7+
import org.testng.SkipException;
78

89
public class MethodListener
910
implements IInvokedMethodListener
@@ -51,7 +52,12 @@ public void afterInvocation( IInvokedMethod method, ITestResult testResult )
5152
}
5253
else
5354
{
54-
System.out.println( "FAILURE." );
55+
//Isn't there an more elegant way?
56+
if (testResult.getThrowable() instanceof SkipException) {
57+
System.out.println( "SKIPPED." );
58+
} else {
59+
System.out.println( "FAILURE." );
60+
}
5561
}
5662
}
5763
}

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.util.List;
77

8+
import org.testng.SkipException;
89
import org.testng.annotations.BeforeMethod;
910
import org.testng.annotations.Test;
1011

@@ -26,8 +27,19 @@ public void beforeMethod()
2627
}
2728

2829
@Test
29-
public void getPluginsShouldReturn26()
30+
public void getPluginsShouldReturn9ForJenkins20()
3031
{
32+
if (!jenkinsServer.getVersion().equals("2.0")) {
33+
throw new SkipException("Not Version 2.0");
34+
}
35+
assertThat( pluginManager.getPlugins() ).hasSize( 9 );
36+
}
37+
@Test
38+
public void getPluginsShouldReturn26ForJenkins1651()
39+
{
40+
if (!jenkinsServer.getVersion().equals("1.651")) {
41+
throw new SkipException("Not Version 1.651");
42+
}
3143
assertThat( pluginManager.getPlugins() ).hasSize( 26 );
3244
}
3345

@@ -39,8 +51,48 @@ private Plugin createPlugin(String shortName, String version) {
3951
}
4052

4153
@Test
42-
public void getPluginsShouldReturnTheListOfInstalledPlugins()
54+
public void getPluginsShouldReturnTheListOfInstalledPluginsForJenkins20() {
55+
56+
if (!jenkinsServer.getVersion().equals("2.0")) {
57+
throw new SkipException("Not Version 2.0");
58+
}
59+
60+
//TODO: The list of plugins is contained in the plugin.txt
61+
// which should be read and used as base of comparison.
62+
// instead of maintaining at two locations.
63+
Plugin[] expectedPlugins = {
64+
createPlugin("token-macro", "1.12.1"),
65+
createPlugin("testng-plugin", "1.10"),
66+
createPlugin("job-dsl", "1.41"),
67+
createPlugin("junit", "1.10"),
68+
createPlugin("jacoco", "1.0.19"),
69+
createPlugin("config-file-provider", "2.10.0"),
70+
createPlugin("timestamper", "1.7.2"),
71+
createPlugin("credentials", "1.24"),
72+
createPlugin("throttle-concurrents", "1.8.4"),
73+
};
74+
List<Plugin> plugins = pluginManager.getPlugins();
75+
76+
for ( Plugin plugin : plugins )
77+
{
78+
boolean found = false;
79+
for ( int i = 0; i < expectedPlugins.length; i++ )
80+
{
81+
if (plugin.getShortName().equals( expectedPlugins[i].getShortName()) &&
82+
plugin.getVersion().equals( expectedPlugins[i].getVersion())) {
83+
found = true;
84+
}
85+
}
86+
assertThat( found ).isTrue().as("Plugin shortName:{} version:{} couldn't be found.", plugin.getShortName(), plugin.getVersion());
87+
}
88+
89+
}
90+
@Test
91+
public void getPluginsShouldReturnTheListOfInstalledPluginsFor1651()
4392
{
93+
if (!jenkinsServer.getVersion().equals("1.651")) {
94+
throw new SkipException("Not Version 1.651");
95+
}
4496
//TODO: The list of plugins is contained in the plugin.txt
4597
// which should be read and used as base of comparison.
4698
// instead of maintaining at two locations.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public boolean isRunning() {
9595
}
9696
}
9797

98+
/**
99+
* @return The Jenkins version.
100+
*/
101+
public String getVersion() {
102+
if (client.getJenkinsVersion().isEmpty()) {
103+
//Force a request to get at least once
104+
//HttpHeader
105+
isRunning();
106+
}
107+
return client.getJenkinsVersion();
108+
}
109+
98110
/**
99111
* Get a list of all the defined jobs on the server (at the summary level)
100112
*

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.apache.commons.io.IOUtils;
1919
import org.apache.commons.lang.StringUtils;
20+
import org.apache.http.Header;
2021
import org.apache.http.HttpResponse;
2122
import org.apache.http.auth.AuthScope;
2223
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -63,6 +64,8 @@ public class JenkinsHttpClient {
6364

6465
private ObjectMapper mapper;
6566
private String context;
67+
68+
private String jenkinsVersion;
6669

6770
/**
6871
* Create an unauthenticated Jenkins HTTP client
@@ -82,6 +85,7 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) {
8285
this.client = client;
8386
this.httpResponseValidator = new HttpResponseValidator();
8487
// this.contentExtractor = new HttpResponseContentExtractor();
88+
this.jenkinsVersion = null;
8589
}
8690

8791
/**
@@ -153,6 +157,7 @@ public JenkinsHttpClient(URI uri, String username, String password) {
153157
public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException {
154158
HttpGet getMethod = new HttpGet(api(path));
155159
HttpResponse response = client.execute(getMethod, localContext);
160+
getJenkinsVersionFromHeader(response);
156161
try {
157162
httpResponseValidator.validateResponse(response);
158163
return objectFromResponse(cls, response);
@@ -175,7 +180,7 @@ public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException
175180
public String get(String path) throws IOException {
176181
HttpGet getMethod = new HttpGet(api(path));
177182
HttpResponse response = client.execute(getMethod, localContext);
178-
183+
getJenkinsVersionFromHeader(response);
179184
try {
180185
httpResponseValidator.validateResponse(response);
181186
return IOUtils.toString(response.getEntity().getContent());
@@ -220,6 +225,7 @@ public <T extends BaseModel> T getQuietly(String path, Class<T> cls) {
220225
public InputStream getFile(URI path) throws IOException {
221226
HttpGet getMethod = new HttpGet(path);
222227
HttpResponse response = client.execute(getMethod, localContext);
228+
getJenkinsVersionFromHeader(response);
223229
httpResponseValidator.validateResponse(response);
224230
return new RequestReleasingInputStream(response.getEntity().getContent(), getMethod);
225231
}
@@ -260,6 +266,7 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolea
260266
request.setEntity(stringEntity);
261267
}
262268
HttpResponse response = client.execute(request, localContext);
269+
getJenkinsVersionFromHeader(response);
263270

264271
try {
265272
httpResponseValidator.validateResponse(response);
@@ -330,6 +337,7 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
330337
}
331338

332339
HttpResponse response = client.execute(request, localContext);
340+
getJenkinsVersionFromHeader(response);
333341

334342
try {
335343
httpResponseValidator.validateResponse(response);
@@ -368,6 +376,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
368376
request.setEntity(new StringEntity(xml_data, ContentType.create("text/xml", "utf-8")));
369377
}
370378
HttpResponse response = client.execute(request, localContext);
379+
getJenkinsVersionFromHeader(response);
371380
httpResponseValidator.validateResponse(response);
372381
try {
373382
return IOUtils.toString(response.getEntity().getContent());
@@ -413,6 +422,7 @@ public String post_text(String path, String textData, ContentType contentType, b
413422
request.setEntity(new StringEntity(textData, contentType));
414423
}
415424
HttpResponse response = client.execute(request, localContext);
425+
getJenkinsVersionFromHeader(response);
416426
httpResponseValidator.validateResponse(response);
417427
try {
418428
return IOUtils.toString(response.getEntity().getContent());
@@ -471,7 +481,6 @@ private URI noapi(String path) {
471481
private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse response) throws IOException {
472482
InputStream content = response.getEntity().getContent();
473483
byte[] bytes = ByteStreams.toByteArray(content);
474-
String bytestring = new String(bytes);
475484
T result = mapper.readValue(bytes, cls);
476485
// TODO: original:
477486
// T result = mapper.readValue(content, cls);
@@ -485,6 +494,20 @@ private ObjectMapper getDefaultMapper() {
485494
return mapper;
486495
}
487496

497+
/**
498+
* @return the version string.
499+
*/
500+
public String getJenkinsVersion() {
501+
return this.jenkinsVersion;
502+
}
503+
504+
private void getJenkinsVersionFromHeader(HttpResponse response) {
505+
Header[] headers = response.getHeaders("X-Jenkins");
506+
if (headers.length == 1) {
507+
this.jenkinsVersion = headers[0].getValue();
508+
}
509+
}
510+
488511
private void releaseConnection(HttpRequestBase httpRequestBase) {
489512
httpRequestBase.releaseConnection();
490513
}

0 commit comments

Comments
 (0)