Skip to content

Commit e16dc93

Browse files
committed
Fixed #268
o Prevent to produce an NPE if getVersion() called before the first communication. So we call isRunning() which makes the first communication and will transfer http headers which contain the version which will be read by JenkinsHttpClient.
1 parent 49be530 commit e16dc93

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

ReleaseNotes.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Release 0.3.8 (NOT RELEASED YET)
44

5-
* [Fixed Issue 98][issue-98]
5+
* [Fixed Issue 268][issue-268]
6+
7+
NullPointerException is thrown unless isRunning() is called first.
8+
9+
* [Fixed Issue 98][issue-98]
610

711
Splitting fix made for jacoco reports from Jenkins #98.
812
Thanks to Shah, Prince.
@@ -1006,6 +1010,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
10061010
[issue-220]: https://github.com/jenkinsci/java-client-api/issues/220
10071011
[issue-222]: https://github.com/jenkinsci/java-client-api/issues/222
10081012
[issue-244]: https://github.com/jenkinsci/java-client-api/issues/244
1013+
[issue-268]: https://github.com/jenkinsci/java-client-api/issues/268
10091014
[pull-123]: https://github.com/jenkinsci/java-client-api/pull/123
10101015
[pull-149]: https://github.com/jenkinsci/java-client-api/pull/149
10111016
[pull-158]: https://github.com/jenkinsci/java-client-api/pull/158

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.io.IOException;
1010
import java.net.URI;
11-
import java.util.ArrayList;
1211
import java.util.Arrays;
1312
import java.util.List;
1413
import java.util.Map;
@@ -103,9 +102,10 @@ public boolean isRunning() {
103102
* @return {@link JenkinsVersion}
104103
*/
105104
public JenkinsVersion getVersion() {
106-
if (client.getJenkinsVersion().isEmpty()) {
105+
if (!client.isJenkinsVersionSet()) {
107106
// Force a request to get at least once
108-
// HttpHeader
107+
// HttpHeader. The header contains the version
108+
// information.
109109
isRunning();
110110
}
111111
JenkinsVersion jv = new JenkinsVersion(client.getJenkinsVersion());

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class JenkinsHttpClient {
6666

6767
private String jenkinsVersion;
6868

69+
public final static String EMPTY_VERSION = "UNKNOWN";
70+
6971
/**
7072
* Create an unauthenticated Jenkins HTTP client
7173
*
@@ -82,7 +84,7 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) {
8284
this.client = client;
8385
this.httpResponseValidator = new HttpResponseValidator();
8486
// this.contentExtractor = new HttpResponseContentExtractor();
85-
this.jenkinsVersion = null;
87+
this.jenkinsVersion = EMPTY_VERSION;
8688
LOGGER.debug("uri={}", uri.toString());
8789
}
8890

@@ -496,6 +498,16 @@ public String getJenkinsVersion() {
496498
return this.jenkinsVersion;
497499
}
498500

501+
/**
502+
* Check to see if the jenkins version has been set
503+
* to something different than the initialization value
504+
* from the constructor. This means there has never been made
505+
* a communication with the Jenkins server.
506+
* @return true if jenkinsVersion has been set by communication, false otherwise.
507+
*/
508+
public boolean isJenkinsVersionSet() {
509+
return !EMPTY_VERSION.equals( this.jenkinsVersion );
510+
}
499511
private void getJenkinsVersionFromHeader(HttpResponse response) {
500512
Header[] headers = response.getHeaders("X-Jenkins");
501513
if (headers.length == 1) {

jenkins-client/src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@
3434
import com.offbytwo.jenkins.model.View;
3535

3636
import static org.mockito.Mockito.mock;
37+
import static org.mockito.Mockito.times;
3738
import static org.mockito.Mockito.verify;
39+
import static org.mockito.Mockito.when;
3840

3941
public class JenkinsServerTest extends BaseUnitTest {
4042

4143
private JenkinsHttpClient client = mock(JenkinsHttpClient.class);
4244
private JenkinsServer server = new JenkinsServer(client);
4345
private MainView mainView = new MainView(new Job("Hello", "http://localhost/job/Hello/"));
4446

45-
public JenkinsServerTest() throws Exception {
46-
}
47-
4847
@Before
4948
public void setUp() throws Exception {
5049
given(client.get("/", MainView.class)).willReturn(mainView);
@@ -314,6 +313,21 @@ public void testGetJobXmls() throws Exception {
314313
shouldGetJobXml("HeLLo");
315314
}
316315

316+
@Test
317+
public void getVersionShouldNotFailWithNPE()
318+
throws Exception
319+
{
320+
when (client.get( "/" )).thenReturn( "TheAnswer");
321+
when (client.getJenkinsVersion()).thenReturn( "1.23");
322+
323+
JenkinsServer server = new JenkinsServer( client);
324+
server.getVersion();
325+
verify( client, times( 1 )).isJenkinsVersionSet();
326+
verify( client, times( 1 )).get( "/" );
327+
verify( client, times( 1 )).getJenkinsVersion();
328+
329+
}
330+
317331
private void shouldGetFolderJobs(String... jobNames) throws IOException {
318332
// given
319333
String path = "http://localhost/jobs/someFolder/";

0 commit comments

Comments
 (0)