From bc41a5421cb433f3705ecfdb4c620c131311bcde Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Mon, 27 Nov 2023 11:04:16 +0200 Subject: [PATCH] Drop Apache `commons-io` in favor of plain Java API (#221) --- pom.xml | 6 ---- .../AbstractSauceTunnelManager.java | 34 ++++++------------- .../sauceconnect/SauceConnectFourManager.java | 13 ++++--- .../saucelabs/ci/BuildInformationTest.java | 3 +- .../saucelabs/ci/BuildJobInformationTest.java | 5 ++- .../com/saucelabs/ci/JobInformationTest.java | 4 +-- 6 files changed, 23 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index 586a6fb..184ed3a 100644 --- a/pom.xml +++ b/pom.xml @@ -259,12 +259,6 @@ commons-compress 1.24.0 - - commons-io - commons-io - 2.15.0 - provided - org.apache.commons commons-lang3 diff --git a/src/main/java/com/saucelabs/ci/sauceconnect/AbstractSauceTunnelManager.java b/src/main/java/com/saucelabs/ci/sauceconnect/AbstractSauceTunnelManager.java index 065dc67..a99094f 100644 --- a/src/main/java/com/saucelabs/ci/sauceconnect/AbstractSauceTunnelManager.java +++ b/src/main/java/com/saucelabs/ci/sauceconnect/AbstractSauceTunnelManager.java @@ -19,8 +19,6 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.output.NullOutputStream; import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.slf4j.Logger; @@ -166,29 +164,9 @@ public void closeTunnelsForPlan(String userName, String options, PrintStream pri private void closeSauceConnectProcess(PrintStream printStream, final Process sauceConnect) { logMessage(printStream, "Flushing Sauce Connect Input Stream"); - new Thread( - new Runnable() { - public void run() { - try { - IOUtils.copy(sauceConnect.getInputStream(), NullOutputStream.INSTANCE); - } catch (IOException e) { - // ignore - } - } - }) - .start(); + new Thread(() -> flushInputStream(sauceConnect.getInputStream())).start(); logMessage(printStream, "Flushing Sauce Connect Error Stream"); - new Thread( - new Runnable() { - public void run() { - try { - IOUtils.copy(sauceConnect.getErrorStream(), NullOutputStream.INSTANCE); - } catch (IOException e) { - // ignore - } - } - }) - .start(); + new Thread(() -> flushInputStream(sauceConnect.getErrorStream())).start(); logMessage(printStream, "Closing Sauce Connect process"); sauceConnect.destroy(); try { @@ -202,6 +180,14 @@ public void run() { } } + private static void flushInputStream(InputStream inputStream) { + try { + inputStream.skip(inputStream.available()); + } catch (IOException e) { + // ignore + } + } + /** * Reduces the count of active Sauce Connect processes for the user by 1. * diff --git a/src/main/java/com/saucelabs/ci/sauceconnect/SauceConnectFourManager.java b/src/main/java/com/saucelabs/ci/sauceconnect/SauceConnectFourManager.java index 90dba1d..e5a239a 100755 --- a/src/main/java/com/saucelabs/ci/sauceconnect/SauceConnectFourManager.java +++ b/src/main/java/com/saucelabs/ci/sauceconnect/SauceConnectFourManager.java @@ -6,7 +6,6 @@ import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.CompressorStreamFactory; -import org.apache.commons.io.IOUtils; import org.json.JSONObject; import java.io.BufferedInputStream; @@ -14,7 +13,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; -import java.nio.charset.StandardCharsets; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.nio.file.Files; import java.nio.file.Path; import java.text.MessageFormat; @@ -254,10 +256,11 @@ public void setCleanUpOnExit(boolean cleanUpOnExit) { public static String getLatestSauceConnectVersion() { try { - URL url = new URL("https://saucelabs.com/versions.json"); - String versionsJson = IOUtils.toString(url, StandardCharsets.UTF_8); + URI url = URI.create("https://saucelabs.com/versions.json"); + HttpRequest request = HttpRequest.newBuilder(url).build(); + String versionsJson = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()).body(); return new JSONObject(versionsJson).getJSONObject("Sauce Connect").getString("version"); - } catch (IOException e) { + } catch (IOException | InterruptedException e) { return null; } } diff --git a/src/test/java/com/saucelabs/ci/BuildInformationTest.java b/src/test/java/com/saucelabs/ci/BuildInformationTest.java index d688207..75f4aa2 100644 --- a/src/test/java/com/saucelabs/ci/BuildInformationTest.java +++ b/src/test/java/com/saucelabs/ci/BuildInformationTest.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.commons.io.IOUtils; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -21,7 +20,7 @@ class BuildInformationTest { void beforeEach() throws Exception { JSONObject obj = new JSONObject( - IOUtils.toString(getClass().getResourceAsStream("/build_info.json"), StandardCharsets.UTF_8)); + new String(getClass().getResourceAsStream("/build_info.json").readAllBytes(), StandardCharsets.UTF_8)); build = new BuildInformation("1234"); build.populateFromJson(obj); } diff --git a/src/test/java/com/saucelabs/ci/BuildJobInformationTest.java b/src/test/java/com/saucelabs/ci/BuildJobInformationTest.java index e886a95..f226014 100644 --- a/src/test/java/com/saucelabs/ci/BuildJobInformationTest.java +++ b/src/test/java/com/saucelabs/ci/BuildJobInformationTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.commons.io.IOUtils; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,7 +19,7 @@ class BuildJobInformationTest { void beforeEach() throws Exception { JSONObject obj = new JSONObject( - IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), StandardCharsets.UTF_8)); + new String(getClass().getResourceAsStream("/build_job.json").readAllBytes(), StandardCharsets.UTF_8)); job = new BuildJobInformation(obj); } @@ -28,7 +27,7 @@ void beforeEach() throws Exception { void testBuildJobInformation_ConstructorFromJSON() throws Exception { JSONObject obj = new JSONObject( - IOUtils.toString(getClass().getResourceAsStream("/build_job.json"), StandardCharsets.UTF_8)); + new String(getClass().getResourceAsStream("/build_job.json").readAllBytes(), StandardCharsets.UTF_8)); job = new BuildJobInformation(obj); assertEquals(1641976754, job.getCreationTime()); diff --git a/src/test/java/com/saucelabs/ci/JobInformationTest.java b/src/test/java/com/saucelabs/ci/JobInformationTest.java index fdd1fd1..d78b85c 100644 --- a/src/test/java/com/saucelabs/ci/JobInformationTest.java +++ b/src/test/java/com/saucelabs/ci/JobInformationTest.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.apache.commons.io.IOUtils; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,7 +19,8 @@ public class JobInformationTest { @BeforeEach void beforeEach() throws Exception { JSONObject obj = - new JSONObject(IOUtils.toString(getClass().getResourceAsStream("/job_info.json"), StandardCharsets.UTF_8)); + new JSONObject( + new String(getClass().getResourceAsStream("/job_info.json").readAllBytes(), StandardCharsets.UTF_8)); job = new JobInformation("1234", "hmac"); job.populateFromJson(obj); }