Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/main/java/hudson/model/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public static String loadJSON(URL src) throws IOException {
} else {
throw new IOException("Could not find JSON in " + src);
}
} catch (IOException ioe) {
// Include basename of exception and original message text
// in the new message so that the person reading the
// message understands the exception type and the text
// provided by the original exception.
String ioeMessage = ioe.getClass().getSimpleName() + ": " + ioe.getMessage();
throw new IOException("Could not load JSON from " + src + " due to " + ioeMessage);
}
}

Expand All @@ -154,6 +161,13 @@ public static String loadJSONHTML(URL src) throws IOException {
} else {
throw new IOException("Could not find JSON in " + src);
}
} catch (IOException ioe) {
// Include basename of exception and original message text
// in the new message so that the person reading the
// message understands the exception type and the text
// provided by the original exception.
String ioeMessage = ioe.getClass().getSimpleName() + ": " + ioe.getMessage();
throw new IOException("Could not load JSON as HTML from " + src + " due to " + ioeMessage);
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/src/test/java/hudson/model/DownloadServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package hudson.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.DownloadService.Downloadable;
import hudson.tasks.Ant.AntInstaller;
import hudson.tasks.Maven;
import hudson.tools.DownloadFromUrlInstaller;
import hudson.tools.ToolInstallation;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -38,6 +43,26 @@ private static void assertRoots(String expected, URL resource) throws Exception
assertEquals(expected, new TreeSet<>(keySet).toString());
}

@WithoutJenkins
@Test
void testLoadJSONException() throws Exception {
String badHostname = "bad.updates.jenkins.io"; // Non-existent host
URL badURL = new URI("https://" + badHostname + "/update-center.json").toURL();
IOException e = assertThrows(IOException.class, () -> DownloadService.loadJSON(badURL));
assertThat(e.getMessage(), containsString(badURL.toString()));
assertThat(e.getMessage(), containsString("due to UnknownHostException:"));
}

@WithoutJenkins
@Test
void testLoadJSONHTMLException() throws Exception {
String badHostname = "bad.updates.jenkins.io"; // Non-existent host
URL badURL = new URI("https://" + badHostname + "/update-center.json").toURL();
IOException e = assertThrows(IOException.class, () -> DownloadService.loadJSONHTML(badURL));
assertThat(e.getMessage(), containsString(badURL.toString()));
assertThat(e.getMessage(), containsString("due to UnknownHostException:"));
}

@Test
void testReduceFunctionWithMavenJsons() throws Exception {
URL resource1 = DownloadServiceTest.class.getResource("hudson.tasks.Maven.MavenInstaller1.json");
Expand Down