This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new b894d66c Port some test code from IO to NIO APIs
b894d66c is described below
commit b894d66cf07f8ab76f048779b8dcb91f7511f0ce
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jan 27 17:09:13 2023 -0500
Port some test code from IO to NIO APIs
---
.../java/org/apache/commons/io/file/PathUtils.java | 10 +++++++
.../org/apache/commons/io/FilenameUtilsTest.java | 32 ++++++++++++----------
.../commons/io/input/AbstractInputStreamTest.java | 15 +++++-----
3 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 65900606..fd6cccbf 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -566,6 +566,16 @@ public final class PathUtils {
return pathCounts;
}
+ /**
+ * Delegates to {@link File#deleteOnExit()}.
+ *
+ * @param path the path to delete.
+ * @since 3.13.0
+ */
+ public static void deleteOnExit(Path path) {
+ Objects.requireNonNull(path.toFile()).deleteOnExit();
+ }
+
/**
* Compares the file sets of two Paths to determine if they are equal or
not while considering file contents. The
* comparison includes all files in all subdirectories.
diff --git a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
index d6dec780..68620673 100644
--- a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
@@ -26,6 +26,7 @@ import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -46,52 +47,53 @@ public class FilenameUtilsTest {
private static final String SEP = "" + File.separatorChar;
private static final boolean WINDOWS = File.separatorChar == '\\';
+
@TempDir
- public File temporaryFolder;
+ public Path temporaryFolder;
- private File testFile1;
- private File testFile2;
+ private Path testFile1;
+ private Path testFile2;
private int testFile1Size;
private int testFile2Size;
@BeforeEach
public void setUp() throws Exception {
- testFile1 = File.createTempFile("test", "1", temporaryFolder);
- testFile2 = File.createTempFile("test", "2", temporaryFolder);
+ testFile1 = Files.createTempFile(temporaryFolder, "test", "1");
+ testFile2 = Files.createTempFile(temporaryFolder, "test", "2");
- testFile1Size = (int) testFile1.length();
- testFile2Size = (int) testFile2.length();
- if (!testFile1.getParentFile().exists()) {
+ testFile1Size = (int) Files.size(testFile1);
+ testFile2Size = (int) Files.size(testFile2);
+ if (!Files.exists(testFile1.getParent())) {
throw new IOException("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output3 =
- new
BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
+ new BufferedOutputStream(Files.newOutputStream(testFile1))) {
TestUtils.generateTestData(output3, testFile1Size);
}
- if (!testFile2.getParentFile().exists()) {
+ if (!Files.exists(testFile2.getParent())) {
throw new IOException("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output2 =
- new
BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
+ new BufferedOutputStream(Files.newOutputStream(testFile2))) {
TestUtils.generateTestData(output2, testFile2Size);
}
- if (!testFile1.getParentFile().exists()) {
+ if (!Files.exists(testFile1.getParent())) {
throw new IOException("Cannot create file " + testFile1
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output1 =
- new
BufferedOutputStream(Files.newOutputStream(testFile1.toPath()))) {
+ new BufferedOutputStream(Files.newOutputStream(testFile1))) {
TestUtils.generateTestData(output1, testFile1Size);
}
- if (!testFile2.getParentFile().exists()) {
+ if (!Files.exists(testFile2.getParent())) {
throw new IOException("Cannot create file " + testFile2
+ " as the parent directory does not exist");
}
try (BufferedOutputStream output =
- new
BufferedOutputStream(Files.newOutputStream(testFile2.toPath()))) {
+ new BufferedOutputStream(Files.newOutputStream(testFile2))) {
TestUtils.generateTestData(output, testFile2Size);
}
}
diff --git
a/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
b/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
index 11435ba8..b8684781 100644
--- a/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
@@ -18,12 +18,11 @@ package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOExceptionList;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.AfterEach;
@@ -41,7 +40,7 @@ public abstract class AbstractInputStreamTest {
private byte[] randomBytes;
- protected File inputFile;
+ protected Path inputFile;
protected InputStream[] inputStreams;
@@ -49,13 +48,13 @@ public abstract class AbstractInputStreamTest {
public void setUp() throws IOException {
// Create a byte array of size 2 MB with random bytes
randomBytes = RandomUtils.nextBytes(2 * 1024 * 1024);
- inputFile = File.createTempFile("temp-file", ".tmp");
- FileUtils.writeByteArrayToFile(inputFile, randomBytes);
+ inputFile = Files.createTempFile("temp-file", ".tmp");
+ Files.write(inputFile, randomBytes);
}
@AfterEach
- public void tearDown() throws IOExceptionList {
- inputFile.delete();
+ public void tearDown() throws IOException {
+ Files.delete(inputFile);
IOUtils.close(inputStreams);
}