You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/11/06 12:37:34 UTC
(commons-io) branch master updated: Add Add RandomAccessFileMode.accept(Path, IOConsumer)
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 152e4f18e Add Add RandomAccessFileMode.accept(Path, IOConsumer<RandomAccessFile>)
152e4f18e is described below
commit 152e4f18e8aa94cf0eae1a8b4edda04adf9993df
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Nov 6 07:37:28 2024 -0500
Add Add RandomAccessFileMode.accept(Path, IOConsumer<RandomAccessFile>)
Add Add RandomAccessFileMode.apply(Path, IOFunction<RandomAccessFile,
T>)
---
src/changes/changes.xml | 2 +
.../apache/commons/io/RandomAccessFileMode.java | 48 ++++++
.../apache/commons/io/build/AbstractOrigin.java | 4 +-
.../io/filefilter/MagicNumberFileFilter.java | 6 +-
.../apache/commons/io/FileCleaningTrackerTest.java | 170 ++++++++++-----------
.../commons/io/RandomAccessFileModeTest.java | 4 +-
.../apache/commons/io/RandomAccessFilesTest.java | 93 +++++------
.../io/input/RandomAccessFileInputStreamTest.java | 3 +
8 files changed, 184 insertions(+), 146 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 75de60bac..4b6961725 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,8 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileAlterationObserver.Builder() and deprecate most constructors.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOUtils.readLines(CharSequence).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ValidatingObjectInputStream.ObjectStreamClassPredicate to allow configuration reuse.</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.accept(Path, IOConsumer<RandomAccessFile>).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.apply(Path, IOFunction<RandomAccessFile>, T).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 74 to 78 #670, #676, #679, #688.</action>
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.15.1 to 1.15.8 #672, #673, #685, #686, #694, #696.</action>
diff --git a/src/main/java/org/apache/commons/io/RandomAccessFileMode.java b/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
index e05f48e5e..eeacaaa4a 100644
--- a/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
+++ b/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
@@ -19,12 +19,16 @@ package org.apache.commons.io;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Objects;
+import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.function.IOFunction;
+
/**
* Enumerates access modes for {@link RandomAccessFile} with factory methods.
*
@@ -147,8 +151,49 @@ public enum RandomAccessFileMode {
this.level = level;
}
+ /**
+ * Performs an operation on the {@link RandomAccessFile} specified at the given {@link Path}.
+ * <p>
+ * This method allocates and releases the {@link RandomAccessFile} given to the consumer.
+ * </p>
+ *
+ * @param file the file specifying the {@link RandomAccessFile} to open.
+ * @param consumer the function to apply.
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
+ * @throws IOException Thrown by the given function.
+ * @since 2.18.0
+ */
+ public void accept(final Path file, final IOConsumer<RandomAccessFile> consumer) throws IOException {
+ try (RandomAccessFile raf = create(file)) {
+ consumer.accept(raf);
+ }
+ }
+
+ /**
+ * Applies the given function for a {@link RandomAccessFile} specified at the given {@link Path}.
+ * <p>
+ * This method allocates and releases the {@link RandomAccessFile} given to the function.
+ * </p>
+ *
+ * @param <T> the return type of the function.
+ * @param file the file specifying the {@link RandomAccessFile} to open.
+ * @param function the function to apply.
+ * @return the function's result value.
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
+ * @throws IOException Thrown by the given function.
+ * @since 2.18.0
+ */
+ public <T> T apply(final Path file, final IOFunction<RandomAccessFile, T> function) throws IOException {
+ try (RandomAccessFile raf = create(file)) {
+ return function.apply(raf);
+ }
+ }
+
/**
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
+ * <p>
+ * Prefer {@link #create(Path)} over this.
+ * </p>
*
* @param file the file object
* @return a random access file
@@ -171,6 +216,9 @@ public enum RandomAccessFileMode {
/**
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
+ * <p>
+ * Prefer {@link #create(Path)} over this.
+ * </p>
*
* @param name the file object
* @return a random access file
diff --git a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
index c9801d810..c33245d3e 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
@@ -413,9 +413,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
@Override
public byte[] getByteArray(final long position, final int length) throws IOException {
- try (RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(origin)) {
- return RandomAccessFiles.read(raf, position, length);
- }
+ return RandomAccessFileMode.READ_ONLY.apply(origin, raf -> RandomAccessFiles.read(raf, position, length));
}
@Override
diff --git a/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
index ee95e104c..68ea69c6d 100644
--- a/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
+++ b/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
@@ -18,7 +18,6 @@ package org.apache.commons.io.filefilter;
import java.io.File;
import java.io.IOException;
-import java.io.RandomAccessFile;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@@ -262,8 +261,9 @@ public class MagicNumberFileFilter extends AbstractFileFilter implements Seriali
@Override
public boolean accept(final File file) {
if (file != null && file.isFile() && file.canRead()) {
- try (RandomAccessFile randomAccessFile = RandomAccessFileMode.READ_ONLY.create(file)) {
- return Arrays.equals(magicNumbers, RandomAccessFiles.read(randomAccessFile, byteOffset, magicNumbers.length));
+ try {
+ return RandomAccessFileMode.READ_ONLY.apply(file.toPath(),
+ raf -> Arrays.equals(magicNumbers, RandomAccessFiles.read(raf, byteOffset, magicNumbers.length)));
} catch (final IOException ignored) {
// Do nothing, fall through and do not accept file
}
diff --git a/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java b/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
index b5074515f..55aa26b30 100644
--- a/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
+++ b/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
@@ -48,7 +48,7 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
private File testFile;
private Path testPath;
- private FileCleaningTracker theInstance;
+ private FileCleaningTracker fileCleaningTracker;
RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
return RandomAccessFileMode.READ_WRITE.create(testFile);
@@ -78,14 +78,14 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
public void setUp() {
testFile = new File(tempDirFile, "file-test.txt");
testPath = testFile.toPath();
- theInstance = newInstance();
+ fileCleaningTracker = newInstance();
}
private String showFailures() {
- if (theInstance.deleteFailures.size() == 1) {
- return "[Delete Failed: " + theInstance.deleteFailures.get(0) + "]";
+ if (fileCleaningTracker.deleteFailures.size() == 1) {
+ return "[Delete Failed: " + fileCleaningTracker.deleteFailures.get(0) + "]";
}
- return "[Delete Failures: " + theInstance.deleteFailures.size() + "]";
+ return "[Delete Failures: " + fileCleaningTracker.deleteFailures.size() + "]";
}
@AfterEach
@@ -99,16 +99,16 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
* not.
*/
{
- if (theInstance != null) {
- theInstance.q = new ReferenceQueue<>();
- theInstance.trackers.clear();
- theInstance.deleteFailures.clear();
- theInstance.exitWhenFinished = false;
- theInstance.reaper = null;
+ if (fileCleaningTracker != null) {
+ fileCleaningTracker.q = new ReferenceQueue<>();
+ fileCleaningTracker.trackers.clear();
+ fileCleaningTracker.deleteFailures.clear();
+ fileCleaningTracker.exitWhenFinished = false;
+ fileCleaningTracker.reaper = null;
}
}
- theInstance = null;
+ fileCleaningTracker = null;
}
@Test
@@ -125,16 +125,16 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(tempDirFile.exists());
Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirFile, obj, FileDeleteStrategy.FORCE);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(tempDirFile, obj, FileDeleteStrategy.FORCE);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
obj = null;
waitUntilTrackCount();
pauseForDeleteToComplete(testFile.getParentFile());
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertFalse(new File(testFile.getPath()).exists(), showFailures());
assertFalse(testFile.getParentFile().exists(), showFailures());
}
@@ -153,16 +153,16 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(Files.exists(tempDirPath));
Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirPath, obj, FileDeleteStrategy.FORCE);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(tempDirPath, obj, FileDeleteStrategy.FORCE);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
obj = null;
waitUntilTrackCount();
pauseForDeleteToComplete(testPath.getParent());
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertFalse(Files.exists(testPath), showFailures());
assertFalse(Files.exists(testPath.getParent()), showFailures());
}
@@ -174,15 +174,15 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(tempDirFile.exists());
Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirFile, obj, null);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(tempDirFile, obj, null);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
obj = null;
waitUntilTrackCount();
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertTrue(testFile.exists()); // not deleted, as dir not empty
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}
@@ -194,15 +194,15 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(tempDirFile.exists());
Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirFile, obj);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(tempDirFile, obj);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
obj = null;
waitUntilTrackCount();
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertTrue(testFile.exists()); // not deleted, as dir not empty
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}
@@ -214,32 +214,32 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(Files.exists(tempDirPath));
Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirPath, obj);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(tempDirPath, obj);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
obj = null;
waitUntilTrackCount();
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
}
@Test
public void testFileCleanerExitWhenFinished_NoTrackAfter() {
- assertFalse(theInstance.exitWhenFinished);
- theInstance.exitWhenFinished();
- assertTrue(theInstance.exitWhenFinished);
- assertNull(theInstance.reaper);
+ assertFalse(fileCleaningTracker.exitWhenFinished);
+ fileCleaningTracker.exitWhenFinished();
+ assertTrue(fileCleaningTracker.exitWhenFinished);
+ assertNull(fileCleaningTracker.reaper);
final String path = testFile.getPath();
final Object marker = new Object();
- assertThrows(IllegalStateException.class, () -> theInstance.track(path, marker));
- assertTrue(theInstance.exitWhenFinished);
- assertNull(theInstance.reaper);
+ assertThrows(IllegalStateException.class, () -> fileCleaningTracker.track(path, marker));
+ assertTrue(fileCleaningTracker.exitWhenFinished);
+ assertNull(fileCleaningTracker.reaper);
}
@Test
@@ -252,16 +252,16 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
RandomAccessFile raf = createRandomAccessFile();
assertTrue(testFile.exists(), "2-testFile exists");
- assertEquals(0, theInstance.getTrackCount(), "3-Track Count");
- theInstance.track(path, raf);
- assertEquals(1, theInstance.getTrackCount(), "4-Track Count");
- assertFalse(theInstance.exitWhenFinished, "5-exitWhenFinished");
- assertTrue(theInstance.reaper.isAlive(), "6-reaper.isAlive");
+ assertEquals(0, fileCleaningTracker.getTrackCount(), "3-Track Count");
+ fileCleaningTracker.track(path, raf);
+ assertEquals(1, fileCleaningTracker.getTrackCount(), "4-Track Count");
+ assertFalse(fileCleaningTracker.exitWhenFinished, "5-exitWhenFinished");
+ assertTrue(fileCleaningTracker.reaper.isAlive(), "6-reaper.isAlive");
- assertFalse(theInstance.exitWhenFinished, "7-exitWhenFinished");
- theInstance.exitWhenFinished();
- assertTrue(theInstance.exitWhenFinished, "8-exitWhenFinished");
- assertTrue(theInstance.reaper.isAlive(), "9-reaper.isAlive");
+ assertFalse(fileCleaningTracker.exitWhenFinished, "7-exitWhenFinished");
+ fileCleaningTracker.exitWhenFinished();
+ assertTrue(fileCleaningTracker.exitWhenFinished, "8-exitWhenFinished");
+ assertTrue(fileCleaningTracker.reaper.isAlive(), "9-reaper.isAlive");
raf.close();
testFile = null;
@@ -270,10 +270,10 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
waitUntilTrackCount();
pauseForDeleteToComplete(new File(path));
- assertEquals(0, theInstance.getTrackCount(), "10-Track Count");
+ assertEquals(0, fileCleaningTracker.getTrackCount(), "10-Track Count");
assertFalse(new File(path).exists(), "11-testFile exists " + showFailures());
- assertTrue(theInstance.exitWhenFinished, "12-exitWhenFinished");
- assertFalse(theInstance.reaper.isAlive(), "13-reaper.isAlive");
+ assertTrue(fileCleaningTracker.exitWhenFinished, "12-exitWhenFinished");
+ assertFalse(fileCleaningTracker.reaper.isAlive(), "13-reaper.isAlive");
}
@Test
@@ -284,11 +284,11 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
RandomAccessFile r = createRandomAccessFile();
assertTrue(testFile.exists());
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(path, r);
- assertEquals(1, theInstance.getTrackCount());
- assertFalse(theInstance.exitWhenFinished);
- assertTrue(theInstance.reaper.isAlive());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(path, r);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
+ assertFalse(fileCleaningTracker.exitWhenFinished);
+ assertTrue(fileCleaningTracker.reaper.isAlive());
r.close();
testFile = null;
@@ -297,32 +297,32 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
waitUntilTrackCount();
pauseForDeleteToComplete(new File(path));
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertFalse(new File(path).exists(), showFailures());
- assertFalse(theInstance.exitWhenFinished);
- assertTrue(theInstance.reaper.isAlive());
+ assertFalse(fileCleaningTracker.exitWhenFinished);
+ assertTrue(fileCleaningTracker.reaper.isAlive());
- assertFalse(theInstance.exitWhenFinished);
- theInstance.exitWhenFinished();
- for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) {
+ assertFalse(fileCleaningTracker.exitWhenFinished);
+ fileCleaningTracker.exitWhenFinished();
+ for (int i = 0; i < 20 && fileCleaningTracker.reaper.isAlive(); i++) {
TestUtils.sleep(500L); // allow reaper thread to die
}
- assertTrue(theInstance.exitWhenFinished);
- assertFalse(theInstance.reaper.isAlive());
+ assertTrue(fileCleaningTracker.exitWhenFinished);
+ assertFalse(fileCleaningTracker.reaper.isAlive());
}
@Test
public void testFileCleanerExitWhenFinishedFirst() throws Exception {
- assertFalse(theInstance.exitWhenFinished);
- theInstance.exitWhenFinished();
- assertTrue(theInstance.exitWhenFinished);
- assertNull(theInstance.reaper);
+ assertFalse(fileCleaningTracker.exitWhenFinished);
+ fileCleaningTracker.exitWhenFinished();
+ assertTrue(fileCleaningTracker.exitWhenFinished);
+ assertNull(fileCleaningTracker.reaper);
waitUntilTrackCount();
- assertEquals(0, theInstance.getTrackCount());
- assertTrue(theInstance.exitWhenFinished);
- assertNull(theInstance.reaper);
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ assertTrue(fileCleaningTracker.exitWhenFinished);
+ assertNull(fileCleaningTracker.reaper);
}
@Test
@@ -330,40 +330,40 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
final String path = testFile.getPath();
assertFalse(testFile.exists());
- RandomAccessFile r = createRandomAccessFile();
+ RandomAccessFile raf = createRandomAccessFile();
assertTrue(testFile.exists());
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(path, r);
- assertEquals(1, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
+ fileCleaningTracker.track(path, raf);
+ assertEquals(1, fileCleaningTracker.getTrackCount());
- r.close();
+ raf.close();
testFile = null;
- r = null;
+ raf = null;
waitUntilTrackCount();
pauseForDeleteToComplete(new File(path));
- assertEquals(0, theInstance.getTrackCount());
+ assertEquals(0, fileCleaningTracker.getTrackCount());
assertFalse(new File(path).exists(), showFailures());
}
@Test
public void testFileCleanerNull() {
- assertThrows(NullPointerException.class, () -> theInstance.track((File) null, new Object()));
- assertThrows(NullPointerException.class, () -> theInstance.track((File) null, new Object(), FileDeleteStrategy.NORMAL));
- assertThrows(NullPointerException.class, () -> theInstance.track((String) null, new Object()));
- assertThrows(NullPointerException.class, () -> theInstance.track((String) null, new Object(), FileDeleteStrategy.NORMAL));
+ assertThrows(NullPointerException.class, () -> fileCleaningTracker.track((File) null, new Object()));
+ assertThrows(NullPointerException.class, () -> fileCleaningTracker.track((File) null, new Object(), FileDeleteStrategy.NORMAL));
+ assertThrows(NullPointerException.class, () -> fileCleaningTracker.track((String) null, new Object()));
+ assertThrows(NullPointerException.class, () -> fileCleaningTracker.track((String) null, new Object(), FileDeleteStrategy.NORMAL));
}
private void waitUntilTrackCount() throws Exception {
System.gc();
TestUtils.sleep(500);
int count = 0;
- while (theInstance.getTrackCount() != 0 && count++ < 5) {
+ while (fileCleaningTracker.getTrackCount() != 0 && count++ < 5) {
List<String> list = new ArrayList<>();
try {
long i = 0;
- while (theInstance.getTrackCount() != 0) {
+ while (fileCleaningTracker.getTrackCount() != 0) {
list.add(
"A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String "
+ i++);
@@ -374,7 +374,7 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
System.gc();
TestUtils.sleep(1000);
}
- if (theInstance.getTrackCount() != 0) {
+ if (fileCleaningTracker.getTrackCount() != 0) {
throw new IllegalStateException("Your JVM is not releasing References, try running the test with less memory (-Xmx)");
}
diff --git a/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java b/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
index 394d5db9c..1acaa80ec 100644
--- a/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
+++ b/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
@@ -69,9 +69,7 @@ public class RandomAccessFileModeTest {
public void testCreatePath(final RandomAccessFileMode randomAccessFileMode) throws IOException {
final byte[] expected = BYTES_FIXTURE;
final Path fixture = writeFixture(expected);
- try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture)) {
- assertArrayEquals(expected, read(randomAccessFile));
- }
+ randomAccessFileMode.accept(fixture, raf -> assertArrayEquals(expected, read(raf)));
}
@ParameterizedTest
diff --git a/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java b/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
index ce7fa8fdf..564a27af3 100644
--- a/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
+++ b/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
@@ -27,49 +27,54 @@ import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Arrays;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
/**
* Tests {@link RandomAccessFiles}.
*/
public class RandomAccessFilesTest {
- private static final String FILE_NAME_RO_20 = "src/test/resources/org/apache/commons/io/test-file-20byteslength.bin";
- private static final String FILE_NAME_RO_0 = "src/test/resources/org/apache/commons/io/test-file-empty.bin";
- private static final String FILE_NAME_RO_0_BIS = "src/test/resources/org/apache/commons/io/test-file-empty2.bin";
+ private static final Path PATH_RO_20 = Paths.get("src/test/resources/org/apache/commons/io/test-file-20byteslength.bin");
+ private static final Path PATH_RO_0 = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
+ private static final Path PATH_RO_0_BIS = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty2.bin");
- @Test
- public void testContentEquals() throws IOException {
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- assertEquals(raf1, raf1);
- assertTrue(RandomAccessFiles.contentEquals(raf1, raf1));
- }
+ @ParameterizedTest()
+ @EnumSource(value = RandomAccessFileMode.class)
+ public void testContentEquals(final RandomAccessFileMode mode) throws IOException {
+ mode.accept(PATH_RO_20, raf -> {
+ assertEquals(raf, raf);
+ assertTrue(RandomAccessFiles.contentEquals(raf, raf));
+ });
// as above, to make sure resources are OK
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- assertEquals(raf1, raf1);
- assertTrue(RandomAccessFiles.contentEquals(raf1, raf1));
- }
- // same 20 bytes
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20);
- RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
+ mode.accept(PATH_RO_20, raf -> {
+ assertEquals(raf, raf);
+ assertTrue(RandomAccessFiles.contentEquals(raf, raf));
+ });
+ // same 20 bytes, 2 RAFs
+ try (RandomAccessFile raf1 = mode.create(PATH_RO_20);
+ RandomAccessFile raf2 = mode.create(PATH_RO_20)) {
assertTrue(RandomAccessFiles.contentEquals(raf1, raf2));
}
+ // as above, nested
+ mode.accept(PATH_RO_20, raf1 -> mode.accept(PATH_RO_20, raf2 -> assertTrue(RandomAccessFiles.contentEquals(raf1, raf2))));
// same empty file
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_0);
- RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_0)) {
+ try (RandomAccessFile raf1 = mode.create(PATH_RO_0);
+ RandomAccessFile raf2 = mode.create(PATH_RO_0)) {
assertTrue(RandomAccessFiles.contentEquals(raf1, raf2));
assertTrue(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
}
// diff empty file
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_0);
- RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_0_BIS)) {
+ try (RandomAccessFile raf1 = mode.create(PATH_RO_0);
+ RandomAccessFile raf2 = mode.create(PATH_RO_0_BIS)) {
assertTrue(RandomAccessFiles.contentEquals(raf1, raf2));
assertTrue(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
}
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_0);
- RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
+ try (RandomAccessFile raf1 = mode.create(PATH_RO_0);
+ RandomAccessFile raf2 = mode.create(PATH_RO_20)) {
assertFalse(RandomAccessFiles.contentEquals(raf1, raf2));
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
}
@@ -85,8 +90,8 @@ public class RandomAccessFilesTest {
Arrays.fill(bytes2, (byte) 2);
Files.write(bigFile1, bytes1);
Files.write(bigFile2, bytes2);
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(bigFile1);
- RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(bigFile2)) {
+ try (RandomAccessFile raf1 = mode.create(bigFile1);
+ RandomAccessFile raf2 = mode.create(bigFile2)) {
assertFalse(RandomAccessFiles.contentEquals(raf1, raf2));
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
assertTrue(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf1), RandomAccessFiles.reset(raf1)));
@@ -95,8 +100,8 @@ public class RandomAccessFilesTest {
final byte[] bytes3 = bytes1.clone();
bytes3[bytes3.length - 1] = 9;
Files.write(bigFile3, bytes3);
- try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(bigFile1);
- RandomAccessFile raf3 = RandomAccessFileMode.READ_ONLY.create(bigFile3)) {
+ try (RandomAccessFile raf1 = mode.create(bigFile1);
+ RandomAccessFile raf3 = mode.create(bigFile3)) {
assertFalse(RandomAccessFiles.contentEquals(raf1, raf3));
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf3), RandomAccessFiles.reset(raf1)));
}
@@ -108,30 +113,14 @@ public class RandomAccessFilesTest {
}
}
- @Test
- public void testRead() throws IOException {
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- final byte[] buffer = RandomAccessFiles.read(raf, 0, 0);
- assertArrayEquals(new byte[] {}, buffer);
- }
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- final byte[] buffer = RandomAccessFiles.read(raf, 1, 0);
- assertArrayEquals(new byte[] {}, buffer);
- }
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- final byte[] buffer = RandomAccessFiles.read(raf, 0, 1);
- assertArrayEquals(new byte[] { '1' }, buffer);
- }
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- final byte[] buffer = RandomAccessFiles.read(raf, 1, 1);
- assertArrayEquals(new byte[] { '2' }, buffer);
- }
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- final byte[] buffer = RandomAccessFiles.read(raf, 0, 20);
- assertEquals(20, buffer.length);
- }
- try (final RandomAccessFile raf = RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO_20)) {
- assertThrows(IOException.class, () -> RandomAccessFiles.read(raf, 0, 21));
- }
+ @ParameterizedTest()
+ @EnumSource(value = RandomAccessFileMode.class)
+ public void testRead(final RandomAccessFileMode mode) throws IOException {
+ mode.accept(PATH_RO_20, raf -> assertArrayEquals(new byte[] {}, RandomAccessFiles.read(raf, 0, 0)));
+ mode.accept(PATH_RO_20, raf -> assertArrayEquals(new byte[] {}, RandomAccessFiles.read(raf, 1, 0)));
+ mode.accept(PATH_RO_20, raf -> assertArrayEquals(new byte[] { '1' }, RandomAccessFiles.read(raf, 0, 1)));
+ mode.accept(PATH_RO_20, raf -> assertArrayEquals(new byte[] { '2' }, RandomAccessFiles.read(raf, 1, 1)));
+ mode.accept(PATH_RO_20, raf -> assertEquals(20, RandomAccessFiles.read(raf, 0, 20).length));
+ mode.accept(PATH_RO_20, raf -> assertThrows(IOException.class, () -> RandomAccessFiles.read(raf, 0, 21)));
}
}
diff --git a/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java b/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
index 6d28c078a..cd01563ad 100644
--- a/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
@@ -36,6 +36,9 @@ import java.nio.file.StandardOpenOption;
import org.apache.commons.io.RandomAccessFileMode;
import org.junit.jupiter.api.Test;
+/**
+ * Tests {@link RandomAccessFileInputStream}.
+ */
public class RandomAccessFileInputStreamTest {
private static final String DATA_FILE_NAME = "src/test/resources/org/apache/commons/io/test-file-iso8859-1.bin";