Port some test code from IO to NIO APIs
MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -566,6 +566,16 @@ public static PathCounters deleteFile(final Path file, final LinkOption[] linkOp
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.

MODIFY
src/test/java/org/apache/commons/io/FilenameUtilsTest.java
src/test/java/org/apache/commons/io/FilenameUtilsTest.java
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;
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);
}
}

MODIFY
src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java
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;
private byte[] randomBytes;
- protected File inputFile;
+ protected Path inputFile;
protected InputStream[] inputStreams;
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);
}

Port some test code from IO to NIO APIs