Skip to content

Commit

Permalink
Minor refactoring and character set cleanup (#242)
Browse files Browse the repository at this point in the history
* use switch, StandardCharsets, diamond operator and removed obsolete semicolons and imports

* add default statement to switch
  • Loading branch information
StefanSpieker authored Oct 1, 2023
1 parent 884f184 commit d111338
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class EmbeddableBadgeConfig implements Serializable {
private static final long serialVersionUID = 1L;
private final Map<String, String> colors = new HashMap<String, String>() {
private final Map<String, String> colors = new HashMap<>() {
private static final long serialVersionUID = 1L;

{
Expand All @@ -21,7 +21,6 @@ public class EmbeddableBadgeConfig implements Serializable {
put("aborted", "aborted");
put("running", "blue");
}
;
};

private final String id;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jenkinsci/plugins/badge/ImageResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Map;

public class ImageResolver {
private final Map<String, String> statuses = new HashMap<String, String>() {
private final Map<String, String> statuses = new HashMap<>() {
private static final long serialVersionUID = 1L;

{
Expand All @@ -41,7 +41,6 @@ public class ImageResolver {
put("disabled", "disabled");
put("notbuilt", "not run");
}
;
};

public StatusImage getImage(
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/jenkinsci/plugins/badge/StatusImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -60,7 +60,7 @@ class StatusImage implements HttpResponse {
private final String length;
private String contentType = null;

private final Map<String, String> colors = new HashMap<String, String>() {
private final Map<String, String> colors = new HashMap<>() {
private static final long serialVersionUID = 1L;

{
Expand All @@ -73,7 +73,6 @@ class StatusImage implements HttpResponse {
put("lightgrey", "#9f9f9f");
put("blue", "#007ec6");
}
;
};

StatusImage() {
Expand Down Expand Up @@ -166,7 +165,7 @@ class StatusImage implements HttpResponse {
if (animatedSnippet != null) {
String reducedStatusWidth = String.valueOf(widths[1] - 4.0);
try (InputStream animatedOverlayStream = animatedSnippet.openStream()) {
animatedOverlay = IOUtils.toString(animatedOverlayStream, "utf-8")
animatedOverlay = IOUtils.toString(animatedOverlayStream, StandardCharsets.UTF_8)
.replace("{{reducedStatusWidth}}", reducedStatusWidth)
.replace("{{animatedColor}}", animatedColor);
}
Expand All @@ -189,7 +188,7 @@ class StatusImage implements HttpResponse {
}

try (InputStream s = image.openStream()) {
payload = IOUtils.toString(s, "utf-8")
payload = IOUtils.toString(s, StandardCharsets.UTF_8)
.replace("{{animatedOverlayColor}}", animatedOverlay)
.replace("{{fullwidth}}", fullwidth)
.replace("{{subjectWidth}}", subjectWidth)
Expand All @@ -200,7 +199,7 @@ class StatusImage implements HttpResponse {
.replace("{{status}}", status)
.replace("{{color}}", color)
.replace("<svg xmlns", linkCode)
.getBytes(Charset.forName("UTF-8"));
.getBytes(StandardCharsets.UTF_8);
}

length = Integer.toString(payload.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@ExportedBean(defaultVisibility = 2)
public class EmbeddableBadgeConfigsAction implements Action, Serializable, BuildBadgeAction {
private static final long serialVersionUID = 1L;
private Map<String, EmbeddableBadgeConfig> badgeConfigs = new HashMap<String, EmbeddableBadgeConfig>();
private Map<String, EmbeddableBadgeConfig> badgeConfigs = new HashMap<>();

Check warning on line 24 in src/main/java/org/jenkinsci/plugins/badge/actions/EmbeddableBadgeConfigsAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 24 is not covered by tests

public EmbeddableBadgeConfigsAction() {}

Expand Down Expand Up @@ -63,8 +63,6 @@ public EmbeddableBadgeConfig getConfig(String id) {
@Exported
public void addConfig(EmbeddableBadgeConfig config) {
String id = config.getID();
if (badgeConfigs.get(id) == null) {
badgeConfigs.put(id, config);
}
badgeConfigs.putIfAbsent(id, config);

Check warning on line 66 in src/main/java/org/jenkinsci/plugins/badge/actions/EmbeddableBadgeConfigsAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 66 is not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public String getDisplayName() {

@Override
public Set<Class<?>> getRequiredContext() {
Set<Class<?>> set = new HashSet<Class<?>>();
Set<Class<?>> set = new HashSet<>();

Check warning on line 111 in src/main/java/org/jenkinsci/plugins/badge/dsl/AddEmbeddableBadgeConfigStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 111 is not covered by tests
set.add(TaskListener.class);
return set;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,35 @@ public Run select(Job project, String build, Run run) {
run = run.getPreviousBuild();
}
} else {
if (build.equals("last")) {
run = project.getLastBuild();
} else if (build.equals("lastFailed")) {
run = project.getLastFailedBuild();
} else if (build.equals("lastSuccessful")) {
run = project.getLastSuccessfulBuild();
} else if (build.equals("lastUnsuccessful")) {
run = project.getLastUnsuccessfulBuild();
} else if (build.equals("lastStable")) {
run = project.getLastStableBuild();
} else if (build.equals("lastUnstable")) {
run = project.getLastUnstableBuild();
} else if (build.equals("lastCompleted")) {
run = project.getLastCompletedBuild();
} else {
// try to get build via ID
run = project.getBuild(build);
if (run == null && buildIdIsInteger) {
run = project.getBuildByNumber(buildNr);
}
switch (build) {
case "last":
run = project.getLastBuild();
break;
case "lastFailed":
run = project.getLastFailedBuild();
break;
case "lastSuccessful":
run = project.getLastSuccessfulBuild();
break;
case "lastUnsuccessful":
run = project.getLastUnsuccessfulBuild();
break;
case "lastStable":
run = project.getLastStableBuild();
break;
case "lastUnstable":
run = project.getLastUnstableBuild();
break;
case "lastCompleted":
run = project.getLastCompletedBuild();
break;
default:
// try to get build via ID
run = project.getBuild(build);
if (run == null && buildIdIsInteger) {
run = project.getBuildByNumber(buildNr);
}
break;
}
}
return run;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,27 @@ private Run findSpecific(Job job, Run run, String what, String specific) {
run = findSpecific(job, job.getFirstBuild(), what, specific);
} else {
if (specific != null) {
if (specific.equals("Failed")) {
run = job.getLastFailedBuild();
} else if (specific.equals("Successful")) {
run = job.getLastSuccessfulBuild();
} else if (specific.equals("Unsuccessful")) {
run = job.getLastUnsuccessfulBuild();
} else if (specific.equals("Stable")) {
run = job.getLastStableBuild();
} else if (specific.equals("Unstable")) {
run = job.getLastUnstableBuild();
} else if (specific.equals("Completed")) {
run = job.getLastCompletedBuild();
switch (specific) {

Check warning on line 55 in src/main/java/org/jenkinsci/plugins/badge/extensions/BuildParameterRunSelectorExtension.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 55 is only partially covered, one branch is missing
case "Failed":
run = job.getLastFailedBuild();
break;
case "Successful":
run = job.getLastSuccessfulBuild();
break;
case "Unsuccessful":
run = job.getLastUnsuccessfulBuild();
break;
case "Stable":
run = job.getLastStableBuild();
break;
case "Unstable":
run = job.getLastUnstableBuild();
break;
case "Completed":
run = job.getLastCompletedBuild();
break;
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import hudson.model.Action;
import hudson.model.Job;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import hudson.model.Job;
Expand Down

0 comments on commit d111338

Please sign in to comment.