Skip to content

Commit

Permalink
Merge pull request #63 from basil/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
BorisYaoA authored Oct 3, 2024
2 parents 2ec1e30 + ad1442a commit 9b15987
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.thoughtworks.xstream.XStream;
import com.tikal.hudson.plugins.notification.model.JobState;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public enum Format {
XML {
Expand All @@ -27,7 +28,7 @@ public enum Format {
@Override
protected byte[] serialize(JobState jobState) throws IOException {
xstream.processAnnotations(JobState.class);
return xstream.toXML(jobState).getBytes("UTF-8");
return xstream.toXML(jobState).getBytes(StandardCharsets.UTF_8);
}
},
JSON {
Expand All @@ -37,7 +38,7 @@ protected byte[] serialize(JobState jobState) throws IOException {

@Override
protected byte[] serialize(JobState jobState) throws IOException {
return gson.toJson(jobState).getBytes("UTF-8");
return gson.toJson(jobState).getBytes(StandardCharsets.UTF_8);

Check warning on line 41 in src/main/java/com/tikal/hudson/plugins/notification/Format.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 31-41 are not covered by tests
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class HudsonNotificationProperty extends JobProperty<Job<?, ?>> {

@DataBoundConstructor
public HudsonNotificationProperty(List<Endpoint> endpoints) {
this.endpoints = new ArrayList<Endpoint>(endpoints);
this.endpoints = new ArrayList<>(endpoints);

Check warning on line 28 in src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 28 is not covered by tests
}

public List<Endpoint> getEndpoints() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private Result findLastBuildThatFinished(Run run) {
return null;
}

@SuppressWarnings("CastToConcreteClass")
public void handle(Run run, TaskListener listener, long timestamp) {
handle(run, listener, timestamp, false, null, 0, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ protected void send(String url, byte[] data, int timeout, boolean isJson) throws
// Verifying if the HTTP_PROXY is available
final String httpProxyUrl = System.getenv().get("http_proxy");
URL proxyUrl = null;
if (httpProxyUrl != null && httpProxyUrl.length() > 0) {
if (httpProxyUrl != null && !httpProxyUrl.isEmpty()) {

Check warning on line 70 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 70 is only partially covered, 3 branches are missing
proxyUrl = new URL(httpProxyUrl);
if (!proxyUrl.getProtocol().startsWith("http")) {
throw new IllegalArgumentException("Not an http(s) url: " + httpProxyUrl);
}
}

Proxy proxy = Proxy.NO_PROXY;
if (Jenkins.getInstance() != null && Jenkins.getInstance().proxy != null) {
proxy = Jenkins.getInstance().proxy.createProxy(targetUrl.getHost());
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins != null && jenkins.proxy != null) {

Check warning on line 79 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 79 is only partially covered, 3 branches are missing
proxy = jenkins.proxy.createProxy(targetUrl.getHost());

Check warning on line 80 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 80 is not covered by tests
} else if (proxyUrl != null) {
// Proxy connection to the address provided
final int proxyPort = proxyUrl.getPort() > 0 ? proxyUrl.getPort() : 80;
Expand All @@ -101,12 +102,9 @@ protected void send(String url, byte[] data, int timeout, boolean isJson) throws
connection.setReadTimeout(timeout);
connection.connect();
try {
OutputStream output = connection.getOutputStream();
try {
try (OutputStream output = connection.getOutputStream()) {
output.write(data);
output.flush();
} finally {
output.close();
}
} finally {
// Follow an HTTP Temporary Redirect if we get one,
Expand Down Expand Up @@ -156,6 +154,6 @@ public void validateUrl(String url) {
}

private static boolean isEmpty(String s) {
return ((s == null) || (s.trim().length() < 1));
return ((s == null) || (s.trim().isEmpty()));

Check warning on line 157 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 157 is not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static boolean isEmpty(String... strings) {
}

for (String s : strings) {
if ((s == null) || (s.trim().length() < 1)) {
if ((s == null) || (s.trim().isEmpty())) {

Check warning on line 30 in src/main/java/com/tikal/hudson/plugins/notification/Utils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 30 is only partially covered, one branch is missing
return true;
}
}
Expand All @@ -40,7 +40,6 @@ public static boolean isEmpty(String... strings) {
* @param strings Strings to check for empty (whitespace is trimmed) or null.
* @throws java.lang.IllegalArgumentException Throws this exception if any string is empty.
*/
@SuppressWarnings("ReturnOfNull")
public static void verifyNotEmpty(String... strings) {
if (isEmpty(strings)) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class BuildState {
* notification.jar:
* archive: http://localhost:8080/job/notification-plugin/78/artifact/target/notification.jar
*/
private final Map<String, Map<String, String>> artifacts = new HashMap<String, Map<String, String>>();
private final Map<String, Map<String, String>> artifacts = new HashMap<>();

public int getNumber() {
return number;
Expand Down Expand Up @@ -140,7 +140,7 @@ public Map<String, String> getParameters() {
}

public void setParameters(Map<String, String> params) {
this.parameters = new HashMap<String, String>(params);
this.parameters = new HashMap<>(params);
}

public Map<String, Map<String, String>> getArtifacts() {
Expand Down Expand Up @@ -202,13 +202,13 @@ private void updateArchivedArtifacts(Run run) {
List<Run.Artifact> buildArtifacts = run.getArtifacts();

for (Run.Artifact a : buildArtifacts) {
String artifactUrl = Jenkins.getInstance().getRootUrl() + run.getUrl() + "artifact/" + a.getHref();
String artifactUrl = Jenkins.get().getRootUrl() + run.getUrl() + "artifact/" + a.getHref();

Check warning on line 205 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 143-205 are not covered by tests
updateArtifact(a.relativePath, "archive", artifactUrl);
}
}

private void updateS3Artifacts(Job job, Run run) {
if (Jenkins.getInstance().getPlugin("s3") == null) {
if (Jenkins.get().getPlugin("s3") == null) {

Check warning on line 211 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 211 is only partially covered, one branch is missing
return;
}
if (!(run instanceof AbstractBuild)) {
Expand Down Expand Up @@ -263,7 +263,7 @@ private void updateArtifact(String fileName, String locationName, String locatio
verifyNotEmpty(fileName, locationName, locationUrl);

if (!artifacts.containsKey(fileName)) {
artifacts.put(fileName, new HashMap<String, String>());
artifacts.put(fileName, new HashMap<>());

Check warning on line 266 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 266 is not covered by tests
}

if (artifacts.get(fileName).containsKey(locationName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.tikal.hudson.plugins.notification.UrlType.PUBLIC;
import static com.tikal.hudson.plugins.notification.UrlType.SECRET;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
Expand All @@ -26,6 +25,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.List;
import jenkins.model.Jenkins;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -184,7 +184,7 @@ public void testRunNoProperty() {
public void testRunNoPreviousRunUrlNull() {
when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);

Phase.STARTED.handle(run, listener, 0L);
Expand All @@ -197,7 +197,7 @@ public void testRunNoPreviousRunUrlNull() {
public void testRunNoPreviousRunUrlTypePublicUnresolvedUrl() throws IOException, InterruptedException {
when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(run.getEnvironment(listener)).thenReturn(environment);
when(urlInfo.getUrlOrId()).thenReturn("$someUrl");
Expand All @@ -216,7 +216,7 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc
byte[] data = "data".getBytes();
try (MockedStatic<Jenkins> jenkinsMockedStatic = mockStatic(Jenkins.class)) {
jenkinsMockedStatic.when(Jenkins::getInstanceOrNull).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::get).thenReturn(jenkins);

Protocol httpProtocolSpy = spy(Protocol.HTTP);
when(endpoint.getProtocol()).thenReturn(httpProtocolSpy);
Expand All @@ -230,7 +230,7 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(endpoint.getBranch()).thenReturn("branchName");
when(run.getEnvironment(listener)).thenReturn(environment);
Expand All @@ -256,7 +256,7 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc
try (MockedStatic<Jenkins> jenkinsMockedStatic = mockStatic(Jenkins.class);
MockedStatic<Utils> utilsMockedStatic = mockStatic(Utils.class)) {
jenkinsMockedStatic.when(Jenkins::getInstanceOrNull).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::get).thenReturn(jenkins);
utilsMockedStatic
.when(() -> Utils.getSecretUrl("credentialsId", jenkins))
.thenReturn("$secretUrl");
Expand All @@ -273,7 +273,7 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(endpoint.getBranch()).thenReturn(".*");
when(run.getEnvironment(listener)).thenReturn(environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected void doPost(HttpServletRequest httpRequest, HttpServletResponse httpRe
doPost(request, httpResponse);
}

protected void doPost(Request request, HttpServletResponse httpResponse) throws IOException {
protected void doPost(Request request, HttpServletResponse httpResponse) {
// noop
}
}
Expand All @@ -147,7 +147,7 @@ static class RedirectHandler extends RecordingServlet {
}

@Override
protected void doPost(Request request, HttpServletResponse httpResponse) throws IOException {
protected void doPost(Request request, HttpServletResponse httpResponse) {
httpResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
httpResponse.setHeader("Location", redirectURI);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public String getUrl(String path) {
}

@Override
public void setUp() throws Exception {
public void setUp() {
servers = new LinkedList<>();
}

Expand All @@ -206,7 +206,7 @@ public void tearDown() throws Exception {
}

public void testHttpPost() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath");

Expand All @@ -220,7 +220,7 @@ public void testHttpPost() throws Exception {
}

public void testHttpPostWithBasicAuth() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startSecureServer(new RecordingServlet(requests), "/realpath", "fred:foo");

Expand All @@ -235,7 +235,7 @@ public void testHttpPostWithBasicAuth() throws Exception {
}

public void testHttpPostWithRedirects() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.tikal.hudson.plugins.notification.test;

import com.tikal.hudson.plugins.notification.HostnamePort;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;

public class HostnamePortTest {
Expand Down

0 comments on commit 9b15987

Please sign in to comment.