From 4ce0ba145d52d446fc0c04bdc592ef33e6f9dacf Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Fri, 8 Nov 2024 00:01:02 -0700 Subject: [PATCH] Use try with resources for WebClient (#355) No need to leak resources from the tests --- .../plugins/badge/ImageResolverTest.java | 83 ++++++++++--------- .../actions/PublicBuildStatusActionTest.java | 54 ++++++------ 2 files changed, 71 insertions(+), 66 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/badge/ImageResolverTest.java b/src/test/java/org/jenkinsci/plugins/badge/ImageResolverTest.java index 9f510322..98abef9a 100644 --- a/src/test/java/org/jenkinsci/plugins/badge/ImageResolverTest.java +++ b/src/test/java/org/jenkinsci/plugins/badge/ImageResolverTest.java @@ -24,54 +24,57 @@ public class ImageResolverTest { @Test public void TestGetDefault32x32Ball() throws Exception { - JenkinsRule.WebClient wc = jenkinsRule.createWebClient(); - String style = "ball"; // should give default due to invalid size in the style - String subject = getSubject(); - String status = getStatus(); - String colorName = null; - BallColor ball = BallColor.BLUE; - StatusImage image = wc.executeOnServer(() -> { - ImageResolver imageResolver = new ImageResolver(); - return imageResolver.getImage(ball, style, subject, status, colorName, null, null); - }); - assertThat(image.getEtag(), containsString(subject)); - assertThat(image.getEtag(), containsString(status)); - assertThat(image.getEtag(), containsString("null")); + try (JenkinsRule.WebClient wc = jenkinsRule.createWebClient()) { + String style = "ball"; // should give default due to invalid size in the style + String subject = getSubject(); + String status = getStatus(); + String colorName = null; + BallColor ball = BallColor.BLUE; + StatusImage image = wc.executeOnServer(() -> { + ImageResolver imageResolver = new ImageResolver(); + return imageResolver.getImage(ball, style, subject, status, colorName, null, null); + }); + assertThat(image.getEtag(), containsString(subject)); + assertThat(image.getEtag(), containsString(status)); + assertThat(image.getEtag(), containsString("null")); + } } @Test public void TestGetNonDefaultBall() throws Exception { - JenkinsRule.WebClient wc = jenkinsRule.createWebClient(); - String sizeHint = "16x16"; - String style = "ball-" + sizeHint; // should give url - String subject = getSubject(); - String status = getStatus(); - BallColor ball = BallColor.RED; - String colorName = ball.toString(); - StatusImage image = wc.executeOnServer(() -> { - ImageResolver imageResolver = new ImageResolver(); - return imageResolver.getImage(ball, style, subject, status, colorName, null, null); - }); - assertThat(image.getEtag(), not(containsString(subject))); - assertThat(image.getEtag(), not(containsString(status))); - assertThat(image.getEtag(), containsString("images/" + sizeHint + "/" + colorName + ".png")); + try (JenkinsRule.WebClient wc = jenkinsRule.createWebClient()) { + String sizeHint = "16x16"; + String style = "ball-" + sizeHint; // should give url + String subject = getSubject(); + String status = getStatus(); + BallColor ball = BallColor.RED; + String colorName = ball.toString(); + StatusImage image = wc.executeOnServer(() -> { + ImageResolver imageResolver = new ImageResolver(); + return imageResolver.getImage(ball, style, subject, status, colorName, null, null); + }); + assertThat(image.getEtag(), not(containsString(subject))); + assertThat(image.getEtag(), not(containsString(status))); + assertThat(image.getEtag(), containsString("images/" + sizeHint + "/" + colorName + ".png")); + } } @Test public void testShouldReturnEmpty() throws Exception { - JenkinsRule.WebClient wc = jenkinsRule.createWebClient(); - String style = "ball-42x45"; // invalid size hint will return default empty image - String subject = getSubject(); - String status = getStatus(); - String colorName = null; - BallColor ball = BallColor.BLUE; - StatusImage image = wc.executeOnServer(() -> { - ImageResolver imageResolver = new ImageResolver(); - return imageResolver.getImage(ball, style, subject, status, colorName, null, null); - }); - assertThat(image.getEtag(), not(containsString(subject))); - assertThat(image.getEtag(), not(containsString(status))); - assertThat(image.getEtag(), containsString("empty")); + try (JenkinsRule.WebClient wc = jenkinsRule.createWebClient()) { + String style = "ball-42x45"; // invalid size hint will return default empty image + String subject = getSubject(); + String status = getStatus(); + String colorName = null; + BallColor ball = BallColor.BLUE; + StatusImage image = wc.executeOnServer(() -> { + ImageResolver imageResolver = new ImageResolver(); + return imageResolver.getImage(ball, style, subject, status, colorName, null, null); + }); + assertThat(image.getEtag(), not(containsString(subject))); + assertThat(image.getEtag(), not(containsString(status))); + assertThat(image.getEtag(), containsString("empty")); + } } private final Random random = new Random(); diff --git a/src/test/java/org/jenkinsci/plugins/badge/actions/PublicBuildStatusActionTest.java b/src/test/java/org/jenkinsci/plugins/badge/actions/PublicBuildStatusActionTest.java index a22070ae..4e536bc8 100644 --- a/src/test/java/org/jenkinsci/plugins/badge/actions/PublicBuildStatusActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/badge/actions/PublicBuildStatusActionTest.java @@ -34,7 +34,6 @@ public class PublicBuildStatusActionTest { private FreeStyleProject job; private String jobStatusUrl; - private JenkinsRule.WebClient webClient; @Before public void createJob() throws IOException { @@ -52,19 +51,16 @@ public void createJob() throws IOException { jobStatusUrl = statusUrl + "?job=" + job.getName(); } - @Before - public void createWebClient() { - webClient = j.createWebClient(); - } - @Test public void testDoIconJobBefore() throws Exception { // Check job status icon is "not run" before job runs - JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl); - String result = json.getContentAsString(); - assertThat(result, containsString("