Skip to content

Commit

Permalink
Use try with resources for WebClient (#355)
Browse files Browse the repository at this point in the history
No need to leak resources from the tests
  • Loading branch information
MarkEWaite authored Nov 8, 2024
1 parent ad068c4 commit 4ce0ba1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 66 deletions.
83 changes: 43 additions & 40 deletions src/test/java/org/jenkinsci/plugins/badge/ImageResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class PublicBuildStatusActionTest {

private FreeStyleProject job;
private String jobStatusUrl;
private JenkinsRule.WebClient webClient;

@Before
public void createJob() throws IOException {
Expand All @@ -52,31 +51,30 @@ 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("<svg "));
assertThat(result, not(containsString(SUCCESS_MARKER)));
assertThat(result, containsString(NOT_RUN_MARKER));
try (JenkinsRule.WebClient webClient = j.createWebClient()) {
JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, not(containsString(SUCCESS_MARKER)));
assertThat(result, containsString(NOT_RUN_MARKER));
}
}

@Test
public void testDoIconBuildBefore() throws Exception {
String buildStatusUrl = jobStatusUrl + "&build=123";

// Check build status icon is "not run" before job runs
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, not(containsString(SUCCESS_MARKER)));
assertThat(result, containsString(NOT_RUN_MARKER));
try (JenkinsRule.WebClient webClient = j.createWebClient()) {
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, not(containsString(SUCCESS_MARKER)));
assertThat(result, containsString(NOT_RUN_MARKER));
}
}

@Test
Expand All @@ -86,11 +84,13 @@ public void testDoIconJobAfter() throws Exception {
j.assertBuildStatusSuccess(build);

// Check job status icon is correct after job runs successfully
JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, containsString(SUCCESS_MARKER));
assertThat(result, not(containsString(NOT_RUN_MARKER)));
try (JenkinsRule.WebClient webClient = j.createWebClient()) {
JenkinsRule.JSONWebResponse json = webClient.getJSON(jobStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, containsString(SUCCESS_MARKER));
assertThat(result, not(containsString(NOT_RUN_MARKER)));
}
}

@Test
Expand All @@ -101,11 +101,13 @@ public void testDoIconBuildAfter() throws Exception {

// Check build status icon is correct after job runs successfully
String buildStatusUrl = jobStatusUrl + "&build=" + build.getNumber();
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, containsString(SUCCESS_MARKER));
assertThat(result, not(containsString(NOT_RUN_MARKER)));
try (JenkinsRule.WebClient webClient = j.createWebClient()) {
JenkinsRule.JSONWebResponse json = webClient.getJSON(buildStatusUrl);
String result = json.getContentAsString();
assertThat(result, containsString("<svg "));
assertThat(result, containsString(SUCCESS_MARKER));
assertThat(result, not(containsString(NOT_RUN_MARKER)));
}
}

@Test
Expand Down

0 comments on commit 4ce0ba1

Please sign in to comment.