From 136aeb86a4e2ffcfa5ec988dc02da6063643dc4a Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Tue, 9 Aug 2016 11:06:48 +0100 Subject: [PATCH] [JENKINS-37189] Refactor to make more testable - A pure unit tests will suffice --- .../main/java/hudson/model/UpdateCenter.java | 16 ++++++- .../java/hudson/model/UpdateCenterTest.java | 44 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/hudson/model/UpdateCenterTest.java diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index dcc551c864..ed1915e53d 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -1042,11 +1042,23 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas * @throws IOException if a connection to the update center server can't be established. */ public void checkUpdateCenter(ConnectionCheckJob job, String updateCenterUrl) throws IOException { + testConnection(toUpdateCenterCheckUrl(updateCenterUrl)); + } + + /** + * Converts an update center URL into the URL to use for checking its connectivity. + * @param updateCenterUrl the URL to convert. + * @return the converted URL. + * @throws MalformedURLException if the supplied URL is malformed. + */ + static URL toUpdateCenterCheckUrl(String updateCenterUrl) throws MalformedURLException { + URL url; if (updateCenterUrl.startsWith("http://") || updateCenterUrl.startsWith("https://")) { - testConnection(new URL(updateCenterUrl + (updateCenterUrl.indexOf('?') == -1 ? "?uctest" : "&uctest"))); + url = new URL(updateCenterUrl + (updateCenterUrl.indexOf('?') == -1 ? "?uctest" : "&uctest")); } else { - testConnection(new URL(updateCenterUrl)); + url = new URL(updateCenterUrl); } + return url; } /** diff --git a/core/src/test/java/hudson/model/UpdateCenterTest.java b/core/src/test/java/hudson/model/UpdateCenterTest.java new file mode 100644 index 0000000000..5d21e77b3f --- /dev/null +++ b/core/src/test/java/hudson/model/UpdateCenterTest.java @@ -0,0 +1,44 @@ +package hudson.model; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class UpdateCenterTest { + + @Test + public void toUpdateCenterCheckUrl_http_noQuery() throws Exception { + assertThat(UpdateCenter.UpdateCenterConfiguration.toUpdateCenterCheckUrl( + "http://updates.jenkins-ci.org/update-center.json").toExternalForm(), + is("http://updates.jenkins-ci.org/update-center.json?uctest")); + } + + @Test + public void toUpdateCenterCheckUrl_https_noQuery() throws Exception { + assertThat(UpdateCenter.UpdateCenterConfiguration.toUpdateCenterCheckUrl( + "https://updates.jenkins-ci.org/update-center.json").toExternalForm(), + is("https://updates.jenkins-ci.org/update-center.json?uctest")); + } + + @Test + public void toUpdateCenterCheckUrl_http_query() throws Exception { + assertThat(UpdateCenter.UpdateCenterConfiguration.toUpdateCenterCheckUrl( + "http://updates.jenkins-ci.org/update-center.json?version=2.7").toExternalForm(), + is("http://updates.jenkins-ci.org/update-center.json?version=2.7&uctest")); + } + + @Test + public void toUpdateCenterCheckUrl_https_query() throws Exception { + assertThat(UpdateCenter.UpdateCenterConfiguration.toUpdateCenterCheckUrl( + "https://updates.jenkins-ci.org/update-center.json?version=2.7").toExternalForm(), + is("https://updates.jenkins-ci.org/update-center.json?version=2.7&uctest")); + } + + @Test + public void toUpdateCenterCheckUrl_file() throws Exception { + assertThat(UpdateCenter.UpdateCenterConfiguration.toUpdateCenterCheckUrl( + "file://./foo.jar!update-center.json").toExternalForm(), + is("file://./foo.jar!update-center.json")); + } +} -- GitLab