Skip to content

Commit

Permalink
Drop Apache commons-io in favor of plain Java API (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored Nov 27, 2023
1 parent b92b916 commit bc41a54
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 42 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@
<artifactId>commons-compress</artifactId>
<version>1.24.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
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;
import java.io.File;
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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/saucelabs/ci/BuildInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/saucelabs/ci/BuildJobInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +19,15 @@ 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);
}

@Test
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());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/saucelabs/ci/JobInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit bc41a54

Please sign in to comment.