From 435c3b5617c3cda3c46d6ade939059b6fd2c14fc Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:22:33 -0700 Subject: [PATCH 1/8] Remove redundant suppression --- src/main/java/com/tikal/hudson/plugins/notification/Phase.java | 1 - src/main/java/com/tikal/hudson/plugins/notification/Utils.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Phase.java b/src/main/java/com/tikal/hudson/plugins/notification/Phase.java index 5649bb9..7e4dad2 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/Phase.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Phase.java @@ -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); } diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Utils.java b/src/main/java/com/tikal/hudson/plugins/notification/Utils.java index afad471..aebb904 100644 --- a/src/main/java/com/tikal/hudson/plugins/notification/Utils.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Utils.java @@ -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( From cdbf39748751d623a21454f198107f9c7cec8a46 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:26:39 -0700 Subject: [PATCH 2/8] Avoid usage of deprecated methods --- .../java/com/tikal/hudson/plugins/notification/Protocol.java | 5 +++-- .../tikal/hudson/plugins/notification/model/BuildState.java | 4 ++-- .../com/tikal/hudson/plugins/notification/PhaseTest.java | 4 ++-- .../hudson/plugins/notification/test/HostnamePortTest.java | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java index 8f2e3b5..800e327 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java @@ -75,8 +75,9 @@ protected void send(String url, byte[] data, int timeout, boolean isJson) throws } 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) { + proxy = jenkins.proxy.createProxy(targetUrl.getHost()); } else if (proxyUrl != null) { // Proxy connection to the address provided final int proxyPort = proxyUrl.getPort() > 0 ? proxyUrl.getPort() : 80; diff --git a/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java b/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java index 546694f..fbbcd05 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java @@ -202,13 +202,13 @@ private void updateArchivedArtifacts(Run run) { List 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(); updateArtifact(a.relativePath, "archive", artifactUrl); } } private void updateS3Artifacts(Job job, Run run) { - if (Jenkins.getInstance().getPlugin("s3") == null) { + if (Jenkins.get().getPlugin("s3") == null) { return; } if (!(run instanceof AbstractBuild)) { diff --git a/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java b/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java index 4ea4185..896efa7 100644 --- a/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java +++ b/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java @@ -216,7 +216,7 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc byte[] data = "data".getBytes(); try (MockedStatic 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); @@ -256,7 +256,7 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc try (MockedStatic jenkinsMockedStatic = mockStatic(Jenkins.class); MockedStatic 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"); diff --git a/src/test/java/com/tikal/hudson/plugins/notification/test/HostnamePortTest.java b/src/test/java/com/tikal/hudson/plugins/notification/test/HostnamePortTest.java index d2288b6..c1ed4f2 100644 --- a/src/test/java/com/tikal/hudson/plugins/notification/test/HostnamePortTest.java +++ b/src/test/java/com/tikal/hudson/plugins/notification/test/HostnamePortTest.java @@ -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 { From dc8548fcb125e048d80405ca0242b2a2b495271d Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:26:49 -0700 Subject: [PATCH 3/8] Use isEmpty() where possible --- .../java/com/tikal/hudson/plugins/notification/Protocol.java | 4 ++-- .../java/com/tikal/hudson/plugins/notification/Utils.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java index 800e327..52bfa26 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java @@ -67,7 +67,7 @@ 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()) { proxyUrl = new URL(httpProxyUrl); if (!proxyUrl.getProtocol().startsWith("http")) { throw new IllegalArgumentException("Not an http(s) url: " + httpProxyUrl); @@ -157,6 +157,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())); } } diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Utils.java b/src/main/java/com/tikal/hudson/plugins/notification/Utils.java index aebb904..becfd85 100644 --- a/src/main/java/com/tikal/hudson/plugins/notification/Utils.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Utils.java @@ -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())) { return true; } } From f3cd85fa92236c234acad5632477ab0933d84376 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:27:37 -0700 Subject: [PATCH 4/8] Use StandardCharsets where possible --- .../java/com/tikal/hudson/plugins/notification/Format.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Format.java b/src/main/java/com/tikal/hudson/plugins/notification/Format.java index e6e27cb..01e057e 100644 --- a/src/main/java/com/tikal/hudson/plugins/notification/Format.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Format.java @@ -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 { @@ -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 { @@ -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); } }; From 803dee621c80fc646c7c4d97c33e7c28df48db97 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:28:07 -0700 Subject: [PATCH 5/8] Use diamond operator where possible --- .../plugins/notification/HudsonNotificationProperty.java | 2 +- .../tikal/hudson/plugins/notification/model/BuildState.java | 6 +++--- .../com/tikal/hudson/plugins/notification/ProtocolTest.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java b/src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java index e6eb959..58bcf79 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java @@ -25,7 +25,7 @@ public class HudsonNotificationProperty extends JobProperty> { @DataBoundConstructor public HudsonNotificationProperty(List endpoints) { - this.endpoints = new ArrayList(endpoints); + this.endpoints = new ArrayList<>(endpoints); } public List getEndpoints() { diff --git a/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java b/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java index fbbcd05..7cd0402 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java @@ -69,7 +69,7 @@ public class BuildState { * notification.jar: * archive: http://localhost:8080/job/notification-plugin/78/artifact/target/notification.jar */ - private final Map> artifacts = new HashMap>(); + private final Map> artifacts = new HashMap<>(); public int getNumber() { return number; @@ -140,7 +140,7 @@ public Map getParameters() { } public void setParameters(Map params) { - this.parameters = new HashMap(params); + this.parameters = new HashMap<>(params); } public Map> getArtifacts() { @@ -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()); + artifacts.put(fileName, new HashMap<>()); } if (artifacts.get(fileName).containsKey(locationName)) { diff --git a/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java b/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java index abbea3a..579fd97 100644 --- a/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java +++ b/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java @@ -206,7 +206,7 @@ public void tearDown() throws Exception { } public void testHttpPost() throws Exception { - BlockingQueue requests = new LinkedBlockingQueue(); + BlockingQueue requests = new LinkedBlockingQueue<>(); UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath"); @@ -220,7 +220,7 @@ public void testHttpPost() throws Exception { } public void testHttpPostWithBasicAuth() throws Exception { - BlockingQueue requests = new LinkedBlockingQueue(); + BlockingQueue requests = new LinkedBlockingQueue<>(); UrlFactory urlFactory = startSecureServer(new RecordingServlet(requests), "/realpath", "fred:foo"); @@ -235,7 +235,7 @@ public void testHttpPostWithBasicAuth() throws Exception { } public void testHttpPostWithRedirects() throws Exception { - BlockingQueue requests = new LinkedBlockingQueue(); + BlockingQueue requests = new LinkedBlockingQueue<>(); UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath"); From 0f4bce631d00de4b42739e9c93343a792225da17 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:28:23 -0700 Subject: [PATCH 6/8] Use try-with-resources where possible --- .../java/com/tikal/hudson/plugins/notification/Protocol.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java index 52bfa26..d1b92c1 100755 --- a/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java +++ b/src/main/java/com/tikal/hudson/plugins/notification/Protocol.java @@ -102,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, From 5b22fab529d3af99d8a2603a3802de333028a907 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:28:51 -0700 Subject: [PATCH 7/8] Use List.of in tests --- .../tikal/hudson/plugins/notification/PhaseTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java b/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java index 896efa7..04f2b41 100644 --- a/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java +++ b/src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java @@ -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; @@ -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; @@ -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); @@ -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"); @@ -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); @@ -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); From ad1442a1a68b28d1d9d2c33e7c10b8cf1009bc55 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 3 Oct 2024 09:30:23 -0700 Subject: [PATCH 8/8] Remove redundant throws in tests --- .../com/tikal/hudson/plugins/notification/ProtocolTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java b/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java index 579fd97..934e4f7 100644 --- a/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java +++ b/src/test/java/com/tikal/hudson/plugins/notification/ProtocolTest.java @@ -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 } } @@ -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); } @@ -194,7 +194,7 @@ public String getUrl(String path) { } @Override - public void setUp() throws Exception { + public void setUp() { servers = new LinkedList<>(); }