From 196a94e090e00c41a12182ced556884b48f292d0 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Thu, 20 Jul 2017 19:45:27 +0200 Subject: [PATCH] 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. --- .../com/offbytwo/jenkins/JenkinsServer.java | 6 +++--- .../jenkins/client/JenkinsHttpClient.java | 14 ++++++++++++- .../offbytwo/jenkins/JenkinsServerTest.java | 20 ++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java index 51788fe8..7b8f77a8 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.net.URI; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -103,9 +102,10 @@ public boolean isRunning() { * @return {@link JenkinsVersion} */ public JenkinsVersion getVersion() { - if (client.getJenkinsVersion().isEmpty()) { + if (!client.isJenkinsVersionSet()) { // Force a request to get at least once - // HttpHeader + // HttpHeader. The header contains the version + // information. isRunning(); } JenkinsVersion jv = new JenkinsVersion(client.getJenkinsVersion()); diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index c4f6f0da..8c5c467a 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -66,6 +66,8 @@ public class JenkinsHttpClient { private String jenkinsVersion; + public final static String EMPTY_VERSION = "UNKNOWN"; + /** * Create an unauthenticated Jenkins HTTP client * @@ -82,7 +84,7 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) { this.client = client; this.httpResponseValidator = new HttpResponseValidator(); // this.contentExtractor = new HttpResponseContentExtractor(); - this.jenkinsVersion = null; + this.jenkinsVersion = EMPTY_VERSION; LOGGER.debug("uri={}", uri.toString()); } @@ -496,6 +498,16 @@ public String getJenkinsVersion() { return this.jenkinsVersion; } + /** + * Check to see if the jenkins version has been set + * to something different than the initialization value + * from the constructor. This means there has never been made + * a communication with the Jenkins server. + * @return true if jenkinsVersion has been set by communication, false otherwise. + */ + public boolean isJenkinsVersionSet() { + return !EMPTY_VERSION.equals( this.jenkinsVersion ); + } private void getJenkinsVersionFromHeader(HttpResponse response) { Header[] headers = response.getHeaders("X-Jenkins"); if (headers.length == 1) { diff --git a/jenkins-client/src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java b/jenkins-client/src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java index b98a9fb3..444d6d3b 100644 --- a/jenkins-client/src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java +++ b/jenkins-client/src/test/java/com/offbytwo/jenkins/JenkinsServerTest.java @@ -34,7 +34,9 @@ import com.offbytwo.jenkins.model.View; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class JenkinsServerTest extends BaseUnitTest { @@ -42,9 +44,6 @@ public class JenkinsServerTest extends BaseUnitTest { private JenkinsServer server = new JenkinsServer(client); private MainView mainView = new MainView(new Job("Hello", "http://localhost/job/Hello/")); - public JenkinsServerTest() throws Exception { - } - @Before public void setUp() throws Exception { given(client.get("/", MainView.class)).willReturn(mainView); @@ -314,6 +313,21 @@ public void testGetJobXmls() throws Exception { shouldGetJobXml("HeLLo"); } + @Test + public void getVersionShouldNotFailWithNPE() + throws Exception + { + when (client.get( "/" )).thenReturn( "TheAnswer"); + when (client.getJenkinsVersion()).thenReturn( "1.23"); + + JenkinsServer server = new JenkinsServer( client); + server.getVersion(); + verify( client, times( 1 )).isJenkinsVersionSet(); + verify( client, times( 1 )).get( "/" ); + verify( client, times( 1 )).getJenkinsVersion(); + + } + private void shouldGetFolderJobs(String... jobNames) throws IOException { // given String path = "http://localhost/jobs/someFolder/";